Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.android.server.am; |
| 18 | |
| 19 | import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; |
| 20 | |
| 21 | import android.app.AppOpsManager; |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 22 | import android.app.Notification; |
| 23 | import android.app.NotificationManager; |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 24 | import android.app.PendingIntent; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 25 | import android.content.ComponentName; |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 26 | import android.content.Context; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 27 | import android.content.IIntentReceiver; |
| 28 | import android.content.Intent; |
| 29 | import android.content.pm.ResolveInfo; |
| 30 | import android.os.Bundle; |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 33 | import android.os.Process; |
| 34 | import android.os.UserHandle; |
| 35 | import android.util.Slog; |
| 36 | |
| 37 | import com.android.internal.R; |
| 38 | import com.android.internal.util.ProgressReporter; |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 39 | import com.android.server.UiThread; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 40 | |
| 41 | import java.util.List; |
| 42 | |
| 43 | /** |
| 44 | * Simple broadcaster that sends {@link Intent#ACTION_PRE_BOOT_COMPLETED} to all |
| 45 | * system apps that register for it. Override {@link #onFinished()} to handle |
| 46 | * when all broadcasts are finished. |
| 47 | */ |
| 48 | public abstract class PreBootBroadcaster extends IIntentReceiver.Stub { |
| 49 | private static final String TAG = "PreBootBroadcaster"; |
| 50 | |
| 51 | private final ActivityManagerService mService; |
| 52 | private final int mUserId; |
| 53 | private final ProgressReporter mProgress; |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 54 | private final boolean mQuiet; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 55 | |
| 56 | private final Intent mIntent; |
| 57 | private final List<ResolveInfo> mTargets; |
| 58 | |
| 59 | private int mIndex = 0; |
| 60 | |
| 61 | public PreBootBroadcaster(ActivityManagerService service, int userId, |
Jeff Sharkey | 24d9491 | 2016-07-07 12:33:48 -0600 | [diff] [blame] | 62 | ProgressReporter progress, boolean quiet) { |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 63 | mService = service; |
| 64 | mUserId = userId; |
| 65 | mProgress = progress; |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 66 | mQuiet = quiet; |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 67 | |
| 68 | mIntent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 69 | mIntent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE | Intent.FLAG_DEBUG_TRIAGED_MISSING); |
| 70 | |
| 71 | mTargets = mService.mContext.getPackageManager().queryBroadcastReceiversAsUser(mIntent, |
| 72 | MATCH_SYSTEM_ONLY, UserHandle.of(userId)); |
| 73 | } |
| 74 | |
| 75 | public void sendNext() { |
| 76 | if (mIndex >= mTargets.size()) { |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 77 | mHandler.obtainMessage(MSG_HIDE).sendToTarget(); |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 78 | onFinished(); |
| 79 | return; |
| 80 | } |
| 81 | |
Jeff Sharkey | 100bd9c | 2016-04-11 16:00:54 -0600 | [diff] [blame] | 82 | if (!mService.isUserRunning(mUserId, 0)) { |
| 83 | Slog.i(TAG, "User " + mUserId + " is no longer running; skipping remaining receivers"); |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 84 | mHandler.obtainMessage(MSG_HIDE).sendToTarget(); |
Jeff Sharkey | 100bd9c | 2016-04-11 16:00:54 -0600 | [diff] [blame] | 85 | onFinished(); |
| 86 | return; |
| 87 | } |
| 88 | |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 89 | if (!mQuiet) { |
| 90 | mHandler.obtainMessage(MSG_SHOW, mTargets.size(), mIndex).sendToTarget(); |
| 91 | } |
| 92 | |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 93 | final ResolveInfo ri = mTargets.get(mIndex++); |
| 94 | final ComponentName componentName = ri.activityInfo.getComponentName(); |
| 95 | |
Jeff Sharkey | fd24108 | 2016-04-19 15:58:24 -0600 | [diff] [blame] | 96 | if (mProgress != null) { |
| 97 | final CharSequence label = ri.activityInfo |
| 98 | .loadLabel(mService.mContext.getPackageManager()); |
| 99 | mProgress.setProgress(mIndex, mTargets.size(), |
| 100 | mService.mContext.getString(R.string.android_preparing_apk, label)); |
| 101 | } |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 102 | |
| 103 | Slog.i(TAG, "Pre-boot of " + componentName.toShortString() + " for user " + mUserId); |
| 104 | EventLogTags.writeAmPreBoot(mUserId, componentName.getPackageName()); |
| 105 | |
| 106 | mIntent.setComponent(componentName); |
| 107 | mService.broadcastIntentLocked(null, null, mIntent, null, this, 0, null, null, null, |
| 108 | AppOpsManager.OP_NONE, null, true, false, ActivityManagerService.MY_PID, |
| 109 | Process.SYSTEM_UID, mUserId); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void performReceive(Intent intent, int resultCode, String data, Bundle extras, |
| 114 | boolean ordered, boolean sticky, int sendingUser) { |
| 115 | sendNext(); |
| 116 | } |
| 117 | |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 118 | private static final int MSG_SHOW = 1; |
| 119 | private static final int MSG_HIDE = 2; |
| 120 | |
| 121 | private Handler mHandler = new Handler(UiThread.get().getLooper(), null, true) { |
| 122 | @Override |
| 123 | public void handleMessage(Message msg) { |
| 124 | final Context context = mService.mContext; |
| 125 | final NotificationManager notifManager = context |
| 126 | .getSystemService(NotificationManager.class); |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 127 | final int max = msg.arg1; |
| 128 | final int index = msg.arg2; |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 129 | |
| 130 | switch (msg.what) { |
| 131 | case MSG_SHOW: |
| 132 | final CharSequence title = context |
Jeff Sharkey | a4600cc | 2016-07-29 16:39:16 -0600 | [diff] [blame] | 133 | .getText(R.string.android_upgrading_notification_title); |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 134 | |
| 135 | final Intent intent = new Intent(); |
| 136 | intent.setClassName("com.android.settings", |
| 137 | "com.android.settings.HelpTrampoline"); |
| 138 | intent.putExtra(Intent.EXTRA_TEXT, "help_url_upgrading"); |
| 139 | |
| 140 | final PendingIntent contentIntent; |
| 141 | if (context.getPackageManager().resolveActivity(intent, 0) != null) { |
| 142 | contentIntent = PendingIntent.getActivity(context, 0, intent, 0); |
| 143 | } else { |
| 144 | contentIntent = null; |
| 145 | } |
| 146 | |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 147 | final Notification notif = new Notification.Builder(mService.mContext) |
| 148 | .setSmallIcon(R.drawable.stat_sys_adb) |
| 149 | .setWhen(0) |
| 150 | .setOngoing(true) |
| 151 | .setTicker(title) |
| 152 | .setDefaults(0) |
| 153 | .setPriority(Notification.PRIORITY_MAX) |
| 154 | .setColor(context.getColor( |
| 155 | com.android.internal.R.color.system_notification_accent_color)) |
| 156 | .setContentTitle(title) |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 157 | .setContentIntent(contentIntent) |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 158 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
Jeff Sharkey | 288414e | 2016-07-18 18:29:48 -0600 | [diff] [blame] | 159 | .setProgress(max, index, false) |
Jeff Sharkey | c4bcacb | 2016-06-28 12:33:12 -0600 | [diff] [blame] | 160 | .build(); |
| 161 | notifManager.notifyAsUser(TAG, 0, notif, UserHandle.of(mUserId)); |
| 162 | break; |
| 163 | |
| 164 | case MSG_HIDE: |
| 165 | notifManager.cancelAsUser(TAG, 0, UserHandle.of(mUserId)); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | }; |
| 170 | |
Jeff Sharkey | bd91e2f | 2016-03-22 15:32:31 -0600 | [diff] [blame] | 171 | public abstract void onFinished(); |
| 172 | } |