瀏覽代碼

Limit number of successful uploads stored in DB, as stated in acceptance criteria

David A. Velasco 9 年之前
父節點
當前提交
e12e5e87c2

+ 3 - 107
src/com/owncloud/android/datamodel/UploadsStorageManager.java

@@ -46,6 +46,7 @@ public class UploadsStorageManager extends Observable {
 
     static private final String TAG = UploadsStorageManager.class.getSimpleName();
 
+
     public enum UploadStatus {
 
         /**
@@ -162,18 +163,6 @@ public class UploadsStorageManager extends Observable {
         return result;
     }
 
-    /**
-     * Update upload status of file in DB.
-     *
-     * @return 1 if file status was updated, else 0.
-     */
-    public int updateUploadStatus(OCUpload ocUpload) {
-        String localPath = (FileUploader.LOCAL_BEHAVIOUR_MOVE == ocUpload.getLocalAction())
-            ? ocUpload.getLocalPath() : null;
-        return updateUploadStatus(ocUpload.getUploadId(), ocUpload.getUploadStatus(),
-                ocUpload.getLastResult(), ocUpload.getRemotePath(), localPath);
-    }
-
     private int updateUploadInternal(Cursor c, UploadStatus status, UploadResult result, String remotePath,
                                      String localPath) {
 
@@ -240,29 +229,6 @@ public class UploadsStorageManager extends Observable {
         return returnValue;
     }
 
-    /*
-    public int updateFileIdUpload(long uploadId, long fileId) {
-        Log_OC.v(TAG, "Updating " + uploadId + " with fileId= " + fileId);
-
-        ContentValues cv = new ContentValues();
-        cv.put(ProviderTableMeta.UPLOADS_FILE_ID, fileId);
-
-        int result = getDB().update(ProviderTableMeta.CONTENT_URI_UPLOADS,
-                cv,
-                ProviderTableMeta._ID + "=?",
-                new String[]{String.valueOf(uploadId)}
-        );
-
-        Log_OC.d(TAG, "updateUpload returns with: " + result + " for fileId: " + fileId);
-        if (result != 1) {
-            Log_OC.e(TAG, "Failed to update item " + uploadId + " into upload db.");
-        } else {
-            notifyObserversNow();
-        }
-
-        return result;
-    }
-    */
 
     /**
      * Should be called when some value of this DB was changed. All observers
@@ -340,10 +306,6 @@ public class UploadsStorageManager extends Observable {
         return getUploads(null, null);
     }
 
-    public OCUpload[] getUploadByLocalPath(String localPath) {
-        return getUploads(ProviderTableMeta.UPLOADS_LOCAL_PATH + "=?", new String[]{localPath});
-    }
-
 
     private OCUpload[] getUploads(String selection, String[] selectionArgs) {
         Cursor c = getDB().query(
@@ -370,26 +332,6 @@ public class UploadsStorageManager extends Observable {
         return list;
     }
 
-    /*
-    private OCFile getUploadFile(long id) {
-        OCFile file = null;
-        Cursor c = getDB().query(
-                ProviderTableMeta.CONTENT_URI_FILE,
-                null,
-                ProviderTableMeta._ID + "=?",
-                new String[]{String.valueOf(id)},
-                null
-        );
-
-        if (c.moveToFirst()) {
-            file = createFileInstance(c);
-        }
-        c.close();
-
-        return file;
-    }
-    */
-
 
     private OCUpload createOCUploadFromCursor(Cursor c) {
         OCUpload upload = null;
@@ -418,15 +360,7 @@ public class UploadsStorageManager extends Observable {
     }
 
     /**
-     * Get all uploads which are currently being uploaded. There should only be
-     * one. No guarantee though.
-     */
-    public OCUpload[] getCurrentUpload() {
-        return getUploads(ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_IN_PROGRESS.value, null);
-    }
-
-    /**
-     * Get all current and pending uploads.
+     * Get all uploads which are currently being uploaded or waiting in the queue to be uploaded.
      */
     public OCUpload[] getCurrentAndPendingUploads() {
         return getUploads(ProviderTableMeta.UPLOADS_STATUS + "==" + UploadStatus.UPLOAD_IN_PROGRESS.value, null);
@@ -491,21 +425,6 @@ public class UploadsStorageManager extends Observable {
         return result;
     }
 
-//    public void setAllCurrentToUploadLater() {
-//        Cursor c = getDB().query(
-//                ProviderTableMeta.CONTENT_URI_UPLOADS,
-//                null,
-//                ProviderTableMeta.UPLOADS_STATUS + "=? ",
-//                new String[]{
-//                        Integer.toString(UploadStatus.UPLOAD_IN_PROGRESS.value)
-//                },
-//                null
-//        );
-//        updateUploadInternal(c, UploadStatus.UPLOAD_LATER, UploadResult.UNKNOWN);
-//        c.close();
-//    }
-
-
     /**
      * Updates the persistent upload database with upload result.
      */
@@ -543,27 +462,7 @@ public class UploadsStorageManager extends Observable {
     }
 
     /**
-     * Determines whether with given uploadResult the upload should be retried later.
-     * @param uploadResult
-     * @return true if upload should be retried later, false if is should be abandoned.
-     */
-    private boolean shouldRetryFailedUpload(RemoteOperationResult uploadResult) {
-        if (uploadResult.isSuccess()) {
-            return false;
-        }
-        switch (uploadResult.getCode()) {
-            case HOST_NOT_AVAILABLE:
-            case NO_NETWORK_CONNECTION:
-            case TIMEOUT:
-            case WRONG_CONNECTION: // SocketException
-                return true;
-            default:
-                return false;
-        }
-    }
-
-    /**
-     * Updates the persistent upload database that upload is in progress.
+     * Updates the persistent upload database with an upload now in progress.
      */
     public void updateDatabaseUploadStart(UploadFileOperation upload) {
         String localPath = (FileUploader.LOCAL_BEHAVIOUR_MOVE == upload.getLocalBehaviour())
@@ -583,8 +482,6 @@ public class UploadsStorageManager extends Observable {
      * Changes the status of any in progress upload from UploadStatus.UPLOAD_IN_PROGRESS
      * to UploadStatus.UPLOAD_FAILED
      *
-     *
-     *
      * @return      Number of uploads which status was changed.
      */
     public int failInProgressUploads(UploadResult fail) {
@@ -614,5 +511,4 @@ public class UploadsStorageManager extends Observable {
         return result;
     }
 
-
 }

+ 51 - 6
src/com/owncloud/android/providers/FileContentProvider.java

@@ -44,6 +44,7 @@ import android.text.TextUtils;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.datamodel.UploadsStorageManager;
 import com.owncloud.android.db.ProviderMeta;
 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
 import com.owncloud.android.lib.common.accounts.AccountUtils;
@@ -70,6 +71,8 @@ public class FileContentProvider extends ContentProvider {
 
     private static final String TAG = FileContentProvider.class.getSimpleName();
 
+    private final String MAX_SUCCESSFUL_UPLOADS = "30";
+
     private UriMatcher mUriMatcher;
 
     @Override
@@ -278,6 +281,7 @@ public class FileContentProvider extends ContentProvider {
                 if (uploadId >0) {
                     insertedUploadUri =
                             ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_UPLOADS, uploadId);
+                    trimSuccessfulUploads(db);
                 } else {
                     throw new SQLException("ERROR " + uri);
 
@@ -472,9 +476,11 @@ public class FileContentProvider extends ContentProvider {
                         ProviderTableMeta.CAPABILITIES_TABLE_NAME, values, selection, selectionArgs
                 );
             case UPLOADS:
-                return db.update(
+                int ret = db.update(
                         ProviderTableMeta.UPLOADS_TABLE_NAME, values, selection, selectionArgs
                 );
+                trimSuccessfulUploads(db);
+                return ret;
             default:
                 return db.update(
                         ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs
@@ -879,7 +885,7 @@ public class FileContentProvider extends ContentProvider {
      * @param db        Database where table of files is included.
      */
     private void updateAccountName(SQLiteDatabase db){
-        Log_OC.d("SQL", "THREAD:  "+ Thread.currentThread().getName());
+        Log_OC.d("SQL", "THREAD:  " + Thread.currentThread().getName());
         AccountManager ama = AccountManager.get(getContext());
         try {
             // get accounts from AccountManager ;  we can't be sure if accounts in it are updated or not although
@@ -940,10 +946,10 @@ public class FileContentProvider extends ContentProvider {
                 ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL";
 
         Cursor c = db.query(ProviderTableMeta.FILE_TABLE_NAME,
-                null,
-                whereClause,
-                new String[] { newAccountName },
-                null, null, null);
+            null,
+            whereClause,
+            new String[]{newAccountName},
+            null, null, null);
 
         try {
             if (c.moveToFirst()) {
@@ -983,4 +989,43 @@ public class FileContentProvider extends ContentProvider {
 
     }
 
+    /**
+     * Grants that total count of successful uploads stored is not greater than MAX_SUCCESSFUL_UPLOADS.
+     *
+     * Removes older uploads if needed.
+     */
+    private void trimSuccessfulUploads(SQLiteDatabase db) {
+        Cursor c = null;
+        try {
+            c = db.rawQuery(
+                "delete from " + ProviderTableMeta.UPLOADS_TABLE_NAME +
+                    " where " + ProviderTableMeta.UPLOADS_STATUS + " == "
+                    + UploadsStorageManager.UploadStatus.UPLOAD_SUCCEEDED.getValue() +
+                    " and " + ProviderTableMeta._ID +
+                    " not in (select " + ProviderTableMeta._ID +
+                    " from " + ProviderTableMeta.UPLOADS_TABLE_NAME +
+                    " where " + ProviderTableMeta.UPLOADS_STATUS + " == "
+                    + UploadsStorageManager.UploadStatus.UPLOAD_SUCCEEDED.getValue() +
+                    " order by " + ProviderTableMeta.UPLOADS_UPLOAD_END_TIMESTAMP +
+                    " desc limit " + MAX_SUCCESSFUL_UPLOADS +
+                    ")",
+                null
+            );
+            c.moveToFirst(); // do something with the cursor, or deletion doesn't happen; true story
+
+        } catch (Exception e) {
+            Log_OC.e(
+                TAG,
+                "Something wrong trimming successful uploads, database could grow more than expected",
+                e
+            );
+
+        } finally {
+            if (c!= null) {
+                c.close();
+            }
+        }
+    }
+
+
 }

+ 0 - 18
src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java

@@ -121,7 +121,6 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
     }
 
     private UploadGroup[] mUploadGroups = null;
-    private final int MAX_NUM_UPLOADS_SHOWN = 30;
 
     public ExpandableUploadListAdapter(FileActivity parentActivity) {
         Log_OC.d(TAG, "ExpandableUploadListAdapter");
@@ -133,7 +132,6 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
             public void refresh() {
                 items = mUploadsStorageManager.getCurrentAndPendingUploads();
                 Arrays.sort(items, comparator);
-                items = trimToMaxLength(items);
             }
 
             @Override
@@ -146,7 +144,6 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
             public void refresh() {
                 items = mUploadsStorageManager.getFailedUploads();
                 Arrays.sort(items, comparator);
-                items = trimToMaxLength(items);
             }
 
             @Override
@@ -160,7 +157,6 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
             public void refresh() {
                 items = mUploadsStorageManager.getFinishedUploads();
                 Arrays.sort(items, comparator);
-                items = trimToMaxLength(items);
             }
 
             @Override
@@ -173,20 +169,6 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
     }
 
 
-    private OCUpload[] trimToMaxLength(OCUpload[] items) {
-        if (items.length > MAX_NUM_UPLOADS_SHOWN) {
-            OCUpload[] arrayTrim = new OCUpload[MAX_NUM_UPLOADS_SHOWN];
-
-            for (int i = 0; i < MAX_NUM_UPLOADS_SHOWN; i++) {
-                arrayTrim[i] = items[i];
-            }
-            return arrayTrim;
-
-        } else {
-            return items;
-        }
-    }
-
     @Override
     public void registerDataSetObserver(DataSetObserver observer) {
         super.registerDataSetObserver(observer);