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