mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Abstract out the handler code in UploadService
Should make building other services much easier
This commit is contained in:
parent
e6a8f4d626
commit
ea509a7847
3 changed files with 99 additions and 63 deletions
|
|
@ -0,0 +1,63 @@
|
||||||
|
package org.wikimedia.commons;
|
||||||
|
|
||||||
|
import android.app.*;
|
||||||
|
import android.content.*;
|
||||||
|
import android.os.*;
|
||||||
|
|
||||||
|
public abstract class HandlerService<T> extends Service {
|
||||||
|
private volatile Looper threadLooper;
|
||||||
|
private volatile ServiceHandler threadHandler;
|
||||||
|
|
||||||
|
private final class ServiceHandler extends Handler {
|
||||||
|
public ServiceHandler(Looper looper) {
|
||||||
|
super(looper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
handle(msg.what, (T)msg.obj);
|
||||||
|
stopSelf(msg.arg1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
threadLooper.quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class HandlerServiceLocalBinder extends Binder {
|
||||||
|
public HandlerService getService() {
|
||||||
|
return HandlerService.this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private final IBinder localBinder = new HandlerServiceLocalBinder();
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return localBinder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
HandlerThread thread = new HandlerThread("UploadService");
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
threadLooper = thread.getLooper();
|
||||||
|
threadHandler = new ServiceHandler(threadLooper);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void postMessage(int type, Object obj) {
|
||||||
|
Message msg = threadHandler.obtainMessage(type);
|
||||||
|
msg.obj = obj;
|
||||||
|
threadHandler.sendMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void queue(int what, T t) {
|
||||||
|
postMessage(what, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void handle(int what, T t);
|
||||||
|
}
|
||||||
|
|
@ -21,7 +21,7 @@ import android.widget.RemoteViews;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import android.net.*;
|
import android.net.*;
|
||||||
|
|
||||||
public class UploadService extends Service {
|
public class UploadService extends HandlerService<Contribution> {
|
||||||
|
|
||||||
private static final String EXTRA_PREFIX = "org.wikimedia.commons.upload";
|
private static final String EXTRA_PREFIX = "org.wikimedia.commons.upload";
|
||||||
|
|
||||||
|
|
@ -31,7 +31,7 @@ public class UploadService extends Service {
|
||||||
public static final String EXTRA_EDIT_SUMMARY = EXTRA_PREFIX + ".summary";
|
public static final String EXTRA_EDIT_SUMMARY = EXTRA_PREFIX + ".summary";
|
||||||
public static final String EXTRA_MIMETYPE = EXTRA_PREFIX + ".mimetype";
|
public static final String EXTRA_MIMETYPE = EXTRA_PREFIX + ".mimetype";
|
||||||
|
|
||||||
private static final int ACTION_UPLOAD_FILE = 1;
|
public static final int ACTION_UPLOAD_FILE = 1;
|
||||||
|
|
||||||
public static final String ACTION_START_SERVICE = EXTRA_PREFIX + ".upload";
|
public static final String ACTION_START_SERVICE = EXTRA_PREFIX + ".upload";
|
||||||
|
|
||||||
|
|
@ -43,27 +43,6 @@ public class UploadService extends Service {
|
||||||
|
|
||||||
private int toUpload;
|
private int toUpload;
|
||||||
|
|
||||||
private volatile Looper uploadThreadLooper;
|
|
||||||
private volatile ServiceHandler uploadThreadHandler;
|
|
||||||
|
|
||||||
private final class ServiceHandler extends Handler {
|
|
||||||
public ServiceHandler(Looper looper) {
|
|
||||||
super(looper);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleMessage(Message msg) {
|
|
||||||
switch(msg.what) {
|
|
||||||
case ACTION_UPLOAD_FILE:
|
|
||||||
Contribution contrib = (Contribution)msg.obj;
|
|
||||||
uploadContribution(contrib);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
stopSelf(msg.arg1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DO NOT HAVE NOTIFICATION ID OF 0 FOR ANYTHING
|
// DO NOT HAVE NOTIFICATION ID OF 0 FOR ANYTHING
|
||||||
// See http://stackoverflow.com/questions/8725909/startforeground-does-not-show-my-notification
|
// See http://stackoverflow.com/questions/8725909/startforeground-does-not-show-my-notification
|
||||||
// Seriously, Android?
|
// Seriously, Android?
|
||||||
|
|
@ -119,32 +98,14 @@ public class UploadService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
uploadThreadLooper.quit();
|
|
||||||
contributionsProviderClient.release();
|
contributionsProviderClient.release();
|
||||||
Log.d("Commons", "ZOMG I AM BEING KILLED HALP!");
|
Log.d("Commons", "ZOMG I AM BEING KILLED HALP!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UploadServiceLocalBinder extends Binder {
|
|
||||||
public UploadService getService() {
|
|
||||||
return UploadService.this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private final IBinder localBinder = new UploadServiceLocalBinder();
|
|
||||||
@Override
|
|
||||||
public IBinder onBind(Intent intent) {
|
|
||||||
return localBinder;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
HandlerThread thread = new HandlerThread("UploadService");
|
|
||||||
thread.start();
|
|
||||||
|
|
||||||
uploadThreadLooper = thread.getLooper();
|
|
||||||
uploadThreadHandler = new ServiceHandler(uploadThreadLooper);
|
|
||||||
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||||
app = (CommonsApplication) this.getApplicationContext();
|
app = (CommonsApplication) this.getApplicationContext();
|
||||||
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
|
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
|
||||||
|
|
@ -186,14 +147,22 @@ public class UploadService extends Service {
|
||||||
return contribution;
|
return contribution;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postMessage(int type, Object obj) {
|
@Override
|
||||||
Message msg = uploadThreadHandler.obtainMessage(type);
|
protected void handle(int what, Contribution contribution) {
|
||||||
msg.obj = obj;
|
switch(what) {
|
||||||
uploadThreadHandler.sendMessage(msg);
|
case ACTION_UPLOAD_FILE:
|
||||||
|
uploadContribution(contribution);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unknown value for what");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void queue(int what, Contribution contribution) {
|
||||||
|
switch (what) {
|
||||||
|
case ACTION_UPLOAD_FILE:
|
||||||
|
|
||||||
public void queueContribution(Contribution contribution) {
|
|
||||||
contribution.setState(Contribution.STATE_QUEUED);
|
contribution.setState(Contribution.STATE_QUEUED);
|
||||||
contribution.setTransferred(0);
|
contribution.setTransferred(0);
|
||||||
contribution.setContentProviderClient(contributionsProviderClient);
|
contribution.setContentProviderClient(contributionsProviderClient);
|
||||||
|
|
@ -206,9 +175,15 @@ public class UploadService extends Service {
|
||||||
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification);
|
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification);
|
||||||
}
|
}
|
||||||
|
|
||||||
postMessage(ACTION_UPLOAD_FILE, contribution);
|
super.queue(what, contribution);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Unknown value for what");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
if(intent.getAction() == ACTION_START_SERVICE) {
|
if(intent.getAction() == ACTION_START_SERVICE) {
|
||||||
|
|
@ -224,12 +199,12 @@ public class UploadService extends Service {
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Contribution contribution = mediaFromIntent(intent);
|
Contribution contribution = mediaFromIntent(intent);
|
||||||
queueContribution(contribution);
|
queue(ACTION_UPLOAD_FILE, contribution);
|
||||||
}
|
}
|
||||||
return START_REDELIVER_INTENT;
|
return START_REDELIVER_INTENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uploadContribution(Contribution contribution) {
|
private void uploadContribution(Contribution contribution) {
|
||||||
MWApi api = app.getApi();
|
MWApi api = app.getApi();
|
||||||
|
|
||||||
ApiResult result;
|
ApiResult result;
|
||||||
|
|
@ -261,8 +236,6 @@ public class UploadService extends Service {
|
||||||
|
|
||||||
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification);
|
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification);
|
||||||
|
|
||||||
Log.d("Commons", "Just before");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(!api.validateLogin()) {
|
if(!api.validateLogin()) {
|
||||||
// Need to revalidate!
|
// Need to revalidate!
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
|
||||||
private UploadService uploadService;
|
private UploadService uploadService;
|
||||||
private ServiceConnection uploadServiceConnection = new ServiceConnection() {
|
private ServiceConnection uploadServiceConnection = new ServiceConnection() {
|
||||||
public void onServiceConnected(ComponentName componentName, IBinder binder) {
|
public void onServiceConnected(ComponentName componentName, IBinder binder) {
|
||||||
uploadService = ((UploadService.UploadServiceLocalBinder)binder).getService();
|
uploadService = (UploadService) ((HandlerService.HandlerServiceLocalBinder)binder).getService();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onServiceDisconnected(ComponentName componentName) {
|
public void onServiceDisconnected(ComponentName componentName) {
|
||||||
|
|
@ -185,7 +185,7 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
|
||||||
Cursor cursor = (Cursor)adapterView.getItemAtPosition(position);
|
Cursor cursor = (Cursor)adapterView.getItemAtPosition(position);
|
||||||
Contribution c = Contribution.fromCursor(cursor);
|
Contribution c = Contribution.fromCursor(cursor);
|
||||||
if(c.getState() == Contribution.STATE_FAILED) {
|
if(c.getState() == Contribution.STATE_FAILED) {
|
||||||
uploadService.queueContribution(c);
|
uploadService.queue(UploadService.ACTION_UPLOAD_FILE, c);
|
||||||
Log.d("Commons", "Restarting for" + c.toContentValues().toString());
|
Log.d("Commons", "Restarting for" + c.toContentValues().toString());
|
||||||
}
|
}
|
||||||
Log.d("Commons", "You clicked on:" + c.toContentValues().toString());
|
Log.d("Commons", "You clicked on:" + c.toContentValues().toString());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue