blob: 3ea1147584985b27fab5f005c5caf3ba74ea1b86 [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;
30import android.os.Bundle;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060031import android.os.Handler;
32import android.os.Message;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060033import android.os.Process;
34import android.os.UserHandle;
35import android.util.Slog;
36
37import com.android.internal.R;
Chris Wren282cfef2017-03-27 15:01:44 -040038import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050039import com.android.internal.notification.SystemNotificationChannels;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060040import com.android.internal.util.ProgressReporter;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060041import com.android.server.UiThread;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060042
43import java.util.List;
44
45/**
46 * Simple broadcaster that sends {@link Intent#ACTION_PRE_BOOT_COMPLETED} to all
47 * system apps that register for it. Override {@link #onFinished()} to handle
48 * when all broadcasts are finished.
49 */
50public abstract class PreBootBroadcaster extends IIntentReceiver.Stub {
51 private static final String TAG = "PreBootBroadcaster";
52
53 private final ActivityManagerService mService;
54 private final int mUserId;
55 private final ProgressReporter mProgress;
Jeff Sharkey288414e2016-07-18 18:29:48 -060056 private final boolean mQuiet;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060057
58 private final Intent mIntent;
59 private final List<ResolveInfo> mTargets;
60
61 private int mIndex = 0;
62
63 public PreBootBroadcaster(ActivityManagerService service, int userId,
Jeff Sharkey24d94912016-07-07 12:33:48 -060064 ProgressReporter progress, boolean quiet) {
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060065 mService = service;
66 mUserId = userId;
67 mProgress = progress;
Jeff Sharkey288414e2016-07-18 18:29:48 -060068 mQuiet = quiet;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060069
70 mIntent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
71 mIntent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE | Intent.FLAG_DEBUG_TRIAGED_MISSING);
72
73 mTargets = mService.mContext.getPackageManager().queryBroadcastReceiversAsUser(mIntent,
74 MATCH_SYSTEM_ONLY, UserHandle.of(userId));
75 }
76
77 public void sendNext() {
78 if (mIndex >= mTargets.size()) {
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060079 mHandler.obtainMessage(MSG_HIDE).sendToTarget();
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060080 onFinished();
81 return;
82 }
83
Jeff Sharkey100bd9c2016-04-11 16:00:54 -060084 if (!mService.isUserRunning(mUserId, 0)) {
85 Slog.i(TAG, "User " + mUserId + " is no longer running; skipping remaining receivers");
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060086 mHandler.obtainMessage(MSG_HIDE).sendToTarget();
Jeff Sharkey100bd9c2016-04-11 16:00:54 -060087 onFinished();
88 return;
89 }
90
Jeff Sharkey288414e2016-07-18 18:29:48 -060091 if (!mQuiet) {
92 mHandler.obtainMessage(MSG_SHOW, mTargets.size(), mIndex).sendToTarget();
93 }
94
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060095 final ResolveInfo ri = mTargets.get(mIndex++);
96 final ComponentName componentName = ri.activityInfo.getComponentName();
97
Jeff Sharkeyfd241082016-04-19 15:58:24 -060098 if (mProgress != null) {
99 final CharSequence label = ri.activityInfo
100 .loadLabel(mService.mContext.getPackageManager());
101 mProgress.setProgress(mIndex, mTargets.size(),
102 mService.mContext.getString(R.string.android_preparing_apk, label));
103 }
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600104
105 Slog.i(TAG, "Pre-boot of " + componentName.toShortString() + " for user " + mUserId);
106 EventLogTags.writeAmPreBoot(mUserId, componentName.getPackageName());
107
108 mIntent.setComponent(componentName);
109 mService.broadcastIntentLocked(null, null, mIntent, null, this, 0, null, null, null,
110 AppOpsManager.OP_NONE, null, true, false, ActivityManagerService.MY_PID,
111 Process.SYSTEM_UID, mUserId);
112 }
113
114 @Override
115 public void performReceive(Intent intent, int resultCode, String data, Bundle extras,
116 boolean ordered, boolean sticky, int sendingUser) {
117 sendNext();
118 }
119
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600120 private static final int MSG_SHOW = 1;
121 private static final int MSG_HIDE = 2;
122
123 private Handler mHandler = new Handler(UiThread.get().getLooper(), null, true) {
124 @Override
125 public void handleMessage(Message msg) {
126 final Context context = mService.mContext;
127 final NotificationManager notifManager = context
128 .getSystemService(NotificationManager.class);
Jeff Sharkey288414e2016-07-18 18:29:48 -0600129 final int max = msg.arg1;
130 final int index = msg.arg2;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600131
132 switch (msg.what) {
133 case MSG_SHOW:
134 final CharSequence title = context
Jeff Sharkeya4600cc2016-07-29 16:39:16 -0600135 .getText(R.string.android_upgrading_notification_title);
Jeff Sharkey288414e2016-07-18 18:29:48 -0600136
137 final Intent intent = new Intent();
138 intent.setClassName("com.android.settings",
139 "com.android.settings.HelpTrampoline");
140 intent.putExtra(Intent.EXTRA_TEXT, "help_url_upgrading");
141
142 final PendingIntent contentIntent;
143 if (context.getPackageManager().resolveActivity(intent, 0) != null) {
144 contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
145 } else {
146 contentIntent = null;
147 }
148
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500149 final Notification notif =
150 new Notification.Builder(mService.mContext,
151 SystemNotificationChannels.UPDATES)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600152 .setSmallIcon(R.drawable.stat_sys_adb)
153 .setWhen(0)
154 .setOngoing(true)
155 .setTicker(title)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600156 .setColor(context.getColor(
157 com.android.internal.R.color.system_notification_accent_color))
158 .setContentTitle(title)
Jeff Sharkey288414e2016-07-18 18:29:48 -0600159 .setContentIntent(contentIntent)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600160 .setVisibility(Notification.VISIBILITY_PUBLIC)
Jeff Sharkey288414e2016-07-18 18:29:48 -0600161 .setProgress(max, index, false)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600162 .build();
Chris Wren282cfef2017-03-27 15:01:44 -0400163 notifManager.notifyAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING, notif,
164 UserHandle.of(mUserId));
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600165 break;
166
167 case MSG_HIDE:
Chris Wren282cfef2017-03-27 15:01:44 -0400168 notifManager.cancelAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING,
169 UserHandle.of(mUserId));
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600170 break;
171 }
172 }
173 };
174
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600175 public abstract void onFinished();
176}