OSDN Git Service

am 438d77e0: MediaFrameworkTest: Fix Camera2 API unit test build breakage
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / statusbar / BaseStatusBar.java
1 /*
2  * Copyright (C) 2010 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;
18
19 import android.app.ActivityManager;
20 import android.app.ActivityManagerNative;
21 import android.app.Notification;
22 import android.app.PendingIntent;
23 import android.app.TaskStackBuilder;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.pm.ApplicationInfo;
29 import android.content.pm.PackageManager;
30 import android.content.pm.PackageManager.NameNotFoundException;
31 import android.content.pm.UserInfo;
32 import android.content.res.Configuration;
33 import android.database.ContentObserver;
34 import android.graphics.Rect;
35 import android.graphics.drawable.Drawable;
36 import android.net.Uri;
37 import android.os.Build;
38 import android.os.Handler;
39 import android.os.IBinder;
40 import android.os.Message;
41 import android.os.PowerManager;
42 import android.os.RemoteException;
43 import android.os.ServiceManager;
44 import android.os.UserHandle;
45 import android.os.UserManager;
46 import android.provider.Settings;
47 import android.service.dreams.DreamService;
48 import android.service.dreams.IDreamManager;
49 import android.service.notification.StatusBarNotification;
50 import android.text.TextUtils;
51 import android.util.Log;
52 import android.util.SparseArray;
53 import android.util.SparseBooleanArray;
54 import android.view.Display;
55 import android.view.IWindowManager;
56 import android.view.LayoutInflater;
57 import android.view.MenuItem;
58 import android.view.MotionEvent;
59 import android.view.View;
60 import android.view.ViewGroup;
61 import android.view.ViewGroup.LayoutParams;
62 import android.view.WindowManager;
63 import android.view.WindowManagerGlobal;
64 import android.widget.ImageView;
65 import android.widget.LinearLayout;
66 import android.widget.PopupMenu;
67 import android.widget.RemoteViews;
68 import android.widget.TextView;
69
70 import com.android.internal.statusbar.IStatusBarService;
71 import com.android.internal.statusbar.StatusBarIcon;
72 import com.android.internal.statusbar.StatusBarIconList;
73 import com.android.internal.util.LegacyNotificationUtil;
74 import com.android.internal.widget.SizeAdaptiveLayout;
75 import com.android.systemui.R;
76 import com.android.systemui.RecentsComponent;
77 import com.android.systemui.SearchPanelView;
78 import com.android.systemui.SystemUI;
79 import com.android.systemui.statusbar.phone.KeyguardTouchDelegate;
80 import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
81
82 import java.util.ArrayList;
83 import java.util.Locale;
84
85 public abstract class BaseStatusBar extends SystemUI implements
86         CommandQueue.Callbacks {
87     public static final String TAG = "StatusBar";
88     public static final boolean DEBUG = false;
89     public static final boolean MULTIUSER_DEBUG = false;
90
91     protected static final int MSG_TOGGLE_RECENTS_PANEL = 1020;
92     protected static final int MSG_CLOSE_RECENTS_PANEL = 1021;
93     protected static final int MSG_PRELOAD_RECENT_APPS = 1022;
94     protected static final int MSG_CANCEL_PRELOAD_RECENT_APPS = 1023;
95     protected static final int MSG_OPEN_SEARCH_PANEL = 1024;
96     protected static final int MSG_CLOSE_SEARCH_PANEL = 1025;
97     protected static final int MSG_SHOW_HEADS_UP = 1026;
98     protected static final int MSG_HIDE_HEADS_UP = 1027;
99     protected static final int MSG_ESCALATE_HEADS_UP = 1028;
100
101     protected static final boolean ENABLE_HEADS_UP = true;
102     // scores above this threshold should be displayed in heads up mode.
103     protected static final int INTERRUPTION_THRESHOLD = 10;
104     protected static final String SETTING_HEADS_UP_TICKER = "ticker_gets_heads_up";
105
106     // Should match the value in PhoneWindowManager
107     public static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
108
109     public static final int EXPANDED_LEAVE_ALONE = -10000;
110     public static final int EXPANDED_FULL_OPEN = -10001;
111
112     protected CommandQueue mCommandQueue;
113     protected IStatusBarService mBarService;
114     protected H mHandler = createHandler();
115
116     // all notifications
117     protected NotificationData mNotificationData = new NotificationData();
118     protected NotificationStackScrollLayout mStackScroller;
119
120     protected NotificationData.Entry mInterruptingNotificationEntry;
121     protected long mInterruptingNotificationTime;
122
123     // used to notify status bar for suppressing notification LED
124     protected boolean mPanelSlightlyVisible;
125
126     // Search panel
127     protected SearchPanelView mSearchPanelView;
128
129     protected PopupMenu mNotificationBlamePopup;
130
131     protected int mCurrentUserId = 0;
132     final protected SparseArray<UserInfo> mCurrentProfiles = new SparseArray<UserInfo>();
133
134     protected int mLayoutDirection = -1; // invalid
135     private Locale mLocale;
136     protected boolean mUseHeadsUp = false;
137     protected boolean mHeadsUpTicker = false;
138
139     protected IDreamManager mDreamManager;
140     PowerManager mPowerManager;
141     protected int mRowMinHeight;
142     protected int mRowMaxHeight;
143
144     // public mode, private notifications, etc
145     private boolean mLockscreenPublicMode = false;
146     private final SparseBooleanArray mUsersAllowingPrivateNotifications = new SparseBooleanArray();
147     private LegacyNotificationUtil mLegacyNotificationUtil = LegacyNotificationUtil.getInstance();
148
149     private UserManager mUserManager;
150
151     // UI-specific methods
152
153     /**
154      * Create all windows necessary for the status bar (including navigation, overlay panels, etc)
155      * and add them to the window manager.
156      */
157     protected abstract void createAndAddWindows();
158
159     protected WindowManager mWindowManager;
160     protected IWindowManager mWindowManagerService;
161     protected abstract void refreshLayout(int layoutDirection);
162
163     protected Display mDisplay;
164
165     private boolean mDeviceProvisioned = false;
166
167     private RecentsComponent mRecents;
168
169     protected int mZenMode;
170
171     protected boolean mOnKeyguard;
172     protected View mKeyguardIconOverflowContainer;
173     protected NotificationOverflowIconsView mOverflowIconsView;
174
175     public boolean isDeviceProvisioned() {
176         return mDeviceProvisioned;
177     }
178
179     protected final ContentObserver mSettingsObserver = new ContentObserver(mHandler) {
180         @Override
181         public void onChange(boolean selfChange) {
182             final boolean provisioned = 0 != Settings.Global.getInt(
183                     mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0);
184             if (provisioned != mDeviceProvisioned) {
185                 mDeviceProvisioned = provisioned;
186                 updateNotificationIcons();
187             }
188             final int mode = Settings.Global.getInt(mContext.getContentResolver(),
189                     Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_OFF);
190             setZenMode(mode);
191         }
192     };
193
194     private final ContentObserver mLockscreenSettingsObserver = new ContentObserver(mHandler) {
195         @Override
196         public void onChange(boolean selfChange) {
197             // We don't know which user changed LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
198             // so we just dump our cache ...
199             mUsersAllowingPrivateNotifications.clear();
200             // ... and refresh all the notifications
201             updateNotificationIcons();
202         }
203     };
204
205     private RemoteViews.OnClickHandler mOnClickHandler = new RemoteViews.OnClickHandler() {
206         @Override
207         public boolean onClickHandler(View view, PendingIntent pendingIntent, Intent fillInIntent) {
208             if (DEBUG) {
209                 Log.v(TAG, "Notification click handler invoked for intent: " + pendingIntent);
210             }
211             final boolean isActivity = pendingIntent.isActivity();
212             if (isActivity) {
213                 try {
214                     // The intent we are sending is for the application, which
215                     // won't have permission to immediately start an activity after
216                     // the user switches to home.  We know it is safe to do at this
217                     // point, so make sure new activity switches are now allowed.
218                     ActivityManagerNative.getDefault().resumeAppSwitches();
219                     // Also, notifications can be launched from the lock screen,
220                     // so dismiss the lock screen when the activity starts.
221                     ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity();
222                 } catch (RemoteException e) {
223                 }
224             }
225
226             boolean handled = super.onClickHandler(view, pendingIntent, fillInIntent);
227
228             if (isActivity && handled) {
229                 // close the shade if it was open
230                 animateCollapsePanels(CommandQueue.FLAG_EXCLUDE_NONE);
231                 visibilityChanged(false);
232             }
233             return handled;
234         }
235     };
236
237     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
238         @Override
239         public void onReceive(Context context, Intent intent) {
240             String action = intent.getAction();
241             if (Intent.ACTION_USER_SWITCHED.equals(action)) {
242                 mCurrentUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
243                 updateCurrentProfilesCache();
244                 if (true) Log.v(TAG, "userId " + mCurrentUserId + " is in the house");
245                 userSwitched(mCurrentUserId);
246             } else if (Intent.ACTION_USER_ADDED.equals(action)) {
247                 updateCurrentProfilesCache();
248             }
249         }
250     };
251
252     private void updateCurrentProfilesCache() {
253         synchronized (mCurrentProfiles) {
254             mCurrentProfiles.clear();
255             if (mUserManager != null) {
256                 for (UserInfo user : mUserManager.getProfiles(mCurrentUserId)) {
257                     mCurrentProfiles.put(user.id, user);
258                 }
259             }
260         }
261     }
262
263     public void start() {
264         mWindowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
265         mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
266         mDisplay = mWindowManager.getDefaultDisplay();
267
268         mDreamManager = IDreamManager.Stub.asInterface(
269                 ServiceManager.checkService(DreamService.DREAM_SERVICE));
270         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
271
272         mSettingsObserver.onChange(false); // set up
273         mContext.getContentResolver().registerContentObserver(
274                 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED), true,
275                 mSettingsObserver);
276         mContext.getContentResolver().registerContentObserver(
277                 Settings.Global.getUriFor(Settings.Global.ZEN_MODE), false,
278                 mSettingsObserver);
279
280         mContext.getContentResolver().registerContentObserver(
281                 Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS),
282                 true,
283                 mLockscreenSettingsObserver,
284                 UserHandle.USER_ALL);
285
286         mBarService = IStatusBarService.Stub.asInterface(
287                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
288
289         mRecents = getComponent(RecentsComponent.class);
290
291         mLocale = mContext.getResources().getConfiguration().locale;
292         mLayoutDirection = TextUtils.getLayoutDirectionFromLocale(mLocale);
293
294         mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
295
296         // Connect in to the status bar manager service
297         StatusBarIconList iconList = new StatusBarIconList();
298         ArrayList<IBinder> notificationKeys = new ArrayList<IBinder>();
299         ArrayList<StatusBarNotification> notifications = new ArrayList<StatusBarNotification>();
300         mCommandQueue = new CommandQueue(this, iconList);
301
302         int[] switches = new int[7];
303         ArrayList<IBinder> binders = new ArrayList<IBinder>();
304         try {
305             mBarService.registerStatusBar(mCommandQueue, iconList, notificationKeys, notifications,
306                     switches, binders);
307         } catch (RemoteException ex) {
308             // If the system process isn't there we're doomed anyway.
309         }
310
311         createAndAddWindows();
312
313         disable(switches[0]);
314         setSystemUiVisibility(switches[1], 0xffffffff);
315         topAppWindowChanged(switches[2] != 0);
316         // StatusBarManagerService has a back up of IME token and it's restored here.
317         setImeWindowStatus(binders.get(0), switches[3], switches[4]);
318         setHardKeyboardStatus(switches[5] != 0, switches[6] != 0);
319
320         // Set up the initial icon state
321         int N = iconList.size();
322         int viewIndex = 0;
323         for (int i=0; i<N; i++) {
324             StatusBarIcon icon = iconList.getIcon(i);
325             if (icon != null) {
326                 addIcon(iconList.getSlot(i), i, viewIndex, icon);
327                 viewIndex++;
328             }
329         }
330
331         // Set up the initial notification state
332         N = notificationKeys.size();
333         if (N == notifications.size()) {
334             for (int i=0; i<N; i++) {
335                 addNotification(notificationKeys.get(i), notifications.get(i));
336             }
337         } else {
338             Log.wtf(TAG, "Notification list length mismatch: keys=" + N
339                     + " notifications=" + notifications.size());
340         }
341
342         if (DEBUG) {
343             Log.d(TAG, String.format(
344                     "init: icons=%d disabled=0x%08x lights=0x%08x menu=0x%08x imeButton=0x%08x",
345                    iconList.size(),
346                    switches[0],
347                    switches[1],
348                    switches[2],
349                    switches[3]
350                    ));
351         }
352
353         mCurrentUserId = ActivityManager.getCurrentUser();
354
355         IntentFilter filter = new IntentFilter();
356         filter.addAction(Intent.ACTION_USER_SWITCHED);
357         filter.addAction(Intent.ACTION_USER_ADDED);
358         mContext.registerReceiver(mBroadcastReceiver, filter);
359
360         updateCurrentProfilesCache();
361     }
362
363     public void userSwitched(int newUserId) {
364         // should be overridden
365     }
366
367     public boolean notificationIsForCurrentProfiles(StatusBarNotification n) {
368         final int thisUserId = mCurrentUserId;
369         final int notificationUserId = n.getUserId();
370         if (DEBUG && MULTIUSER_DEBUG) {
371             Log.v(TAG, String.format("%s: current userid: %d, notification userid: %d",
372                     n, thisUserId, notificationUserId));
373         }
374         synchronized (mCurrentProfiles) {
375             return notificationUserId == UserHandle.USER_ALL
376                     || mCurrentProfiles.get(notificationUserId) != null;
377         }
378     }
379
380     @Override
381     protected void onConfigurationChanged(Configuration newConfig) {
382         final Locale locale = mContext.getResources().getConfiguration().locale;
383         final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
384         if (! locale.equals(mLocale) || ld != mLayoutDirection) {
385             if (DEBUG) {
386                 Log.v(TAG, String.format(
387                         "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
388                         locale, ld));
389             }
390             mLocale = locale;
391             mLayoutDirection = ld;
392             refreshLayout(ld);
393         }
394     }
395
396     protected View updateNotificationVetoButton(View row, StatusBarNotification n) {
397         View vetoButton = row.findViewById(R.id.veto);
398         if (n.isClearable() || (mInterruptingNotificationEntry != null
399                 && mInterruptingNotificationEntry.row == row)) {
400             final String _pkg = n.getPackageName();
401             final String _tag = n.getTag();
402             final int _id = n.getId();
403             final int _userId = n.getUserId();
404             vetoButton.setOnClickListener(new View.OnClickListener() {
405                     public void onClick(View v) {
406                         // Accessibility feedback
407                         v.announceForAccessibility(
408                                 mContext.getString(R.string.accessibility_notification_dismissed));
409                         try {
410                             mBarService.onNotificationClear(_pkg, _tag, _id, _userId);
411
412                         } catch (RemoteException ex) {
413                             // system process is dead if we're here.
414                         }
415                     }
416                 });
417             vetoButton.setVisibility(View.VISIBLE);
418         } else {
419             vetoButton.setVisibility(View.GONE);
420         }
421         vetoButton.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
422         return vetoButton;
423     }
424
425
426     protected void applyLegacyRowBackground(StatusBarNotification sbn, View content) {
427         if (sbn.getNotification().contentView.getLayoutId() !=
428                 com.android.internal.R.layout.notification_template_base) {
429             int version = 0;
430             try {
431                 ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(sbn.getPackageName(), 0);
432                 version = info.targetSdkVersion;
433             } catch (NameNotFoundException ex) {
434                 Log.e(TAG, "Failed looking up ApplicationInfo for " + sbn.getPackageName(), ex);
435             }
436             if (version > 0 && version < Build.VERSION_CODES.GINGERBREAD) {
437                 content.setBackgroundResource(R.drawable.notification_row_legacy_bg);
438             }
439         }
440     }
441
442     private void startApplicationDetailsActivity(String packageName) {
443         Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
444                 Uri.fromParts("package", packageName, null));
445         intent.setComponent(intent.resolveActivity(mContext.getPackageManager()));
446         TaskStackBuilder.create(mContext).addNextIntentWithParentStack(intent).startActivities(
447                 null, UserHandle.CURRENT);
448     }
449
450     protected View.OnLongClickListener getNotificationLongClicker() {
451         return new View.OnLongClickListener() {
452             @Override
453             public boolean onLongClick(View v) {
454                 final String packageNameF = (String) v.getTag();
455                 if (packageNameF == null) return false;
456                 if (v.getWindowToken() == null) return false;
457                 mNotificationBlamePopup = new PopupMenu(mContext, v);
458                 mNotificationBlamePopup.getMenuInflater().inflate(
459                         R.menu.notification_popup_menu,
460                         mNotificationBlamePopup.getMenu());
461                 mNotificationBlamePopup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
462                     public boolean onMenuItemClick(MenuItem item) {
463                         if (item.getItemId() == R.id.notification_inspect_item) {
464                             startApplicationDetailsActivity(packageNameF);
465                             animateCollapsePanels(CommandQueue.FLAG_EXCLUDE_NONE);
466                         } else {
467                             return false;
468                         }
469                         return true;
470                     }
471                 });
472                 mNotificationBlamePopup.show();
473
474                 return true;
475             }
476         };
477     }
478
479     public void dismissPopups() {
480         if (mNotificationBlamePopup != null) {
481             mNotificationBlamePopup.dismiss();
482             mNotificationBlamePopup = null;
483         }
484     }
485
486     public void onHeadsUpDismissed() {
487     }
488
489     @Override
490     public void toggleRecentApps() {
491         int msg = MSG_TOGGLE_RECENTS_PANEL;
492         mHandler.removeMessages(msg);
493         mHandler.sendEmptyMessage(msg);
494     }
495
496     @Override
497     public void preloadRecentApps() {
498         int msg = MSG_PRELOAD_RECENT_APPS;
499         mHandler.removeMessages(msg);
500         mHandler.sendEmptyMessage(msg);
501     }
502
503     @Override
504     public void cancelPreloadRecentApps() {
505         int msg = MSG_CANCEL_PRELOAD_RECENT_APPS;
506         mHandler.removeMessages(msg);
507         mHandler.sendEmptyMessage(msg);
508     }
509
510     @Override
511     public void showSearchPanel() {
512         int msg = MSG_OPEN_SEARCH_PANEL;
513         mHandler.removeMessages(msg);
514         mHandler.sendEmptyMessage(msg);
515     }
516
517     @Override
518     public void hideSearchPanel() {
519         int msg = MSG_CLOSE_SEARCH_PANEL;
520         mHandler.removeMessages(msg);
521         mHandler.sendEmptyMessage(msg);
522     }
523
524     protected abstract WindowManager.LayoutParams getSearchLayoutParams(
525             LayoutParams layoutParams);
526
527     protected void updateSearchPanel() {
528         // Search Panel
529         boolean visible = false;
530         if (mSearchPanelView != null) {
531             visible = mSearchPanelView.isShowing();
532             mWindowManager.removeView(mSearchPanelView);
533         }
534
535         // Provide SearchPanel with a temporary parent to allow layout params to work.
536         LinearLayout tmpRoot = new LinearLayout(mContext);
537         mSearchPanelView = (SearchPanelView) LayoutInflater.from(mContext).inflate(
538                  R.layout.status_bar_search_panel, tmpRoot, false);
539         mSearchPanelView.setOnTouchListener(
540                  new TouchOutsideListener(MSG_CLOSE_SEARCH_PANEL, mSearchPanelView));
541         mSearchPanelView.setVisibility(View.GONE);
542
543         WindowManager.LayoutParams lp = getSearchLayoutParams(mSearchPanelView.getLayoutParams());
544
545         mWindowManager.addView(mSearchPanelView, lp);
546         mSearchPanelView.setBar(this);
547         if (visible) {
548             mSearchPanelView.show(true, false);
549         }
550     }
551
552     protected H createHandler() {
553          return new H();
554     }
555
556     static void sendCloseSystemWindows(Context context, String reason) {
557         if (ActivityManagerNative.isSystemReady()) {
558             try {
559                 ActivityManagerNative.getDefault().closeSystemDialogs(reason);
560             } catch (RemoteException e) {
561             }
562         }
563     }
564
565     protected abstract View getStatusBarView();
566
567     protected View.OnTouchListener mRecentsPreloadOnTouchListener = new View.OnTouchListener() {
568         // additional optimization when we have software system buttons - start loading the recent
569         // tasks on touch down
570         @Override
571         public boolean onTouch(View v, MotionEvent event) {
572             int action = event.getAction() & MotionEvent.ACTION_MASK;
573             if (action == MotionEvent.ACTION_DOWN) {
574                 preloadRecentTasksList();
575             } else if (action == MotionEvent.ACTION_CANCEL) {
576                 cancelPreloadingRecentTasksList();
577             } else if (action == MotionEvent.ACTION_UP) {
578                 if (!v.isPressed()) {
579                     cancelPreloadingRecentTasksList();
580                 }
581
582             }
583             return false;
584         }
585     };
586
587     protected void toggleRecentsActivity() {
588         if (mRecents != null) {
589             sendCloseSystemWindows(mContext, SYSTEM_DIALOG_REASON_RECENT_APPS);
590             mRecents.toggleRecents(mDisplay, mLayoutDirection, getStatusBarView());
591         }
592     }
593
594     protected void preloadRecentTasksList() {
595         if (mRecents != null) {
596             mRecents.preloadRecentTasksList();
597         }
598     }
599
600     protected void cancelPreloadingRecentTasksList() {
601         if (mRecents != null) {
602             mRecents.cancelPreloadingRecentTasksList();
603         }
604     }
605
606     protected void closeRecents() {
607         if (mRecents != null) {
608             mRecents.closeRecents();
609         }
610     }
611
612     public abstract void resetHeadsUpDecayTimer();
613
614     /**
615      * Save the current "public" (locked and secure) state of the lockscreen.
616      */
617     public void setLockscreenPublicMode(boolean publicMode) {
618         mLockscreenPublicMode = publicMode;
619     }
620
621     public boolean isLockscreenPublicMode() {
622         return mLockscreenPublicMode;
623     }
624
625     /**
626      * Has the given user chosen to allow their private (full) notifications to be shown even
627      * when the lockscreen is in "public" (secure & locked) mode?
628      */
629     public boolean userAllowsPrivateNotificationsInPublic(int userHandle) {
630         if (userHandle == UserHandle.USER_ALL) {
631             return true;
632         }
633
634         if (mUsersAllowingPrivateNotifications.indexOfKey(userHandle) < 0) {
635             final boolean allowed = 0 != Settings.Secure.getIntForUser(
636                     mContext.getContentResolver(),
637                     Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, userHandle);
638             mUsersAllowingPrivateNotifications.append(userHandle, allowed);
639             return allowed;
640         }
641
642         return mUsersAllowingPrivateNotifications.get(userHandle);
643     }
644
645     protected class H extends Handler {
646         public void handleMessage(Message m) {
647             Intent intent;
648             switch (m.what) {
649              case MSG_TOGGLE_RECENTS_PANEL:
650                  toggleRecentsActivity();
651                  break;
652              case MSG_CLOSE_RECENTS_PANEL:
653                  closeRecents();
654                  break;
655              case MSG_PRELOAD_RECENT_APPS:
656                   preloadRecentTasksList();
657                   break;
658              case MSG_CANCEL_PRELOAD_RECENT_APPS:
659                   cancelPreloadingRecentTasksList();
660                   break;
661              case MSG_OPEN_SEARCH_PANEL:
662                  if (DEBUG) Log.d(TAG, "opening search panel");
663                  if (mSearchPanelView != null && mSearchPanelView.isAssistantAvailable()) {
664                      mSearchPanelView.show(true, true);
665                      onShowSearchPanel();
666                  }
667                  break;
668              case MSG_CLOSE_SEARCH_PANEL:
669                  if (DEBUG) Log.d(TAG, "closing search panel");
670                  if (mSearchPanelView != null && mSearchPanelView.isShowing()) {
671                      mSearchPanelView.show(false, true);
672                      onHideSearchPanel();
673                  }
674                  break;
675             }
676         }
677     }
678
679     public class TouchOutsideListener implements View.OnTouchListener {
680         private int mMsg;
681         private StatusBarPanel mPanel;
682
683         public TouchOutsideListener(int msg, StatusBarPanel panel) {
684             mMsg = msg;
685             mPanel = panel;
686         }
687
688         public boolean onTouch(View v, MotionEvent ev) {
689             final int action = ev.getAction();
690             if (action == MotionEvent.ACTION_OUTSIDE
691                 || (action == MotionEvent.ACTION_DOWN
692                     && !mPanel.isInContentArea((int)ev.getX(), (int)ev.getY()))) {
693                 mHandler.removeMessages(mMsg);
694                 mHandler.sendEmptyMessage(mMsg);
695                 return true;
696             }
697             return false;
698         }
699     }
700
701     protected void workAroundBadLayerDrawableOpacity(View v) {
702     }
703
704     protected void onHideSearchPanel() {
705     }
706
707     protected void onShowSearchPanel() {
708     }
709
710     public boolean inflateViews(NotificationData.Entry entry, ViewGroup parent) {
711             return inflateViews(entry, parent, false);
712     }
713
714     public boolean inflateViewsForHeadsUp(NotificationData.Entry entry, ViewGroup parent) {
715             return inflateViews(entry, parent, true);
716     }
717
718     public boolean inflateViews(NotificationData.Entry entry, ViewGroup parent, boolean isHeadsUp) {
719         int minHeight =
720                 mContext.getResources().getDimensionPixelSize(R.dimen.notification_min_height);
721         int maxHeight =
722                 mContext.getResources().getDimensionPixelSize(R.dimen.notification_max_height);
723         StatusBarNotification sbn = entry.notification;
724         RemoteViews contentView = sbn.getNotification().contentView;
725         RemoteViews bigContentView = sbn.getNotification().bigContentView;
726
727         if (isHeadsUp) {
728             maxHeight =
729                     mContext.getResources().getDimensionPixelSize(R.dimen.notification_mid_height);
730             bigContentView = sbn.getNotification().headsUpContentView;
731         }
732
733         if (contentView == null) {
734             return false;
735         }
736
737         Log.v(TAG, "publicNotification: "
738                 + sbn.getNotification().publicVersion);
739
740         Notification publicNotification = sbn.getNotification().publicVersion;
741
742         // create the row view
743         LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
744                 Context.LAYOUT_INFLATER_SERVICE);
745         ExpandableNotificationRow row = (ExpandableNotificationRow) inflater.inflate(
746                 R.layout.status_bar_notification_row, parent, false);
747
748         // for blaming (see SwipeHelper.setLongPressListener)
749         row.setTag(sbn.getPackageName());
750
751         workAroundBadLayerDrawableOpacity(row);
752         View vetoButton = updateNotificationVetoButton(row, sbn);
753         vetoButton.setContentDescription(mContext.getString(
754                 R.string.accessibility_remove_notification));
755
756         // NB: the large icon is now handled entirely by the template
757
758         // bind the click event to the content area
759         ViewGroup content = (ViewGroup)row.findViewById(R.id.container);
760         SizeAdaptiveLayout expanded = (SizeAdaptiveLayout)row.findViewById(R.id.expanded);
761         SizeAdaptiveLayout expandedPublic
762                 = (SizeAdaptiveLayout)row.findViewById(R.id.expandedPublic);
763
764         content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
765
766         PendingIntent contentIntent = sbn.getNotification().contentIntent;
767         if (contentIntent != null) {
768             final View.OnClickListener listener = makeClicker(contentIntent,
769                     sbn.getPackageName(), sbn.getTag(), sbn.getId(), isHeadsUp, sbn.getUserId());
770             content.setOnClickListener(listener);
771         } else {
772             content.setOnClickListener(null);
773         }
774
775         // set up the adaptive layout
776         View contentViewLocal = null;
777         View bigContentViewLocal = null;
778         try {
779             contentViewLocal = contentView.apply(mContext, expanded,
780                     mOnClickHandler);
781             if (bigContentView != null) {
782                 bigContentViewLocal = bigContentView.apply(mContext, expanded,
783                         mOnClickHandler);
784             }
785         }
786         catch (RuntimeException e) {
787             final String ident = sbn.getPackageName() + "/0x" + Integer.toHexString(sbn.getId());
788             Log.e(TAG, "couldn't inflate view for notification " + ident, e);
789             return false;
790         }
791
792         if (contentViewLocal != null) {
793             contentViewLocal.setIsRootNamespace(true);
794             SizeAdaptiveLayout.LayoutParams params =
795                     new SizeAdaptiveLayout.LayoutParams(contentViewLocal.getLayoutParams());
796             params.minHeight = minHeight;
797             params.maxHeight = minHeight;
798             expanded.addView(contentViewLocal, params);
799         }
800         if (bigContentViewLocal != null) {
801             bigContentViewLocal.setIsRootNamespace(true);
802             SizeAdaptiveLayout.LayoutParams params =
803                     new SizeAdaptiveLayout.LayoutParams(bigContentViewLocal.getLayoutParams());
804             params.minHeight = minHeight+1;
805             params.maxHeight = maxHeight;
806             expanded.addView(bigContentViewLocal, params);
807         }
808
809         PackageManager pm = mContext.getPackageManager();
810
811         // now the public version
812         View publicViewLocal = null;
813         if (publicNotification != null) {
814             try {
815                 publicViewLocal = publicNotification.contentView.apply(mContext, expandedPublic,
816                         mOnClickHandler);
817
818                 if (publicViewLocal != null) {
819                     publicViewLocal.setIsRootNamespace(true);
820                     SizeAdaptiveLayout.LayoutParams params =
821                             new SizeAdaptiveLayout.LayoutParams(publicViewLocal.getLayoutParams());
822                     params.minHeight = minHeight;
823                     params.maxHeight = minHeight;
824                     expandedPublic.addView(publicViewLocal, params);
825                 }
826             }
827             catch (RuntimeException e) {
828                 final String ident = sbn.getPackageName() + "/0x" + Integer.toHexString(sbn.getId());
829                 Log.e(TAG, "couldn't inflate public view for notification " + ident, e);
830                 publicViewLocal = null;
831             }
832         }
833
834         if (publicViewLocal == null) {
835             // Add a basic notification template
836             publicViewLocal = LayoutInflater.from(mContext).inflate(
837                     com.android.internal.R.layout.notification_template_quantum_base,
838                     expandedPublic, true);
839
840             final TextView title = (TextView) publicViewLocal.findViewById(com.android.internal.R.id.title);
841             try {
842                 title.setText(pm.getApplicationLabel(
843                         pm.getApplicationInfo(entry.notification.getPackageName(), 0)));
844             } catch (NameNotFoundException e) {
845                 title.setText(entry.notification.getPackageName());
846             }
847
848             final ImageView icon = (ImageView) publicViewLocal.findViewById(com.android.internal.R.id.icon);
849
850             final StatusBarIcon ic = new StatusBarIcon(entry.notification.getPackageName(),
851                     entry.notification.getUser(),
852                     entry.notification.getNotification().icon,
853                     entry.notification.getNotification().iconLevel,
854                     entry.notification.getNotification().number,
855                     entry.notification.getNotification().tickerText);
856
857             Drawable iconDrawable = StatusBarIconView.getIcon(mContext, ic);
858             icon.setImageDrawable(iconDrawable);
859             if (mLegacyNotificationUtil.isGrayscale(iconDrawable)) {
860                 icon.setBackgroundResource(
861                         com.android.internal.R.drawable.notification_icon_legacy_bg_inset);
862             }
863
864             final TextView text = (TextView) publicViewLocal.findViewById(com.android.internal.R.id.text);
865             text.setText("Unlock your device to see this notification.");
866
867             // TODO: fill out "time" as well
868         }
869
870         row.setDrawingCacheEnabled(true);
871
872         applyLegacyRowBackground(sbn, content);
873
874         if (MULTIUSER_DEBUG) {
875             TextView debug = (TextView) row.findViewById(R.id.debug_info);
876             if (debug != null) {
877                 debug.setVisibility(View.VISIBLE);
878                 debug.setText("CU " + mCurrentUserId +" NU " + entry.notification.getUserId());
879             }
880         }
881         entry.row = row;
882         entry.row.setHeightRange(mRowMinHeight, mRowMaxHeight);
883         entry.content = content;
884         entry.expanded = contentViewLocal;
885         entry.expandedPublic = publicViewLocal;
886         entry.setBigContentView(bigContentViewLocal);
887
888         return true;
889     }
890
891     public NotificationClicker makeClicker(PendingIntent intent, String pkg, String tag,
892             int id, boolean forHun, int userId) {
893         return new NotificationClicker(intent, pkg, tag, id, forHun, userId);
894     }
895
896     protected class NotificationClicker implements View.OnClickListener {
897         private PendingIntent mIntent;
898         private String mPkg;
899         private String mTag;
900         private int mId;
901         private boolean mIsHeadsUp;
902         private int mUserId;
903
904         public NotificationClicker(PendingIntent intent, String pkg, String tag, int id,
905                 boolean forHun, int userId) {
906             mIntent = intent;
907             mPkg = pkg;
908             mTag = tag;
909             mId = id;
910             mIsHeadsUp = forHun;
911             mUserId = userId;
912         }
913
914         public void onClick(View v) {
915             try {
916                 // The intent we are sending is for the application, which
917                 // won't have permission to immediately start an activity after
918                 // the user switches to home.  We know it is safe to do at this
919                 // point, so make sure new activity switches are now allowed.
920                 ActivityManagerNative.getDefault().resumeAppSwitches();
921                 // Also, notifications can be launched from the lock screen,
922                 // so dismiss the lock screen when the activity starts.
923                 ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity();
924             } catch (RemoteException e) {
925             }
926
927             if (mIntent != null) {
928                 int[] pos = new int[2];
929                 v.getLocationOnScreen(pos);
930                 Intent overlay = new Intent();
931                 overlay.setSourceBounds(
932                         new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
933                 try {
934                     mIntent.send(mContext, 0, overlay);
935                 } catch (PendingIntent.CanceledException e) {
936                     // the stack trace isn't very helpful here.  Just log the exception message.
937                     Log.w(TAG, "Sending contentIntent failed: " + e);
938                 }
939
940                 KeyguardTouchDelegate.getInstance(mContext).dismiss();
941             }
942
943             try {
944                 if (mIsHeadsUp) {
945                     mHandler.sendEmptyMessage(MSG_HIDE_HEADS_UP);
946                 }
947                 mBarService.onNotificationClick(mPkg, mTag, mId, mUserId);
948             } catch (RemoteException ex) {
949                 // system process is dead if we're here.
950             }
951
952             // close the shade if it was open
953             animateCollapsePanels(CommandQueue.FLAG_EXCLUDE_NONE);
954             visibilityChanged(false);
955         }
956     }
957
958     /**
959      * The LEDs are turned o)ff when the notification panel is shown, even just a little bit.
960      * This was added last-minute and is inconsistent with the way the rest of the notifications
961      * are handled, because the notification isn't really cancelled.  The lights are just
962      * turned off.  If any other notifications happen, the lights will turn back on.  Steve says
963      * this is what he wants. (see bug 1131461)
964      */
965     protected void visibilityChanged(boolean visible) {
966         if (mPanelSlightlyVisible != visible) {
967             mPanelSlightlyVisible = visible;
968             try {
969                 if (visible) {
970                     mBarService.onPanelRevealed();
971                 } else {
972                     mBarService.onPanelHidden();
973                 }
974             } catch (RemoteException ex) {
975                 // Won't fail unless the world has ended.
976             }
977         }
978     }
979
980     /**
981      * Cancel this notification and tell the StatusBarManagerService / NotificationManagerService
982      * about the failure.
983      *
984      * WARNING: this will call back into us.  Don't hold any locks.
985      */
986     void handleNotificationError(IBinder key, StatusBarNotification n, String message) {
987         removeNotification(key);
988         try {
989             mBarService.onNotificationError(n.getPackageName(), n.getTag(), n.getId(), n.getUid(),
990                     n.getInitialPid(), message, n.getUserId());
991         } catch (RemoteException ex) {
992             // The end is nigh.
993         }
994     }
995
996     protected StatusBarNotification removeNotificationViews(IBinder key) {
997         NotificationData.Entry entry = mNotificationData.remove(key);
998         if (entry == null) {
999             Log.w(TAG, "removeNotification for unknown key: " + key);
1000             return null;
1001         }
1002         // Remove the expanded view.
1003         ViewGroup rowParent = (ViewGroup)entry.row.getParent();
1004         if (rowParent != null) rowParent.removeView(entry.row);
1005         updateRowStates();
1006         updateNotificationIcons();
1007
1008         return entry.notification;
1009     }
1010
1011     protected NotificationData.Entry createNotificationViews(IBinder key,
1012             StatusBarNotification notification) {
1013         if (DEBUG) {
1014             Log.d(TAG, "createNotificationViews(key=" + key + ", notification=" + notification);
1015         }
1016         // Construct the icon.
1017         final StatusBarIconView iconView = new StatusBarIconView(mContext,
1018                 notification.getPackageName() + "/0x" + Integer.toHexString(notification.getId()),
1019                 notification.getNotification());
1020         iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
1021
1022         final StatusBarIcon ic = new StatusBarIcon(notification.getPackageName(),
1023                 notification.getUser(),
1024                     notification.getNotification().icon,
1025                     notification.getNotification().iconLevel,
1026                     notification.getNotification().number,
1027                     notification.getNotification().tickerText);
1028         if (!iconView.set(ic)) {
1029             handleNotificationError(key, notification, "Couldn't create icon: " + ic);
1030             return null;
1031         }
1032         // Construct the expanded view.
1033         NotificationData.Entry entry = new NotificationData.Entry(key, notification, iconView);
1034         if (!inflateViews(entry, mStackScroller)) {
1035             handleNotificationError(key, notification, "Couldn't expand RemoteViews for: "
1036                     + notification);
1037             return null;
1038         }
1039         return entry;
1040     }
1041
1042     protected void addNotificationViews(NotificationData.Entry entry) {
1043         // Add the expanded view and icon.
1044         int pos = mNotificationData.add(entry);
1045         if (DEBUG) {
1046             Log.d(TAG, "addNotificationViews: added at " + pos);
1047         }
1048         updateRowStates();
1049         updateNotificationIcons();
1050     }
1051
1052     private void addNotificationViews(IBinder key, StatusBarNotification notification) {
1053         addNotificationViews(createNotificationViews(key, notification));
1054     }
1055
1056     /**
1057      * @return The number of notifications we show on Keyguard.
1058      */
1059     protected abstract int getMaxKeyguardNotifications();
1060
1061     /**
1062      * Updates expanded, dimmed and locked states of notification rows.
1063      */
1064     protected void updateRowStates() {
1065         int maxKeyguardNotifications = getMaxKeyguardNotifications();
1066         mOverflowIconsView.removeAllViews();
1067         int n = mNotificationData.size();
1068         int visibleNotifications = 0;
1069         for (int i = n-1; i >= 0; i--) {
1070             NotificationData.Entry entry = mNotificationData.get(i);
1071             if (mOnKeyguard) {
1072                 entry.row.setSystemExpanded(false);
1073             } else {
1074                 if (!entry.row.isUserLocked()) {
1075                     boolean top = (i == n-1);
1076                     entry.row.setSystemExpanded(top || entry.row.isUserExpanded());
1077                 }
1078             }
1079             entry.row.setDimmed(mOnKeyguard);
1080             entry.row.setLocked(mOnKeyguard);
1081             boolean showOnKeyguard = shouldShowOnKeyguard(entry.notification);
1082             if (mOnKeyguard && (visibleNotifications >= maxKeyguardNotifications
1083                     || !showOnKeyguard)) {
1084                 entry.row.setVisibility(View.GONE);
1085                 if (showOnKeyguard) {
1086                     mOverflowIconsView.addNotification(entry);
1087                 }
1088             } else {
1089                 entry.row.setVisibility(View.VISIBLE);
1090                 visibleNotifications++;
1091             }
1092         }
1093
1094         if (mOnKeyguard && mOverflowIconsView.getChildCount() > 0) {
1095             mKeyguardIconOverflowContainer.setVisibility(View.VISIBLE);
1096         } else {
1097             mKeyguardIconOverflowContainer.setVisibility(View.GONE);
1098         }
1099     }
1100
1101     private boolean shouldShowOnKeyguard(StatusBarNotification sbn) {
1102         return sbn.getNotification().priority >= Notification.PRIORITY_LOW;
1103     }
1104
1105     protected void setZenMode(int mode) {
1106         if (!isDeviceProvisioned()) return;
1107         mZenMode = mode;
1108         updateNotificationIcons();
1109     }
1110
1111     protected abstract void haltTicker();
1112     protected abstract void setAreThereNotifications();
1113     protected abstract void updateNotificationIcons();
1114     protected abstract void tick(IBinder key, StatusBarNotification n, boolean firstTime);
1115     protected abstract void updateExpandedViewPos(int expandedPosition);
1116     protected abstract int getExpandedViewMaxHeight();
1117     protected abstract boolean shouldDisableNavbarGestures();
1118
1119     protected boolean isTopNotification(ViewGroup parent, NotificationData.Entry entry) {
1120         return parent != null && parent.indexOfChild(entry.row) == 0;
1121     }
1122
1123     public void updateNotification(IBinder key, StatusBarNotification notification) {
1124         if (DEBUG) Log.d(TAG, "updateNotification(" + key + " -> " + notification + ")");
1125
1126         final NotificationData.Entry oldEntry = mNotificationData.findByKey(key);
1127         if (oldEntry == null) {
1128             Log.w(TAG, "updateNotification for unknown key: " + key);
1129             return;
1130         }
1131
1132         final StatusBarNotification oldNotification = oldEntry.notification;
1133
1134         // XXX: modify when we do something more intelligent with the two content views
1135         final RemoteViews oldContentView = oldNotification.getNotification().contentView;
1136         final RemoteViews contentView = notification.getNotification().contentView;
1137         final RemoteViews oldBigContentView = oldNotification.getNotification().bigContentView;
1138         final RemoteViews bigContentView = notification.getNotification().bigContentView;
1139         final RemoteViews oldHeadsUpContentView = oldNotification.getNotification().headsUpContentView;
1140         final RemoteViews headsUpContentView = notification.getNotification().headsUpContentView;
1141         final Notification oldPublicNotification = oldNotification.getNotification().publicVersion;
1142         final RemoteViews oldPublicContentView = oldPublicNotification != null
1143                 ? oldPublicNotification.contentView : null;
1144         final Notification publicNotification = notification.getNotification().publicVersion;
1145         final RemoteViews publicContentView = publicNotification != null
1146                 ? publicNotification.contentView : null;
1147
1148         if (DEBUG) {
1149             Log.d(TAG, "old notification: when=" + oldNotification.getNotification().when
1150                     + " ongoing=" + oldNotification.isOngoing()
1151                     + " expanded=" + oldEntry.expanded
1152                     + " contentView=" + oldContentView
1153                     + " bigContentView=" + oldBigContentView
1154                     + " publicView=" + oldPublicContentView
1155                     + " rowParent=" + oldEntry.row.getParent());
1156             Log.d(TAG, "new notification: when=" + notification.getNotification().when
1157                     + " ongoing=" + oldNotification.isOngoing()
1158                     + " contentView=" + contentView
1159                     + " bigContentView=" + bigContentView
1160                     + " publicView=" + publicContentView);
1161         }
1162
1163         // Can we just reapply the RemoteViews in place?  If when didn't change, the order
1164         // didn't change.
1165
1166         // 1U is never null
1167         boolean contentsUnchanged = oldEntry.expanded != null
1168                 && contentView.getPackage() != null
1169                 && oldContentView.getPackage() != null
1170                 && oldContentView.getPackage().equals(contentView.getPackage())
1171                 && oldContentView.getLayoutId() == contentView.getLayoutId();
1172         // large view may be null
1173         boolean bigContentsUnchanged =
1174                 (oldEntry.getBigContentView() == null && bigContentView == null)
1175                 || ((oldEntry.getBigContentView() != null && bigContentView != null)
1176                     && bigContentView.getPackage() != null
1177                     && oldBigContentView.getPackage() != null
1178                     && oldBigContentView.getPackage().equals(bigContentView.getPackage())
1179                     && oldBigContentView.getLayoutId() == bigContentView.getLayoutId());
1180         boolean headsUpContentsUnchanged =
1181                 (oldHeadsUpContentView == null && headsUpContentView == null)
1182                 || ((oldHeadsUpContentView != null && headsUpContentView != null)
1183                     && headsUpContentView.getPackage() != null
1184                     && oldHeadsUpContentView.getPackage() != null
1185                     && oldHeadsUpContentView.getPackage().equals(headsUpContentView.getPackage())
1186                     && oldHeadsUpContentView.getLayoutId() == headsUpContentView.getLayoutId());
1187         boolean publicUnchanged  =
1188                 (oldPublicContentView == null && publicContentView == null)
1189                 || ((oldPublicContentView != null && publicContentView != null)
1190                         && publicContentView.getPackage() != null
1191                         && oldPublicContentView.getPackage() != null
1192                         && oldPublicContentView.getPackage().equals(publicContentView.getPackage())
1193                         && oldPublicContentView.getLayoutId() == publicContentView.getLayoutId());
1194
1195         ViewGroup rowParent = (ViewGroup) oldEntry.row.getParent();
1196         boolean orderUnchanged =
1197                    notification.getNotification().when == oldNotification.getNotification().when
1198                 && notification.getScore() == oldNotification.getScore();
1199                 // score now encompasses/supersedes isOngoing()
1200
1201         boolean updateTicker = notification.getNotification().tickerText != null
1202                 && !TextUtils.equals(notification.getNotification().tickerText,
1203                         oldEntry.notification.getNotification().tickerText);
1204         boolean isTopAnyway = isTopNotification(rowParent, oldEntry);
1205         if (contentsUnchanged && bigContentsUnchanged && headsUpContentsUnchanged && publicUnchanged
1206                 && (orderUnchanged || isTopAnyway)) {
1207             if (DEBUG) Log.d(TAG, "reusing notification for key: " + key);
1208             oldEntry.notification = notification;
1209             try {
1210                 updateNotificationViews(oldEntry, notification);
1211
1212                 if (ENABLE_HEADS_UP && mInterruptingNotificationEntry != null
1213                         && oldNotification == mInterruptingNotificationEntry.notification) {
1214                     if (!shouldInterrupt(notification)) {
1215                         if (DEBUG) Log.d(TAG, "no longer interrupts!");
1216                         mHandler.sendEmptyMessage(MSG_HIDE_HEADS_UP);
1217                     } else {
1218                         if (DEBUG) Log.d(TAG, "updating the current heads up:" + notification);
1219                         mInterruptingNotificationEntry.notification = notification;
1220                         updateHeadsUpViews(mInterruptingNotificationEntry, notification);
1221                     }
1222                 }
1223
1224                 // Update the icon.
1225                 final StatusBarIcon ic = new StatusBarIcon(notification.getPackageName(),
1226                         notification.getUser(),
1227                         notification.getNotification().icon, notification.getNotification().iconLevel,
1228                         notification.getNotification().number,
1229                         notification.getNotification().tickerText);
1230                 if (!oldEntry.icon.set(ic)) {
1231                     handleNotificationError(key, notification, "Couldn't update icon: " + ic);
1232                     return;
1233                 }
1234                 updateRowStates();
1235             }
1236             catch (RuntimeException e) {
1237                 // It failed to add cleanly.  Log, and remove the view from the panel.
1238                 Log.w(TAG, "Couldn't reapply views for package " + contentView.getPackage(), e);
1239                 removeNotificationViews(key);
1240                 addNotificationViews(key, notification);
1241             }
1242         } else {
1243             if (DEBUG) Log.d(TAG, "not reusing notification for key: " + key);
1244             if (DEBUG) Log.d(TAG, "contents was " + (contentsUnchanged ? "unchanged" : "changed"));
1245             if (DEBUG) Log.d(TAG, "order was " + (orderUnchanged ? "unchanged" : "changed"));
1246             if (DEBUG) Log.d(TAG, "notification is " + (isTopAnyway ? "top" : "not top"));
1247             removeNotificationViews(key);
1248             addNotificationViews(key, notification);  // will also replace the heads up
1249             final NotificationData.Entry newEntry = mNotificationData.findByKey(key);
1250             final boolean userChangedExpansion = oldEntry.row.hasUserChangedExpansion();
1251             if (userChangedExpansion) {
1252                 boolean userExpanded = oldEntry.row.isUserExpanded();
1253                 newEntry.row.applyExpansionToLayout(userExpanded);
1254                 newEntry.row.setUserExpanded(userExpanded);
1255             }
1256         }
1257
1258         // Update the veto button accordingly (and as a result, whether this row is
1259         // swipe-dismissable)
1260         updateNotificationVetoButton(oldEntry.row, notification);
1261
1262         // Is this for you?
1263         boolean isForCurrentUser = notificationIsForCurrentProfiles(notification);
1264         if (DEBUG) Log.d(TAG, "notification is " + (isForCurrentUser ? "" : "not ") + "for you");
1265
1266         // Restart the ticker if it's still running
1267         if (updateTicker && isForCurrentUser) {
1268             haltTicker();
1269             tick(key, notification, false);
1270         }
1271
1272         // Recalculate the position of the sliding windows and the titles.
1273         setAreThereNotifications();
1274         updateExpandedViewPos(EXPANDED_LEAVE_ALONE);
1275     }
1276
1277     private void updateNotificationViews(NotificationData.Entry entry,
1278             StatusBarNotification notification) {
1279         updateNotificationViews(entry, notification, false);
1280     }
1281
1282     private void updateHeadsUpViews(NotificationData.Entry entry,
1283             StatusBarNotification notification) {
1284         updateNotificationViews(entry, notification, true);
1285     }
1286
1287     private void updateNotificationViews(NotificationData.Entry entry,
1288             StatusBarNotification notification, boolean isHeadsUp) {
1289         final RemoteViews contentView = notification.getNotification().contentView;
1290         final RemoteViews bigContentView = isHeadsUp
1291                 ? notification.getNotification().headsUpContentView
1292                 : notification.getNotification().bigContentView;
1293         final Notification publicVersion = notification.getNotification().publicVersion;
1294         final RemoteViews publicContentView = publicVersion != null ? publicVersion.contentView
1295                 : null;
1296
1297         // Reapply the RemoteViews
1298         contentView.reapply(mContext, entry.expanded, mOnClickHandler);
1299         if (bigContentView != null && entry.getBigContentView() != null) {
1300             bigContentView.reapply(mContext, entry.getBigContentView(),
1301                     mOnClickHandler);
1302         }
1303         if (publicContentView != null && entry.getPublicContentView() != null) {
1304             publicContentView.reapply(mContext, entry.getPublicContentView(), mOnClickHandler);
1305         }
1306         // update the contentIntent
1307         final PendingIntent contentIntent = notification.getNotification().contentIntent;
1308         if (contentIntent != null) {
1309             final View.OnClickListener listener = makeClicker(contentIntent,
1310                     notification.getPackageName(), notification.getTag(), notification.getId(),
1311                     isHeadsUp, notification.getUserId());
1312             entry.content.setOnClickListener(listener);
1313         } else {
1314             entry.content.setOnClickListener(null);
1315         }
1316     }
1317
1318     protected void notifyHeadsUpScreenOn(boolean screenOn) {
1319         if (!screenOn && mInterruptingNotificationEntry != null) {
1320             mHandler.sendEmptyMessage(MSG_ESCALATE_HEADS_UP);
1321         }
1322     }
1323
1324     protected boolean shouldInterrupt(StatusBarNotification sbn) {
1325         Notification notification = sbn.getNotification();
1326         // some predicates to make the boolean logic legible
1327         boolean isNoisy = (notification.defaults & Notification.DEFAULT_SOUND) != 0
1328                 || (notification.defaults & Notification.DEFAULT_VIBRATE) != 0
1329                 || notification.sound != null
1330                 || notification.vibrate != null;
1331         boolean isHighPriority = sbn.getScore() >= INTERRUPTION_THRESHOLD;
1332         boolean isFullscreen = notification.fullScreenIntent != null;
1333         boolean hasTicker = mHeadsUpTicker && !TextUtils.isEmpty(notification.tickerText);
1334         boolean isAllowed = notification.extras.getInt(Notification.EXTRA_AS_HEADS_UP,
1335                 Notification.HEADS_UP_ALLOWED) != Notification.HEADS_UP_NEVER;
1336
1337         final KeyguardTouchDelegate keyguard = KeyguardTouchDelegate.getInstance(mContext);
1338         boolean interrupt = (isFullscreen || (isHighPriority && (isNoisy || hasTicker)))
1339                 && isAllowed
1340                 && mPowerManager.isScreenOn()
1341                 && !keyguard.isShowingAndNotOccluded()
1342                 && !keyguard.isInputRestricted();
1343         try {
1344             interrupt = interrupt && !mDreamManager.isDreaming();
1345         } catch (RemoteException e) {
1346             Log.d(TAG, "failed to query dream manager", e);
1347         }
1348         if (DEBUG) Log.d(TAG, "interrupt: " + interrupt);
1349         return interrupt;
1350     }
1351
1352     // Q: What kinds of notifications should show during setup?
1353     // A: Almost none! Only things coming from the system (package is "android") that also
1354     // have special "kind" tags marking them as relevant for setup (see below).
1355     protected boolean showNotificationEvenIfUnprovisioned(StatusBarNotification sbn) {
1356         return "android".equals(sbn.getPackageName())
1357                 && sbn.getNotification().extras.getBoolean(Notification.EXTRA_ALLOW_DURING_SETUP);
1358     }
1359
1360     public boolean inKeyguardRestrictedInputMode() {
1361         return KeyguardTouchDelegate.getInstance(mContext).isInputRestricted();
1362     }
1363
1364     public void setInteracting(int barWindow, boolean interacting) {
1365         // hook for subclasses
1366     }
1367
1368     public void destroy() {
1369         if (mSearchPanelView != null) {
1370             mWindowManager.removeViewImmediate(mSearchPanelView);
1371         }
1372         mContext.unregisterReceiver(mBroadcastReceiver);
1373     }
1374 }