blob: f4f6b661c0e1f6d30bb52e218df7535b24b63cda [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;
38import com.android.internal.util.ProgressReporter;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060039import com.android.server.UiThread;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060040
41import 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 */
48public 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 Sharkey288414e2016-07-18 18:29:48 -060054 private final boolean mQuiet;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060055
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 Sharkey24d94912016-07-07 12:33:48 -060062 ProgressReporter progress, boolean quiet) {
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060063 mService = service;
64 mUserId = userId;
65 mProgress = progress;
Jeff Sharkey288414e2016-07-18 18:29:48 -060066 mQuiet = quiet;
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060067
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 Sharkeyc4bcacb2016-06-28 12:33:12 -060077 mHandler.obtainMessage(MSG_HIDE).sendToTarget();
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060078 onFinished();
79 return;
80 }
81
Jeff Sharkey100bd9c2016-04-11 16:00:54 -060082 if (!mService.isUserRunning(mUserId, 0)) {
83 Slog.i(TAG, "User " + mUserId + " is no longer running; skipping remaining receivers");
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -060084 mHandler.obtainMessage(MSG_HIDE).sendToTarget();
Jeff Sharkey100bd9c2016-04-11 16:00:54 -060085 onFinished();
86 return;
87 }
88
Jeff Sharkey288414e2016-07-18 18:29:48 -060089 if (!mQuiet) {
90 mHandler.obtainMessage(MSG_SHOW, mTargets.size(), mIndex).sendToTarget();
91 }
92
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -060093 final ResolveInfo ri = mTargets.get(mIndex++);
94 final ComponentName componentName = ri.activityInfo.getComponentName();
95
Jeff Sharkeyfd241082016-04-19 15:58:24 -060096 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 Sharkeybd91e2f2016-03-22 15:32:31 -0600102
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 Sharkeyc4bcacb2016-06-28 12:33:12 -0600118 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 Sharkey288414e2016-07-18 18:29:48 -0600127 final int max = msg.arg1;
128 final int index = msg.arg2;
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600129
130 switch (msg.what) {
131 case MSG_SHOW:
132 final CharSequence title = context
Jeff Sharkeya4600cc2016-07-29 16:39:16 -0600133 .getText(R.string.android_upgrading_notification_title);
Jeff Sharkey288414e2016-07-18 18:29:48 -0600134
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 Sharkeyc4bcacb2016-06-28 12:33:12 -0600147 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 Sharkey288414e2016-07-18 18:29:48 -0600157 .setContentIntent(contentIntent)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600158 .setVisibility(Notification.VISIBILITY_PUBLIC)
Jeff Sharkey288414e2016-07-18 18:29:48 -0600159 .setProgress(max, index, false)
Jeff Sharkeyc4bcacb2016-06-28 12:33:12 -0600160 .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 Sharkeybd91e2f2016-03-22 15:32:31 -0600171 public abstract void onFinished();
172}