Start service to pause/resume downloads from notification

These operations require the service, so it's better to let
DownloadService handle them. This also ensures we always fulfil
the requests, even when sent while the service is not running.
This commit is contained in:
Gabriele M
2017-07-03 16:08:33 +02:00
parent 789dc7d737
commit dc95edbf80
2 changed files with 29 additions and 48 deletions

View File

@@ -36,6 +36,13 @@ public class DownloadService extends Service {
private static final String TAG = "DownloadService"; private static final String TAG = "DownloadService";
public static final String ACTION_DOWNLOAD_CONTROL = "action_download_control";
public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
public static final String EXTRA_DOWNLOAD_CONTROL = "extra_download_control";
public static final int DOWNLOAD_RESUME = 0;
public static final int DOWNLOAD_PAUSE = 1;
private static final int NOTIFICATION_ID = 10; private static final int NOTIFICATION_ID = 10;
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();
@@ -135,6 +142,16 @@ public class DownloadService extends Service {
mNotificationBuilder.setOngoing(false); mNotificationBuilder.setOngoing(false);
mNotificationManager.cancel(NOTIFICATION_ID); mNotificationManager.cancel(NOTIFICATION_ID);
stopSelf(); stopSelf();
} else if (ACTION_DOWNLOAD_CONTROL.equals(intent.getAction())) {
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
int action = intent.getIntExtra(EXTRA_DOWNLOAD_CONTROL, -1);
if (action == DOWNLOAD_RESUME) {
mDownloadController.resumeDownload(downloadId);
} else if (action == DOWNLOAD_PAUSE) {
mDownloadController.pauseDownload(downloadId);
} else {
Log.e(TAG, "Unknown download action");
}
} }
Log.d(TAG, "Service started"); Log.d(TAG, "Service started");
return START_STICKY; return START_STICKY;
@@ -250,22 +267,20 @@ public class DownloadService extends Service {
} }
private PendingIntent getResumePendingIntent(String downloadId) { private PendingIntent getResumePendingIntent(String downloadId) {
final Intent intent = new Intent(this, UpdaterBroadcastReceiver.class); final Intent intent = new Intent(this, DownloadService.class);
intent.setAction(UpdaterBroadcastReceiver.ACTION_DOWNLOAD); intent.setAction(ACTION_DOWNLOAD_CONTROL);
intent.putExtra(UpdaterBroadcastReceiver.EXTRA_DOWNLOAD_ID, downloadId); intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(UpdaterBroadcastReceiver.EXTRA_DOWNLOAD_ACTION, intent.putExtra(EXTRA_DOWNLOAD_CONTROL, DOWNLOAD_RESUME);
UpdaterBroadcastReceiver.RESUME); return PendingIntent.getService(this, 0, intent,
return PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
} }
private PendingIntent getPausePendingIntent(String downloadId) { private PendingIntent getPausePendingIntent(String downloadId) {
final Intent intent = new Intent(this, UpdaterBroadcastReceiver.class); final Intent intent = new Intent(this, DownloadService.class);
intent.setAction(UpdaterBroadcastReceiver.ACTION_DOWNLOAD); intent.setAction(ACTION_DOWNLOAD_CONTROL);
intent.putExtra(UpdaterBroadcastReceiver.EXTRA_DOWNLOAD_ID, downloadId); intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(UpdaterBroadcastReceiver.EXTRA_DOWNLOAD_ACTION, intent.putExtra(EXTRA_DOWNLOAD_CONTROL, DOWNLOAD_PAUSE);
UpdaterBroadcastReceiver.PAUSE); return PendingIntent.getService(this, 0, intent,
return PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
} }

View File

@@ -26,56 +26,22 @@ import java.io.IOException;
public class UpdaterBroadcastReceiver extends BroadcastReceiver { public class UpdaterBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_DOWNLOAD =
"org.lineageos.updater.action.DOWNLOAD";
public static final String EXTRA_DOWNLOAD_ID = public static final String EXTRA_DOWNLOAD_ID =
"org.lineageos.updater.extra.DOWNLOAD_ID"; "org.lineageos.updater.extra.DOWNLOAD_ID";
public static final String EXTRA_DOWNLOAD_ACTION =
"org.lineageos.updater.extra.DOWNLOAD_CHANGE";
public static final String ACTION_INSTALL_UPDATE = public static final String ACTION_INSTALL_UPDATE =
"org.lineageos.updater.action.INSTALL_UPDATE"; "org.lineageos.updater.action.INSTALL_UPDATE";
public static final int PAUSE = 0;
public static final int RESUME = 1;
private final static String TAG = "BroadcastReceiver"; private final static String TAG = "BroadcastReceiver";
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
final String action = intent.getAction(); final String action = intent.getAction();
if (ACTION_DOWNLOAD.equals(action)) { if (ACTION_INSTALL_UPDATE.equals(action)) {
if (!intent.hasExtra(EXTRA_DOWNLOAD_ID)) { if (!intent.hasExtra(EXTRA_DOWNLOAD_ID)) {
Log.e(TAG, "Missing download ID"); Log.e(TAG, "Missing download ID");
return; return;
} }
DownloadController downloadController = DownloadController.getInstance(); DownloadController downloadController = DownloadController.getInstance(context);
if (downloadController == null) {
Log.e(TAG, "No download controller instance found");
return;
}
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
if (intent.hasExtra(EXTRA_DOWNLOAD_ACTION)) {
int requestedAction = intent.getIntExtra(EXTRA_DOWNLOAD_ACTION, -1);
if (requestedAction == PAUSE) {
downloadController.pauseDownload(downloadId);
} else if (requestedAction == RESUME) {
downloadController.resumeDownload(downloadId);
} else {
Log.e(TAG, "Unknown action");
}
} else {
Log.e(TAG, "Missing extra data");
}
} else if (ACTION_INSTALL_UPDATE.equals(action)) {
if (!intent.hasExtra(EXTRA_DOWNLOAD_ID)) {
Log.e(TAG, "Missing download ID");
return;
}
DownloadController downloadController = DownloadController.getInstance();
if (downloadController == null) {
Log.e(TAG, "No download controller instance found");
return;
}
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID); String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
UpdateDownload update = downloadController.getUpdate(downloadId); UpdateDownload update = downloadController.getUpdate(downloadId);
try { try {