Skip to content

Commit

Permalink
Convert IntentServices to JobIntentServices
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Oct 4, 2019
1 parent b97c09c commit 915d9e8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 54 deletions.
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@
<meta-data android:name="android.provider.CONTACTS_STRUCTURE" android:resource="@xml/contacts"/>
</service>
<service android:name=".service.DownloadService"
android:label="@string/download_service"/>
<service android:name=".service.UploadService"/>
android:label="@string/download_service"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<service android:name=".service.UploadService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<service android:name=".service.KeyPairGeneratorService"/>
<service android:name=".service.registration.RegistrationService"/>
<service android:name=".service.MediaService"
Expand Down
47 changes: 15 additions & 32 deletions app/src/main/java/org/kontalk/service/DownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@

import org.greenrobot.eventbus.EventBus;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;

import org.kontalk.BuildConfig;
import org.kontalk.Kontalk;
Expand Down Expand Up @@ -75,9 +75,11 @@
* TODO implement multiple downloads in queue or in parallel
* @author Daniele Ricci
*/
public class DownloadService extends IntentService implements DownloadListener {
public class DownloadService extends JobIntentService implements DownloadListener {
private static final String TAG = MessageCenterService.TAG;

private static final int JOB_ID = 1001;

// used only for events to UI
private static final EventBus BUS;

Expand Down Expand Up @@ -122,42 +124,23 @@ public class DownloadService extends IntentService implements DownloadListener {
private ClientHTTPConnection mDownloadClient;
private boolean mCanceled;

public DownloadService() {
super(DownloadService.class.getSimpleName());
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// crappy firmware - as per docs, intent can't be null in this case
if (intent != null) {
if (mNotificationManager == null)
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (ACTION_DOWNLOAD_ABORT.equals(intent.getAction())) {
final Uri uri = intent.getData();
new Thread(new Runnable() {
@Override
public void run() {
onDownloadAbort(uri);
}
}).start();
}
}

return super.onStartCommand(intent, flags, startId);
public void onCreate() {
super.onCreate();
if (mNotificationManager == null)
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}

@Override
protected void onHandleIntent(Intent intent) {
// crappy firmware - as per docs, intent can't be null in this case
if (intent == null)
return;

protected void onHandleWork(@NonNull Intent intent) {
String action = intent.getAction();

if (ACTION_DOWNLOAD_URL.equals(action)) {
onDownloadURL(intent.getData(), intent.getExtras());
}
else if (ACTION_DOWNLOAD_ABORT.equals(intent.getAction())) {
onDownloadAbort(intent.getData());
}
}

@Override
Expand Down Expand Up @@ -485,14 +468,14 @@ public static void start(Context context, long databaseId, String sender,
i.putExtra(EXTRA_MSG_ENCRYPTED, encrypted);
i.putExtra(EXTRA_NOTIFY, notify);
i.setData(Uri.parse(url));
ContextCompat.startForegroundService(context, i);
enqueueWork(context, DownloadService.class, JOB_ID, i);
}

public static void abort(Context context, Uri uri) {
Intent i = new Intent(context, DownloadService.class);
i.setAction(ACTION_DOWNLOAD_ABORT);
i.setData(uri);
ContextCompat.startForegroundService(context, i);
enqueueWork(context, DownloadService.class, JOB_ID, i);
}

public static class WritePermissionDenied {
Expand Down
64 changes: 45 additions & 19 deletions app/src/main/java/org/kontalk/service/UploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
import java.util.LinkedHashMap;
import java.util.Map;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;

import org.kontalk.Log;
import org.kontalk.R;
Expand All @@ -59,9 +60,11 @@
* TODO implement multiple concurrent uploads
* @author Daniele Ricci
*/
public class UploadService extends IntentService implements ProgressListener {
public class UploadService extends JobIntentService implements ProgressListener {
private static final String TAG = MessageCenterService.TAG;

private static final int JOB_ID = 1002;

/** A map to avoid duplicate uploads. */
private static final Map<String, Long> queue = new LinkedHashMap<>();

Expand Down Expand Up @@ -91,8 +94,11 @@ public class UploadService extends IntentService implements ProgressListener {
private UploadConnection mConn;
private boolean mCanceled;

public UploadService() {
super(UploadService.class.getSimpleName());
@Override
public void onCreate() {
super.onCreate();
if (mNotificationManager == null)
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}

@Override
Expand Down Expand Up @@ -124,27 +130,31 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}

@Override
protected void onHandleIntent(Intent intent) {
// crappy firmware - as per docs, intent can't be null in this case
if (intent == null)
return;
// check for unknown action
if (!ACTION_UPLOAD.equals(intent.getAction()))
return;
protected void onHandleWork(@NonNull Intent intent) {
String action = intent.getAction();

if (ACTION_UPLOAD.equals(action)) {
onUploadURL(intent.getData(), intent.getExtras());
}
else if (ACTION_UPLOAD_ABORT.equals(intent.getAction())) {
onUploadAbort(intent.getData());
}

}

private void onUploadURL(Uri file, Bundle args) {
// local file to upload
Uri file = intent.getData();
String filename = file.toString();
// message database id
long databaseId = intent.getLongExtra(EXTRA_DATABASE_ID, 0);
long databaseId = args.getLong(EXTRA_DATABASE_ID, 0);
// url to post to
String url = intent.getStringExtra(EXTRA_POST_URL);
String url = args.getString(EXTRA_POST_URL);
// url to fetch from (will be requested to the connection if null)
String fetchUrl = intent.getStringExtra(EXTRA_GET_URL);
String fetchUrl = args.getString(EXTRA_GET_URL);
// media mime type
String mime = intent.getStringExtra(EXTRA_MIME);
String mime = args.getString(EXTRA_MIME);
// delete original
boolean deleteOriginal = intent.getBooleanExtra(EXTRA_DELETE_ORIGINAL, false);
boolean deleteOriginal = args.getBoolean(EXTRA_DELETE_ORIGINAL, false);

// check if upload has already been queued
if (queue.get(filename) != null) return;
Expand Down Expand Up @@ -194,6 +204,22 @@ protected void onHandleIntent(Intent intent) {
}
}

private void onUploadAbort(Uri uri) {
String filename = uri.toString();
// TODO check for race conditions on queue
Long msgId = queue.get(filename);
if (msgId != null) {
// interrupt worker if running
if (msgId == mMessageId) {
mConn.abort();
mCanceled = true;
}
// remove from queue - will never be processed
else
queue.remove(filename);
}
}

public void startForeground(long totalBytes) {
Log.d(TAG, "starting foreground progress notification");

Expand Down Expand Up @@ -320,6 +346,6 @@ public static void start(Context context, Uri mediaUri,
i.putExtra(UploadService.EXTRA_MIME, mime);
// delete original (actually it's the encrypted temp file) if we already encrypted it
i.putExtra(UploadService.EXTRA_DELETE_ORIGINAL, deleteOriginal);
ContextCompat.startForegroundService(context, i);
enqueueWork(context, UploadService.class, JOB_ID, i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class StartMessageCenterJob extends JobService {

public static int JOB_ID = 1000;
private static int JOB_ID = 1000;

@Override
public boolean onStartJob(JobParameters params) {
Expand Down

0 comments on commit 915d9e8

Please sign in to comment.