diff --git a/clients/android/uploader/src/com/danga/camli/CamliActivity.java b/clients/android/uploader/src/com/danga/camli/CamliActivity.java index 72b84aa54..635625475 100644 --- a/clients/android/uploader/src/com/danga/camli/CamliActivity.java +++ b/clients/android/uploader/src/com/danga/camli/CamliActivity.java @@ -1,19 +1,14 @@ package com.danga.camli; -import java.util.ArrayList; - import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; -import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; -import android.os.Parcelable; import android.os.RemoteException; import android.util.Log; import android.view.Menu; @@ -34,7 +29,6 @@ public class CamliActivity extends Activity { private IStatusCallback mCallback = null; private final Handler mHandler = new Handler(); - private final ArrayList mPendingUrisToUpload = new ArrayList(); private final ServiceConnection mServiceConnection = new ServiceConnection() { @@ -44,11 +38,6 @@ public class CamliActivity extends Activity { try { mServiceStub.registerCallback(mCallback); - if (!mPendingUrisToUpload.isEmpty()) { - // Drain the queue from before the service was connected. - startDownloadOfUriList(mPendingUrisToUpload); - mPendingUrisToUpload.clear(); - } } catch (RemoteException e) { e.printStackTrace(); } @@ -259,94 +248,13 @@ public class CamliActivity extends Activity { String action = intent.getAction(); Log.d(TAG, "onResume; action=" + action); - if (Intent.ACTION_SEND.equals(action)) { - handleSend(intent); - setIntent(new Intent(this, CamliActivity.class)); - } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { - handleSendMultiple(intent); + if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) { + Intent serviceIntent = new Intent(intent); + serviceIntent.setClass(this, UploadService.class); + startService(serviceIntent); setIntent(new Intent(this, CamliActivity.class)); } else { Log.d(TAG, "Normal CamliActivity viewing."); } } - - private void handleSendMultiple(Intent intent) { - ArrayList items = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); - ArrayList uris = new ArrayList(items.size()); - for (Parcelable p : items) { - if (!(p instanceof Uri)) { - Log.d(TAG, "uh, unknown thing " + p); - continue; - } - uris.add((Uri) p); - } - startDownloadOfUriList(uris); - } - - private void handleSend(Intent intent) { - Bundle extras = intent.getExtras(); - if (extras == null) { - Log.w(TAG, "expected extras in handleSend"); - return; - } - - extras.keySet(); // unparcel - Log.d(TAG, "handleSend; extras=" + extras); - - Object streamValue = extras.get("android.intent.extra.STREAM"); - if (!(streamValue instanceof Uri)) { - Log.w(TAG, "Expected URI for STREAM; got: " + streamValue); - return; - } - - Uri uri = (Uri) streamValue; - startDownloadOfUri(uri); - } - - private void startDownloadOfUri(final Uri uri) { - Log.d(TAG, "startDownload of " + uri); - if (mServiceStub == null) { - Log.d(TAG, "serviceStub is null in startDownloadOfUri, enqueing"); - mPendingUrisToUpload.add(uri); - return; - } - new AsyncTask() { - @Override - protected Void doInBackground(Void... unused) { - try { - mServiceStub.enqueueUpload(uri); - } catch (RemoteException e) { - Log.d(TAG, "failure to enqueue upload", e); - } - return null; // void - } - }.execute(); - } - - private void startDownloadOfUriList(ArrayList uriList) { - // We need to make a copy of it for our AsyncTask, as our caller may - // clear their owned copy of it before our AsyncTask runs. - final ArrayList uriListCopy = new ArrayList(uriList); - - Log.d(TAG, "startDownload of list: " + uriListCopy); - if (mServiceStub == null) { - Log.d(TAG, "serviceStub is null in startDownloadOfUri, enqueing"); - mPendingUrisToUpload.addAll(uriListCopy); - return; - } - new AsyncTask() { - @Override - protected Void doInBackground(Void... unused) { - try { - Log.d(TAG, "From AsyncTask thread, enqueing uriList of size " - + uriListCopy.size()); - mServiceStub.enqueueUploadList(uriListCopy); - } catch (RemoteException e) { - Log.d(TAG, "failure to enqueue upload", e); - } - return null; // void - } - }.execute(); - } - } diff --git a/clients/android/uploader/src/com/danga/camli/UploadService.java b/clients/android/uploader/src/com/danga/camli/UploadService.java index af428882b..338cf684d 100644 --- a/clients/android/uploader/src/com/danga/camli/UploadService.java +++ b/clients/android/uploader/src/com/danga/camli/UploadService.java @@ -19,10 +19,12 @@ import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.net.wifi.WifiManager; +import android.os.Bundle; import android.os.Environment; import android.os.FileObserver; import android.os.IBinder; import android.os.ParcelFileDescriptor; +import android.os.Parcelable; import android.os.PowerManager; import android.os.RemoteException; import android.util.Log; @@ -106,6 +108,8 @@ public class UploadService extends Service { stopServiceIfEmpty(); return; } + String action = intent.getAction(); + try { if (INTENT_POWER_CONNECTED.equals(intent.getAction())) { service.resume(); @@ -117,11 +121,74 @@ public class UploadService extends Service { service.pause(); stopBackgroundWatchers(); } + + if (Intent.ACTION_SEND.equals(action)) { + handleSend(intent); + return; + } + + if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { + handleSendMultiple(intent); + return; + } + } catch (RemoteException e) { // Ignore. } } + private void handleSend(Intent intent) { + Bundle extras = intent.getExtras(); + if (extras == null) { + Log.w(TAG, "expected extras in handleSend"); + return; + } + + extras.keySet(); // unparcel + Log.d(TAG, "handleSend; extras=" + extras); + + Object streamValue = extras.get("android.intent.extra.STREAM"); + if (!(streamValue instanceof Uri)) { + Log.w(TAG, "Expected URI for STREAM; got: " + streamValue); + return; + } + + final Uri uri = (Uri) streamValue; + Util.runAsync(new Runnable() { + public void run() { + try { + service.enqueueUpload(uri); + } catch (RemoteException e) { + } finally { + stopServiceIfEmpty(); + } + } + }); + } + + private void handleSendMultiple(Intent intent) { + ArrayList items = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); + ArrayList uris = new ArrayList(items.size()); + for (Parcelable p : items) { + if (!(p instanceof Uri)) { + Log.d(TAG, "uh, unknown thing " + p); + continue; + } + uris.add((Uri) p); + } + final ArrayList finalUris = uris; + Util.runAsync(new Runnable() { + public void run() { + try { + service.enqueueUploadList(finalUris); + } catch (RemoteException e) { + } finally { + stopServiceIfEmpty(); + } + } + }); + } + private void stopBackgroundWatchers() { synchronized (UploadService.this) { if (mObservers.isEmpty()) { diff --git a/clients/android/uploader/src/com/danga/camli/Util.java b/clients/android/uploader/src/com/danga/camli/Util.java index 9236cd6bd..1b7b6ac9b 100644 --- a/clients/android/uploader/src/com/danga/camli/Util.java +++ b/clients/android/uploader/src/com/danga/camli/Util.java @@ -8,6 +8,7 @@ import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import android.os.AsyncTask; import android.util.Log; public class Util { @@ -22,6 +23,16 @@ public class Util { return sb.toString(); } + public static void runAsync(final Runnable r) { + new AsyncTask() { + @Override + protected Void doInBackground(Void... unused) { + r.run(); + return null; + } + }.execute(); + } + private static final String HEX = "0123456789abcdef"; private static String getHex(byte[] raw) {