Эх сурвалжийг харах

make FileUploadService a Service (instead of IntentService)

Luke Owncloud 10 жил өмнө
parent
commit
a22fbdf335

+ 43 - 5
src/com/owncloud/android/files/services/FileUploadService.java

@@ -34,13 +34,17 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountsException;
-import android.app.IntentService;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
+import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Binder;
+import android.os.Handler;
+import android.os.HandlerThread;
 import android.os.IBinder;
+import android.os.Looper;
+import android.os.Message;
 import android.os.Parcelable;
 import android.support.v4.app.NotificationCompat;
 import android.webkit.MimeTypeMap;
@@ -96,10 +100,13 @@ import com.owncloud.android.utils.UriUtils;
  * 
  */
 @SuppressWarnings("unused")
-public class FileUploadService extends IntentService implements OnDatatransferProgressListener {
+public class FileUploadService extends Service implements OnDatatransferProgressListener {
+
+    private volatile Looper mServiceLooper;
+    private volatile ServiceHandler mServiceHandler;
 
     public FileUploadService() {
-        super("FileUploadService");        
+        super();
     }
 
     private static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
@@ -278,10 +285,42 @@ public class FileUploadService extends IntentService implements OnDatatransferPr
         //when this service starts there is no upload in progress. if db says so, app probably crashed before.
         mDb.setAllCurrentToUploadLater();
         
+        HandlerThread thread = new HandlerThread("FileUploadService");
+        thread.start();
+
+        mServiceLooper = thread.getLooper();
+        mServiceHandler = new ServiceHandler(mServiceLooper);
+
         Log_OC.d(TAG, "FileUploadService.retry() called by onCreate()");
         FileUploadService.retry(getApplicationContext());
     }
 
+    private final class ServiceHandler extends Handler {
+        public ServiceHandler(Looper looper) {
+            super(looper);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            onHandleIntent((Intent) msg.obj);
+            stopSelf(msg.arg1);
+        }
+    }
+
+    @Override
+    public void onStart(Intent intent, int startId) {
+        Message msg = mServiceHandler.obtainMessage();
+        msg.arg1 = startId;
+        msg.obj = intent;
+        mServiceHandler.sendMessage(msg);
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        onStart(intent, startId);
+        return START_NOT_STICKY;
+    }
+
     /**
      * The IntentService calls this method from the default worker thread with
      * the intent that started the service. When this method returns,
@@ -305,7 +344,6 @@ public class FileUploadService extends IntentService implements OnDatatransferPr
      * {@link UploadDbHandler} is taken and upload is started.
      */
 
-    @Override
     protected void onHandleIntent(Intent intent) {
         Log_OC.d(TAG, "onHandleIntent start");
         Log_OC.d(TAG, "mPendingUploads size:" + mPendingUploads.size() + " - before adding new uploads.");
@@ -543,7 +581,7 @@ public class FileUploadService extends IntentService implements OnDatatransferPr
             return reason.toString();
         }
         if (uploadDbObject.getUploadStatus() == UploadStatus.UPLOAD_LATER) {
-            return "Upload delayed for unknown reason. Fix that!";
+            return "Upload is pending and will start shortly.";
         }
         return null;
     }

+ 3 - 2
src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java

@@ -9,7 +9,6 @@ import android.app.Activity;
 import android.content.Context;
 import android.database.DataSetObserver;
 import android.graphics.Bitmap;
-import android.os.Environment;
 import android.text.format.DateUtils;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -24,6 +23,7 @@ import android.widget.TextView;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.db.UploadDbHandler;
+import com.owncloud.android.db.UploadDbHandler.UploadStatus;
 import com.owncloud.android.db.UploadDbObject;
 import com.owncloud.android.files.services.FileUploadService;
 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
@@ -205,7 +205,8 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
             statusView.setText(status);
 
             Button rightButton = (Button) view.findViewById(R.id.upload_right_button);
-            if (UploadUtils.userCanRetryUpload(uploadObject)) {
+            if (UploadUtils.userCanRetryUpload(uploadObject)
+                    && uploadObject.getUploadStatus() != UploadStatus.UPLOAD_SUCCEEDED) {
                 rightButton.setText("\u21BA"); //Anticlockwise Open Circle Arrow U+21BA
                 rightButton.setOnClickListener(new OnClickListener() {                
                     @Override

+ 4 - 2
src/com/owncloud/android/utils/UploadUtils.java

@@ -1,7 +1,5 @@
 package com.owncloud.android.utils;
 
-import com.owncloud.android.db.UploadDbObject;
-
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -9,6 +7,8 @@ import android.net.ConnectivityManager;
 import android.net.NetworkInfo.State;
 import android.os.BatteryManager;
 
+import com.owncloud.android.db.UploadDbObject;
+
 
 public class UploadUtils {
 
@@ -58,6 +58,8 @@ public class UploadUtils {
         case UPLOAD_FAILED_GIVE_UP: //TODO this case needs to be handled as described by
             // https://github.com/owncloud/android/issues/765#issuecomment-66490312
         case UPLOAD_LATER: //upload is already schedule but allow user to increase priority
+        case UPLOAD_SUCCEEDED: // if user wants let him to re-upload (maybe
+                               // remote file was deleted...)
             return true;
         default:
             return false;