| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
@SystemApi |
| 68 |
public class DynamicSystemClient { |
| 69 |
private static final String TAG = "DynamicSystemClient"; |
| 70 |
|
| 71 |
|
| 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 |
|
| 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 |
|
| 96 |
public interface OnStatusChangedListener { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void onStatusChanged(@InstallationStatus int status, @StatusChangedCause int cause, |
| 107 |
@BytesLong long progress, @Nullable Throwable detail); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static final int STATUS_UNKNOWN = 0; |
| 115 |
|
| 116 |
|
| 117 |
public static final int STATUS_NOT_STARTED = 1; |
| 118 |
|
| 119 |
|
| 120 |
public static final int STATUS_IN_PROGRESS = 2; |
| 121 |
|
| 122 |
|
| 123 |
public static final int STATUS_READY = 3; |
| 124 |
|
| 125 |
|
| 126 |
public static final int STATUS_IN_USE = 4; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static final int CAUSE_NOT_SPECIFIED = 0; |
| 133 |
|
| 134 |
|
| 135 |
public static final int CAUSE_INSTALL_COMPLETED = 1; |
| 136 |
|
| 137 |
|
| 138 |
public static final int CAUSE_INSTALL_CANCELLED = 2; |
| 139 |
|
| 140 |
|
| 141 |
public static final int CAUSE_ERROR_IO = 3; |
| 142 |
|
| 143 |
|
| 144 |
public static final int CAUSE_ERROR_INVALID_URL = 4; |
| 145 |
|
| 146 |
|
| 147 |
public static final int CAUSE_ERROR_IPC = 5; |
| 148 |
|
| 149 |
|
| 150 |
public static final int CAUSE_ERROR_EXCEPTION = 6; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static final int MSG_REGISTER_LISTENER = 1; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static final int MSG_UNREGISTER_LISTENER = 2; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static final int MSG_POST_STATUS = 3; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public static final String KEY_INSTALLED_SIZE = "KEY_INSTALLED_SIZE"; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public static final String KEY_EXCEPTION_DETAIL = "KEY_EXCEPTION_DETAIL"; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static final String ACTION_START_INSTALL = |
| 196 |
"android.os.image.action.START_INSTALL"; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public static final String ACTION_NOTIFY_IF_IN_USE = |
| 203 |
"android.os.image.action.NOTIFY_IF_IN_USE"; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public static final String ACTION_HIDE_NOTIFICATION = |
| 210 |
"android.os.image.action.HIDE_NOTIFICATION"; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public static final String KEY_SYSTEM_SIZE = "KEY_SYSTEM_SIZE"; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public static final String KEY_USERDATA_SIZE = "KEY_USERDATA_SIZE"; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public static final String KEY_ENABLE_WHEN_COMPLETED = "KEY_ENABLE_WHEN_COMPLETED"; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 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 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 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 |
|
| 307 |
|
| 308 |
|
| 309 |
public void setOnStatusChangedListener( |
| 310 |
@NonNull @CallbackExecutor Executor executor, |
| 311 |
@NonNull OnStatusChangedListener listener) { |
| 312 |
mListener = listener; |
| 313 |
mExecutor = executor; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 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 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 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 |
|
| 360 |
|
| 361 |
|
| 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 |
|
| 381 |
mContext.unbindService(mConnection); |
| 382 |
|
| 383 |
mBound = false; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 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 ); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 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 |
|
| 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 |
|
| 451 |
|
| 452 |
} |
| 453 |
} |
| 454 |
} |