From 9ea843b8a90bbe27da537c70ad235d4df43e35c8 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Fri, 8 Feb 2013 02:57:38 +0530 Subject: [PATCH] Make UploadService a Service rather than an IntentService --- .../org/wikimedia/commons/UploadService.java | 55 +++++++++++++------ .../java/org/wikimedia/commons/Utils.java | 12 ++++ 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/commons/src/main/java/org/wikimedia/commons/UploadService.java b/commons/src/main/java/org/wikimedia/commons/UploadService.java index aa8124bcd..04d7c057c 100644 --- a/commons/src/main/java/org/wikimedia/commons/UploadService.java +++ b/commons/src/main/java/org/wikimedia/commons/UploadService.java @@ -21,7 +21,7 @@ import android.widget.RemoteViews; import android.widget.Toast; import android.net.*; -public class UploadService extends IntentService { +public class UploadService extends Service { private static final String EXTRA_PREFIX = "org.wikimedia.commons.upload"; @@ -39,12 +39,21 @@ public class UploadService extends IntentService { private int toUpload; - public UploadService(String name) { - super(name); - } + private volatile Looper mServiceLooper; + private volatile ServiceHandler mServiceHandler; + private String mName; + private boolean mRedelivery; - public UploadService() { - super("UploadService"); + 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); + } } // DO NOT HAVE NOTIFICATION ID OF 0 FOR ANYTHING @@ -102,27 +111,29 @@ public class UploadService extends IntentService { @Override public void onDestroy() { super.onDestroy(); + mServiceLooper.quit(); contributionsProviderClient.release(); Log.d("Commons", "ZOMG I AM BEING KILLED HALP!"); } + @Override + public IBinder onBind(Intent intent) { + return null; + } + @Override public void onCreate() { super.onCreate(); + HandlerThread thread = new HandlerThread("UploadService"); + thread.start(); + + mServiceLooper = thread.getLooper(); + mServiceHandler = new ServiceHandler(mServiceLooper); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); app = (CommonsApplication) this.getApplicationContext(); contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY); } - private long countBytes(InputStream stream) throws IOException { - long count = 0; - BufferedInputStream bis = new BufferedInputStream(stream); - while(bis.read() != -1) { - count++; - } - return count; - } - private Contribution mediaFromIntent(Intent intent) { Bundle extras = intent.getExtras(); Uri mediaUri = (Uri) extras.getParcelable(EXTRA_MEDIA_URI); @@ -137,7 +148,7 @@ public class UploadService extends IntentService { length = this.getContentResolver().openAssetFileDescriptor(mediaUri, "r").getLength(); if(length == -1) { // Let us find out the long way! - length = countBytes(this.getContentResolver().openInputStream(mediaUri)); + length = Utils.countBytes(this.getContentResolver().openInputStream(mediaUri)); } } catch(IOException e) { throw new RuntimeException(e); @@ -159,6 +170,14 @@ public class UploadService extends IntentService { return contribution; } + @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) { toUpload++; @@ -176,10 +195,10 @@ public class UploadService extends IntentService { Intent mediaUploadQueuedIntent = new Intent(); mediaUploadQueuedIntent.putExtra("dummy-data", contribution); // FIXME: Move to separate handler, do not inherit from IntentService - return super.onStartCommand(mediaUploadQueuedIntent, flags, startId); + onStart(mediaUploadQueuedIntent, startId); + return START_REDELIVER_INTENT; } - @Override protected void onHandleIntent(Intent intent) { MWApi api = app.getApi(); diff --git a/commons/src/main/java/org/wikimedia/commons/Utils.java b/commons/src/main/java/org/wikimedia/commons/Utils.java index 99196ce6c..3ddfc3d98 100644 --- a/commons/src/main/java/org/wikimedia/commons/Utils.java +++ b/commons/src/main/java/org/wikimedia/commons/Utils.java @@ -10,6 +10,9 @@ import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; import java.io.StringWriter; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -86,4 +89,13 @@ public class Utils { return title; } } + + public static long countBytes(InputStream stream) throws IOException { + long count = 0; + BufferedInputStream bis = new BufferedInputStream(stream); + while(bis.read() != -1) { + count++; + } + return count; + } }