OSDN Git Service

ea22486a56e237864abf8cbd0780ce2c745a92f9
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / statusbar / phone / PhoneStatusBarPolicy.java
1 /*
2  * Copyright (C) 2008 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.systemui.statusbar.phone;
18
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;
86
87 import java.util.List;
88
89 /**
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.
93  */
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);
99
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;
102
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;
115
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);
133
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;
137
138     private boolean mZenVisible;
139     private boolean mVolumeVisible;
140     private boolean mCurrentUserSetup;
141     private boolean mDockedStackExists;
142
143     private boolean mManagedProfileIconVisible = false;
144     private boolean mManagedProfileInQuietMode = false;
145
146     private BluetoothController mBluetooth;
147
148     public PhoneStatusBarPolicy(Context context, StatusBarIconController iconController) {
149         mContext = context;
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);
164
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);
178
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);
190
191         // listen for user / profile change.
192         try {
193             ActivityManager.getService().registerUserSwitchObserver(mUserSwitchListener, TAG);
194         } catch (RemoteException e) {
195             // Ignore
196         }
197
198         // TTY status
199         updateTTY();
200
201         // bluetooth status
202         updateBluetooth();
203
204         // Alarm clock
205         mIconController.setIcon(mSlotAlarmClock, R.drawable.stat_sys_alarm, null);
206         mIconController.setIconVisibility(mSlotAlarmClock, false);
207
208         // zen
209         mIconController.setIcon(mSlotZen, R.drawable.stat_sys_zen_important, null);
210         mIconController.setIconVisibility(mSlotZen, false);
211
212         // volume
213         mIconController.setIcon(mSlotVolume, R.drawable.stat_sys_ringer_vibrate, null);
214         mIconController.setIconVisibility(mSlotVolume, false);
215         updateVolumeZen();
216
217         // cast
218         mIconController.setIcon(mSlotCast, R.drawable.stat_sys_cast, null);
219         mIconController.setIconVisibility(mSlotCast, false);
220
221         // hotspot
222         mIconController.setIcon(mSlotHotspot, R.drawable.stat_sys_hotspot,
223                 mContext.getString(R.string.accessibility_status_bar_hotspot));
224         mIconController.setIconVisibility(mSlotHotspot, mHotspot.isHotspotEnabled());
225
226         // managed profile
227         mIconController.setIcon(mSlotManagedProfile, R.drawable.stat_sys_managed_profile_status,
228                 mContext.getString(R.string.accessibility_managed_profile));
229         mIconController.setIconVisibility(mSlotManagedProfile, mManagedProfileIconVisible);
230
231         // data saver
232         mIconController.setIcon(mSlotDataSaver, R.drawable.stat_sys_data_saver,
233                 context.getString(R.string.accessibility_data_saver_on));
234         mIconController.setIconVisibility(mSlotDataSaver, false);
235
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);
246
247         SysUiServiceProvider.getComponent(mContext, CommandQueue.class).addCallbacks(this);
248         SystemServicesProxy.getInstance(mContext).registerTaskStackListener(mTaskListener);
249
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());
255             }
256         }
257         DockedStackExistsListener.register(exists -> {
258             mDockedStackExists = exists;
259             updateForegroundInstantApps();
260         });
261     }
262
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);
276
277         NotificationManager noMan = mContext.getSystemService(NotificationManager.class);
278         mCurrentNotifs.forEach(v -> noMan.cancelAsUser(v.first, SystemMessage.NOTE_INSTANT_APPS,
279                 new UserHandle(v.second)));
280     }
281
282     @Override
283     public void onZenChanged(int zen) {
284         updateVolumeZen();
285     }
286
287     @Override
288     public void onLocationActiveChanged(boolean active) {
289         updateLocation();
290     }
291
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));
297         } else {
298             mIconController.removeIcon(mSlotLocation);
299         }
300     }
301
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);
310     }
311
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;
329             } else {
330                 mSimState = IccCardConstants.State.NETWORK_LOCKED;
331             }
332         } else {
333             mSimState = IccCardConstants.State.UNKNOWN;
334         }
335     }
336
337     private final void updateVolumeZen() {
338         AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
339
340         boolean zenVisible = false;
341         int zenIconId = 0;
342         String zenDescription = null;
343
344         boolean volumeVisible = false;
345         int volumeIconId = 0;
346         String volumeDescription = null;
347         int zen = mZenController.getZen();
348
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) {
355             zenVisible = true;
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) {
359             zenVisible = true;
360             zenIconId = R.drawable.stat_sys_zen_important;
361             zenDescription = mContext.getString(R.string.interruption_level_priority);
362         }
363
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);
374         }
375
376         if (zenVisible) {
377             mIconController.setIcon(mSlotZen, zenIconId, zenDescription);
378         }
379         if (zenVisible != mZenVisible) {
380             mIconController.setIconVisibility(mSlotZen, zenVisible);
381             mZenVisible = zenVisible;
382         }
383
384         if (volumeVisible) {
385             mIconController.setIcon(mSlotVolume, volumeIconId, volumeDescription);
386         }
387         if (volumeVisible != mVolumeVisible) {
388             mIconController.setIconVisibility(mSlotVolume, volumeVisible);
389             mVolumeVisible = volumeVisible;
390         }
391         updateAlarm();
392     }
393
394     @Override
395     public void onBluetoothDevicesChanged() {
396         updateBluetooth();
397     }
398
399     @Override
400     public void onBluetoothStateChange(boolean enabled) {
401         updateBluetooth();
402     }
403
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);
414             }
415         }
416
417         mIconController.setIcon(mSlotBluetooth, iconId, contentDescription);
418         mIconController.setIconVisibility(mSlotBluetooth, bluetoothEnabled);
419     }
420
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);
426         } else {
427             updateTTY(telecomManager.getCurrentTtyMode());
428         }
429     }
430
431     private final void updateTTY(int currentTtyMode) {
432         boolean enabled = currentTtyMode != TelecomManager.TTY_MODE_OFF;
433
434         if (DEBUG) Log.v(TAG, "updateTTY: enabled: " + enabled);
435
436         if (enabled) {
437             // TTY is on
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);
442         } else {
443             // TTY is off
444             if (DEBUG) Log.v(TAG, "updateTTY: set TTY off");
445             mIconController.setIconVisibility(mSlotTty, false);
446         }
447     }
448
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) {
454                 isCasting = true;
455                 break;
456             }
457         }
458         if (DEBUG) Log.v(TAG, "updateCast: isCasting: " + isCasting);
459         mHandler.removeCallbacks(mRemoveCastIconRunnable);
460         if (isCasting) {
461             mIconController.setIcon(mSlotCast, R.drawable.stat_sys_cast,
462                     mContext.getString(R.string.accessibility_casting));
463             mIconController.setIconVisibility(mSlotCast, true);
464         } else {
465             // don't turn off the screen-record icon for a few seconds, just to make sure the user
466             // has seen it
467             if (DEBUG) Log.v(TAG, "updateCast: hiding icon in 3 sec...");
468             mHandler.postDelayed(mRemoveCastIconRunnable, 3000);
469         }
470     }
471
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;
478                 return;
479             }
480         }
481     }
482
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(() -> {
488             final int userId;
489             try {
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())) {
496                         showIcon = true;
497                         mIconController.setIcon(mSlotManagedProfile,
498                                 R.drawable.stat_sys_managed_profile_status,
499                                 mContext.getString(R.string.accessibility_managed_profile));
500                     } else if (mManagedProfileInQuietMode) {
501                         showIcon = true;
502                         mIconController.setIcon(mSlotManagedProfile,
503                                 R.drawable.stat_sys_managed_profile_status_off,
504                                 mContext.getString(R.string.accessibility_managed_profile));
505                     } else {
506                         showIcon = false;
507                     }
508                     if (mManagedProfileIconVisible != showIcon) {
509                         mIconController.setIconVisibility(mSlotManagedProfile, showIcon);
510                         mManagedProfileIconVisible = showIcon;
511                     }
512                 });
513             } catch (RemoteException e) {
514                 Log.w(TAG, "updateManagedProfile: ", e);
515             }
516         });
517     }
518
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(() -> {
525             try {
526                 int focusedId = ActivityManager.getService().getFocusedStackId();
527                 if (focusedId == StackId.FULLSCREEN_WORKSPACE_STACK_ID) {
528                     checkStack(StackId.FULLSCREEN_WORKSPACE_STACK_ID, notifs, noMan, pm);
529                 }
530                 if (mDockedStackExists) {
531                     checkStack(StackId.DOCKED_STACK_ID, notifs, noMan, pm);
532                 }
533             } catch (RemoteException e) {
534                 e.rethrowFromSystemServer();
535             }
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)));
539         });
540     }
541
542     private void checkStack(int stackId, ArraySet<Pair<String, Integer>> notifs,
543             NotificationManager noMan, IPackageManager pm) {
544         try {
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]);
555                 }
556             }
557         } catch (RemoteException e) {
558             e.rethrowFromSystemServer();
559         }
560     }
561
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();
575
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)
581                     .setPackage(null)
582                     .addFlags(Intent.FLAG_IGNORE_EPHEMERAL)
583                     .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
584
585             PendingIntent pendingIntent = PendingIntent.getActivity(mContext,
586                     0 /* requestCode */, browserIntent, PendingIntent.FLAG_IMMUTABLE);
587             ComponentName aiaComponent = null;
588             try {
589                 aiaComponent = AppGlobals.getPackageManager().getInstantAppInstallerComponent();
590             } catch (RemoteException e) {
591                 e.rethrowFromSystemServer();
592             }
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);
601
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);
607         }
608
609         noMan.notifyAsUser(pkg, SystemMessage.NOTE_INSTANT_APPS, builder
610                         .addExtras(extras)
611                         .addAction(action)
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)
619                         .setOngoing(true)
620                         .build(),
621                 new UserHandle(userId));
622     }
623
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;
630             }
631         }
632         return null;
633     }
634
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);
639             return true;
640         }
641         return false;
642     }
643
644     private final SynchronousUserSwitchObserver mUserSwitchListener =
645             new SynchronousUserSwitchObserver() {
646                 @Override
647                 public void onUserSwitching(int newUserId) throws RemoteException {
648                     mHandler.post(() -> mUserInfoController.reloadUserInfo());
649                 }
650
651                 @Override
652                 public void onUserSwitchComplete(int newUserId) throws RemoteException {
653                     mHandler.post(() -> {
654                         updateAlarm();
655                         updateQuietState();
656                         updateManagedProfile();
657                         updateForegroundInstantApps();
658                     });
659                 }
660             };
661
662     private final HotspotController.Callback mHotspotCallback = new HotspotController.Callback() {
663         @Override
664         public void onHotspotChanged(boolean enabled) {
665             mIconController.setIconVisibility(mSlotHotspot, enabled);
666         }
667     };
668
669     private final CastController.Callback mCastCallback = new CastController.Callback() {
670         @Override
671         public void onCastDevicesChanged() {
672             updateCast();
673         }
674     };
675
676     private final NextAlarmController.NextAlarmChangeCallback mNextAlarmCallback =
677             new NextAlarmController.NextAlarmChangeCallback() {
678                 @Override
679                 public void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm) {
680                     updateAlarm();
681                 }
682             };
683
684     @Override
685     public void appTransitionStarting(long startTime, long duration, boolean forced) {
686         updateManagedProfile();
687         updateForegroundInstantApps();
688     }
689
690     @Override
691     public void onKeyguardShowingChanged() {
692         updateManagedProfile();
693         updateForegroundInstantApps();
694     }
695
696     @Override
697     public void onUserSetupChanged() {
698         boolean userSetup = mProvisionedController.isUserSetup(
699                 mProvisionedController.getCurrentUser());
700         if (mCurrentUserSetup == userSetup) return;
701         mCurrentUserSetup = userSetup;
702         updateAlarm();
703         updateQuietState();
704     }
705
706     @Override
707     public void preloadRecentApps() {
708         updateForegroundInstantApps();
709     }
710
711     @Override
712     public void onRotationLockStateChanged(boolean rotationLocked, boolean affordanceVisible) {
713         boolean portrait = RotationLockTile.isCurrentOrientationLockPortrait(
714                 mRotationLockController, mContext);
715         if (rotationLocked) {
716             if (portrait) {
717                 mIconController.setIcon(mSlotRotate, R.drawable.stat_sys_rotate_portrait,
718                         mContext.getString(R.string.accessibility_rotation_lock_on_portrait));
719             } else {
720                 mIconController.setIcon(mSlotRotate, R.drawable.stat_sys_rotate_landscape,
721                         mContext.getString(R.string.accessibility_rotation_lock_on_landscape));
722             }
723             mIconController.setIconVisibility(mSlotRotate, true);
724         } else {
725             mIconController.setIconVisibility(mSlotRotate, false);
726         }
727     }
728
729     private void updateHeadsetPlug(Intent intent) {
730         boolean connected = intent.getIntExtra("state", 0) != 0;
731         boolean hasMic = intent.getIntExtra("microphone", 0) != 0;
732         if (connected) {
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);
739         } else {
740             mIconController.setIconVisibility(mSlotHeadset, false);
741         }
742     }
743
744     @Override
745     public void onDataSaverChanged(boolean isDataSaving) {
746         mIconController.setIconVisibility(mSlotDataSaver, isDataSaving);
747     }
748
749     private final TaskStackListener mTaskListener = new TaskStackListener() {
750         @Override
751         public void onTaskStackChanged() {
752             // Listen for changes to stacks and then check which instant apps are foreground.
753             updateForegroundInstantApps();
754         }
755     };
756
757     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
758         @Override
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)) {
763                 updateVolumeZen();
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)) {
772                 updateQuietState();
773                 updateManagedProfile();
774             } else if (action.equals(AudioManager.ACTION_HEADSET_PLUG)) {
775                 updateHeadsetPlug(intent);
776             }
777         }
778     };
779
780     private Runnable mRemoveCastIconRunnable = new Runnable() {
781         @Override
782         public void run() {
783             if (DEBUG) Log.v(TAG, "updateCast: hiding icon NOW");
784             mIconController.setIconVisibility(mSlotCast, false);
785         }
786     };
787 }