2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.statusbar.phone;
19 import android.app.ActivityManager;
20 import android.app.ActivityManager.StackId;
21 import android.app.ActivityManager.StackInfo;
22 import android.app.AlarmManager;
23 import android.app.AlarmManager.AlarmClockInfo;
24 import android.app.AppGlobals;
25 import android.app.Notification;
26 import android.app.Notification.Action;
27 import android.app.NotificationManager;
28 import android.app.PendingIntent;
29 import android.app.SynchronousUserSwitchObserver;
30 import android.content.BroadcastReceiver;
31 import android.content.ComponentName;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.content.IntentFilter;
35 import android.content.pm.ApplicationInfo;
36 import android.content.pm.IPackageManager;
37 import android.content.pm.PackageManager;
38 import android.content.pm.UserInfo;
39 import android.graphics.drawable.Icon;
40 import android.media.AudioManager;
41 import android.net.Uri;
42 import android.os.Bundle;
43 import android.os.Handler;
44 import android.os.RemoteException;
45 import android.os.UserHandle;
46 import android.os.UserManager;
47 import android.provider.Settings;
48 import android.provider.Settings.Global;
49 import android.service.notification.StatusBarNotification;
50 import android.telecom.TelecomManager;
51 import android.util.ArraySet;
52 import android.util.Log;
53 import android.util.Pair;
54 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
55 import com.android.internal.telephony.IccCardConstants;
56 import com.android.internal.telephony.TelephonyIntents;
57 import com.android.systemui.Dependency;
58 import com.android.systemui.DockedStackExistsListener;
59 import com.android.systemui.R;
60 import com.android.systemui.SysUiServiceProvider;
61 import com.android.systemui.UiOffloadThread;
62 import com.android.systemui.qs.tiles.DndTile;
63 import com.android.systemui.qs.tiles.RotationLockTile;
64 import com.android.systemui.recents.misc.SystemServicesProxy;
65 import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
66 import com.android.systemui.statusbar.CommandQueue;
67 import com.android.systemui.statusbar.CommandQueue.Callbacks;
68 import com.android.systemui.statusbar.policy.BluetoothController;
69 import com.android.systemui.statusbar.policy.BluetoothController.Callback;
70 import com.android.systemui.statusbar.policy.CastController;
71 import com.android.systemui.statusbar.policy.CastController.CastDevice;
72 import com.android.systemui.statusbar.policy.DataSaverController;
73 import com.android.systemui.statusbar.policy.DataSaverController.Listener;
74 import com.android.systemui.statusbar.policy.DeviceProvisionedController;
75 import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
76 import com.android.systemui.statusbar.policy.HotspotController;
77 import com.android.systemui.statusbar.policy.KeyguardMonitor;
78 import com.android.systemui.statusbar.policy.LocationController;
79 import com.android.systemui.statusbar.policy.LocationController.LocationChangeCallback;
80 import com.android.systemui.statusbar.policy.NextAlarmController;
81 import com.android.systemui.statusbar.policy.RotationLockController;
82 import com.android.systemui.statusbar.policy.RotationLockController.RotationLockControllerCallback;
83 import com.android.systemui.statusbar.policy.UserInfoController;
84 import com.android.systemui.statusbar.policy.ZenModeController;
85 import com.android.systemui.util.NotificationChannels;
87 import java.util.List;
90 * This class contains all of the policy about which icons are installed in the status
91 * bar at boot time. It goes through the normal API for icons, even though it probably
92 * strictly doesn't need to.
94 public class PhoneStatusBarPolicy implements Callback, Callbacks,
95 RotationLockControllerCallback, Listener, LocationChangeCallback,
96 ZenModeController.Callback, DeviceProvisionedListener, KeyguardMonitor.Callback {
97 private static final String TAG = "PhoneStatusBarPolicy";
98 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
100 public static final int LOCATION_STATUS_ICON_ID = R.drawable.stat_sys_location;
101 public static final int NUM_TASKS_FOR_INSTANT_APP_INFO = 5;
103 private final String mSlotCast;
104 private final String mSlotHotspot;
105 private final String mSlotBluetooth;
106 private final String mSlotTty;
107 private final String mSlotZen;
108 private final String mSlotVolume;
109 private final String mSlotAlarmClock;
110 private final String mSlotManagedProfile;
111 private final String mSlotRotate;
112 private final String mSlotHeadset;
113 private final String mSlotDataSaver;
114 private final String mSlotLocation;
116 private final Context mContext;
117 private final Handler mHandler = new Handler();
118 private final CastController mCast;
119 private final HotspotController mHotspot;
120 private final NextAlarmController mNextAlarm;
121 private final AlarmManager mAlarmManager;
122 private final UserInfoController mUserInfoController;
123 private final UserManager mUserManager;
124 private final StatusBarIconController mIconController;
125 private final RotationLockController mRotationLockController;
126 private final DataSaverController mDataSaver;
127 private final ZenModeController mZenController;
128 private final DeviceProvisionedController mProvisionedController;
129 private final KeyguardMonitor mKeyguardMonitor;
130 private final LocationController mLocationController;
131 private final ArraySet<Pair<String, Integer>> mCurrentNotifs = new ArraySet<>();
132 private final UiOffloadThread mUiOffloadThread = Dependency.get(UiOffloadThread.class);
134 // Assume it's all good unless we hear otherwise. We don't always seem
135 // to get broadcasts that it *is* there.
136 IccCardConstants.State mSimState = IccCardConstants.State.READY;
138 private boolean mZenVisible;
139 private boolean mVolumeVisible;
140 private boolean mCurrentUserSetup;
141 private boolean mDockedStackExists;
143 private boolean mManagedProfileIconVisible = false;
144 private boolean mManagedProfileInQuietMode = false;
146 private BluetoothController mBluetooth;
148 public PhoneStatusBarPolicy(Context context, StatusBarIconController iconController) {
150 mIconController = iconController;
151 mCast = Dependency.get(CastController.class);
152 mHotspot = Dependency.get(HotspotController.class);
153 mBluetooth = Dependency.get(BluetoothController.class);
154 mNextAlarm = Dependency.get(NextAlarmController.class);
155 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
156 mUserInfoController = Dependency.get(UserInfoController.class);
157 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
158 mRotationLockController = Dependency.get(RotationLockController.class);
159 mDataSaver = Dependency.get(DataSaverController.class);
160 mZenController = Dependency.get(ZenModeController.class);
161 mProvisionedController = Dependency.get(DeviceProvisionedController.class);
162 mKeyguardMonitor = Dependency.get(KeyguardMonitor.class);
163 mLocationController = Dependency.get(LocationController.class);
165 mSlotCast = context.getString(com.android.internal.R.string.status_bar_cast);
166 mSlotHotspot = context.getString(com.android.internal.R.string.status_bar_hotspot);
167 mSlotBluetooth = context.getString(com.android.internal.R.string.status_bar_bluetooth);
168 mSlotTty = context.getString(com.android.internal.R.string.status_bar_tty);
169 mSlotZen = context.getString(com.android.internal.R.string.status_bar_zen);
170 mSlotVolume = context.getString(com.android.internal.R.string.status_bar_volume);
171 mSlotAlarmClock = context.getString(com.android.internal.R.string.status_bar_alarm_clock);
172 mSlotManagedProfile = context.getString(
173 com.android.internal.R.string.status_bar_managed_profile);
174 mSlotRotate = context.getString(com.android.internal.R.string.status_bar_rotate);
175 mSlotHeadset = context.getString(com.android.internal.R.string.status_bar_headset);
176 mSlotDataSaver = context.getString(com.android.internal.R.string.status_bar_data_saver);
177 mSlotLocation = context.getString(com.android.internal.R.string.status_bar_location);
179 // listen for broadcasts
180 IntentFilter filter = new IntentFilter();
181 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
182 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
183 filter.addAction(AudioManager.ACTION_HEADSET_PLUG);
184 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
185 filter.addAction(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
186 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
187 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
188 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
189 mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
191 // listen for user / profile change.
193 ActivityManager.getService().registerUserSwitchObserver(mUserSwitchListener, TAG);
194 } catch (RemoteException e) {
205 mIconController.setIcon(mSlotAlarmClock, R.drawable.stat_sys_alarm, null);
206 mIconController.setIconVisibility(mSlotAlarmClock, false);
209 mIconController.setIcon(mSlotZen, R.drawable.stat_sys_zen_important, null);
210 mIconController.setIconVisibility(mSlotZen, false);
213 mIconController.setIcon(mSlotVolume, R.drawable.stat_sys_ringer_vibrate, null);
214 mIconController.setIconVisibility(mSlotVolume, false);
218 mIconController.setIcon(mSlotCast, R.drawable.stat_sys_cast, null);
219 mIconController.setIconVisibility(mSlotCast, false);
222 mIconController.setIcon(mSlotHotspot, R.drawable.stat_sys_hotspot,
223 mContext.getString(R.string.accessibility_status_bar_hotspot));
224 mIconController.setIconVisibility(mSlotHotspot, mHotspot.isHotspotEnabled());
227 mIconController.setIcon(mSlotManagedProfile, R.drawable.stat_sys_managed_profile_status,
228 mContext.getString(R.string.accessibility_managed_profile));
229 mIconController.setIconVisibility(mSlotManagedProfile, mManagedProfileIconVisible);
232 mIconController.setIcon(mSlotDataSaver, R.drawable.stat_sys_data_saver,
233 context.getString(R.string.accessibility_data_saver_on));
234 mIconController.setIconVisibility(mSlotDataSaver, false);
236 mRotationLockController.addCallback(this);
237 mBluetooth.addCallback(this);
238 mProvisionedController.addCallback(this);
239 mZenController.addCallback(this);
240 mCast.addCallback(mCastCallback);
241 mHotspot.addCallback(mHotspotCallback);
242 mNextAlarm.addCallback(mNextAlarmCallback);
243 mDataSaver.addCallback(this);
244 mKeyguardMonitor.addCallback(this);
245 mLocationController.addCallback(this);
247 SysUiServiceProvider.getComponent(mContext, CommandQueue.class).addCallbacks(this);
248 SystemServicesProxy.getInstance(mContext).registerTaskStackListener(mTaskListener);
250 // Clear out all old notifications on startup (only present in the case where sysui dies)
251 NotificationManager noMan = mContext.getSystemService(NotificationManager.class);
252 for (StatusBarNotification notification : noMan.getActiveNotifications()) {
253 if (notification.getId() == SystemMessage.NOTE_INSTANT_APPS) {
254 noMan.cancel(notification.getTag(), notification.getId());
257 DockedStackExistsListener.register(exists -> {
258 mDockedStackExists = exists;
259 updateForegroundInstantApps();
263 public void destroy() {
264 mRotationLockController.removeCallback(this);
265 mBluetooth.removeCallback(this);
266 mProvisionedController.removeCallback(this);
267 mZenController.removeCallback(this);
268 mCast.removeCallback(mCastCallback);
269 mHotspot.removeCallback(mHotspotCallback);
270 mNextAlarm.removeCallback(mNextAlarmCallback);
271 mDataSaver.removeCallback(this);
272 mKeyguardMonitor.removeCallback(this);
273 mLocationController.removeCallback(this);
274 SysUiServiceProvider.getComponent(mContext, CommandQueue.class).removeCallbacks(this);
275 mContext.unregisterReceiver(mIntentReceiver);
277 NotificationManager noMan = mContext.getSystemService(NotificationManager.class);
278 mCurrentNotifs.forEach(v -> noMan.cancelAsUser(v.first, SystemMessage.NOTE_INSTANT_APPS,
279 new UserHandle(v.second)));
283 public void onZenChanged(int zen) {
288 public void onLocationActiveChanged(boolean active) {
292 // Updates the status view based on the current state of location requests.
293 private void updateLocation() {
294 if (mLocationController.isLocationActive()) {
295 mIconController.setIcon(mSlotLocation, LOCATION_STATUS_ICON_ID,
296 mContext.getString(R.string.accessibility_location_active));
298 mIconController.removeIcon(mSlotLocation);
302 private void updateAlarm() {
303 final AlarmClockInfo alarm = mAlarmManager.getNextAlarmClock(UserHandle.USER_CURRENT);
304 final boolean hasAlarm = alarm != null && alarm.getTriggerTime() > 0;
305 int zen = mZenController.getZen();
306 final boolean zenNone = zen == Global.ZEN_MODE_NO_INTERRUPTIONS;
307 mIconController.setIcon(mSlotAlarmClock, zenNone ? R.drawable.stat_sys_alarm_dim
308 : R.drawable.stat_sys_alarm, null);
309 mIconController.setIconVisibility(mSlotAlarmClock, mCurrentUserSetup && hasAlarm);
312 private final void updateSimState(Intent intent) {
313 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
314 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
315 mSimState = IccCardConstants.State.ABSENT;
316 } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
317 mSimState = IccCardConstants.State.CARD_IO_ERROR;
318 } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED.equals(stateExtra)) {
319 mSimState = IccCardConstants.State.CARD_RESTRICTED;
320 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
321 mSimState = IccCardConstants.State.READY;
322 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
323 final String lockedReason =
324 intent.getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
325 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
326 mSimState = IccCardConstants.State.PIN_REQUIRED;
327 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
328 mSimState = IccCardConstants.State.PUK_REQUIRED;
330 mSimState = IccCardConstants.State.NETWORK_LOCKED;
333 mSimState = IccCardConstants.State.UNKNOWN;
337 private final void updateVolumeZen() {
338 AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
340 boolean zenVisible = false;
342 String zenDescription = null;
344 boolean volumeVisible = false;
345 int volumeIconId = 0;
346 String volumeDescription = null;
347 int zen = mZenController.getZen();
349 if (DndTile.isVisible(mContext) || DndTile.isCombinedIcon(mContext)) {
350 zenVisible = zen != Global.ZEN_MODE_OFF;
351 zenIconId = zen == Global.ZEN_MODE_NO_INTERRUPTIONS
352 ? R.drawable.stat_sys_dnd_total_silence : R.drawable.stat_sys_dnd;
353 zenDescription = mContext.getString(R.string.quick_settings_dnd_label);
354 } else if (zen == Global.ZEN_MODE_NO_INTERRUPTIONS) {
356 zenIconId = R.drawable.stat_sys_zen_none;
357 zenDescription = mContext.getString(R.string.interruption_level_none);
358 } else if (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
360 zenIconId = R.drawable.stat_sys_zen_important;
361 zenDescription = mContext.getString(R.string.interruption_level_priority);
364 if (DndTile.isVisible(mContext) && !DndTile.isCombinedIcon(mContext)
365 && audioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_SILENT) {
366 volumeVisible = true;
367 volumeIconId = R.drawable.stat_sys_ringer_silent;
368 volumeDescription = mContext.getString(R.string.accessibility_ringer_silent);
369 } else if (zen != Global.ZEN_MODE_NO_INTERRUPTIONS && zen != Global.ZEN_MODE_ALARMS &&
370 audioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_VIBRATE) {
371 volumeVisible = true;
372 volumeIconId = R.drawable.stat_sys_ringer_vibrate;
373 volumeDescription = mContext.getString(R.string.accessibility_ringer_vibrate);
377 mIconController.setIcon(mSlotZen, zenIconId, zenDescription);
379 if (zenVisible != mZenVisible) {
380 mIconController.setIconVisibility(mSlotZen, zenVisible);
381 mZenVisible = zenVisible;
385 mIconController.setIcon(mSlotVolume, volumeIconId, volumeDescription);
387 if (volumeVisible != mVolumeVisible) {
388 mIconController.setIconVisibility(mSlotVolume, volumeVisible);
389 mVolumeVisible = volumeVisible;
395 public void onBluetoothDevicesChanged() {
400 public void onBluetoothStateChange(boolean enabled) {
404 private final void updateBluetooth() {
405 int iconId = R.drawable.stat_sys_data_bluetooth;
406 String contentDescription =
407 mContext.getString(R.string.accessibility_quick_settings_bluetooth_on);
408 boolean bluetoothEnabled = false;
409 if (mBluetooth != null) {
410 bluetoothEnabled = mBluetooth.isBluetoothEnabled();
411 if (mBluetooth.isBluetoothConnected()) {
412 iconId = R.drawable.stat_sys_data_bluetooth_connected;
413 contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);
417 mIconController.setIcon(mSlotBluetooth, iconId, contentDescription);
418 mIconController.setIconVisibility(mSlotBluetooth, bluetoothEnabled);
421 private final void updateTTY() {
422 TelecomManager telecomManager =
423 (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
424 if (telecomManager == null) {
425 updateTTY(TelecomManager.TTY_MODE_OFF);
427 updateTTY(telecomManager.getCurrentTtyMode());
431 private final void updateTTY(int currentTtyMode) {
432 boolean enabled = currentTtyMode != TelecomManager.TTY_MODE_OFF;
434 if (DEBUG) Log.v(TAG, "updateTTY: enabled: " + enabled);
438 if (DEBUG) Log.v(TAG, "updateTTY: set TTY on");
439 mIconController.setIcon(mSlotTty, R.drawable.stat_sys_tty_mode,
440 mContext.getString(R.string.accessibility_tty_enabled));
441 mIconController.setIconVisibility(mSlotTty, true);
444 if (DEBUG) Log.v(TAG, "updateTTY: set TTY off");
445 mIconController.setIconVisibility(mSlotTty, false);
449 private void updateCast() {
450 boolean isCasting = false;
451 for (CastDevice device : mCast.getCastDevices()) {
452 if (device.state == CastDevice.STATE_CONNECTING
453 || device.state == CastDevice.STATE_CONNECTED) {
458 if (DEBUG) Log.v(TAG, "updateCast: isCasting: " + isCasting);
459 mHandler.removeCallbacks(mRemoveCastIconRunnable);
461 mIconController.setIcon(mSlotCast, R.drawable.stat_sys_cast,
462 mContext.getString(R.string.accessibility_casting));
463 mIconController.setIconVisibility(mSlotCast, true);
465 // don't turn off the screen-record icon for a few seconds, just to make sure the user
467 if (DEBUG) Log.v(TAG, "updateCast: hiding icon in 3 sec...");
468 mHandler.postDelayed(mRemoveCastIconRunnable, 3000);
472 private void updateQuietState() {
473 mManagedProfileInQuietMode = false;
474 int currentUserId = ActivityManager.getCurrentUser();
475 for (UserInfo ui : mUserManager.getEnabledProfiles(currentUserId)) {
476 if (ui.isManagedProfile() && ui.isQuietModeEnabled()) {
477 mManagedProfileInQuietMode = true;
483 private void updateManagedProfile() {
484 // getLastResumedActivityUserId needds to acquire the AM lock, which may be contended in
485 // some cases. Since it doesn't really matter here whether it's updated in this frame
486 // or in the next one, we call this method from our UI offload thread.
487 mUiOffloadThread.submit(() -> {
490 userId = ActivityManager.getService().getLastResumedActivityUserId();
491 boolean isManagedProfile = mUserManager.isManagedProfile(userId);
492 mHandler.post(() -> {
493 final boolean showIcon;
494 if (isManagedProfile &&
495 (!mKeyguardMonitor.isShowing() || mKeyguardMonitor.isOccluded())) {
497 mIconController.setIcon(mSlotManagedProfile,
498 R.drawable.stat_sys_managed_profile_status,
499 mContext.getString(R.string.accessibility_managed_profile));
500 } else if (mManagedProfileInQuietMode) {
502 mIconController.setIcon(mSlotManagedProfile,
503 R.drawable.stat_sys_managed_profile_status_off,
504 mContext.getString(R.string.accessibility_managed_profile));
508 if (mManagedProfileIconVisible != showIcon) {
509 mIconController.setIconVisibility(mSlotManagedProfile, showIcon);
510 mManagedProfileIconVisible = showIcon;
513 } catch (RemoteException e) {
514 Log.w(TAG, "updateManagedProfile: ", e);
519 private void updateForegroundInstantApps() {
520 NotificationManager noMan = mContext.getSystemService(NotificationManager.class);
521 ArraySet<Pair<String, Integer>> notifs = new ArraySet<>(mCurrentNotifs);
522 IPackageManager pm = AppGlobals.getPackageManager();
523 mCurrentNotifs.clear();
524 mUiOffloadThread.submit(() -> {
526 int focusedId = ActivityManager.getService().getFocusedStackId();
527 if (focusedId == StackId.FULLSCREEN_WORKSPACE_STACK_ID) {
528 checkStack(StackId.FULLSCREEN_WORKSPACE_STACK_ID, notifs, noMan, pm);
530 if (mDockedStackExists) {
531 checkStack(StackId.DOCKED_STACK_ID, notifs, noMan, pm);
533 } catch (RemoteException e) {
534 e.rethrowFromSystemServer();
536 // Cancel all the leftover notifications that don't have a foreground process anymore.
537 notifs.forEach(v -> noMan.cancelAsUser(v.first, SystemMessage.NOTE_INSTANT_APPS,
538 new UserHandle(v.second)));
542 private void checkStack(int stackId, ArraySet<Pair<String, Integer>> notifs,
543 NotificationManager noMan, IPackageManager pm) {
545 StackInfo info = ActivityManager.getService().getStackInfo(stackId);
546 if (info == null || info.topActivity == null) return;
547 String pkg = info.topActivity.getPackageName();
548 if (!hasNotif(notifs, pkg, info.userId)) {
549 // TODO: Optimize by not always needing to get application info.
550 // Maybe cache non-ephemeral packages?
551 ApplicationInfo appInfo = pm.getApplicationInfo(pkg,
552 PackageManager.MATCH_UNINSTALLED_PACKAGES, info.userId);
553 if (appInfo.isInstantApp()) {
554 postEphemeralNotif(pkg, info.userId, appInfo, noMan, info.taskIds[info.taskIds.length - 1]);
557 } catch (RemoteException e) {
558 e.rethrowFromSystemServer();
562 private void postEphemeralNotif(String pkg, int userId, ApplicationInfo appInfo,
563 NotificationManager noMan, int taskId) {
564 final Bundle extras = new Bundle();
565 extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME,
566 mContext.getString(R.string.instant_apps));
567 mCurrentNotifs.add(new Pair<>(pkg, userId));
568 String message = mContext.getString(R.string.instant_apps_message);
569 PendingIntent appInfoAction = PendingIntent.getActivity(mContext, 0,
570 new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
571 .setData(Uri.fromParts("package", pkg, null)),
572 PendingIntent.FLAG_IMMUTABLE);
573 Action action = new Notification.Action.Builder(null, mContext.getString(R.string.app_info),
574 appInfoAction).build();
576 Intent browserIntent = getTaskIntent(taskId, userId);
577 Notification.Builder builder = new Notification.Builder(mContext, NotificationChannels.GENERAL);
578 if (browserIntent != null) {
579 // Make sure that this doesn't resolve back to an instant app
580 browserIntent.setComponent(null)
582 .addFlags(Intent.FLAG_IGNORE_EPHEMERAL)
583 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
585 PendingIntent pendingIntent = PendingIntent.getActivity(mContext,
586 0 /* requestCode */, browserIntent, PendingIntent.FLAG_IMMUTABLE);
587 ComponentName aiaComponent = null;
589 aiaComponent = AppGlobals.getPackageManager().getInstantAppInstallerComponent();
590 } catch (RemoteException e) {
591 e.rethrowFromSystemServer();
593 Intent goToWebIntent = new Intent()
594 .setComponent(aiaComponent)
595 .setAction(Intent.ACTION_VIEW)
596 .addCategory(Intent.CATEGORY_BROWSABLE)
597 .addCategory("unique:" + System.currentTimeMillis())
598 .putExtra(Intent.EXTRA_PACKAGE_NAME, appInfo.packageName)
599 .putExtra(Intent.EXTRA_VERSION_CODE, appInfo.versionCode)
600 .putExtra(Intent.EXTRA_EPHEMERAL_FAILURE, pendingIntent);
602 PendingIntent webPendingIntent = PendingIntent.getActivity(
603 mContext, 0, goToWebIntent, PendingIntent.FLAG_IMMUTABLE);
604 Action webAction = new Notification.Action.Builder(null, mContext.getString(R.string.go_to_web),
605 webPendingIntent).build();
606 builder.addAction(webAction);
609 noMan.notifyAsUser(pkg, SystemMessage.NOTE_INSTANT_APPS, builder
612 .setContentIntent(appInfoAction)
613 .setColor(mContext.getColor(R.color.instant_apps_color))
614 .setContentTitle(appInfo.loadLabel(mContext.getPackageManager()))
615 .setLargeIcon(Icon.createWithResource(pkg, appInfo.icon))
616 .setSmallIcon(Icon.createWithResource(mContext.getPackageName(),
617 R.drawable.instant_icon))
618 .setContentText(message)
621 new UserHandle(userId));
624 private Intent getTaskIntent(int taskId, int userId) {
625 List<ActivityManager.RecentTaskInfo> tasks = mContext.getSystemService(ActivityManager.class)
626 .getRecentTasksForUser(NUM_TASKS_FOR_INSTANT_APP_INFO, 0, userId);
627 for (int i = 0; i < tasks.size(); i++) {
628 if (tasks.get(i).id == taskId) {
629 return tasks.get(i).baseIntent;
635 private boolean hasNotif(ArraySet<Pair<String, Integer>> notifs, String pkg, int userId) {
636 Pair<String, Integer> key = new Pair<>(pkg, userId);
637 if (notifs.remove(key)) {
638 mCurrentNotifs.add(key);
644 private final SynchronousUserSwitchObserver mUserSwitchListener =
645 new SynchronousUserSwitchObserver() {
647 public void onUserSwitching(int newUserId) throws RemoteException {
648 mHandler.post(() -> mUserInfoController.reloadUserInfo());
652 public void onUserSwitchComplete(int newUserId) throws RemoteException {
653 mHandler.post(() -> {
656 updateManagedProfile();
657 updateForegroundInstantApps();
662 private final HotspotController.Callback mHotspotCallback = new HotspotController.Callback() {
664 public void onHotspotChanged(boolean enabled) {
665 mIconController.setIconVisibility(mSlotHotspot, enabled);
669 private final CastController.Callback mCastCallback = new CastController.Callback() {
671 public void onCastDevicesChanged() {
676 private final NextAlarmController.NextAlarmChangeCallback mNextAlarmCallback =
677 new NextAlarmController.NextAlarmChangeCallback() {
679 public void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm) {
685 public void appTransitionStarting(long startTime, long duration, boolean forced) {
686 updateManagedProfile();
687 updateForegroundInstantApps();
691 public void onKeyguardShowingChanged() {
692 updateManagedProfile();
693 updateForegroundInstantApps();
697 public void onUserSetupChanged() {
698 boolean userSetup = mProvisionedController.isUserSetup(
699 mProvisionedController.getCurrentUser());
700 if (mCurrentUserSetup == userSetup) return;
701 mCurrentUserSetup = userSetup;
707 public void preloadRecentApps() {
708 updateForegroundInstantApps();
712 public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) {
713 boolean portrait = RotationLockTile.isCurrentOrientationLockPortrait(
714 mRotationLockController, mContext);
715 if (rotationLocked) {
717 mIconController.setIcon(mSlotRotate, R.drawable.stat_sys_rotate_portrait,
718 mContext.getString(R.string.accessibility_rotation_lock_on_portrait));
720 mIconController.setIcon(mSlotRotate, R.drawable.stat_sys_rotate_landscape,
721 mContext.getString(R.string.accessibility_rotation_lock_on_landscape));
723 mIconController.setIconVisibility(mSlotRotate, true);
725 mIconController.setIconVisibility(mSlotRotate, false);
729 private void updateHeadsetPlug(Intent intent) {
730 boolean connected = intent.getIntExtra("state", 0) != 0;
731 boolean hasMic = intent.getIntExtra("microphone", 0) != 0;
733 String contentDescription = mContext.getString(hasMic
734 ? R.string.accessibility_status_bar_headset
735 : R.string.accessibility_status_bar_headphones);
736 mIconController.setIcon(mSlotHeadset, hasMic ? R.drawable.ic_headset_mic
737 : R.drawable.ic_headset, contentDescription);
738 mIconController.setIconVisibility(mSlotHeadset, true);
740 mIconController.setIconVisibility(mSlotHeadset, false);
745 public void onDataSaverChanged(boolean isDataSaving) {
746 mIconController.setIconVisibility(mSlotDataSaver, isDataSaving);
749 private final TaskStackListener mTaskListener = new TaskStackListener() {
751 public void onTaskStackChanged() {
752 // Listen for changes to stacks and then check which instant apps are foreground.
753 updateForegroundInstantApps();
757 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
759 public void onReceive(Context context, Intent intent) {
760 String action = intent.getAction();
761 if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION) ||
762 action.equals(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION)) {
764 } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
765 updateSimState(intent);
766 } else if (action.equals(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED)) {
767 updateTTY(intent.getIntExtra(TelecomManager.EXTRA_CURRENT_TTY_MODE,
768 TelecomManager.TTY_MODE_OFF));
769 } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_AVAILABLE) ||
770 action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE) ||
771 action.equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)) {
773 updateManagedProfile();
774 } else if (action.equals(AudioManager.ACTION_HEADSET_PLUG)) {
775 updateHeadsetPlug(intent);
780 private Runnable mRemoveCastIconRunnable = new Runnable() {
783 if (DEBUG) Log.v(TAG, "updateCast: hiding icon NOW");
784 mIconController.setIconVisibility(mSlotCast, false);