blob: 376999dfd80d53f33af6c8ac32f12eec2595cdfa [file] [log] [blame]
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -06001/*
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
17package com.android.server.am;
18
19import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
20
21import android.app.AppOpsManager;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060022import android.app.Notification;
23import android.app.NotificationManager;
Jeff Sharkey288414e2016-07-18 18:29:48 -060024import android.app.PendingIntent;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060025import android.content.ComponentName;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060026import android.content.Context;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060027import android.content.IIntentReceiver;
28import android.content.Intent;
29import android.content.pm.ResolveInfo;
Michal Karpinskic99d7182019-02-17 13:15:23 +000030import android.os.Binder;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060031import android.os.Bundle;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060032import android.os.Handler;
33import android.os.Message;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060034import android.os.Process;
35import android.os.UserHandle;
36import android.util.Slog;
37
38import com.android.internal.R;
Chris Wren282cfef2017-03-27 15:01:44 -040039import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050040import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060041import com.android.internal.util.ProgressReporter;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060042import com.android.server.UiThread;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060043
44import 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 */
51public 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 Sharkey288414e2016-07-18 18:29:48 -060057 private final boolean mQuiet;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060058
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 Sharkey24d94912016-07-07 12:33:48 -060065 ProgressReporter progress, boolean quiet) {
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060066 mService = service;
67 mUserId = userId;
68 mProgress = progress;
Jeff Sharkey288414e2016-07-18 18:29:48 -060069 mQuiet = quiet;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060070
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 Sharkeyc4bcacb2016-06-28 12:33:12 -060080 mHandler.obtainMessage(MSG_HIDE).sendToTarget();
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060081 onFinished();
82 return;
83 }
84
Jeff Sharkey100bd9c2016-04-11 16:00:54 -060085 if (!mService.isUserRunning(mUserId, 0)) {
86 Slog.i(TAG, "User " + mUserId + " is no longer running; skipping remaining receivers");
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060087 mHandler.obtainMessage(MSG_HIDE).sendToTarget();
Jeff Sharkey100bd9c2016-04-11 16:00:54 -060088 onFinished();
89 return;
90 }
91
Jeff Sharkey288414e2016-07-18 18:29:48 -060092 if (!mQuiet) {
93 mHandler.obtainMessage(MSG_SHOW, mTargets.size(), mIndex).sendToTarget();
94 }
95
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060096 final ResolveInfo ri = mTargets.get(mIndex++);
97 final ComponentName componentName = ri.activityInfo.getComponentName();
98
Jeff Sharkeyfd241082016-04-19 15:58:24 -060099 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 Sharkeybd91e2f2016-03-22 15:32:31 -0600105
106 Slog.i(TAG, "Pre-boot of " + componentName.toShortString() + " for user " + mUserId);
107 EventLogTags.writeAmPreBoot(mUserId, componentName.getPackageName());
108
109 mIntent.setComponent(componentName);
110 mService.broadcastIntentLocked(null, null, mIntent, null, this, 0, null, null, null,
111 AppOpsManager.OP_NONE, null, true, false, ActivityManagerService.MY_PID,
Michal Karpinskic99d7182019-02-17 13:15:23 +0000112 Process.SYSTEM_UID, Binder.getCallingUid(), Binder.getCallingPid(), mUserId);
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600113 }
114
115 @Override
116 public void performReceive(Intent intent, int resultCode, String data, Bundle extras,
117 boolean ordered, boolean sticky, int sendingUser) {
118 sendNext();
119 }
120
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600121 private static final int MSG_SHOW = 1;
122 private static final int MSG_HIDE = 2;
123
124 private Handler mHandler = new Handler(UiThread.get().getLooper(), null, true) {
125 @Override
126 public void handleMessage(Message msg) {
127 final Context context = mService.mContext;
128 final NotificationManager notifManager = context
129 .getSystemService(NotificationManager.class);
Jeff Sharkey288414e2016-07-18 18:29:48 -0600130 final int max = msg.arg1;
131 final int index = msg.arg2;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600132
133 switch (msg.what) {
134 case MSG_SHOW:
135 final CharSequence title = context
Jeff Sharkeya4600cc2016-07-29 16:39:16 -0600136 .getText(R.string.android_upgrading_notification_title);
Jeff Sharkey288414e2016-07-18 18:29:48 -0600137
138 final Intent intent = new Intent();
139 intent.setClassName("com.android.settings",
140 "com.android.settings.HelpTrampoline");
141 intent.putExtra(Intent.EXTRA_TEXT, "help_url_upgrading");
142
143 final PendingIntent contentIntent;
144 if (context.getPackageManager().resolveActivity(intent, 0) != null) {
145 contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
146 } else {
147 contentIntent = null;
148 }
149
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500150 final Notification notif =
151 new Notification.Builder(mService.mContext,
152 SystemNotificationChannels.UPDATES)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600153 .setSmallIcon(R.drawable.stat_sys_adb)
154 .setWhen(0)
155 .setOngoing(true)
156 .setTicker(title)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600157 .setColor(context.getColor(
158 com.android.internal.R.color.system_notification_accent_color))
159 .setContentTitle(title)
Jeff Sharkey288414e2016-07-18 18:29:48 -0600160 .setContentIntent(contentIntent)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600161 .setVisibility(Notification.VISIBILITY_PUBLIC)
Jeff Sharkey288414e2016-07-18 18:29:48 -0600162 .setProgress(max, index, false)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600163 .build();
Chris Wren282cfef2017-03-27 15:01:44 -0400164 notifManager.notifyAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING, notif,
165 UserHandle.of(mUserId));
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600166 break;
167
168 case MSG_HIDE:
Chris Wren282cfef2017-03-27 15:01:44 -0400169 notifManager.cancelAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING,
170 UserHandle.of(mUserId));
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600171 break;
172 }
173 }
174 };
175
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600176 public abstract void onFinished();
177}