Replace 'Download' prefix with 'Updater'
These classes do more than controlling the download of the updates.
This commit is contained in:
309
src/org/lineageos/updater/controller/UpdaterService.java
Normal file
309
src/org/lineageos/updater/controller/UpdaterService.java
Normal file
@@ -0,0 +1,309 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.lineageos.updater.controller;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.support.v7.app.NotificationCompat;
|
||||
import android.text.format.DateUtils;
|
||||
import android.text.format.Formatter;
|
||||
import android.util.Log;
|
||||
|
||||
import org.lineageos.updater.R;
|
||||
import org.lineageos.updater.UpdateDownload;
|
||||
import org.lineageos.updater.UpdatesActivity;
|
||||
import org.lineageos.updater.misc.Utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
public class UpdaterService extends Service {
|
||||
|
||||
private static final String TAG = "UpdaterService";
|
||||
|
||||
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 String ACTION_INSTALL_UPDATE = "action_install_update";
|
||||
|
||||
public static final int DOWNLOAD_RESUME = 0;
|
||||
public static final int DOWNLOAD_PAUSE = 1;
|
||||
|
||||
private static final int NOTIFICATION_ID = 10;
|
||||
|
||||
private final IBinder mBinder = new LocalBinder();
|
||||
private boolean mHasClients;
|
||||
|
||||
private BroadcastReceiver mBroadcastReceiver;
|
||||
private NotificationCompat.Builder mNotificationBuilder;
|
||||
private NotificationManager mNotificationManager;
|
||||
private NotificationCompat.BigTextStyle mNotificationStyle;;
|
||||
|
||||
private UpdaterControllerInt mUpdaterController;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
mUpdaterController = UpdaterController.getInstance(this);
|
||||
|
||||
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
mNotificationBuilder = new NotificationCompat.Builder(this);
|
||||
mNotificationBuilder.setSmallIcon(R.drawable.ic_system_update);
|
||||
mNotificationBuilder.setShowWhen(false);
|
||||
mNotificationStyle = new NotificationCompat.BigTextStyle();
|
||||
mNotificationBuilder.setStyle(mNotificationStyle);
|
||||
|
||||
Intent notificationIntent = new Intent(this, UpdatesActivity.class);
|
||||
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
mNotificationBuilder.setContentIntent(intent);
|
||||
|
||||
mBroadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String downloadId = intent.getStringExtra(UpdaterController.DOWNLOAD_ID_EXTRA);
|
||||
if (UpdaterController.UPDATE_STATUS_ACTION.equals(intent.getAction())) {
|
||||
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
||||
mNotificationBuilder.setContentTitle(update.getName());
|
||||
handleUpdateStatusChange(update);
|
||||
} else if (UpdaterController.PROGRESS_ACTION.equals(intent.getAction())) {
|
||||
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
||||
int progress = update.getProgress();
|
||||
mNotificationBuilder.setProgress(100, progress, false);
|
||||
|
||||
String percent = NumberFormat.getPercentInstance().format(progress / 100.f);
|
||||
mNotificationStyle.setSummaryText(percent);
|
||||
|
||||
mNotificationStyle.setBigContentTitle(update.getName());
|
||||
mNotificationBuilder.setContentTitle(update.getName());
|
||||
|
||||
String speed = Formatter.formatFileSize(context, update.getSpeed());
|
||||
CharSequence eta = DateUtils.formatDuration(update.getEta() * 1000);
|
||||
mNotificationStyle.bigText(
|
||||
getString(R.string.text_download_speed, eta, speed));
|
||||
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
}
|
||||
}
|
||||
};
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(UpdaterController.PROGRESS_ACTION);
|
||||
intentFilter.addAction(UpdaterController.UPDATE_STATUS_ACTION);
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public class LocalBinder extends Binder {
|
||||
public UpdaterService getService() {
|
||||
return UpdaterService.this;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
mHasClients = true;
|
||||
return mBinder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onUnbind(Intent intent) {
|
||||
mHasClients = false;
|
||||
tryStopSelf();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (intent == null) {
|
||||
Log.d(TAG, "The service is being restarted, exit");
|
||||
// The service is being restarted, exit without doing anything
|
||||
stopForeground(STOP_FOREGROUND_DETACH);
|
||||
mNotificationBuilder.setOngoing(false);
|
||||
mNotificationManager.cancel(NOTIFICATION_ID);
|
||||
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) {
|
||||
mUpdaterController.resumeDownload(downloadId);
|
||||
} else if (action == DOWNLOAD_PAUSE) {
|
||||
mUpdaterController.pauseDownload(downloadId);
|
||||
} else {
|
||||
Log.e(TAG, "Unknown download action");
|
||||
}
|
||||
} else if (ACTION_INSTALL_UPDATE.equals(intent.getAction())) {
|
||||
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
|
||||
try {
|
||||
Utils.triggerUpdate(this, mUpdaterController.getUpdate(downloadId));
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Could not install update");
|
||||
// TODO: user facing message
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "Service started");
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
public UpdaterControllerInt getUpdaterController() {
|
||||
return mUpdaterController;
|
||||
}
|
||||
|
||||
private void tryStopSelf() {
|
||||
if (!mHasClients && !mUpdaterController.hasActiveDownloads()) {
|
||||
Log.d(TAG, "Service no longer needed, stopping");
|
||||
stopSelf();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleUpdateStatusChange(UpdateDownload update) {
|
||||
switch (update.getStatus()) {
|
||||
case DELETED: {
|
||||
stopForeground(STOP_FOREGROUND_DETACH);
|
||||
mNotificationBuilder.setOngoing(false);
|
||||
mNotificationManager.cancel(NOTIFICATION_ID);
|
||||
tryStopSelf();
|
||||
break;
|
||||
}
|
||||
case STARTING: {
|
||||
mNotificationBuilder.mActions.clear();
|
||||
mNotificationBuilder.setProgress(0, 0, true);
|
||||
mNotificationStyle.setSummaryText(null);
|
||||
String text = getString(R.string.download_starting_notification);
|
||||
mNotificationStyle.bigText(text);
|
||||
mNotificationBuilder.setTicker(text);
|
||||
mNotificationBuilder.setOngoing(true);
|
||||
startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
break;
|
||||
}
|
||||
case DOWNLOADING: {
|
||||
String text = getString(R.string.downloading_notification);
|
||||
mNotificationStyle.bigText(text);
|
||||
mNotificationBuilder.addAction(com.android.internal.R.drawable.ic_media_pause,
|
||||
getString(R.string.pause_button),
|
||||
getPausePendingIntent(update.getDownloadId()));
|
||||
mNotificationBuilder.setTicker(text);
|
||||
mNotificationBuilder.setOngoing(true);
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
break;
|
||||
}
|
||||
case PAUSED: {
|
||||
stopForeground(STOP_FOREGROUND_DETACH);
|
||||
// In case we pause before the first progress update
|
||||
mNotificationBuilder.setProgress(100, update.getProgress(), false);
|
||||
mNotificationBuilder.mActions.clear();
|
||||
String text = getString(R.string.download_paused_notification);
|
||||
mNotificationStyle.bigText(text);
|
||||
mNotificationBuilder.addAction(com.android.internal.R.drawable.ic_media_play,
|
||||
getString(R.string.resume_button),
|
||||
getResumePendingIntent(update.getDownloadId()));
|
||||
mNotificationBuilder.setTicker(text);
|
||||
mNotificationBuilder.setOngoing(false);
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
break;
|
||||
}
|
||||
case PAUSED_ERROR: {
|
||||
stopForeground(STOP_FOREGROUND_DETACH);
|
||||
mNotificationBuilder.mActions.clear();
|
||||
String text = getString(R.string.download_paused_error_notification);
|
||||
mNotificationStyle.bigText(text);
|
||||
mNotificationBuilder.addAction(com.android.internal.R.drawable.ic_media_play,
|
||||
getString(R.string.resume_button),
|
||||
getResumePendingIntent(update.getDownloadId()));
|
||||
mNotificationBuilder.setTicker(text);
|
||||
mNotificationBuilder.setOngoing(false);
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
break;
|
||||
}
|
||||
case VERIFYING: {
|
||||
mNotificationBuilder.setProgress(0, 0, true);
|
||||
mNotificationStyle.setSummaryText(null);
|
||||
mNotificationBuilder.mActions.clear();
|
||||
String text = getString(R.string.verifying_download_notification);
|
||||
mNotificationStyle.bigText(text);
|
||||
mNotificationBuilder.setTicker(text);
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
break;
|
||||
}
|
||||
case VERIFIED: {
|
||||
stopForeground(STOP_FOREGROUND_DETACH);
|
||||
mNotificationBuilder.setProgress(100, 100, false);
|
||||
String text = getString(R.string.download_completed_notification);
|
||||
mNotificationStyle.bigText(text);
|
||||
mNotificationBuilder.addAction(R.drawable.ic_tab_install,
|
||||
getString(R.string.install_button),
|
||||
getInstallPendingIntent(update.getDownloadId()));
|
||||
mNotificationBuilder.setTicker(text);
|
||||
mNotificationBuilder.setOngoing(false);
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
tryStopSelf();
|
||||
break;
|
||||
}
|
||||
case VERIFICATION_FAILED: {
|
||||
stopForeground(STOP_FOREGROUND_DETACH);
|
||||
mNotificationBuilder.setProgress(0, 0, false);
|
||||
String text = getString(R.string.verification_failed_notification);
|
||||
mNotificationStyle.bigText(text);
|
||||
mNotificationBuilder.setTicker(text);
|
||||
mNotificationBuilder.setOngoing(false);
|
||||
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
|
||||
tryStopSelf();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PendingIntent getResumePendingIntent(String downloadId) {
|
||||
final Intent intent = new Intent(this, UpdaterService.class);
|
||||
intent.setAction(ACTION_DOWNLOAD_CONTROL);
|
||||
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
|
||||
intent.putExtra(EXTRA_DOWNLOAD_CONTROL, DOWNLOAD_RESUME);
|
||||
return PendingIntent.getService(this, 0, intent,
|
||||
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
private PendingIntent getPausePendingIntent(String downloadId) {
|
||||
final Intent intent = new Intent(this, UpdaterService.class);
|
||||
intent.setAction(ACTION_DOWNLOAD_CONTROL);
|
||||
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
|
||||
intent.putExtra(EXTRA_DOWNLOAD_CONTROL, DOWNLOAD_PAUSE);
|
||||
return PendingIntent.getService(this, 0, intent,
|
||||
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
private PendingIntent getInstallPendingIntent(String downloadId) {
|
||||
final Intent intent = new Intent(this, UpdaterService.class);
|
||||
intent.setAction(ACTION_INSTALL_UPDATE);
|
||||
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
|
||||
return PendingIntent.getService(this, 0, intent,
|
||||
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user