mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Make UploadService a Service rather than an IntentService
This commit is contained in:
parent
df950dec50
commit
9ea843b8a9
2 changed files with 49 additions and 18 deletions
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue