OpenDelta: Refactor state handling out of UpdateService

TBD: Move more things out such as download handling
This commit is contained in:
Ido Ben-Hur 2022-09-05 23:35:16 +03:00
parent adf8449d9e
commit 5f25015f18
No known key found for this signature in database
GPG Key ID: 0B827201D8C20BFE
4 changed files with 247 additions and 185 deletions

View File

@ -137,7 +137,7 @@
<string name="flash_file_notice_message"><b>Attention:</b> There will be no checks if the selected ZIP file is suitable for your device!\n\n<b>Flashing the wrong ZIP can lead to a non functional device!</b></string>
<string name="channel_name">Update progress</string>
<string name="channel_description"></string>
<string name="text_update_file_flash_title">Update file</string>
<string name="text_update_file_flash_title">Update file:</string>
<string name="text_info_section">Press check to see if an update is available. After an update has been flashed you will be asked to reboot. Devices with a traditional recovery based update will enter recovery on reboot to execute the flash process. If your device supports seamless updates (A/B) the reboot will activate the update.</string>
<string name="error_ab_timestamp">Current version is newer than the update</string>
<string name="error_ab_inactive">Update is already installed and a reboot is needed</string>

View File

@ -154,7 +154,7 @@ public class MainActivity extends Activity {
updateInfoVisibility();
if (mUpdateService == null) {
startUpdateService(UpdateService.STATE_ACTION_NONE);
startUpdateService(State.ACTION_NONE);
}
}
@ -217,12 +217,12 @@ public class MainActivity extends Activity {
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
UpdateService.LocalBinder binder = (UpdateService.LocalBinder) iBinder;
mUpdateService = binder.getService();
mUpdateService.registerStateCallback(updateReceiver);
mUpdateService.getState().registerStateCallback(updateReceiver);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mUpdateService.unregisterStateCallback();
mUpdateService.getState().unregisterStateCallback();
mUpdateService = null;
}
};
@ -251,7 +251,7 @@ public class MainActivity extends Activity {
mUpdateService.setFlashFilename(flashFilename);
}
private final UpdateService.StateCallback updateReceiver = new UpdateService.StateCallback() {
private final State.StateCallback updateReceiver = new State.StateCallback() {
private String formatLastChecked(long ms) {
if (ms == 0) {
return "";
@ -262,7 +262,7 @@ public class MainActivity extends Activity {
}
@Override
public void updateState(String state, Float progress,
public void update(String state, Float progress,
Long current, Long total, String filename,
Long ms, int errorCode) {
mHandler.post(() -> {
@ -312,17 +312,17 @@ public class MainActivity extends Activity {
}
// check for first start until check button has been pressed
// use a special title then - but only once
if (UpdateService.STATE_ACTION_NONE.equals(state)
if (State.ACTION_NONE.equals(state)
&& !mPrefs.getBoolean(SettingsActivity.PREF_START_HINT_SHOWN, false)) {
title = getString(R.string.last_checked_never_title_new);
}
// don't spill for progress
if (!UpdateService.isProgressState(state)) {
if (!State.isProgressState(state)) {
Logger.d("onReceive state = " + state);
} else if (state.equals(mState)) {
// same progress state as before.
// save a lot of time by only updating progress
disableDataSpeed = UpdateService.STATE_ACTION_AB_FLASH.equals(state);
disableDataSpeed = State.ACTION_AB_FLASH.equals(state);
// long --> int overflows FTL (progress.setXXX)
boolean progressInK = false;
if (localTotal > 1024L * 1024L * 1024L) {
@ -365,7 +365,7 @@ public class MainActivity extends Activity {
mState = state;
}
if (UpdateService.STATE_ERROR_DISK_SPACE.equals(state)) {
if (State.ERROR_DISK_SPACE.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
localCurrent /= 1024L * 1024L;
@ -373,28 +373,28 @@ public class MainActivity extends Activity {
extraText = getString(R.string.error_disk_space_sub,
localCurrent, localTotal);
} else if (UpdateService.STATE_ERROR_UNKNOWN.equals(state)) {
} else if (State.ERROR_UNKNOWN.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
} else if (UpdateService.STATE_ERROR_UNOFFICIAL.equals(state)) {
} else if (State.ERROR_UNOFFICIAL.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
extraText = getString(R.string.state_error_not_official_extra, versionType);
} else if (UpdateService.STATE_ERROR_DOWNLOAD.equals(state)) {
} else if (State.ERROR_DOWNLOAD.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
} else if (UpdateService.STATE_ERROR_CONNECTION.equals(state)) {
} else if (State.ERROR_CONNECTION.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
} else if (UpdateService.STATE_ERROR_PERMISSIONS.equals(state)) {
} else if (State.ERROR_PERMISSIONS.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
} else if (UpdateService.STATE_ERROR_FLASH.equals(state)) {
} else if (State.ERROR_FLASH.equals(state)) {
enableCheck = true;
enableFlash = true;
mProgress.setIndeterminate(false);
title = getString(R.string.state_error_flash_title);
} else if (UpdateService.STATE_ERROR_AB_FLASH.equals(state)) {
} else if (State.ERROR_AB_FLASH.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
title = getString(R.string.state_error_ab_flash_title);
@ -403,14 +403,14 @@ public class MainActivity extends Activity {
} else if (errorCode == UpdateEngine.ErrorCodeConstants.UPDATED_BUT_NOT_ACTIVE) {
extraText = getString(R.string.error_ab_inactive);
}
} else if (UpdateService.STATE_ERROR_FLASH_FILE.equals(state)) {
} else if (State.ERROR_FLASH_FILE.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
title = getString(R.string.state_error_flash_file_title);
} else if (UpdateService.STATE_ACTION_NONE.equals(state)) {
} else if (State.ACTION_NONE.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
} else if (UpdateService.STATE_ACTION_READY.equals(state)) {
} else if (State.ACTION_READY.equals(state)) {
enableCheck = true;
enableFlash = true;
enableChangelog = true;
@ -425,7 +425,7 @@ public class MainActivity extends Activity {
flashImageBase.lastIndexOf('.'));
}
mUpdateVersionTitle.setText(R.string.text_update_version_title);
} else if (UpdateService.STATE_ACTION_FLASH_FILE_READY.equals(state)) {
} else if (State.ACTION_FLASH_FILE_READY.equals(state)) {
enableCheck = true;
enableFlash = true;
mProgress.setIndeterminate(false);
@ -438,7 +438,7 @@ public class MainActivity extends Activity {
updateVersion = flashImageBase;
}
mUpdateVersionTitle.setText(R.string.text_update_file_flash_title);
} else if (UpdateService.STATE_ACTION_AB_FINISHED.equals(state)) {
} else if (State.ACTION_AB_FINISHED.equals(state)) {
enableReboot = true;
disableCheckNow = true;
enableChangelog = !mPrefs.getBoolean(UpdateService.PREF_FILE_FLASH, false);
@ -455,7 +455,7 @@ public class MainActivity extends Activity {
mPrefs.edit().putString(UpdateService.PREF_READY_FILENAME_NAME, null).commit();
mPrefs.edit().putString(UpdateService.PREF_LATEST_FULL_NAME, null).commit();
} else if (UpdateService.STATE_ACTION_BUILD.equals(state)) {
} else if (State.ACTION_BUILD.equals(state)) {
enableCheck = true;
mProgress.setIndeterminate(false);
@ -494,21 +494,21 @@ public class MainActivity extends Activity {
} else {
downloadSizeText = Formatter.formatFileSize(getApplicationContext(), downloadSize);
}
} else if (UpdateService.STATE_ACTION_SEARCHING.equals(state)
|| UpdateService.STATE_ACTION_CHECKING.equals(state)) {
} else if (State.ACTION_SEARCHING.equals(state)
|| State.ACTION_CHECKING.equals(state)) {
enableProgress = true;
mProgress.setIndeterminate(true);
localCurrent = 1L;
} else {
enableChangelog = !mPrefs.getBoolean(UpdateService.PREF_FILE_FLASH, false);
enableProgress = true;
if (UpdateService.STATE_ACTION_AB_FLASH.equals(state)) {
if (State.ACTION_AB_FLASH.equals(state)) {
disableDataSpeed = true;
} else if (UpdateService.STATE_ACTION_DOWNLOADING.equals(state)) {
} else if (State.ACTION_DOWNLOADING.equals(state)) {
disableCheckNow = true;
enableDownload = true;
} else if (UpdateService.STATE_ERROR_DOWNLOAD_RESUME.equals(state) ||
UpdateService.STATE_ACTION_DOWNLOADING_PAUSED.equals(state)) {
} else if (State.ERROR_DOWNLOAD_RESUME.equals(state) ||
State.ACTION_DOWNLOADING_PAUSED.equals(state)) {
disableCheckNow = true;
enableDownload = true;
enableResume = true;
@ -772,7 +772,7 @@ public class MainActivity extends Activity {
startUpdateServiceFile(flashFilename);
} else {
Intent i = new Intent(UpdateService.BROADCAST_INTENT);
i.putExtra(UpdateService.EXTRA_STATE, UpdateService.STATE_ERROR_FLASH_FILE);
i.putExtra(UpdateService.EXTRA_STATE, State.ERROR_FLASH_FILE);
sendBroadcast(i);
}
} else if (requestCode == PERMISSIONS_REQUEST_MANAGE_EXTERNAL_STORAGE
@ -842,7 +842,7 @@ public class MainActivity extends Activity {
ACTIVITY_SELECT_FLASH_FILE);
} catch (android.content.ActivityNotFoundException ex) {
Intent i = new Intent(UpdateService.BROADCAST_INTENT);
i.putExtra(UpdateService.EXTRA_STATE, UpdateService.STATE_ERROR_FLASH_FILE);
i.putExtra(UpdateService.EXTRA_STATE, State.ERROR_FLASH_FILE);
sendBroadcast(i);
}
}

View File

@ -0,0 +1,146 @@
/*
* Copyright (C) 2022 Yet Another AOSP Project
*/
/*
* This file is part of OpenDelta.
*
* OpenDelta is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenDelta is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenDelta. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.chainfire.opendelta;
import java.util.Set;
public class State {
public static final String ACTION_NONE = "action_none";
public static final String ACTION_CHECKING = "action_checking";
public static final String ACTION_CHECKING_SUM = "action_checking_sum";
public static final String ACTION_SEARCHING = "action_searching";
public static final String ACTION_SEARCHING_SUM = "action_searching_sum";
public static final String ACTION_DOWNLOADING = "action_downloading";
public static final String ACTION_DOWNLOADING_PAUSED = "action_downloading_paused";
public static final String ACTION_APPLYING = "action_applying";
public static final String ACTION_APPLYING_PATCH = "action_applying_patch";
public static final String ACTION_APPLYING_SUM = "action_applying_sum";
public static final String ACTION_READY = "action_ready";
public static final String ACTION_AB_FLASH = "action_ab_flash";
public static final String ACTION_AB_FINISHED = "action_ab_finished";
public static final String ACTION_BUILD = "action_build";
public static final String ACTION_FLASH_FILE_READY = "action_flash_file_ready";
public static final String ERROR_DISK_SPACE = "error_disk_space";
public static final String ERROR_UNKNOWN = "error_unknown";
public static final String ERROR_UNOFFICIAL = "error_unofficial";
public static final String ERROR_DOWNLOAD = "error_download";
public static final String ERROR_DOWNLOAD_RESUME = "error_download_resume";
public static final String ERROR_CONNECTION = "error_connection";
public static final String ERROR_PERMISSIONS = "error_permissions";
public static final String ERROR_FLASH = "error_flash";
public static final String ERROR_AB_FLASH = "error_ab_flash";
public static final String ERROR_FLASH_FILE = "error_flash_file";
private static final Set<String> mProgressStates = Set.of(
ACTION_DOWNLOADING,
ACTION_SEARCHING,
ACTION_SEARCHING_SUM,
ACTION_CHECKING,
ACTION_CHECKING_SUM,
ACTION_APPLYING,
ACTION_APPLYING_SUM,
ACTION_APPLYING_PATCH,
ACTION_AB_FLASH
);
private static final Set<String> mErrorStates = Set.of(
ERROR_DOWNLOAD,
ERROR_DOWNLOAD_RESUME,
ERROR_DISK_SPACE,
ERROR_UNKNOWN,
ERROR_UNOFFICIAL,
ERROR_CONNECTION,
ERROR_AB_FLASH,
ERROR_FLASH_FILE,
ERROR_FLASH
);
private static State mState;
private String mStateStr = ACTION_NONE;
private StateCallback mStateCallback;
private State() {}
public interface StateCallback {
void update(String state, Float progress,
Long current, Long total, String filename,
Long ms, int errorCode);
}
public void registerStateCallback(StateCallback callback) {
mStateCallback = callback;
update(mStateStr);
}
public void unregisterStateCallback() {
mStateCallback = null;
}
public static State getInstance() {
if (mState == null) mState = new State();
return mState;
}
public void update(String state) {
update(state, null, null, null, null, null);
}
public void update(String state, Float progress,
Long current, Long total, String filename, Long ms) {
update(state, progress, current, total, filename, ms, -1);
}
public synchronized void update(String state, Float progress,
Long current, Long total, String filename, Long ms, int errorCode) {
mStateStr = state;
if (mStateCallback != null)
mStateCallback.update(state, progress, current,
total, filename, ms, errorCode);
}
public synchronized String getState() {
return mStateStr;
}
public boolean isProgressState() {
return isProgressState(mStateStr);
}
public boolean isErrorState() {
return isErrorState(mStateStr);
}
public static boolean isProgressState(String state) {
return mProgressStates.contains(state);
}
public static boolean isErrorState(String state) {
return mErrorStates.contains(state);
}
public boolean equals(String state) {
return state.equals(mStateStr);
}
@Override
public String toString() {
return mStateStr;
}
}

View File

@ -114,32 +114,6 @@ public class UpdateService extends Service implements OnNetworkStateListener,
public static final String EXTRA_STATE = "eu.chainfire.opendelta.extra.ACTION_STATE";
public static final String EXTRA_FILENAME = "eu.chainfire.opendelta.extra.FILENAME";
public static final String STATE_ACTION_NONE = "action_none";
public static final String STATE_ACTION_CHECKING = "action_checking";
public static final String STATE_ACTION_CHECKING_SUM = "action_checking_sum";
public static final String STATE_ACTION_SEARCHING = "action_searching";
public static final String STATE_ACTION_SEARCHING_SUM = "action_searching_sum";
public static final String STATE_ACTION_DOWNLOADING = "action_downloading";
public static final String STATE_ACTION_DOWNLOADING_PAUSED = "action_downloading_paused";
public static final String STATE_ACTION_APPLYING = "action_applying";
public static final String STATE_ACTION_APPLYING_PATCH = "action_applying_patch";
public static final String STATE_ACTION_APPLYING_SUM = "action_applying_sum";
public static final String STATE_ACTION_READY = "action_ready";
public static final String STATE_ACTION_AB_FLASH = "action_ab_flash";
public static final String STATE_ACTION_AB_FINISHED = "action_ab_finished";
public static final String STATE_ERROR_DISK_SPACE = "error_disk_space";
public static final String STATE_ERROR_UNKNOWN = "error_unknown";
public static final String STATE_ERROR_UNOFFICIAL = "error_unofficial";
public static final String STATE_ACTION_BUILD = "action_build";
public static final String STATE_ERROR_DOWNLOAD = "error_download";
public static final String STATE_ERROR_DOWNLOAD_RESUME = "error_download_resume";
public static final String STATE_ERROR_CONNECTION = "error_connection";
public static final String STATE_ERROR_PERMISSIONS = "error_permissions";
public static final String STATE_ERROR_FLASH = "error_flash";
public static final String STATE_ERROR_AB_FLASH = "error_ab_flash";
public static final String STATE_ERROR_FLASH_FILE = "error_flash_file";
public static final String STATE_ACTION_FLASH_FILE_READY = "action_flash_file_ready";
public static final String ACTION_CHECK = "eu.chainfire.opendelta.action.CHECK";
public static final String ACTION_FLASH = "eu.chainfire.opendelta.action.FLASH";
public static final String ACTION_ALARM = "eu.chainfire.opendelta.action.ALARM";
@ -204,7 +178,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
private HandlerThread mHandlerThread;
private Handler mHandler;
private String mState = STATE_ACTION_NONE;
private State mState = State.getInstance();
private NetworkState mNetworkState;
private BatteryState mBatteryState;
@ -237,7 +211,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (now >= mLastProgressTime[0] + 250L) {
long ms = SystemClock.elapsedRealtime() - mLastProgressTime[1];
int sec = (int) (((((float) total / (float) current) * (float) ms) - ms) / 1000f);
updateState(STATE_ACTION_AB_FLASH, progress, current, total, this.status, ms);
mState.update(State.ACTION_AB_FLASH, progress, current, total, this.status, ms);
setFlashNotificationProgress((int) progress, sec);
mLastProgressTime[0] = now;
}
@ -256,23 +230,6 @@ public class UpdateService extends Service implements OnNetworkStateListener,
}
}
private StateCallback mStateCallback;
public interface StateCallback {
void updateState(String state, Float progress,
Long current, Long total, String filename,
Long ms, int errorCode);
}
public void registerStateCallback(StateCallback callback) {
mStateCallback = callback;
updateState(mState);
}
public void unregisterStateCallback() {
mStateCallback = null;
}
/*
* Using reflection voodoo instead calling the hidden class directly, to
* dev/test outside of AOSP tree
@ -412,8 +369,8 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (mNotificationManager != null)
mNotificationManager.cancel(NOTIFICATION_BUSY);
// if we have a paused download in progress we need to manually stop it
if (mState.equals(STATE_ERROR_DOWNLOAD_RESUME) ||
mState.equals(STATE_ACTION_DOWNLOADING_PAUSED)) {
if (mState.equals(State.ERROR_DOWNLOAD_RESUME) ||
mState.equals(State.ACTION_DOWNLOADING_PAUSED)) {
// to do so we just need to remove the file and update state
File[] files = new File(mConfig.getPathBase()).listFiles();
for (File file : files)
@ -423,8 +380,8 @@ public class UpdateService extends Service implements OnNetworkStateListener,
}
break;
case ACTION_DOWNLOAD_PAUSE:
final boolean isPaused = mState.equals(STATE_ACTION_DOWNLOADING_PAUSED) ||
mState.equals(STATE_ERROR_DOWNLOAD_RESUME);
final boolean isPaused = mState.equals(State.ACTION_DOWNLOADING_PAUSED) ||
mState.equals(State.ERROR_DOWNLOAD_RESUME);
if (isPaused) {
// resume
mStopDownload = -1;
@ -441,24 +398,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
}
}
private void updateState(String state) {
updateState(state, null, null, null, null, null);
}
private void updateState(String state, Float progress,
Long current, Long total, String filename, Long ms) {
updateState(state, progress, current, total, filename, ms, -1);
}
private synchronized void updateState(String state, Float progress,
Long current, Long total, String filename, Long ms, int errorCode) {
mState = state;
if (mStateCallback != null)
mStateCallback.updateState(state, progress, current,
total, filename, ms, errorCode);
}
public synchronized String getState() {
public State getState() {
return mState;
}
@ -480,7 +420,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
@Override
public boolean onWantUpdateCheck() {
if (isProgressState(mState)) {
if (mState.isProgressState()) {
Logger.i("Blocked scheduler requests while running in state " + mState);
return false;
}
@ -519,7 +459,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
private void autoState(boolean userInitiated, int checkOnly, boolean notify) {
Logger.d("autoState state = " + mState + " userInitiated = " + userInitiated + " checkOnly = " + checkOnly);
if (isErrorState(mState)) {
if (mState.isErrorState()) {
return;
}
@ -534,11 +474,11 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (mLastProgressTime == null)
mLastProgressTime = new long[] { 0, SystemClock.elapsedRealtime() };
mProgressListener.setStatus(_filename);
updateState(STATE_ACTION_AB_FLASH, 0f, 0L, 100L, _filename, null);
mState.update(State.ACTION_AB_FLASH, 0f, 0L, 100L, _filename, null);
mIsUpdateRunning = ABUpdate.resume(flashFilename, mProgressListener, this);
if (!mIsUpdateRunning) {
mNotificationManager.cancel(NOTIFICATION_UPDATE);
updateState(STATE_ERROR_AB_FLASH);
mState.update(State.ERROR_AB_FLASH);
} else {
newFlashNotification(_filename);
}
@ -572,7 +512,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
final long current = found.length();
final long lastTime = mPrefs.getLong(PREF_LAST_DOWNLOAD_TIME, 0);
final float progress = ((float) current / (float) total) * 100f;
updateState(STATE_ACTION_DOWNLOADING_PAUSED, progress, current, total, latestFullBuild, lastTime);
mState.update(State.ACTION_DOWNLOADING_PAUSED, progress, current, total, latestFullBuild, lastTime);
// display paused notification with the proper title
newDownloadNotification(true, getString(R.string.state_action_downloading_paused));
mDownloadNotificationBuilder.setProgress(100, Math.round(progress), false);
@ -587,12 +527,12 @@ public class UpdateService extends Service implements OnNetworkStateListener,
Logger.d("Checking step done");
if (!updateAvailable()) {
Logger.d("System up to date");
updateState(STATE_ACTION_NONE, null, null, null, null,
mState.update(State.ACTION_NONE, null, null, null, null,
mPrefs.getLong(PREF_LAST_CHECK_TIME_NAME,
PREF_LAST_CHECK_TIME_DEFAULT));
} else {
Logger.d("Update available");
updateState(STATE_ACTION_BUILD, null, null, null, null,
mState.update(State.ACTION_BUILD, null, null, null, null,
mPrefs.getLong(PREF_LAST_CHECK_TIME_NAME,
PREF_LAST_CHECK_TIME_DEFAULT));
if (!userInitiated && notify) {
@ -608,12 +548,12 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (filename == null) {
Logger.d("System up to date");
updateState(STATE_ACTION_NONE, null, null, null, null,
mState.update(State.ACTION_NONE, null, null, null, null,
mPrefs.getLong(PREF_LAST_CHECK_TIME_NAME,
PREF_LAST_CHECK_TIME_DEFAULT));
} else {
Logger.d("Update found: %s", filename);
updateState(STATE_ACTION_READY, null, null, null, (new File(
mState.update(State.ACTION_READY, null, null, null, (new File(
filename)).getName(), mPrefs.getLong(
PREF_LAST_CHECK_TIME_NAME, PREF_LAST_CHECK_TIME_DEFAULT));
@ -944,7 +884,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
try {
final String userFN = f.getName().substring(0, f.getName().length() - 5);
updateState(STATE_ACTION_DOWNLOADING, 0f, 0L, 0L, userFN, null);
mState.update(State.ACTION_DOWNLOADING, 0f, 0L, 0L, userFN, null);
urlConnection = setupHttpsRequest(url);
if (urlConnection == null) return false;
@ -957,12 +897,12 @@ public class UpdateService extends Service implements OnNetworkStateListener,
Logger.d("Resuming download at: " + offset);
}
updateState(STATE_ACTION_DOWNLOADING, 0f, 0L, len, userFN, null);
mState.update(State.ACTION_DOWNLOADING, 0f, 0L, len, userFN, null);
long freeSpace = (new StatFs(mConfig.getPathBase()))
.getAvailableBytes();
if (freeSpace < len - offset) {
updateState(STATE_ERROR_DISK_SPACE, null, freeSpace, len, null,
mState.update(State.ERROR_DISK_SPACE, null, freeSpace, len, null,
null);
Logger.d("not enough space!");
return false;
@ -979,7 +919,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
progress = ((float) current / (float) total) * 100f;
long now = SystemClock.elapsedRealtime();
if (now >= last[2] + 250L) {
updateState(STATE_ACTION_DOWNLOADING, progress,
mState.update(State.ACTION_DOWNLOADING, progress,
current, total, userFN, now - last[3]);
setDownloadNotificationProgress(progress, current,
total,now - last[3]);
@ -1016,7 +956,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
StringBuilder SUM;
if (offset > 0) {
SUM = new StringBuilder(getFileSHA256(f, getSUMProgress(STATE_ACTION_CHECKING_SUM, f.getName())));
SUM = new StringBuilder(getFileSHA256(f, getSUMProgress(State.ACTION_CHECKING_SUM, f.getName())));
} else {
if (digest == null) return false;
SUM = new StringBuilder(new BigInteger(1, digest.digest())
@ -1033,7 +973,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
Logger.i("SUM check failed for " + url);
// if sum does not match when done, get rid
f.delete();
updateState(STATE_ERROR_DOWNLOAD);
mState.update(State.ERROR_DOWNLOAD);
}
return sumCheck;
}
@ -1110,7 +1050,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
String buildData = downloadUrlMemoryAsString(url);
if (buildData == null || buildData.length() == 0) {
updateState(STATE_ERROR_DOWNLOAD, null, null, null, url, null);
mState.update(State.ERROR_DOWNLOAD, null, null, null, url, null);
mNotificationManager.cancel(NOTIFICATION_BUSY);
return null;
}
@ -1168,7 +1108,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
} catch (Exception e) {
Logger.ex(e);
}
updateState(STATE_ERROR_UNOFFICIAL, null, null, null, mConfig.getVersion(), null);
mState.update(State.ERROR_UNOFFICIAL, null, null, null, mConfig.getVersion(), null);
return null;
}
@ -1183,7 +1123,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
public void onProgress(float progress, long current, long total) {
long now = SystemClock.elapsedRealtime();
if (now >= last[0] + 16L) {
updateState(_state, progress, current, total, _filename,
mState.update(_state, progress, current, total, _filename,
SystemClock.elapsedRealtime() - last[1]);
last[0] = now;
}
@ -1220,7 +1160,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (mStopDownload >= 0) {
Logger.d("download stopped");
} else {
updateState(STATE_ERROR_DOWNLOAD, null, null, null,
mState.update(State.ERROR_DOWNLOAD, null, null, null,
fn, null);
Logger.d("download error");
mNotificationManager.cancel(NOTIFICATION_BUSY);
@ -1249,7 +1189,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
while (true) {
try {
long current = _currentOut + _file.length();
updateState(STATE_ACTION_APPLYING_PATCH,
mState.update(State.ACTION_APPLYING_PATCH,
((float) current / (float) _totalOut) * 100f,
current, _totalOut, _display,
SystemClock.elapsedRealtime() - _start);
@ -1358,12 +1298,12 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (!isSupportedVersion()) {
// TODO - to be more generic this should maybe use the info from getNewestFullBuild
updateState(STATE_ERROR_UNOFFICIAL, null, null, null, mConfig.getVersion(), null);
mState.update(State.ERROR_UNOFFICIAL, null, null, null, mConfig.getVersion(), null);
Logger.i("Ignoring request to check for updates - not compatible for update! " + mConfig.getVersion());
return false;
}
if (!mNetworkState.isConnected()) {
updateState(STATE_ERROR_CONNECTION);
mState.update(State.ERROR_CONNECTION);
Logger.i("Ignoring request to check for updates - no data connection");
return false;
}
@ -1394,7 +1334,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
}
private long getDeltaDownloadSize(List<DeltaInfo> deltas) {
updateState(STATE_ACTION_CHECKING);
mState.update(State.ACTION_CHECKING);
long deltaDownloadSize = 0L;
for (DeltaInfo di : deltas) {
@ -1402,7 +1342,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (di.getUpdate().match(
new File(fn),
true,
getSUMProgress(STATE_ACTION_CHECKING_SUM, di.getUpdate()
getSUMProgress(State.ACTION_CHECKING_SUM, di.getUpdate()
.getName())) == di.getUpdate().getUpdate()) {
di.getUpdate().setTag(fn);
} else {
@ -1418,7 +1358,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (lastDelta.getSignature().match(
new File(fn),
true,
getSUMProgress(STATE_ACTION_CHECKING_SUM, lastDelta
getSUMProgress(State.ACTION_CHECKING_SUM, lastDelta
.getSignature().getName())) == lastDelta
.getSignature().getUpdate()) {
lastDelta.getSignature().setTag(fn);
@ -1429,7 +1369,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
}
}
updateState(STATE_ACTION_CHECKING);
mState.update(State.ACTION_CHECKING);
return deltaDownloadSize;
}
@ -1478,7 +1418,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
DeltaInfo firstDelta = deltas.get(0);
updateState(STATE_ACTION_SEARCHING);
mState.update(State.ACTION_SEARCHING);
String initialFile = null;
@ -1499,7 +1439,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
match = firstDelta.getIn().match(
new File(expectedLocation),
true,
getSUMProgress(STATE_ACTION_SEARCHING_SUM, firstDelta
getSUMProgress(State.ACTION_SEARCHING_SUM, firstDelta
.getIn().getName()));
if (match != null) {
initialFile = expectedLocation;
@ -1521,7 +1461,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
DeltaInfo lastDelta = deltas.get(deltas.size() - 1);
final String[] filename = new String[] { null };
updateState(STATE_ACTION_DOWNLOADING, 0f, 0L, totalDownloadSize, null,
mState.update(State.ACTION_DOWNLOADING, 0f, 0L, totalDownloadSize, null,
null);
final long[] last = new long[] { 0, totalDownloadSize, 0,
@ -1534,7 +1474,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
progress = ((float) current / (float) total) * 100f;
long now = SystemClock.elapsedRealtime();
if (now >= last[2] + 250L) {
updateState(STATE_ACTION_DOWNLOADING, progress, current,
mState.update(State.ACTION_DOWNLOADING, progress, current,
total, filename[0], SystemClock.elapsedRealtime()
- last[3]);
setDownloadNotificationProgress(progress, current, total,
@ -1565,7 +1505,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
return false;
}
}
updateState(STATE_ACTION_DOWNLOADING, 100f, totalDownloadSize,
mState.update(State.ACTION_DOWNLOADING, 100f, totalDownloadSize,
totalDownloadSize, null, null);
return true;
@ -1601,17 +1541,17 @@ public class UpdateService extends Service implements OnNetworkStateListener,
autoState(false, PREF_AUTO_DOWNLOAD_DISABLED, false);
mNotificationManager.cancel(NOTIFICATION_BUSY);
} else if (mStopDownload != PREF_STOP_DOWNLOAD_RESUME &&
!mState.equals(STATE_ERROR_DOWNLOAD)) {
!mState.equals(State.ERROR_DOWNLOAD)) {
// either pause or error
final Long current = f.length();
final Long total = mPrefs.getLong(PREF_DOWNLOAD_SIZE, 1500000000L /* 1.5GB */);
final Long lastTime = mPrefs.getLong(PREF_LAST_DOWNLOAD_TIME, 0);
final float progress = ((float) current / (float) total) * 100f;
final boolean isPause = mStopDownload == PREF_STOP_DOWNLOAD_PAUSE;
final String newState = isPause ? STATE_ACTION_DOWNLOADING_PAUSED
: STATE_ERROR_DOWNLOAD_RESUME;
final String newState = isPause ? State.ACTION_DOWNLOADING_PAUSED
: State.ERROR_DOWNLOAD_RESUME;
Logger.d("download " + (isPause ? "paused" : "error"));
updateState(newState, progress, current, total, imageName, lastTime);
mState.update(newState, progress, current, total, imageName, lastTime);
// display paused notification with the proper title
String title = getString(isPause
? R.string.state_action_downloading_paused
@ -1635,7 +1575,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
try {
String fileSUM = getFileSHA256(
file,
getSUMProgress(STATE_ACTION_CHECKING_SUM,
getSUMProgress(State.ACTION_CHECKING_SUM,
file.getName()));
boolean sumCheck = fileSUM.equals(latestFullSUM);
Logger.d("fileSUM=" + fileSUM + " latestFullSUM=" + latestFullSUM);
@ -1712,7 +1652,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (initialFileNeedsProcessing) {
if (!zipadjust(initialFile, tempFiles[tempFile], start,
current, total)) {
updateState(STATE_ERROR_UNKNOWN, null, null, null, null,
mState.update(State.ERROR_UNKNOWN, null, null, null, null,
null);
Logger.d("zipadjust error");
return false;
@ -1733,7 +1673,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (!dedelta(inFile, mConfig.getPathBase()
+ di.getUpdate().getName(), outFile, start, current,
total)) {
updateState(STATE_ERROR_UNKNOWN, null, null, null, null,
mState.update(State.ERROR_UNKNOWN, null, null, null, null,
null);
Logger.d("dedelta error");
return false;
@ -1748,7 +1688,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
+ lastDelta.getSignature().getName(),
mConfig.getPathBase() + lastDelta.getOut().getName(),
start, current, total)) {
updateState(STATE_ERROR_UNKNOWN, null, null, null, null,
mState.update(State.ERROR_UNKNOWN, null, null, null, null,
null);
Logger.d("dedelta error");
return false;
@ -1801,9 +1741,9 @@ public class UpdateService extends Service implements OnNetworkStateListener,
mPrefs.edit().putString(PREF_CURRENT_FILENAME_NAME, flashFilename).commit();
}
startABRebootNotification(flashFilename);
updateState(STATE_ACTION_AB_FINISHED);
mState.update(State.ACTION_AB_FINISHED);
} else {
updateState(STATE_ERROR_AB_FLASH, null, null, null, null, null, errorCode);
mState.update(State.ERROR_AB_FLASH, null, null, null, null, null, errorCode);
}
}
@ -1862,7 +1802,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
try {
flashFilename = handleUpdateCleanup();
} catch (Exception ex) {
updateState(STATE_ERROR_AB_FLASH);
mState.update(State.ERROR_AB_FLASH);
mIsUpdateRunning = false;
Logger.ex(ex);
return;
@ -1875,7 +1815,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
mPrefs.edit().putLong(PREF_DOWNLOAD_SIZE, -1).commit();
final String _filename = new File(flashFilename).getName();
updateState(STATE_ACTION_AB_FLASH, 0f, 0L, 100L, _filename, null);
mState.update(State.ACTION_AB_FLASH, 0f, 0L, 100L, _filename, null);
newFlashNotification(_filename);
@ -1889,12 +1829,12 @@ public class UpdateService extends Service implements OnNetworkStateListener,
mIsUpdateRunning = true;
if (!ABUpdate.start(flashFilename, mProgressListener, this)) {
mNotificationManager.cancel(NOTIFICATION_UPDATE);
updateState(STATE_ERROR_AB_FLASH);
mState.update(State.ERROR_AB_FLASH);
mIsUpdateRunning = false;
}
} else {
mNotificationManager.cancel(NOTIFICATION_UPDATE);
updateState(STATE_ERROR_AB_FLASH);
mState.update(State.ERROR_AB_FLASH);
mIsUpdateRunning = false;
}
} catch (Exception ex) {
@ -1924,7 +1864,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
try {
flashFilename = handleUpdateCleanup();
} catch (Exception ex) {
updateState(STATE_ERROR_FLASH);
mState.update(State.ERROR_FLASH);
Logger.ex(ex);
return;
}
@ -2028,14 +1968,14 @@ public class UpdateService extends Service implements OnNetworkStateListener,
dst.delete();
Logger.d("flashUpdate - Could not install OTA package:");
Logger.ex(e);
updateState(STATE_ERROR_FLASH);
mState.update(State.ERROR_FLASH);
}
}
} catch (Exception e) {
// We have failed to write something. There's not really anything
// else to do at this stage than give up. No reason to crash though.
Logger.ex(e);
updateState(STATE_ERROR_FLASH);
mState.update(State.ERROR_FLASH);
}
}
@ -2096,30 +2036,6 @@ public class UpdateService extends Service implements OnNetworkStateListener,
return true;
}
public static boolean isProgressState(String state) {
return state.equals(UpdateService.STATE_ACTION_DOWNLOADING) ||
state.equals(UpdateService.STATE_ACTION_SEARCHING) ||
state.equals(UpdateService.STATE_ACTION_SEARCHING_SUM) ||
state.equals(UpdateService.STATE_ACTION_CHECKING) ||
state.equals(UpdateService.STATE_ACTION_CHECKING_SUM) ||
state.equals(UpdateService.STATE_ACTION_APPLYING) ||
state.equals(UpdateService.STATE_ACTION_APPLYING_SUM) ||
state.equals(UpdateService.STATE_ACTION_APPLYING_PATCH) ||
state.equals(UpdateService.STATE_ACTION_AB_FLASH);
}
public static boolean isErrorState(String state) {
return state.equals(UpdateService.STATE_ERROR_DOWNLOAD) ||
state.equals(UpdateService.STATE_ERROR_DOWNLOAD_RESUME) ||
state.equals(UpdateService.STATE_ERROR_DISK_SPACE) ||
state.equals(UpdateService.STATE_ERROR_UNKNOWN) ||
state.equals(UpdateService.STATE_ERROR_UNOFFICIAL) ||
state.equals(UpdateService.STATE_ERROR_CONNECTION) ||
state.equals(UpdateService.STATE_ERROR_AB_FLASH) ||
state.equals(UpdateService.STATE_ERROR_FLASH_FILE) ||
state.equals(UpdateService.STATE_ERROR_FLASH);
}
private boolean isSnoozeNotification() {
// check if we're snoozed, using abs for clock changes
boolean timeSnooze = Math.abs(System.currentTimeMillis()
@ -2168,7 +2084,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
private void checkForUpdatesAsync(final boolean userInitiated, final int checkOnly) {
Logger.d("checkForUpdatesAsync " + getPrefs().getAll());
updateState(STATE_ACTION_CHECKING);
mState.update(State.ACTION_CHECKING);
mWakeLock.acquire();
mWifiLock.acquire();
@ -2283,7 +2199,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (di.getOut()
.match(new File(fn),
true,
getSUMProgress(STATE_ACTION_CHECKING_SUM, di.getOut()
getSUMProgress(State.ACTION_CHECKING_SUM, di.getOut()
.getName())) != null) {
if (latestFullBuild.equals(di.getOut().getName())) {
boolean signedFile = di.getOut().isSignedFile(new File(fn));
@ -2429,7 +2345,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
Logger.d("requiredSpace = " + requiredSpace + " freeSpace = " + freeSpace);
if (freeSpace < requiredSpace) {
updateState(STATE_ERROR_DISK_SPACE, null, freeSpace, requiredSpace,
mState.update(State.ERROR_DISK_SPACE, null, freeSpace, requiredSpace,
null, null);
Logger.d("not enough space!");
return;
@ -2454,9 +2370,9 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (lastDelta.getOut().match(
new File(mConfig.getPathBase() + lastDelta.getOut().getName()),
true,
getSUMProgress(STATE_ACTION_APPLYING_SUM, lastDelta.getOut()
getSUMProgress(State.ACTION_APPLYING_SUM, lastDelta.getOut()
.getName())) == null) {
updateState(STATE_ERROR_UNKNOWN);
mState.update(State.ERROR_UNKNOWN);
Logger.d("final verification error");
return;
}
@ -2487,11 +2403,11 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (latestFullSUM != null) {
downloadFullBuild(latestFullFetch, latestFullSUM, latestFullBuild); // download full
} else {
updateState(STATE_ERROR_DOWNLOAD);
mState.update(State.ERROR_DOWNLOAD);
Logger.d("aborting download due to sha256sum not found");
}
} else {
updateState(STATE_ERROR_DOWNLOAD);
mState.update(State.ERROR_DOWNLOAD);
Logger.d("aborting download due to network state");
}
}
@ -2501,7 +2417,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
if (mWifiLock.isHeld()) mWifiLock.release();
if (mWakeLock.isHeld()) mWakeLock.release();
if (isErrorState(mState)) {
if (mState.isErrorState()) {
mFailedUpdateCount++;
clearState();
if (!userInitiated) {
@ -2536,7 +2452,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
private boolean checkForFinishedUpdate() {
final boolean finished =
mPrefs.getBoolean(PREF_PENDING_REBOOT, false) ||
mState.equals(STATE_ACTION_AB_FINISHED) ||
mState.equals(State.ACTION_AB_FINISHED) ||
ABUpdate.isInstallingUpdate(this);
if (finished) {
final String lastFilename = mPrefs.getString(PREF_CURRENT_AB_FILENAME_NAME, null);
@ -2549,7 +2465,7 @@ public class UpdateService extends Service implements OnNetworkStateListener,
private boolean checkPermissions() {
if (!Environment.isExternalStorageManager()) {
Logger.d("checkPermissions failed");
updateState(STATE_ERROR_PERMISSIONS);
mState.update(State.ERROR_PERMISSIONS);
return false;
}
return true;
@ -2581,16 +2497,16 @@ public class UpdateService extends Service implements OnNetworkStateListener,
Logger.d("Flash file set: %s", flashFilename);
File fn = new File(flashFilename);
if (!fn.exists()) {
updateState(STATE_ERROR_FLASH_FILE);
mState.update(State.ERROR_FLASH_FILE);
return;
}
if (!fn.getName().endsWith(".zip")) {
updateState(STATE_ERROR_FLASH_FILE);
mState.update(State.ERROR_FLASH_FILE);
return;
}
Logger.d("Set flash possible: %s", flashFilename);
mPrefs.edit().putString(PREF_READY_FILENAME_NAME, flashFilename).commit();
updateState(STATE_ACTION_FLASH_FILE_READY, null, null, null, (new File(flashFilename)).getName(), null);
mState.update(State.ACTION_FLASH_FILE_READY, null, null, null, (new File(flashFilename)).getName(), null);
}
private void createNotificationChannel() {