Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.example.android.systemupdatersample.ui; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 21 | import android.graphics.Color; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 22 | import android.os.Build; |
| 23 | import android.os.Bundle; |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 24 | import android.os.Handler; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 25 | import android.os.UpdateEngine; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | import android.view.View; |
| 28 | import android.widget.ArrayAdapter; |
| 29 | import android.widget.Button; |
| 30 | import android.widget.ProgressBar; |
| 31 | import android.widget.Spinner; |
| 32 | import android.widget.TextView; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 33 | |
| 34 | import com.example.android.systemupdatersample.R; |
| 35 | import com.example.android.systemupdatersample.UpdateConfig; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 36 | import com.example.android.systemupdatersample.UpdateManager; |
Zhomart Mukhamejanov | 674aa6c | 2018-05-25 17:00:11 -0700 | [diff] [blame] | 37 | import com.example.android.systemupdatersample.UpdaterState; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 38 | import com.example.android.systemupdatersample.util.UpdateConfigs; |
| 39 | import com.example.android.systemupdatersample.util.UpdateEngineErrorCodes; |
| 40 | import com.example.android.systemupdatersample.util.UpdateEngineStatuses; |
| 41 | |
| 42 | import java.util.List; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * UI for SystemUpdaterSample app. |
| 46 | */ |
| 47 | public class MainActivity extends Activity { |
| 48 | |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 49 | private static final String TAG = "MainActivity"; |
| 50 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 51 | private TextView mTextViewBuild; |
| 52 | private Spinner mSpinnerConfigs; |
| 53 | private TextView mTextViewConfigsDirHint; |
| 54 | private Button mButtonReload; |
| 55 | private Button mButtonApplyConfig; |
| 56 | private Button mButtonStop; |
| 57 | private Button mButtonReset; |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 58 | private Button mButtonSuspend; |
| 59 | private Button mButtonResume; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 60 | private ProgressBar mProgressBar; |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 61 | private TextView mTextViewUpdaterState; |
| 62 | private TextView mTextViewEngineStatus; |
| 63 | private TextView mTextViewEngineErrorCode; |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 64 | private TextView mTextViewUpdateInfo; |
| 65 | private Button mButtonSwitchSlot; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 66 | |
| 67 | private List<UpdateConfig> mConfigs; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 68 | |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 69 | private final UpdateManager mUpdateManager = |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 70 | new UpdateManager(new UpdateEngine(), new Handler()); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 71 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 72 | @Override |
| 73 | protected void onCreate(Bundle savedInstanceState) { |
| 74 | super.onCreate(savedInstanceState); |
| 75 | setContentView(R.layout.activity_main); |
| 76 | |
| 77 | this.mTextViewBuild = findViewById(R.id.textViewBuild); |
| 78 | this.mSpinnerConfigs = findViewById(R.id.spinnerConfigs); |
| 79 | this.mTextViewConfigsDirHint = findViewById(R.id.textViewConfigsDirHint); |
| 80 | this.mButtonReload = findViewById(R.id.buttonReload); |
| 81 | this.mButtonApplyConfig = findViewById(R.id.buttonApplyConfig); |
| 82 | this.mButtonStop = findViewById(R.id.buttonStop); |
| 83 | this.mButtonReset = findViewById(R.id.buttonReset); |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 84 | this.mButtonSuspend = findViewById(R.id.buttonSuspend); |
| 85 | this.mButtonResume = findViewById(R.id.buttonResume); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 86 | this.mProgressBar = findViewById(R.id.progressBar); |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 87 | this.mTextViewUpdaterState = findViewById(R.id.textViewUpdaterState); |
| 88 | this.mTextViewEngineStatus = findViewById(R.id.textViewEngineStatus); |
| 89 | this.mTextViewEngineErrorCode = findViewById(R.id.textViewEngineErrorCode); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 90 | this.mTextViewUpdateInfo = findViewById(R.id.textViewUpdateInfo); |
| 91 | this.mButtonSwitchSlot = findViewById(R.id.buttonSwitchSlot); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 92 | |
| 93 | this.mTextViewConfigsDirHint.setText(UpdateConfigs.getConfigsRoot(this)); |
| 94 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 95 | uiResetWidgets(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 96 | loadUpdateConfigs(); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 97 | |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 98 | this.mUpdateManager.setOnStateChangeCallback(this::onUpdaterStateChange); |
| 99 | this.mUpdateManager.setOnEngineStatusUpdateCallback(this::onEngineStatusUpdate); |
| 100 | this.mUpdateManager.setOnEngineCompleteCallback(this::onEnginePayloadApplicationComplete); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 101 | this.mUpdateManager.setOnProgressUpdateCallback(this::onProgressUpdate); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | protected void onDestroy() { |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 106 | this.mUpdateManager.setOnEngineStatusUpdateCallback(null); |
| 107 | this.mUpdateManager.setOnProgressUpdateCallback(null); |
| 108 | this.mUpdateManager.setOnEngineCompleteCallback(null); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 109 | super.onDestroy(); |
| 110 | } |
| 111 | |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 112 | @Override |
| 113 | protected void onResume() { |
| 114 | super.onResume(); |
Zhomart Mukhamejanov | 7671f68 | 2018-05-24 09:11:47 -0700 | [diff] [blame] | 115 | // Binding to UpdateEngine invokes onStatusUpdate callback, |
| 116 | // persisted updater state has to be loaded and prepared beforehand. |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 117 | this.mUpdateManager.bind(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | protected void onPause() { |
| 122 | this.mUpdateManager.unbind(); |
| 123 | super.onPause(); |
| 124 | } |
| 125 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 126 | /** |
| 127 | * reload button is clicked |
| 128 | */ |
| 129 | public void onReloadClick(View view) { |
| 130 | loadUpdateConfigs(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * view config button is clicked |
| 135 | */ |
| 136 | public void onViewConfigClick(View view) { |
| 137 | UpdateConfig config = mConfigs.get(mSpinnerConfigs.getSelectedItemPosition()); |
| 138 | new AlertDialog.Builder(this) |
| 139 | .setTitle(config.getName()) |
| 140 | .setMessage(config.getRawJson()) |
| 141 | .setPositiveButton(R.string.close, (dialog, id) -> dialog.dismiss()) |
| 142 | .show(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * apply config button is clicked |
| 147 | */ |
| 148 | public void onApplyConfigClick(View view) { |
| 149 | new AlertDialog.Builder(this) |
| 150 | .setTitle("Apply Update") |
| 151 | .setMessage("Do you really want to apply this update?") |
| 152 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 153 | .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> { |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 154 | uiResetWidgets(); |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 155 | uiResetEngineText(); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 156 | applyUpdate(getSelectedConfig()); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 157 | }) |
| 158 | .setNegativeButton(android.R.string.cancel, null) |
| 159 | .show(); |
| 160 | } |
| 161 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 162 | private void applyUpdate(UpdateConfig config) { |
| 163 | try { |
| 164 | mUpdateManager.applyUpdate(this, config); |
| 165 | } catch (UpdaterState.InvalidTransitionException e) { |
| 166 | Log.e(TAG, "Failed to apply update " + config.getName(), e); |
| 167 | } |
| 168 | } |
| 169 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 170 | /** |
| 171 | * stop button clicked |
| 172 | */ |
| 173 | public void onStopClick(View view) { |
| 174 | new AlertDialog.Builder(this) |
| 175 | .setTitle("Stop Update") |
| 176 | .setMessage("Do you really want to cancel running update?") |
| 177 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 178 | .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> { |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 179 | cancelRunningUpdate(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 180 | }) |
| 181 | .setNegativeButton(android.R.string.cancel, null).show(); |
| 182 | } |
| 183 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 184 | private void cancelRunningUpdate() { |
| 185 | try { |
| 186 | mUpdateManager.cancelRunningUpdate(); |
| 187 | } catch (UpdaterState.InvalidTransitionException e) { |
| 188 | Log.e(TAG, "Failed to cancel running update", e); |
| 189 | } |
| 190 | } |
| 191 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 192 | /** |
| 193 | * reset button clicked |
| 194 | */ |
| 195 | public void onResetClick(View view) { |
| 196 | new AlertDialog.Builder(this) |
| 197 | .setTitle("Reset Update") |
| 198 | .setMessage("Do you really want to cancel running update" |
| 199 | + " and restore old version?") |
| 200 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 201 | .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> { |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 202 | resetUpdate(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 203 | }) |
| 204 | .setNegativeButton(android.R.string.cancel, null).show(); |
| 205 | } |
| 206 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 207 | private void resetUpdate() { |
| 208 | try { |
| 209 | mUpdateManager.resetUpdate(); |
| 210 | } catch (UpdaterState.InvalidTransitionException e) { |
| 211 | Log.e(TAG, "Failed to reset update", e); |
| 212 | } |
| 213 | } |
| 214 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 215 | /** |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 216 | * suspend button clicked |
| 217 | */ |
| 218 | public void onSuspendClick(View view) { |
| 219 | try { |
| 220 | mUpdateManager.suspend(); |
| 221 | } catch (UpdaterState.InvalidTransitionException e) { |
| 222 | Log.e(TAG, "Failed to suspend running update", e); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * resume button clicked |
| 228 | */ |
| 229 | public void onResumeClick(View view) { |
| 230 | try { |
| 231 | uiResetWidgets(); |
| 232 | uiResetEngineText(); |
| 233 | mUpdateManager.resume(); |
| 234 | } catch (UpdaterState.InvalidTransitionException e) { |
| 235 | Log.e(TAG, "Failed to resume running update", e); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 240 | * switch slot button clicked |
| 241 | */ |
| 242 | public void onSwitchSlotClick(View view) { |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 243 | uiResetWidgets(); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 244 | mUpdateManager.setSwitchSlotOnReboot(); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 248 | * Invoked when SystemUpdaterSample app state changes. |
| 249 | * Value of {@code state} will be one of the |
Zhomart Mukhamejanov | 674aa6c | 2018-05-25 17:00:11 -0700 | [diff] [blame] | 250 | * values from {@link UpdaterState}. |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 251 | */ |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 252 | private void onUpdaterStateChange(int state) { |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 253 | Log.i(TAG, "UpdaterStateChange state=" |
| 254 | + UpdaterState.getStateText(state) |
| 255 | + "/" + state); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 256 | runOnUiThread(() -> { |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 257 | setUiUpdaterState(state); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 258 | |
| 259 | if (state == UpdaterState.IDLE) { |
| 260 | uiStateIdle(); |
| 261 | } else if (state == UpdaterState.RUNNING) { |
| 262 | uiStateRunning(); |
| 263 | } else if (state == UpdaterState.PAUSED) { |
| 264 | uiStatePaused(); |
| 265 | } else if (state == UpdaterState.ERROR) { |
| 266 | uiStateError(); |
| 267 | } else if (state == UpdaterState.SLOT_SWITCH_REQUIRED) { |
| 268 | uiStateSlotSwitchRequired(); |
| 269 | } else if (state == UpdaterState.REBOOT_REQUIRED) { |
| 270 | uiStateRebootRequired(); |
| 271 | } |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 272 | }); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Invoked when {@link UpdateEngine} status changes. Value of {@code status} will |
| 277 | * be one of the values from {@link UpdateEngine.UpdateStatusConstants}. |
| 278 | */ |
| 279 | private void onEngineStatusUpdate(int status) { |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 280 | Log.i(TAG, "StatusUpdate - status=" |
| 281 | + UpdateEngineStatuses.getStatusText(status) |
| 282 | + "/" + status); |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 283 | runOnUiThread(() -> { |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 284 | setUiEngineStatus(status); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 285 | }); |
| 286 | } |
| 287 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Invoked when the payload has been applied, whether successfully or |
| 290 | * unsuccessfully. The value of {@code errorCode} will be one of the |
| 291 | * values from {@link UpdateEngine.ErrorCodeConstants}. |
| 292 | */ |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 293 | private void onEnginePayloadApplicationComplete(int errorCode) { |
| 294 | final String completionState = UpdateEngineErrorCodes.isUpdateSucceeded(errorCode) |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 295 | ? "SUCCESS" |
| 296 | : "FAILURE"; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 297 | Log.i(TAG, |
| 298 | "PayloadApplicationCompleted - errorCode=" |
| 299 | + UpdateEngineErrorCodes.getCodeName(errorCode) + "/" + errorCode |
| 300 | + " " + completionState); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 301 | runOnUiThread(() -> { |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 302 | setUiEngineErrorCode(errorCode); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 303 | }); |
| 304 | } |
| 305 | |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Invoked when update progress changes. |
| 308 | */ |
| 309 | private void onProgressUpdate(double progress) { |
| 310 | mProgressBar.setProgress((int) (100 * progress)); |
| 311 | } |
| 312 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 313 | /** resets ui */ |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 314 | private void uiResetWidgets() { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 315 | mTextViewBuild.setText(Build.DISPLAY); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 316 | mSpinnerConfigs.setEnabled(false); |
| 317 | mButtonReload.setEnabled(false); |
| 318 | mButtonApplyConfig.setEnabled(false); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 319 | mButtonStop.setEnabled(false); |
| 320 | mButtonReset.setEnabled(false); |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 321 | mButtonSuspend.setEnabled(false); |
| 322 | mButtonResume.setEnabled(false); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 323 | mProgressBar.setEnabled(false); |
| 324 | mProgressBar.setVisibility(ProgressBar.INVISIBLE); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 325 | mButtonSwitchSlot.setEnabled(false); |
| 326 | mTextViewUpdateInfo.setTextColor(Color.parseColor("#aaaaaa")); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 329 | private void uiResetEngineText() { |
| 330 | mTextViewEngineStatus.setText(R.string.unknown); |
| 331 | mTextViewEngineErrorCode.setText(R.string.unknown); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 332 | // Note: Do not reset mTextViewUpdaterState; UpdateManager notifies updater state properly. |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 335 | private void uiStateIdle() { |
| 336 | uiResetWidgets(); |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 337 | mButtonReset.setEnabled(true); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 338 | mSpinnerConfigs.setEnabled(true); |
| 339 | mButtonReload.setEnabled(true); |
| 340 | mButtonApplyConfig.setEnabled(true); |
| 341 | mProgressBar.setProgress(0); |
| 342 | } |
| 343 | |
| 344 | private void uiStateRunning() { |
| 345 | uiResetWidgets(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 346 | mProgressBar.setEnabled(true); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 347 | mProgressBar.setVisibility(ProgressBar.VISIBLE); |
| 348 | mButtonStop.setEnabled(true); |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 349 | mButtonSuspend.setEnabled(true); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | private void uiStatePaused() { |
| 353 | uiResetWidgets(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 354 | mButtonReset.setEnabled(true); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 355 | mProgressBar.setEnabled(true); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 356 | mProgressBar.setVisibility(ProgressBar.VISIBLE); |
Zhomart Mukhamejanov | 16db994 | 2018-05-31 10:47:09 -0700 | [diff] [blame] | 357 | mButtonResume.setEnabled(true); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 360 | private void uiStateSlotSwitchRequired() { |
| 361 | uiResetWidgets(); |
| 362 | mButtonReset.setEnabled(true); |
| 363 | mProgressBar.setEnabled(true); |
| 364 | mProgressBar.setVisibility(ProgressBar.VISIBLE); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 365 | mButtonSwitchSlot.setEnabled(true); |
| 366 | mTextViewUpdateInfo.setTextColor(Color.parseColor("#777777")); |
| 367 | } |
| 368 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 369 | private void uiStateError() { |
| 370 | uiResetWidgets(); |
| 371 | mButtonReset.setEnabled(true); |
| 372 | mProgressBar.setEnabled(true); |
| 373 | mProgressBar.setVisibility(ProgressBar.VISIBLE); |
| 374 | } |
| 375 | |
| 376 | private void uiStateRebootRequired() { |
| 377 | uiResetWidgets(); |
| 378 | mButtonReset.setEnabled(true); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 381 | /** |
| 382 | * loads json configurations from configs dir that is defined in {@link UpdateConfigs}. |
| 383 | */ |
| 384 | private void loadUpdateConfigs() { |
| 385 | mConfigs = UpdateConfigs.getUpdateConfigs(this); |
| 386 | loadConfigsToSpinner(mConfigs); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * @param status update engine status code |
| 391 | */ |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 392 | private void setUiEngineStatus(int status) { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 393 | String statusText = UpdateEngineStatuses.getStatusText(status); |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 394 | mTextViewEngineStatus.setText(statusText + "/" + status); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * @param errorCode update engine error code |
| 399 | */ |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 400 | private void setUiEngineErrorCode(int errorCode) { |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 401 | String errorText = UpdateEngineErrorCodes.getCodeName(errorCode); |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 402 | mTextViewEngineErrorCode.setText(errorText + "/" + errorCode); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * @param state updater sample state |
| 407 | */ |
| 408 | private void setUiUpdaterState(int state) { |
Zhomart Mukhamejanov | 674aa6c | 2018-05-25 17:00:11 -0700 | [diff] [blame] | 409 | String stateText = UpdaterState.getStateText(state); |
Zhomart Mukhamejanov | 8f4059d | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 410 | mTextViewUpdaterState.setText(stateText + "/" + state); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | private void loadConfigsToSpinner(List<UpdateConfig> configs) { |
| 414 | String[] spinnerArray = UpdateConfigs.configsToNames(configs); |
| 415 | ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this, |
| 416 | android.R.layout.simple_spinner_item, |
| 417 | spinnerArray); |
| 418 | spinnerArrayAdapter.setDropDownViewResource(android.R.layout |
| 419 | .simple_spinner_dropdown_item); |
| 420 | mSpinnerConfigs.setAdapter(spinnerArrayAdapter); |
| 421 | } |
| 422 | |
| 423 | private UpdateConfig getSelectedConfig() { |
| 424 | return mConfigs.get(mSpinnerConfigs.getSelectedItemPosition()); |
| 425 | } |
| 426 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 427 | } |