Debian Codegraph @

core/java/android/os/image/DynamicSystemClient.java

raw symbols SCIP
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 package android.os.image;
17
18 import android.annotation.BytesLong;
19 import android.annotation.CallbackExecutor;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.RequiresPermission;
24 import android.annotation.SystemApi;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.ServiceConnection;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.IBinder;
33 import android.os.Looper;
34 import android.os.Message;
35 import android.os.Messenger;
36 import android.os.ParcelableException;
37 import android.os.RemoteException;
38 import android.util.Slog;
39
40 import java.lang.annotation.Retention;
41 import java.lang.annotation.RetentionPolicy;
42 import java.lang.ref.WeakReference;
43 import java.util.concurrent.Executor;
44
45 /**
46 * <p>This class contains methods and constants used to start a {@code DynamicSystem} installation,
47 * and a listener for status updates.</p>
48 *
49 * <p>{@code DynamicSystem} allows users to run certified system images in a non destructive manner
50 * without needing to prior OEM unlock. It creates a temporary system partition to install the new
51 * system image, and a temporary data partition for the newly installed system to run with.</p>
52 *
53 * After the installation is completed, the device will be running in the new system on next the
54 * reboot. Then, when the user reboots the device again, it will leave {@code DynamicSystem} and go
55 * back to the original system. While running in {@code DynamicSystem}, persitent storage for
56 * factory reset protection (FRP) remains unchanged. Since the user is running the new system with
57 * a temporarily created data partition, their original user data are kept unchanged.</p>
58 *
59 * <p>With {@link #setOnStatusChangedListener}, API users can register an
60 * {@link #OnStatusChangedListener} to get status updates and their causes when the installation is
61 * started, stopped, or cancelled. It also sends progress updates during the installation. With
62 * {@link #start}, API users can start an installation with the {@link Uri} to a unsparsed and
63 * gzipped system image. The {@link Uri} can be a web URL or a content Uri to a local path.</p>
64 *
65 * @hide
66 */
67 @SystemApi
68 public class DynamicSystemClient {
69 private static final String TAG = "DynamicSystemClient";
70
71 /** @hide */
72 @IntDef(prefix = { "STATUS_" }, value = {
73 STATUS_UNKNOWN,
74 STATUS_NOT_STARTED,
75 STATUS_IN_PROGRESS,
76 STATUS_READY,
77 STATUS_IN_USE,
78 })
79 @Retention(RetentionPolicy.SOURCE)
80 public @interface InstallationStatus {}
81
82 /** @hide */
83 @IntDef(prefix = { "CAUSE_" }, value = {
84 CAUSE_NOT_SPECIFIED,
85 CAUSE_INSTALL_COMPLETED,
86 CAUSE_INSTALL_CANCELLED,
87 CAUSE_ERROR_IO,
88 CAUSE_ERROR_INVALID_URL,
89 CAUSE_ERROR_IPC,
90 CAUSE_ERROR_EXCEPTION,
91 })
92 @Retention(RetentionPolicy.SOURCE)
93 public @interface StatusChangedCause {}
94
95 /** Listener for installation status updates. */
96 public interface OnStatusChangedListener {
97 /**
98 * This callback is called when installation status is changed, and when the
99 * client is {@link #bind} to {@code DynamicSystem} installation service.
100 *
101 * @param status status code, also defined in {@code DynamicSystemClient}.
102 * @param cause cause code, also defined in {@code DynamicSystemClient}.
103 * @param progress number of bytes installed.
104 * @param detail additional detail about the error if available, otherwise null.
105 */
106 void onStatusChanged(@InstallationStatus int status, @StatusChangedCause int cause,
107 @BytesLong long progress, @Nullable Throwable detail);
108 }
109
110 /*
111 * Status codes
112 */
113 /** We are bound to installation service, but failed to get its status */
114 public static final int STATUS_UNKNOWN = 0;
115
116 /** Installation is not started yet. */
117 public static final int STATUS_NOT_STARTED = 1;
118
119 /** Installation is in progress. */
120 public static final int STATUS_IN_PROGRESS = 2;
121
122 /** Installation is finished but the user has not launched it. */
123 public static final int STATUS_READY = 3;
124
125 /** Device is running in {@code DynamicSystem}. */
126 public static final int STATUS_IN_USE = 4;
127
128 /*
129 * Causes
130 */
131 /** Cause is not specified. This means the status is not changed. */
132 public static final int CAUSE_NOT_SPECIFIED = 0;
133
134 /** Status changed because installation is completed. */
135 public static final int CAUSE_INSTALL_COMPLETED = 1;
136
137 /** Status changed because installation is cancelled. */
138 public static final int CAUSE_INSTALL_CANCELLED = 2;
139
140 /** Installation failed due to {@code IOException}. */
141 public static final int CAUSE_ERROR_IO = 3;
142
143 /** Installation failed because the image URL source is not supported. */
144 public static final int CAUSE_ERROR_INVALID_URL = 4;
145
146 /** Installation failed due to IPC error. */
147 public static final int CAUSE_ERROR_IPC = 5;
148
149 /** Installation failed due to unhandled exception. */
150 public static final int CAUSE_ERROR_EXCEPTION = 6;
151
152 /*
153 * IPC Messages
154 */
155 /**
156 * Message to register listener.
157 * @hide
158 */
159 public static final int MSG_REGISTER_LISTENER = 1;
160
161 /**
162 * Message to unregister listener.
163 * @hide
164 */
165 public static final int MSG_UNREGISTER_LISTENER = 2;
166
167 /**
168 * Message for status updates.
169 * @hide
170 */
171 public static final int MSG_POST_STATUS = 3;
172
173 /*
174 * Messages keys
175 */
176 /**
177 * Message key, for progress updates.
178 * @hide
179 */
180 public static final String KEY_INSTALLED_SIZE = "KEY_INSTALLED_SIZE";
181
182 /**
183 * Message key, used when the service is sending exception detail to the client.
184 * @hide
185 */
186 public static final String KEY_EXCEPTION_DETAIL = "KEY_EXCEPTION_DETAIL";
187
188 /*
189 * Intent Actions
190 */
191 /**
192 * Intent action: start installation.
193 * @hide
194 */
195 public static final String ACTION_START_INSTALL =
196 "android.os.image.action.START_INSTALL";
197
198 /**
199 * Intent action: notify user if we are currently running in {@code DynamicSystem}.
200 * @hide
201 */
202 public static final String ACTION_NOTIFY_IF_IN_USE =
203 "android.os.image.action.NOTIFY_IF_IN_USE";
204
205 /**
206 * Intent action: hide notifications about the status of {@code DynamicSystem}.
207 * @hide
208 */
209 public static final String ACTION_HIDE_NOTIFICATION =
210 "android.os.image.action.HIDE_NOTIFICATION";
211
212 /*
213 * Intent Keys
214 */
215 /**
216 * Intent key: Size of the system image, in bytes.
217 * @hide
218 */
219 public static final String KEY_SYSTEM_SIZE = "KEY_SYSTEM_SIZE";
220
221 /**
222 * Intent key: Number of bytes to reserve for userdata.
223 * @hide
224 */
225 public static final String KEY_USERDATA_SIZE = "KEY_USERDATA_SIZE";
226
227 /**
228 * Intent key: Whether to enable DynamicSystem immediately after installation is done.
229 * Note this will reboot the device automatically.
230 * @hide
231 */
232 public static final String KEY_ENABLE_WHEN_COMPLETED = "KEY_ENABLE_WHEN_COMPLETED";
233
234 /**
235 * Intent key: Whether to leave DynamicSystem on device reboot.
236 * False indicates a sticky mode where device stays in DynamicSystem across reboots.
237 * @hide
238 */
239 public static final String KEY_ONE_SHOT = "KEY_ONE_SHOT";
240
241 private static class IncomingHandler extends Handler {
242 private final WeakReference<DynamicSystemClient> mWeakClient;
243
244 IncomingHandler(DynamicSystemClient service) {
245 super(Looper.getMainLooper());
246 mWeakClient = new WeakReference<>(service);
247 }
248
249 @Override
250 public void handleMessage(Message msg) {
251 DynamicSystemClient service = mWeakClient.get();
252
253 if (service != null) {
254 service.handleMessage(msg);
255 }
256 }
257 }
258
259 private class DynSystemServiceConnection implements ServiceConnection {
260 public void onServiceConnected(ComponentName className, IBinder service) {
261 Slog.v(TAG, "onServiceConnected: " + className);
262
263 mService = new Messenger(service);
264
265 try {
266 Message msg = Message.obtain(null, MSG_REGISTER_LISTENER);
267 msg.replyTo = mMessenger;
268
269 mService.send(msg);
270 } catch (RemoteException e) {
271 Slog.e(TAG, "Unable to get status from installation service");
272 notifyOnStatusChangedListener(STATUS_UNKNOWN, CAUSE_ERROR_IPC, 0, e);
273 }
274 }
275
276 public void onServiceDisconnected(ComponentName className) {
277 Slog.v(TAG, "onServiceDisconnected: " + className);
278 mService = null;
279 }
280 }
281
282 private final Context mContext;
283 private final DynSystemServiceConnection mConnection;
284 private final Messenger mMessenger;
285
286 private boolean mBound;
287 private Executor mExecutor;
288 private OnStatusChangedListener mListener;
289 private Messenger mService;
290
291 /**
292 * Create a new {@code DynamicSystem} client.
293 *
294 * @param context a {@link Context} will be used to bind the installation service.
295 *
296 * @hide
297 */
298 @SystemApi
299 public DynamicSystemClient(@NonNull Context context) {
300 mContext = context;
301 mConnection = new DynSystemServiceConnection();
302 mMessenger = new Messenger(new IncomingHandler(this));
303 }
304
305 /**
306 * This method register a listener for status change. The listener is called using
307 * the executor.
308 */
309 public void setOnStatusChangedListener(
310 @NonNull @CallbackExecutor Executor executor,
311 @NonNull OnStatusChangedListener listener) {
312 mListener = listener;
313 mExecutor = executor;
314 }
315
316 /**
317 * This method register a listener for status change. The listener is called in main
318 * thread.
319 */
320 public void setOnStatusChangedListener(
321 @NonNull OnStatusChangedListener listener) {
322 mListener = listener;
323 mExecutor = null;
324 }
325
326 private void notifyOnStatusChangedListener(
327 int status, int cause, long progress, Throwable detail) {
328 if (mListener != null) {
329 if (mExecutor != null) {
330 mExecutor.execute(
331 () -> {
332 mListener.onStatusChanged(status, cause, progress, detail);
333 });
334 } else {
335 mListener.onStatusChanged(status, cause, progress, detail);
336 }
337 }
338 }
339
340 /**
341 * Bind to {@code DynamicSystem} installation service. Binding to the installation service
342 * allows it to send status updates to {@link #OnStatusChangedListener}. It is recommanded
343 * to bind before calling {@link #start} and get status updates.
344 * @hide
345 */
346 @RequiresPermission(android.Manifest.permission.INSTALL_DYNAMIC_SYSTEM)
347 @SystemApi
348 public void bind() {
349 Intent intent = new Intent();
350 intent.setClassName("com.android.dynsystem",
351 "com.android.dynsystem.DynamicSystemInstallationService");
352
353 mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
354
355 mBound = true;
356 }
357
358 /**
359 * Unbind from {@code DynamicSystem} installation service. Unbinding from the installation
360 * service stops it from sending following status updates.
361 * @hide
362 */
363 @RequiresPermission(android.Manifest.permission.INSTALL_DYNAMIC_SYSTEM)
364 @SystemApi
365 public void unbind() {
366 if (!mBound) {
367 return;
368 }
369
370 if (mService != null) {
371 try {
372 Message msg = Message.obtain(null, MSG_UNREGISTER_LISTENER);
373 msg.replyTo = mMessenger;
374 mService.send(msg);
375 } catch (RemoteException e) {
376 Slog.e(TAG, "Unable to unregister from installation service");
377 }
378 }
379
380 // Detach our existing connection.
381 mContext.unbindService(mConnection);
382
383 mBound = false;
384 }
385
386 /**
387 * Start installing {@code DynamicSystem} from URL with default userdata size.
388 *
389 * Calling this function will first start an Activity to confirm device credential, using
390 * {@link KeyguardManager}. If it's confirmed, the installation service will be started.
391 *
392 * This function doesn't require prior calling {@link #bind}.
393 *
394 * @param systemUrl a network Uri, a file Uri or a content Uri pointing to a system image file.
395 * @param systemSize size of system image.
396 * @hide
397 */
398 @RequiresPermission(android.Manifest.permission.INSTALL_DYNAMIC_SYSTEM)
399 @SystemApi
400 public void start(@NonNull Uri systemUrl, @BytesLong long systemSize) {
401 start(systemUrl, systemSize, 0 /* Use the default userdata size */);
402 }
403
404 /**
405 * Start installing {@code DynamicSystem} from URL.
406 *
407 * Calling this function will first start an Activity to confirm device credential, using
408 * {@link KeyguardManager}. If it's confirmed, the installation service will be started.
409 *
410 * This function doesn't require prior calling {@link #bind}.
411 *
412 * @param systemUrl a network Uri, a file Uri or a content Uri pointing to a system image file.
413 * @param systemSize size of system image.
414 * @param userdataSize bytes reserved for userdata.
415 */
416 @RequiresPermission(android.Manifest.permission.INSTALL_DYNAMIC_SYSTEM)
417 public void start(@NonNull Uri systemUrl, @BytesLong long systemSize,
418 @BytesLong long userdataSize) {
419 Intent intent = new Intent();
420
421 intent.setClassName("com.android.dynsystem",
422 "com.android.dynsystem.VerificationActivity");
423
424 intent.setData(systemUrl);
425 intent.setAction(ACTION_START_INSTALL);
426 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
427
428 intent.putExtra(KEY_SYSTEM_SIZE, systemSize);
429 intent.putExtra(KEY_USERDATA_SIZE, userdataSize);
430
431 mContext.startActivity(intent);
432 }
433
434 private void handleMessage(Message msg) {
435 switch (msg.what) {
436 case MSG_POST_STATUS:
437 int status = msg.arg1;
438 int cause = msg.arg2;
439 // obj is non-null
440 Bundle bundle = (Bundle) msg.obj;
441 long progress = bundle.getLong(KEY_INSTALLED_SIZE);
442 ParcelableException t = (ParcelableException) bundle.getSerializable(
443 KEY_EXCEPTION_DETAIL);
444
445 Throwable detail = t == null ? null : t.getCause();
446
447 notifyOnStatusChangedListener(status, cause, progress, detail);
448 break;
449 default:
450 // do nothing
451
452 }
453 }
454 }