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; |
| 24 | import android.os.UpdateEngine; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | import android.view.View; |
| 27 | import android.widget.ArrayAdapter; |
| 28 | import android.widget.Button; |
| 29 | import android.widget.ProgressBar; |
| 30 | import android.widget.Spinner; |
| 31 | import android.widget.TextView; |
| 32 | import android.widget.Toast; |
| 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 | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 37 | import com.example.android.systemupdatersample.util.PayloadSpecs; |
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; |
| 58 | private ProgressBar mProgressBar; |
| 59 | private TextView mTextViewStatus; |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 60 | private TextView mTextViewCompletion; |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 61 | private TextView mTextViewUpdateInfo; |
| 62 | private Button mButtonSwitchSlot; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 63 | |
| 64 | private List<UpdateConfig> mConfigs; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 65 | |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 66 | private final UpdateManager mUpdateManager = |
| 67 | new UpdateManager(new UpdateEngine(), new PayloadSpecs()); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 68 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 69 | @Override |
| 70 | protected void onCreate(Bundle savedInstanceState) { |
| 71 | super.onCreate(savedInstanceState); |
| 72 | setContentView(R.layout.activity_main); |
| 73 | |
| 74 | this.mTextViewBuild = findViewById(R.id.textViewBuild); |
| 75 | this.mSpinnerConfigs = findViewById(R.id.spinnerConfigs); |
| 76 | this.mTextViewConfigsDirHint = findViewById(R.id.textViewConfigsDirHint); |
| 77 | this.mButtonReload = findViewById(R.id.buttonReload); |
| 78 | this.mButtonApplyConfig = findViewById(R.id.buttonApplyConfig); |
| 79 | this.mButtonStop = findViewById(R.id.buttonStop); |
| 80 | this.mButtonReset = findViewById(R.id.buttonReset); |
| 81 | this.mProgressBar = findViewById(R.id.progressBar); |
| 82 | this.mTextViewStatus = findViewById(R.id.textViewStatus); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 83 | this.mTextViewCompletion = findViewById(R.id.textViewCompletion); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 84 | this.mTextViewUpdateInfo = findViewById(R.id.textViewUpdateInfo); |
| 85 | this.mButtonSwitchSlot = findViewById(R.id.buttonSwitchSlot); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 86 | |
| 87 | this.mTextViewConfigsDirHint.setText(UpdateConfigs.getConfigsRoot(this)); |
| 88 | |
| 89 | uiReset(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 90 | loadUpdateConfigs(); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 91 | |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 92 | this.mUpdateManager.setOnEngineStatusUpdateCallback(this::onStatusUpdate); |
| 93 | this.mUpdateManager.setOnProgressUpdateCallback(this::onProgressUpdate); |
| 94 | this.mUpdateManager.setOnEngineCompleteCallback(this::onPayloadApplicationComplete); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void onDestroy() { |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 99 | this.mUpdateManager.setOnEngineStatusUpdateCallback(null); |
| 100 | this.mUpdateManager.setOnProgressUpdateCallback(null); |
| 101 | this.mUpdateManager.setOnEngineCompleteCallback(null); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 102 | super.onDestroy(); |
| 103 | } |
| 104 | |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 105 | @Override |
| 106 | protected void onResume() { |
| 107 | super.onResume(); |
| 108 | this.mUpdateManager.bind(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void onPause() { |
| 113 | this.mUpdateManager.unbind(); |
| 114 | super.onPause(); |
| 115 | } |
| 116 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 117 | /** |
| 118 | * reload button is clicked |
| 119 | */ |
| 120 | public void onReloadClick(View view) { |
| 121 | loadUpdateConfigs(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * view config button is clicked |
| 126 | */ |
| 127 | public void onViewConfigClick(View view) { |
| 128 | UpdateConfig config = mConfigs.get(mSpinnerConfigs.getSelectedItemPosition()); |
| 129 | new AlertDialog.Builder(this) |
| 130 | .setTitle(config.getName()) |
| 131 | .setMessage(config.getRawJson()) |
| 132 | .setPositiveButton(R.string.close, (dialog, id) -> dialog.dismiss()) |
| 133 | .show(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * apply config button is clicked |
| 138 | */ |
| 139 | public void onApplyConfigClick(View view) { |
| 140 | new AlertDialog.Builder(this) |
| 141 | .setTitle("Apply Update") |
| 142 | .setMessage("Do you really want to apply this update?") |
| 143 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 144 | .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> { |
| 145 | uiSetUpdating(); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 146 | mUpdateManager.applyUpdate(this, getSelectedConfig()); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 147 | }) |
| 148 | .setNegativeButton(android.R.string.cancel, null) |
| 149 | .show(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * stop button clicked |
| 154 | */ |
| 155 | public void onStopClick(View view) { |
| 156 | new AlertDialog.Builder(this) |
| 157 | .setTitle("Stop Update") |
| 158 | .setMessage("Do you really want to cancel running update?") |
| 159 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 160 | .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> { |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 161 | mUpdateManager.cancelRunningUpdate(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 162 | }) |
| 163 | .setNegativeButton(android.R.string.cancel, null).show(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * reset button clicked |
| 168 | */ |
| 169 | public void onResetClick(View view) { |
| 170 | new AlertDialog.Builder(this) |
| 171 | .setTitle("Reset Update") |
| 172 | .setMessage("Do you really want to cancel running update" |
| 173 | + " and restore old version?") |
| 174 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 175 | .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> { |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 176 | mUpdateManager.resetUpdate(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 177 | }) |
| 178 | .setNegativeButton(android.R.string.cancel, null).show(); |
| 179 | } |
| 180 | |
| 181 | /** |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 182 | * switch slot button clicked |
| 183 | */ |
| 184 | public void onSwitchSlotClick(View view) { |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 185 | mUpdateManager.setSwitchSlotOnReboot(); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 189 | * Invoked when anything changes. The value of {@code status} will |
| 190 | * be one of the values from {@link UpdateEngine.UpdateStatusConstants}, |
| 191 | * and {@code percent} will be from {@code 0.0} to {@code 1.0}. |
| 192 | */ |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 193 | private void onStatusUpdate(int status) { |
| 194 | runOnUiThread(() -> { |
| 195 | Log.e("UpdateEngine", "StatusUpdate - status=" |
| 196 | + UpdateEngineStatuses.getStatusText(status) |
| 197 | + "/" + status); |
| 198 | Toast.makeText(this, "Update Status changed", Toast.LENGTH_LONG) |
| 199 | .show(); |
| 200 | if (status == UpdateEngine.UpdateStatusConstants.IDLE) { |
| 201 | Log.d(TAG, "status changed, resetting ui"); |
| 202 | uiReset(); |
| 203 | } else { |
| 204 | Log.d(TAG, "status changed, setting ui to updating mode"); |
| 205 | uiSetUpdating(); |
| 206 | } |
| 207 | setUiStatus(status); |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | private void onProgressUpdate(double progress) { |
| 212 | mProgressBar.setProgress((int) (100 * progress)); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Invoked when the payload has been applied, whether successfully or |
| 217 | * unsuccessfully. The value of {@code errorCode} will be one of the |
| 218 | * values from {@link UpdateEngine.ErrorCodeConstants}. |
| 219 | */ |
| 220 | private void onPayloadApplicationComplete(int errorCode) { |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 221 | final String state = UpdateEngineErrorCodes.isUpdateSucceeded(errorCode) |
| 222 | ? "SUCCESS" |
| 223 | : "FAILURE"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 224 | runOnUiThread(() -> { |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 225 | Log.i("UpdateEngine", |
| 226 | "Completed - errorCode=" |
| 227 | + UpdateEngineErrorCodes.getCodeName(errorCode) + "/" + errorCode |
| 228 | + " " + state); |
| 229 | Toast.makeText(this, "Update completed", Toast.LENGTH_LONG).show(); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 230 | setUiCompletion(errorCode); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 231 | if (errorCode == UpdateEngineErrorCodes.UPDATED_BUT_NOT_ACTIVE) { |
| 232 | // if update was successfully applied. |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 233 | if (mUpdateManager.manualSwitchSlotRequired()) { |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 234 | // Show "Switch Slot" button. |
| 235 | uiShowSwitchSlotInfo(); |
| 236 | } |
| 237 | } |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 238 | }); |
| 239 | } |
| 240 | |
| 241 | /** resets ui */ |
| 242 | private void uiReset() { |
| 243 | mTextViewBuild.setText(Build.DISPLAY); |
| 244 | mSpinnerConfigs.setEnabled(true); |
| 245 | mButtonReload.setEnabled(true); |
| 246 | mButtonApplyConfig.setEnabled(true); |
| 247 | mButtonStop.setEnabled(false); |
| 248 | mButtonReset.setEnabled(false); |
| 249 | mProgressBar.setProgress(0); |
| 250 | mProgressBar.setEnabled(false); |
| 251 | mProgressBar.setVisibility(ProgressBar.INVISIBLE); |
| 252 | mTextViewStatus.setText(R.string.unknown); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 253 | mTextViewCompletion.setText(R.string.unknown); |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 254 | uiHideSwitchSlotInfo(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** sets ui updating mode */ |
| 258 | private void uiSetUpdating() { |
| 259 | mTextViewBuild.setText(Build.DISPLAY); |
| 260 | mSpinnerConfigs.setEnabled(false); |
| 261 | mButtonReload.setEnabled(false); |
| 262 | mButtonApplyConfig.setEnabled(false); |
| 263 | mButtonStop.setEnabled(true); |
| 264 | mProgressBar.setEnabled(true); |
| 265 | mButtonReset.setEnabled(true); |
| 266 | mProgressBar.setVisibility(ProgressBar.VISIBLE); |
| 267 | } |
| 268 | |
Zhomart Mukhamejanov | 238beb7 | 2018-05-09 16:25:40 -0700 | [diff] [blame] | 269 | private void uiShowSwitchSlotInfo() { |
| 270 | mButtonSwitchSlot.setEnabled(true); |
| 271 | mTextViewUpdateInfo.setTextColor(Color.parseColor("#777777")); |
| 272 | } |
| 273 | |
| 274 | private void uiHideSwitchSlotInfo() { |
| 275 | mTextViewUpdateInfo.setTextColor(Color.parseColor("#AAAAAA")); |
| 276 | mButtonSwitchSlot.setEnabled(false); |
| 277 | } |
| 278 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 279 | /** |
| 280 | * loads json configurations from configs dir that is defined in {@link UpdateConfigs}. |
| 281 | */ |
| 282 | private void loadUpdateConfigs() { |
| 283 | mConfigs = UpdateConfigs.getUpdateConfigs(this); |
| 284 | loadConfigsToSpinner(mConfigs); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @param status update engine status code |
| 289 | */ |
| 290 | private void setUiStatus(int status) { |
| 291 | String statusText = UpdateEngineStatuses.getStatusText(status); |
Zhomart Mukhamejanov | f7a7038 | 2018-05-02 20:37:12 -0700 | [diff] [blame] | 292 | mTextViewStatus.setText(statusText + "/" + status); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @param errorCode update engine error code |
| 297 | */ |
| 298 | private void setUiCompletion(int errorCode) { |
| 299 | final String state = UpdateEngineErrorCodes.isUpdateSucceeded(errorCode) |
| 300 | ? "SUCCESS" |
| 301 | : "FAILURE"; |
| 302 | String errorText = UpdateEngineErrorCodes.getCodeName(errorCode); |
| 303 | mTextViewCompletion.setText(state + " " + errorText + "/" + errorCode); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | private void loadConfigsToSpinner(List<UpdateConfig> configs) { |
| 307 | String[] spinnerArray = UpdateConfigs.configsToNames(configs); |
| 308 | ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this, |
| 309 | android.R.layout.simple_spinner_item, |
| 310 | spinnerArray); |
| 311 | spinnerArrayAdapter.setDropDownViewResource(android.R.layout |
| 312 | .simple_spinner_dropdown_item); |
| 313 | mSpinnerConfigs.setAdapter(spinnerArrayAdapter); |
| 314 | } |
| 315 | |
| 316 | private UpdateConfig getSelectedConfig() { |
| 317 | return mConfigs.get(mSpinnerConfigs.getSelectedItemPosition()); |
| 318 | } |
| 319 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 320 | } |