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