Turn UpdateInstaller into a singleton
For symmetry with ABUpdateInstaller. Change-Id: I9b61ed286c91f10140705e7b3e24b0eab0c75f49
This commit is contained in:
@@ -35,27 +35,42 @@ class UpdateInstaller {
|
|||||||
|
|
||||||
private static final String TAG = "UpdateInstaller";
|
private static final String TAG = "UpdateInstaller";
|
||||||
|
|
||||||
private static Thread sPrepareUpdateThread;
|
private static UpdateInstaller sInstance = null;
|
||||||
private static boolean sCancelled;
|
private static String sInstallingUpdate = null;
|
||||||
|
|
||||||
|
private Thread mPrepareUpdateThread;
|
||||||
|
private volatile boolean mCanCancel;
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final UpdaterController mUpdaterController;
|
private final UpdaterController mUpdaterController;
|
||||||
|
|
||||||
UpdateInstaller(Context context, UpdaterController controller) {
|
private UpdateInstaller(Context context, UpdaterController controller) {
|
||||||
mContext = context;
|
mContext = context.getApplicationContext();
|
||||||
mUpdaterController = controller;
|
mUpdaterController = controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean isInstalling() {
|
static synchronized UpdateInstaller getInstance(Context context,
|
||||||
return !sCancelled && sPrepareUpdateThread != null;
|
UpdaterController updaterController) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new UpdateInstaller(context, updaterController);
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean isInstalling(String downloadId) {
|
static synchronized boolean isInstalling() {
|
||||||
return !sCancelled && sPrepareUpdateThread != null &&
|
return sInstallingUpdate != null;
|
||||||
downloadId.equals(sPrepareUpdateThread.getName());
|
}
|
||||||
|
|
||||||
|
static synchronized boolean isInstalling(String downloadId) {
|
||||||
|
return sInstallingUpdate != null && sInstallingUpdate.equals(downloadId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void install(String downloadId) {
|
void install(String downloadId) {
|
||||||
|
if (isInstalling()) {
|
||||||
|
Log.e(TAG, "Already installing an update");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
UpdateInfo update = mUpdaterController.getUpdate(downloadId);
|
UpdateInfo update = mUpdaterController.getUpdate(downloadId);
|
||||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||||
long buildTimestamp = SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0);
|
long buildTimestamp = SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0);
|
||||||
@@ -90,12 +105,7 @@ class UpdateInstaller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareForUncryptAndInstall(UpdateInfo update) {
|
private synchronized void prepareForUncryptAndInstall(UpdateInfo update) {
|
||||||
if (sPrepareUpdateThread != null) {
|
|
||||||
Log.e(TAG, "Already preparing an update");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String uncryptFilePath = update.getFile().getAbsolutePath() + Constants.UNCRYPT_FILE_EXT;
|
String uncryptFilePath = update.getFile().getAbsolutePath() + Constants.UNCRYPT_FILE_EXT;
|
||||||
File uncryptFile = new File(uncryptFilePath);
|
File uncryptFile = new File(uncryptFilePath);
|
||||||
|
|
||||||
@@ -118,49 +128,49 @@ class UpdateInstaller {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
mCanCancel = true;
|
||||||
FileUtils.copyFile(update.getFile(), uncryptFile, mProgressCallBack);
|
FileUtils.copyFile(update.getFile(), uncryptFile, mProgressCallBack);
|
||||||
|
mCanCancel = false;
|
||||||
// Use INSTALLATION_CANCELLED to clear everything.
|
if (mPrepareUpdateThread.isInterrupted()) {
|
||||||
// This shouldn't really matter in case of success.
|
mUpdaterController.getActualUpdate(update.getDownloadId())
|
||||||
mUpdaterController.getActualUpdate(update.getDownloadId())
|
.setStatus(UpdateStatus.INSTALLATION_CANCELLED);
|
||||||
.setStatus(UpdateStatus.INSTALLATION_CANCELLED);
|
mUpdaterController.getActualUpdate(update.getDownloadId())
|
||||||
mUpdaterController.getActualUpdate(update.getDownloadId())
|
.setInstallProgress(0);
|
||||||
.setInstallProgress(0);
|
|
||||||
mUpdaterController.notifyUpdateChange(update.getDownloadId());
|
|
||||||
|
|
||||||
if (!sPrepareUpdateThread.isInterrupted()) {
|
|
||||||
installPackage(uncryptFile, update.getDownloadId());
|
|
||||||
} else {
|
|
||||||
uncryptFile.delete();
|
uncryptFile.delete();
|
||||||
|
} else {
|
||||||
|
installPackage(uncryptFile, update.getDownloadId());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Could not copy update", e);
|
Log.e(TAG, "Could not copy update", e);
|
||||||
uncryptFile.delete();
|
uncryptFile.delete();
|
||||||
mUpdaterController.getActualUpdate(update.getDownloadId())
|
mUpdaterController.getActualUpdate(update.getDownloadId())
|
||||||
.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
||||||
mUpdaterController.notifyUpdateChange(update.getDownloadId());
|
|
||||||
} finally {
|
} finally {
|
||||||
sPrepareUpdateThread = null;
|
synchronized (UpdateInstaller.this) {
|
||||||
|
mCanCancel = false;
|
||||||
|
mPrepareUpdateThread = null;
|
||||||
|
sInstallingUpdate = null;
|
||||||
|
}
|
||||||
|
mUpdaterController.notifyUpdateChange(update.getDownloadId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
sPrepareUpdateThread = new Thread(copyUpdateRunnable);
|
mPrepareUpdateThread = new Thread(copyUpdateRunnable);
|
||||||
sPrepareUpdateThread.setName(update.getDownloadId());
|
mPrepareUpdateThread.start();
|
||||||
sPrepareUpdateThread.start();
|
sInstallingUpdate = update.getDownloadId();
|
||||||
sCancelled = false;
|
mCanCancel = false;
|
||||||
|
|
||||||
mUpdaterController.getActualUpdate(update.getDownloadId())
|
mUpdaterController.getActualUpdate(update.getDownloadId())
|
||||||
.setStatus(UpdateStatus.INSTALLING);
|
.setStatus(UpdateStatus.INSTALLING);
|
||||||
mUpdaterController.notifyUpdateChange(update.getDownloadId());
|
mUpdaterController.notifyUpdateChange(update.getDownloadId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancel() {
|
public synchronized void cancel() {
|
||||||
if (sCancelled || sPrepareUpdateThread == null) {
|
if (!mCanCancel) {
|
||||||
Log.d(TAG, "Nothing is being copied");
|
Log.d(TAG, "Nothing to cancel");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sCancelled = true;
|
mPrepareUpdateThread.interrupt();
|
||||||
sPrepareUpdateThread.interrupt();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -191,7 +191,8 @@ public class UpdaterService extends Service {
|
|||||||
mUpdaterController);
|
mUpdaterController);
|
||||||
installer.install(downloadId);
|
installer.install(downloadId);
|
||||||
} else {
|
} else {
|
||||||
UpdateInstaller installer = new UpdateInstaller(this, mUpdaterController);
|
UpdateInstaller installer = UpdateInstaller.getInstance(this,
|
||||||
|
mUpdaterController);
|
||||||
installer.install(downloadId);
|
installer.install(downloadId);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -202,7 +203,8 @@ public class UpdaterService extends Service {
|
|||||||
}
|
}
|
||||||
} else if (ACTION_INSTALL_STOP.equals(intent.getAction())) {
|
} else if (ACTION_INSTALL_STOP.equals(intent.getAction())) {
|
||||||
if (UpdateInstaller.isInstalling()) {
|
if (UpdateInstaller.isInstalling()) {
|
||||||
UpdateInstaller installer = new UpdateInstaller(this, mUpdaterController);
|
UpdateInstaller installer = UpdateInstaller.getInstance(this,
|
||||||
|
mUpdaterController);
|
||||||
installer.cancel();
|
installer.cancel();
|
||||||
} else if (ABUpdateInstaller.isInstallingUpdate(this)) {
|
} else if (ABUpdateInstaller.isInstallingUpdate(this)) {
|
||||||
ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this,
|
ABUpdateInstaller installer = ABUpdateInstaller.getInstance(this,
|
||||||
|
Reference in New Issue
Block a user