Browse Source

sort by date

Luke Owncloud 10 years ago
parent
commit
b2d8cecfff

+ 25 - 14
src/com/owncloud/android/db/UploadDbHandler.java

@@ -49,13 +49,13 @@ public class UploadDbHandler extends Observable {
 
     //for testing only
     public void recreateDb() {
-//        mDB.beginTransaction();
-//        try {
-//            mHelper.onUpgrade(mDB, 0, mDatabaseVersion);
-//            mDB.setTransactionSuccessful();
-//        } finally {
-//            mDB.endTransaction();
-//        }
+        // mDB.beginTransaction();
+        // try {
+        // mHelper.onUpgrade(mDB, 0, mDatabaseVersion);
+        // mDB.setTransactionSuccessful();
+        // } finally {
+        // mDB.endTransaction();
+        // }
 
     }
 
@@ -74,7 +74,6 @@ public class UploadDbHandler extends Observable {
     private UploadDbHandler(Context context) {
         mDatabaseName = MainApp.getDBName();
         mHelper = new OpenerHelper(context);
-        mDB = mHelper.getWritableDatabase();
     }
     
     private static UploadDbHandler me = null;
@@ -86,7 +85,8 @@ public class UploadDbHandler extends Observable {
     }
     
     public void close() {
-        mDB.close();
+        getDB().close();
+        setDB(null);
         me = null;
     }
 
@@ -205,7 +205,7 @@ public class UploadDbHandler extends Observable {
         cv.put("uploadStatus", uploadObject.getUploadStatus().value);
         cv.put("uploadObject", uploadObject.toString());
         
-        long result = mDB.insert(TABLE_UPLOAD, null, cv);
+        long result = getDB().insert(TABLE_UPLOAD, null, cv);
         Log_OC.d(TAG, "putFileForLater returns with: " + result + " for file: " + uploadObject.getLocalPath());
         if (result == 1) {
             notifyObserversNow();
@@ -232,7 +232,7 @@ public class UploadDbHandler extends Observable {
      * @return 1 if file status was updated, else 0.
      */
     public int updateUpload(String filepath, UploadStatus status, RemoteOperationResult result) {
-        Cursor c = mDB.query(TABLE_UPLOAD, null, "path=?", new String[] {filepath}, null, null, null);
+        Cursor c = getDB().query(TABLE_UPLOAD, null, "path=?", new String[] {filepath}, null, null, null);
         if(c.getCount() != 1) {
             Log_OC.e(TAG, c.getCount() + " items for path=" + filepath + " available in UploadDb. Expected 1." );
             return 0;
@@ -248,7 +248,7 @@ public class UploadDbHandler extends Observable {
             ContentValues cv = new ContentValues();
             cv.put("uploadStatus", status.value);
             cv.put("uploadObject", uploadObjectString);
-            int r = mDB.update(TABLE_UPLOAD, cv, "path=?", new String[] { filepath });
+            int r = getDB().update(TABLE_UPLOAD, cv, "path=?", new String[] { filepath });
             if (r == 1) {
                 notifyObserversNow();
             } else {
@@ -274,7 +274,7 @@ public class UploadDbHandler extends Observable {
      * @return true when one or more upload entries were removed
      */
     public boolean removeUpload(String localPath) {
-        long result = mDB.delete(TABLE_UPLOAD, "path = ?", new String[] { localPath });
+        long result = getDB().delete(TABLE_UPLOAD, "path = ?", new String[] { localPath });
         Log_OC.d(TABLE_UPLOAD, "delete returns with: " + result + " for file: " + localPath);
         return result != 0;
     }
@@ -284,7 +284,7 @@ public class UploadDbHandler extends Observable {
     }
 
     private List<UploadDbObject> getUploads(String selection, String[] selectionArgs) {
-        Cursor c = mDB.query(TABLE_UPLOAD, null, selection, selectionArgs, null, null, null);
+        Cursor c = getDB().query(TABLE_UPLOAD, null, selection, selectionArgs, null, null, null);
         List<UploadDbObject> list = new ArrayList<UploadDbObject>();
         if (c.moveToFirst()) {
             do {
@@ -304,5 +304,16 @@ public class UploadDbHandler extends Observable {
         return getUploads("uploadStatus!=" + UploadStatus.UPLOAD_SUCCEEDED.value + " AND uploadStatus!="
                 + UploadStatus.UPLOAD_FAILED_GIVE_UP.value, null);
     }
+
+    private SQLiteDatabase getDB() {
+        if (mDB == null) {
+            mDB = mHelper.getWritableDatabase();
+        }
+        return mDB;
+    }
+
+    private void setDB(SQLiteDatabase mDB) {
+        this.mDB = mDB;
+    }
     
 }

+ 12 - 1
src/com/owncloud/android/db/UploadDbObject.java

@@ -5,6 +5,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
 
 import android.accounts.Account;
 import android.content.Context;
@@ -27,7 +29,7 @@ public class UploadDbObject implements Serializable {
 
     /** Generated - should be refreshed every time the class changes!! */
     ;
-    private static final long serialVersionUID = -2306246191385279924L;
+    private static final long serialVersionUID = -2306246191385279928L;
 
     private static final String TAG = "UploadDbObject";
     /**
@@ -48,6 +50,15 @@ public class UploadDbObject implements Serializable {
      */
     LocalBehaviour localAction;
 
+    /**
+     * Date and time when this upload was first requested.
+     */
+    Calendar uploadTime = new GregorianCalendar();
+
+    public Calendar getUploadTime() {
+        return uploadTime;
+    }
+
     /**
      * @return the uploadStatus
      */

+ 8 - 4
src/com/owncloud/android/files/services/FileUploadService.java

@@ -366,9 +366,10 @@ public class FileUploadService extends Service {
                 uploadObject.setUploadStatus(UploadStatus.UPLOAD_LATER);
                 boolean success = mDb.storeUpload(uploadObject);
                 if(!success) {
-                    Log_OC.e(TAG, "Could not add upload to database.");
+                    Log_OC.e(TAG, "Could not add upload to database. It is probably a duplicate. Ignore.");
+                } else {
+                    requestedUploads.add(uploadObject);
                 }
-                requestedUploads.add(uploadObject);
             }
             
             
@@ -518,19 +519,22 @@ public class FileUploadService extends Service {
             for (Entry<String, UploadFileOperation> entry : uploads) {
                 if(entry.getValue().getStoragePath().equals(localFileName)) {
                     if(currentUpload != null) {
-                        Log_OC.e(TAG, "Found two current uploads with same remote path. Ignore.");
+                        Log_OC.e(TAG, "Found two current uploads with same remote path " + localFileName + ". Ignore.");
                         return;
                     }
                     currentUpload = entry.getValue();
                 }
             }
+            if (currentUpload == null) {
+                Log_OC.e(TAG, "Found no current upload with remote path " + localFileName + ". Ignore.");
+                return;
+            }
             String key = buildRemoteName(currentUpload.getAccount(), currentUpload.getFile());
             OnDatatransferProgressListener boundListener = mBoundListeners.get(key);
             if (boundListener != null) {
                 boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, localFileName);
             }
         }
-
     }
 
     /**

+ 10 - 8
src/com/owncloud/android/ui/adapter/UploadListAdapter.java

@@ -1,7 +1,5 @@
 package com.owncloud.android.ui.adapter;
 
-import java.io.File;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
@@ -11,20 +9,19 @@ import java.util.Observer;
 import android.app.Activity;
 import android.content.Context;
 import android.database.DataSetObserver;
+import android.text.format.DateUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.ListAdapter;
-import android.widget.ListView;
 import android.widget.TextView;
 
 import com.owncloud.android.R;
 import com.owncloud.android.db.UploadDbHandler;
 import com.owncloud.android.db.UploadDbObject;
 import com.owncloud.android.lib.common.utils.Log_OC;
-import com.owncloud.android.ui.activity.UploadListActivity;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.FileStorageUtils;
 
@@ -104,8 +101,7 @@ public class UploadListAdapter extends BaseAdapter implements ListAdapter, Obser
             UploadDbObject uploadObject = mUploads[position];
 
             TextView fileName = (TextView) view.findViewById(R.id.upload_name);
-            String name = FileStorageUtils.removeDataFolderPath(uploadObject.getLocalPath());
-            fileName.setText(name);
+            fileName.setText(uploadObject.getLocalPath());
 
             TextView statusView = (TextView) view.findViewById(R.id.upload_status);
             String status = uploadObject.getUploadStatus().toString();
@@ -123,6 +119,9 @@ public class UploadListAdapter extends BaseAdapter implements ListAdapter, Obser
             // }
 
             TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
+            CharSequence dateString = DisplayUtils.getRelativeDateTimeString(mActivity, uploadObject.getUploadTime()
+                    .getTimeInMillis(), DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
+            fileSizeV.setText(dateString);
             ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
             // if (!file.isDirectory()) {
             // fileSizeV.setVisibility(View.VISIBLE);
@@ -142,7 +141,6 @@ public class UploadListAdapter extends BaseAdapter implements ListAdapter, Obser
             // }
             //
             // } else {
-            fileSizeV.setVisibility(View.GONE);
             checkBoxV.setVisibility(View.GONE);
             // }
 
@@ -196,13 +194,17 @@ public class UploadListAdapter extends BaseAdapter implements ListAdapter, Obser
                     // } else if (!lhs.isDirectory() && rhs.isDirectory()) {
                     // return 1;
                     // }
-                    return compareNames(lhs, rhs);
+                    return compareUploadTime(lhs, rhs);
                 }
 
                 private int compareNames(UploadDbObject lhs, UploadDbObject rhs) {
                     return lhs.getLocalPath().toLowerCase().compareTo(rhs.getLocalPath().toLowerCase());
                 }
 
+                private int compareUploadTime(UploadDbObject lhs, UploadDbObject rhs) {
+                    return rhs.getUploadTime().compareTo(lhs.getUploadTime());
+                }
+
             });
         }
         mActivity.runOnUiThread(new Runnable() {