@@ -73,8 +73,8 @@ final class WifiDisplayAdapter extends DisplayAdapter {
7373 private final boolean mSupportsProtectedBuffers ;
7474 private final NotificationManager mNotificationManager ;
7575
76- private final PendingIntent mSettingsPendingIntent ;
77- private final PendingIntent mDisconnectPendingIntent ;
76+ private PendingIntent mSettingsPendingIntent ;
77+ private PendingIntent mDisconnectPendingIntent ;
7878
7979 private WifiDisplayController mDisplayController ;
8080 private WifiDisplayDevice mDisplayDevice ;
@@ -90,6 +90,7 @@ final class WifiDisplayAdapter extends DisplayAdapter {
9090 private boolean mPendingStatusChangeBroadcast ;
9191 private boolean mPendingNotificationUpdate ;
9292
93+ // Called with SyncRoot lock held.
9394 public WifiDisplayAdapter (DisplayManagerService .SyncRoot syncRoot ,
9495 Context context , Handler handler , Listener listener ,
9596 PersistentDataStore persistentDataStore ) {
@@ -100,20 +101,6 @@ public WifiDisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
100101 com .android .internal .R .bool .config_wifiDisplaySupportsProtectedBuffers );
101102 mNotificationManager = (NotificationManager )context .getSystemService (
102103 Context .NOTIFICATION_SERVICE );
103-
104- Intent settingsIntent = new Intent (Settings .ACTION_WIFI_DISPLAY_SETTINGS );
105- settingsIntent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK
106- | Intent .FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
107- | Intent .FLAG_ACTIVITY_CLEAR_TOP );
108- mSettingsPendingIntent = PendingIntent .getActivityAsUser (
109- context , 0 , settingsIntent , 0 , null , UserHandle .CURRENT );
110-
111- Intent disconnectIntent = new Intent (ACTION_DISCONNECT );
112- mDisconnectPendingIntent = PendingIntent .getBroadcastAsUser (
113- context , 0 , disconnectIntent , 0 , UserHandle .CURRENT );
114-
115- context .registerReceiverAsUser (mBroadcastReceiver , UserHandle .ALL ,
116- new IntentFilter (ACTION_DISCONNECT ), null , mHandler );
117104 }
118105
119106 @ Override
@@ -153,6 +140,9 @@ public void registerLocked() {
153140 public void run () {
154141 mDisplayController = new WifiDisplayController (
155142 getContext (), getHandler (), mWifiDisplayListener );
143+
144+ getContext ().registerReceiverAsUser (mBroadcastReceiver , UserHandle .ALL ,
145+ new IntentFilter (ACTION_DISCONNECT ), null , mHandler );
156146 }
157147 });
158148 }
@@ -366,12 +356,31 @@ private void handleUpdateNotification() {
366356 isConnected = (mDisplayDevice != null );
367357 }
368358
359+ // Cancel the old notification if there is one.
369360 mNotificationManager .cancelAsUser (null ,
370361 R .string .wifi_display_notification_title , UserHandle .ALL );
371362
372363 if (isConnected ) {
373364 Context context = getContext ();
374365
366+ // Initialize pending intents for the notification outside of the lock because
367+ // creating a pending intent requires a call into the activity manager.
368+ if (mSettingsPendingIntent == null ) {
369+ Intent settingsIntent = new Intent (Settings .ACTION_WIFI_DISPLAY_SETTINGS );
370+ settingsIntent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK
371+ | Intent .FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
372+ | Intent .FLAG_ACTIVITY_CLEAR_TOP );
373+ mSettingsPendingIntent = PendingIntent .getActivityAsUser (
374+ context , 0 , settingsIntent , 0 , null , UserHandle .CURRENT );
375+ }
376+
377+ if (mDisconnectPendingIntent == null ) {
378+ Intent disconnectIntent = new Intent (ACTION_DISCONNECT );
379+ mDisconnectPendingIntent = PendingIntent .getBroadcastAsUser (
380+ context , 0 , disconnectIntent , 0 , UserHandle .CURRENT );
381+ }
382+
383+ // Post the notification.
375384 Resources r = context .getResources ();
376385 Notification notification = new Notification .Builder (context )
377386 .setContentTitle (r .getString (
0 commit comments