Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 17 | package com.android.dynsystem; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 18 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 19 | import static android.os.AsyncTask.Status.FINISHED; |
| 20 | import static android.os.AsyncTask.Status.PENDING; |
| 21 | import static android.os.AsyncTask.Status.RUNNING; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 22 | import static android.os.image.DynamicSystemClient.ACTION_NOTIFY_IF_IN_USE; |
| 23 | import static android.os.image.DynamicSystemClient.ACTION_START_INSTALL; |
| 24 | import static android.os.image.DynamicSystemClient.CAUSE_ERROR_EXCEPTION; |
| 25 | import static android.os.image.DynamicSystemClient.CAUSE_ERROR_INVALID_URL; |
| 26 | import static android.os.image.DynamicSystemClient.CAUSE_ERROR_IO; |
| 27 | import static android.os.image.DynamicSystemClient.CAUSE_INSTALL_CANCELLED; |
| 28 | import static android.os.image.DynamicSystemClient.CAUSE_INSTALL_COMPLETED; |
| 29 | import static android.os.image.DynamicSystemClient.CAUSE_NOT_SPECIFIED; |
| 30 | import static android.os.image.DynamicSystemClient.STATUS_IN_PROGRESS; |
| 31 | import static android.os.image.DynamicSystemClient.STATUS_IN_USE; |
| 32 | import static android.os.image.DynamicSystemClient.STATUS_NOT_STARTED; |
| 33 | import static android.os.image.DynamicSystemClient.STATUS_READY; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 34 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 35 | import static com.android.dynsystem.InstallationAsyncTask.RESULT_ERROR_EXCEPTION; |
| 36 | import static com.android.dynsystem.InstallationAsyncTask.RESULT_ERROR_INVALID_URL; |
| 37 | import static com.android.dynsystem.InstallationAsyncTask.RESULT_ERROR_IO; |
| 38 | import static com.android.dynsystem.InstallationAsyncTask.RESULT_OK; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 39 | |
| 40 | import android.app.Notification; |
| 41 | import android.app.NotificationChannel; |
| 42 | import android.app.NotificationManager; |
| 43 | import android.app.PendingIntent; |
| 44 | import android.app.Service; |
| 45 | import android.content.Context; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 46 | import android.content.Intent; |
| 47 | import android.os.Bundle; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 48 | import android.os.Handler; |
| 49 | import android.os.IBinder; |
| 50 | import android.os.Message; |
| 51 | import android.os.Messenger; |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 52 | import android.os.ParcelableException; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 53 | import android.os.PowerManager; |
| 54 | import android.os.RemoteException; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 55 | import android.os.image.DynamicSystemClient; |
| 56 | import android.os.image.DynamicSystemManager; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 57 | import android.util.Log; |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 58 | import android.widget.Toast; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 59 | |
| 60 | import java.lang.ref.WeakReference; |
| 61 | import java.util.ArrayList; |
| 62 | |
| 63 | /** |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 64 | * This class is the service in charge of DynamicSystem installation. |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 65 | * It also posts status to notification bar and wait for user's |
| 66 | * cancel and confirm commnands. |
| 67 | */ |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 68 | public class DynamicSystemInstallationService extends Service |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 69 | implements InstallationAsyncTask.InstallStatusListener { |
| 70 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 71 | private static final String TAG = "DynSystemInstallationService"; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 72 | |
Po-Chien Hsueh | dfd69d7 | 2019-05-07 17:23:44 +0800 | [diff] [blame] | 73 | |
| 74 | // TODO (b/131866826): This is currently for test only. Will move this to System API. |
| 75 | static final String KEY_ENABLE_WHEN_COMPLETED = "KEY_ENABLE_WHEN_COMPLETED"; |
| 76 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 77 | /* |
| 78 | * Intent actions |
| 79 | */ |
| 80 | private static final String ACTION_CANCEL_INSTALL = |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 81 | "com.android.dynsystem.ACTION_CANCEL_INSTALL"; |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 82 | private static final String ACTION_DISCARD_INSTALL = |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 83 | "com.android.dynsystem.ACTION_DISCARD_INSTALL"; |
| 84 | private static final String ACTION_REBOOT_TO_DYN_SYSTEM = |
| 85 | "com.android.dynsystem.ACTION_REBOOT_TO_DYN_SYSTEM"; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 86 | private static final String ACTION_REBOOT_TO_NORMAL = |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 87 | "com.android.dynsystem.ACTION_REBOOT_TO_NORMAL"; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * For notification |
| 91 | */ |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 92 | private static final String NOTIFICATION_CHANNEL_ID = "com.android.dynsystem"; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 93 | private static final int NOTIFICATION_ID = 1; |
| 94 | |
| 95 | /* |
| 96 | * IPC |
| 97 | */ |
| 98 | /** Keeps track of all current registered clients. */ |
| 99 | ArrayList<Messenger> mClients = new ArrayList<>(); |
| 100 | |
| 101 | /** Handler of incoming messages from clients. */ |
| 102 | final Messenger mMessenger = new Messenger(new IncomingHandler(this)); |
| 103 | |
| 104 | static class IncomingHandler extends Handler { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 105 | private final WeakReference<DynamicSystemInstallationService> mWeakService; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 106 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 107 | IncomingHandler(DynamicSystemInstallationService service) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 108 | mWeakService = new WeakReference<>(service); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void handleMessage(Message msg) { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 113 | DynamicSystemInstallationService service = mWeakService.get(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 114 | |
| 115 | if (service != null) { |
| 116 | service.handleMessage(msg); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 121 | private DynamicSystemManager mDynSystem; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 122 | private NotificationManager mNM; |
| 123 | |
| 124 | private long mSystemSize; |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 125 | private long mUserdataSize; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 126 | private long mInstalledSize; |
| 127 | private boolean mJustCancelledByUser; |
| 128 | |
Po-Chien Hsueh | dfd69d7 | 2019-05-07 17:23:44 +0800 | [diff] [blame] | 129 | // This is for testing only now |
| 130 | private boolean mEnableWhenCompleted; |
| 131 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 132 | private InstallationAsyncTask mInstallTask; |
| 133 | |
| 134 | |
| 135 | @Override |
| 136 | public void onCreate() { |
| 137 | super.onCreate(); |
| 138 | |
| 139 | prepareNotification(); |
| 140 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 141 | mDynSystem = (DynamicSystemManager) getSystemService(Context.DYNAMIC_SYSTEM_SERVICE); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void onDestroy() { |
| 146 | // Cancel the persistent notification. |
| 147 | mNM.cancel(NOTIFICATION_ID); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public IBinder onBind(Intent intent) { |
| 152 | return mMessenger.getBinder(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 157 | String action = intent.getAction(); |
| 158 | |
| 159 | Log.d(TAG, "onStartCommand(): action=" + action); |
| 160 | |
| 161 | if (ACTION_START_INSTALL.equals(action)) { |
| 162 | executeInstallCommand(intent); |
| 163 | } else if (ACTION_CANCEL_INSTALL.equals(action)) { |
| 164 | executeCancelCommand(); |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 165 | } else if (ACTION_DISCARD_INSTALL.equals(action)) { |
| 166 | executeDiscardCommand(); |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 167 | } else if (ACTION_REBOOT_TO_DYN_SYSTEM.equals(action)) { |
| 168 | executeRebootToDynSystemCommand(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 169 | } else if (ACTION_REBOOT_TO_NORMAL.equals(action)) { |
| 170 | executeRebootToNormalCommand(); |
| 171 | } else if (ACTION_NOTIFY_IF_IN_USE.equals(action)) { |
| 172 | executeNotifyIfInUseCommand(); |
| 173 | } |
| 174 | |
| 175 | return Service.START_NOT_STICKY; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onProgressUpdate(long installedSize) { |
| 180 | mInstalledSize = installedSize; |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 181 | postStatus(STATUS_IN_PROGRESS, CAUSE_NOT_SPECIFIED, null); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 185 | public void onResult(int result, Throwable detail) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 186 | if (result == RESULT_OK) { |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 187 | postStatus(STATUS_READY, CAUSE_INSTALL_COMPLETED, null); |
Po-Chien Hsueh | dfd69d7 | 2019-05-07 17:23:44 +0800 | [diff] [blame] | 188 | |
| 189 | // For testing: enable DSU and restart the device when install completed |
| 190 | if (mEnableWhenCompleted) { |
| 191 | executeRebootToDynSystemCommand(); |
| 192 | } |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
| 196 | // if it's not successful, reset the task and stop self. |
| 197 | resetTaskAndStop(); |
| 198 | |
| 199 | switch (result) { |
| 200 | case RESULT_ERROR_IO: |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 201 | postStatus(STATUS_NOT_STARTED, CAUSE_ERROR_IO, detail); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 202 | break; |
| 203 | |
| 204 | case RESULT_ERROR_INVALID_URL: |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 205 | postStatus(STATUS_NOT_STARTED, CAUSE_ERROR_INVALID_URL, detail); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 206 | break; |
| 207 | |
| 208 | case RESULT_ERROR_EXCEPTION: |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 209 | postStatus(STATUS_NOT_STARTED, CAUSE_ERROR_EXCEPTION, detail); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void onCancelled() { |
| 216 | resetTaskAndStop(); |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 217 | postStatus(STATUS_NOT_STARTED, CAUSE_INSTALL_CANCELLED, null); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private void executeInstallCommand(Intent intent) { |
| 221 | if (!verifyRequest(intent)) { |
| 222 | Log.e(TAG, "Verification failed. Did you use VerificationActivity?"); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | if (mInstallTask != null) { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 227 | Log.e(TAG, "There is already an installation task running"); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 231 | if (isInDynamicSystem()) { |
| 232 | Log.e(TAG, "We are already running in DynamicSystem"); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 236 | String url = intent.getDataString(); |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 237 | mSystemSize = intent.getLongExtra(DynamicSystemClient.KEY_SYSTEM_SIZE, 0); |
| 238 | mUserdataSize = intent.getLongExtra(DynamicSystemClient.KEY_USERDATA_SIZE, 0); |
Po-Chien Hsueh | dfd69d7 | 2019-05-07 17:23:44 +0800 | [diff] [blame] | 239 | mEnableWhenCompleted = intent.getBooleanExtra(KEY_ENABLE_WHEN_COMPLETED, false); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 240 | |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 241 | mInstallTask = new InstallationAsyncTask( |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 242 | url, mSystemSize, mUserdataSize, this, mDynSystem, this); |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 243 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 244 | mInstallTask.execute(); |
| 245 | |
| 246 | // start fore ground |
| 247 | startForeground(NOTIFICATION_ID, |
| 248 | buildNotification(STATUS_IN_PROGRESS, CAUSE_NOT_SPECIFIED)); |
| 249 | } |
| 250 | |
| 251 | private void executeCancelCommand() { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 252 | if (mInstallTask == null || mInstallTask.getStatus() != RUNNING) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 253 | Log.e(TAG, "Cancel command triggered, but there is no task running"); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | |
| 257 | mJustCancelledByUser = true; |
| 258 | |
| 259 | if (mInstallTask.cancel(false)) { |
| 260 | // Will cleanup and post status in onCancelled() |
| 261 | Log.d(TAG, "Cancel request filed successfully"); |
| 262 | } else { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 263 | Log.e(TAG, "Trying to cancel installation while it's already completed."); |
| 264 | } |
| 265 | } |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 266 | |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 267 | private void executeDiscardCommand() { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 268 | if (isInDynamicSystem()) { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 269 | Log.e(TAG, "We are now running in AOT, please reboot to normal system first"); |
| 270 | return; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 271 | } |
| 272 | |
Po-Chien Hsueh | 91508ea | 2019-04-01 16:51:47 +0800 | [diff] [blame] | 273 | if (!isDynamicSystemInstalled() && (getStatus() != STATUS_READY)) { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 274 | Log.e(TAG, "Trying to discard AOT while there is no complete installation"); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | Toast.makeText(this, |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 279 | getString(R.string.toast_dynsystem_discarded), |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 280 | Toast.LENGTH_LONG).show(); |
| 281 | |
| 282 | resetTaskAndStop(); |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 283 | postStatus(STATUS_NOT_STARTED, CAUSE_INSTALL_CANCELLED, null); |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 284 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 285 | mDynSystem.remove(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 286 | } |
| 287 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 288 | private void executeRebootToDynSystemCommand() { |
Po-Chien Hsueh | 91508ea | 2019-04-01 16:51:47 +0800 | [diff] [blame] | 289 | boolean enabled = false; |
| 290 | |
Po-Chien Hsueh | dfd69d7 | 2019-05-07 17:23:44 +0800 | [diff] [blame] | 291 | if (mInstallTask != null && mInstallTask.getResult() == RESULT_OK) { |
Po-Chien Hsueh | 91508ea | 2019-04-01 16:51:47 +0800 | [diff] [blame] | 292 | enabled = mInstallTask.commit(); |
| 293 | } else if (isDynamicSystemInstalled()) { |
Howard Chen | 295d749 | 2019-08-02 17:54:49 +0800 | [diff] [blame] | 294 | enabled = mDynSystem.setEnable(true, true); |
Po-Chien Hsueh | 91508ea | 2019-04-01 16:51:47 +0800 | [diff] [blame] | 295 | } else { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 296 | Log.e(TAG, "Trying to reboot to AOT while there is no complete installation"); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
Po-Chien Hsueh | 91508ea | 2019-04-01 16:51:47 +0800 | [diff] [blame] | 300 | if (enabled) { |
| 301 | PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 302 | |
| 303 | if (powerManager != null) { |
| 304 | powerManager.reboot("dynsystem"); |
| 305 | } |
| 306 | } else { |
| 307 | Log.e(TAG, "Failed to enable DynamicSystem because of native runtime error."); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 308 | mNM.cancel(NOTIFICATION_ID); |
| 309 | |
Po-Chien Hsueh | 646d9f3 | 2019-03-06 14:20:22 +0800 | [diff] [blame] | 310 | Toast.makeText(this, |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 311 | getString(R.string.toast_failed_to_reboot_to_dynsystem), |
Po-Chien Hsueh | 646d9f3 | 2019-03-06 14:20:22 +0800 | [diff] [blame] | 312 | Toast.LENGTH_LONG).show(); |
| 313 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 314 | mDynSystem.remove(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | private void executeRebootToNormalCommand() { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 319 | if (!isInDynamicSystem()) { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 320 | Log.e(TAG, "It's already running in normal system."); |
| 321 | return; |
| 322 | } |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 323 | |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 324 | // Per current design, we don't have disable() API. AOT is disabled on next reboot. |
| 325 | // TODO: Use better status query when b/125079548 is done. |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 326 | PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 327 | |
| 328 | if (powerManager != null) { |
| 329 | powerManager.reboot(null); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | private void executeNotifyIfInUseCommand() { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 334 | int status = getStatus(); |
| 335 | |
| 336 | if (status == STATUS_IN_USE) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 337 | startForeground(NOTIFICATION_ID, |
| 338 | buildNotification(STATUS_IN_USE, CAUSE_NOT_SPECIFIED)); |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 339 | } else if (status == STATUS_READY) { |
| 340 | startForeground(NOTIFICATION_ID, |
| 341 | buildNotification(STATUS_READY, CAUSE_NOT_SPECIFIED)); |
Po-Chien Hsueh | 6769739 | 2019-04-10 15:30:16 +0800 | [diff] [blame] | 342 | } else { |
| 343 | stopSelf(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | private void resetTaskAndStop() { |
| 348 | mInstallTask = null; |
| 349 | |
| 350 | stopForeground(true); |
| 351 | |
| 352 | // stop self, but this service is not destroyed yet if it's still bound |
| 353 | stopSelf(); |
| 354 | } |
| 355 | |
| 356 | private void prepareNotification() { |
| 357 | NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, |
| 358 | getString(R.string.notification_channel_name), |
| 359 | NotificationManager.IMPORTANCE_LOW); |
| 360 | |
| 361 | mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 362 | |
| 363 | if (mNM != null) { |
| 364 | mNM.createNotificationChannel(chan); |
| 365 | } |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 366 | } |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 367 | |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 368 | private PendingIntent createPendingIntent(String action) { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 369 | Intent intent = new Intent(this, DynamicSystemInstallationService.class); |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 370 | intent.setAction(action); |
| 371 | return PendingIntent.getService(this, 0, intent, 0); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | private Notification buildNotification(int status, int cause) { |
| 375 | Notification.Builder builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID) |
| 376 | .setSmallIcon(R.drawable.ic_system_update_googblue_24dp) |
| 377 | .setProgress(0, 0, false); |
| 378 | |
| 379 | switch (status) { |
| 380 | case STATUS_IN_PROGRESS: |
| 381 | builder.setContentText(getString(R.string.notification_install_inprogress)); |
| 382 | |
Po-Chien Hsueh | 9c1500f | 2019-03-04 14:47:46 +0800 | [diff] [blame] | 383 | int max = (int) Math.max((mSystemSize + mUserdataSize) >> 20, 1); |
| 384 | int progress = (int) (mInstalledSize >> 20); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 385 | |
| 386 | builder.setProgress(max, progress, false); |
| 387 | |
| 388 | builder.addAction(new Notification.Action.Builder( |
| 389 | null, getString(R.string.notification_action_cancel), |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 390 | createPendingIntent(ACTION_CANCEL_INSTALL)).build()); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 391 | |
| 392 | break; |
| 393 | |
| 394 | case STATUS_READY: |
| 395 | builder.setContentText(getString(R.string.notification_install_completed)); |
| 396 | |
| 397 | builder.addAction(new Notification.Action.Builder( |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 398 | null, getString(R.string.notification_action_discard), |
| 399 | createPendingIntent(ACTION_DISCARD_INSTALL)).build()); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 400 | |
Po-Chien Hsueh | 9a1627f | 2019-04-01 10:40:51 +0800 | [diff] [blame] | 401 | builder.addAction(new Notification.Action.Builder( |
| 402 | null, getString(R.string.notification_action_reboot_to_dynsystem), |
| 403 | createPendingIntent(ACTION_REBOOT_TO_DYN_SYSTEM)).build()); |
| 404 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 405 | break; |
| 406 | |
| 407 | case STATUS_IN_USE: |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 408 | builder.setContentText(getString(R.string.notification_dynsystem_in_use)); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 409 | |
| 410 | builder.addAction(new Notification.Action.Builder( |
| 411 | null, getString(R.string.notification_action_uninstall), |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 412 | createPendingIntent(ACTION_REBOOT_TO_NORMAL)).build()); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 413 | |
| 414 | break; |
| 415 | |
| 416 | case STATUS_NOT_STARTED: |
| 417 | if (cause != CAUSE_NOT_SPECIFIED && cause != CAUSE_INSTALL_CANCELLED) { |
| 418 | builder.setContentText(getString(R.string.notification_install_failed)); |
| 419 | } else { |
| 420 | // no need to notify the user if the task is not started, or cancelled. |
| 421 | } |
| 422 | break; |
| 423 | |
| 424 | default: |
| 425 | throw new IllegalStateException("status is invalid"); |
| 426 | } |
| 427 | |
| 428 | return builder.build(); |
| 429 | } |
| 430 | |
| 431 | private boolean verifyRequest(Intent intent) { |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 432 | String url = intent.getDataString(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 433 | |
| 434 | return VerificationActivity.isVerified(url); |
| 435 | } |
| 436 | |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 437 | private void postStatus(int status, int cause, Throwable detail) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 438 | Log.d(TAG, "postStatus(): statusCode=" + status + ", causeCode=" + cause); |
| 439 | |
| 440 | boolean notifyOnNotificationBar = true; |
| 441 | |
| 442 | if (status == STATUS_NOT_STARTED |
| 443 | && cause == CAUSE_INSTALL_CANCELLED |
| 444 | && mJustCancelledByUser) { |
| 445 | // if task is cancelled by user, do not notify them |
| 446 | notifyOnNotificationBar = false; |
| 447 | mJustCancelledByUser = false; |
| 448 | } |
| 449 | |
| 450 | if (notifyOnNotificationBar) { |
| 451 | mNM.notify(NOTIFICATION_ID, buildNotification(status, cause)); |
| 452 | } |
| 453 | |
| 454 | for (int i = mClients.size() - 1; i >= 0; i--) { |
| 455 | try { |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 456 | notifyOneClient(mClients.get(i), status, cause, detail); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 457 | } catch (RemoteException e) { |
| 458 | mClients.remove(i); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 463 | private void notifyOneClient(Messenger client, int status, int cause, Throwable detail) |
| 464 | throws RemoteException { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 465 | Bundle bundle = new Bundle(); |
| 466 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 467 | bundle.putLong(DynamicSystemClient.KEY_INSTALLED_SIZE, mInstalledSize); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 468 | |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 469 | if (detail != null) { |
| 470 | bundle.putSerializable(DynamicSystemClient.KEY_EXCEPTION_DETAIL, |
| 471 | new ParcelableException(detail)); |
| 472 | } |
| 473 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 474 | client.send(Message.obtain(null, |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 475 | DynamicSystemClient.MSG_POST_STATUS, status, cause, bundle)); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | private int getStatus() { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 479 | if (isInDynamicSystem()) { |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 480 | return STATUS_IN_USE; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 481 | } else if (isDynamicSystemInstalled()) { |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 482 | return STATUS_READY; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 483 | } else if (mInstallTask == null) { |
| 484 | return STATUS_NOT_STARTED; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | switch (mInstallTask.getStatus()) { |
| 488 | case PENDING: |
| 489 | return STATUS_NOT_STARTED; |
| 490 | |
| 491 | case RUNNING: |
| 492 | return STATUS_IN_PROGRESS; |
| 493 | |
| 494 | case FINISHED: |
| 495 | int result = mInstallTask.getResult(); |
| 496 | |
| 497 | if (result == RESULT_OK) { |
| 498 | return STATUS_READY; |
| 499 | } else { |
| 500 | throw new IllegalStateException("A failed InstallationTask is not reset"); |
| 501 | } |
| 502 | |
| 503 | default: |
| 504 | return STATUS_NOT_STARTED; |
| 505 | } |
| 506 | } |
| 507 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 508 | private boolean isInDynamicSystem() { |
| 509 | return mDynSystem.isInUse(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 510 | } |
| 511 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 512 | private boolean isDynamicSystemInstalled() { |
| 513 | return mDynSystem.isInstalled(); |
Po-Chien Hsueh | 34de5af | 2019-03-05 16:00:45 +0800 | [diff] [blame] | 514 | } |
| 515 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 516 | void handleMessage(Message msg) { |
| 517 | switch (msg.what) { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 518 | case DynamicSystemClient.MSG_REGISTER_LISTENER: |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 519 | try { |
| 520 | Messenger client = msg.replyTo; |
| 521 | |
| 522 | int status = getStatus(); |
| 523 | |
| 524 | // tell just registered client my status, but do not specify cause |
Po-Chien Hsueh | a5bd084 | 2019-03-19 12:56:19 +0800 | [diff] [blame] | 525 | notifyOneClient(client, status, CAUSE_NOT_SPECIFIED, null); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 526 | |
| 527 | mClients.add(client); |
| 528 | } catch (RemoteException e) { |
| 529 | // do nothing if we cannot send update to the client |
| 530 | e.printStackTrace(); |
| 531 | } |
| 532 | |
| 533 | break; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 534 | case DynamicSystemClient.MSG_UNREGISTER_LISTENER: |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 535 | mClients.remove(msg.replyTo); |
| 536 | break; |
| 537 | default: |
| 538 | // do nothing |
| 539 | } |
| 540 | } |
| 541 | } |