TabletStatusBar.java revision 5113382e95d5f5f322bc6618bef7d1c7f96e295f
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 17package com.android.systemui.statusbar.tablet; 18 19import java.io.FileDescriptor; 20import java.io.PrintWriter; 21import java.util.ArrayList; 22import java.util.Map; 23import java.util.IdentityHashMap; 24 25import android.animation.LayoutTransition; 26import android.animation.ObjectAnimator; 27import android.animation.AnimatorSet; 28import android.app.ActivityManagerNative; 29import android.app.PendingIntent; 30import android.app.Notification; 31import android.app.StatusBarManager; 32import android.content.Context; 33import android.content.Intent; 34import android.content.res.Configuration; 35import android.content.res.Resources; 36import android.graphics.PixelFormat; 37import android.graphics.Rect; 38import android.graphics.drawable.Drawable; 39import android.graphics.drawable.LayerDrawable; 40import android.os.Handler; 41import android.os.IBinder; 42import android.os.Message; 43import android.os.RemoteException; 44import android.os.ServiceManager; 45import android.text.TextUtils; 46import android.util.Slog; 47import android.view.animation.Animation; 48import android.view.animation.AnimationUtils; 49import android.view.Gravity; 50import android.view.IWindowManager; 51import android.view.KeyEvent; 52import android.view.LayoutInflater; 53import android.view.MotionEvent; 54import android.view.VelocityTracker; 55import android.view.View; 56import android.view.ViewConfiguration; 57import android.view.ViewGroup; 58import android.view.WindowManager; 59import android.view.WindowManagerImpl; 60import android.widget.FrameLayout; 61import android.widget.ImageView; 62import android.widget.LinearLayout; 63import android.widget.RemoteViews; 64import android.widget.ScrollView; 65import android.widget.TextSwitcher; 66import android.widget.TextView; 67 68import com.android.internal.statusbar.StatusBarIcon; 69import com.android.internal.statusbar.StatusBarNotification; 70 71import com.android.systemui.R; 72import com.android.systemui.statusbar.*; 73import com.android.systemui.statusbar.policy.BatteryController; 74import com.android.systemui.statusbar.policy.NetworkController; 75import com.android.systemui.recent.RecentApplicationsActivity; 76 77public class TabletStatusBar extends StatusBar implements 78 HeightReceiver.OnBarHeightChangedListener { 79 public static final boolean DEBUG = false; 80 public static final String TAG = "TabletStatusBar"; 81 82 public static final int MAX_NOTIFICATION_ICONS = 5; 83 // IME switcher icon is big and occupy width of two icons 84 public static final int MAX_NOTIFICATION_ICONS_IME_BUTTON_VISIBLE = MAX_NOTIFICATION_ICONS - 1; 85 86 public static final int MSG_OPEN_NOTIFICATION_PANEL = 1000; 87 public static final int MSG_CLOSE_NOTIFICATION_PANEL = 1001; 88 public static final int MSG_OPEN_NOTIFICATION_PEEK = 1002; 89 public static final int MSG_CLOSE_NOTIFICATION_PEEK = 1003; 90 public static final int MSG_OPEN_RECENTS_PANEL = 1020; 91 public static final int MSG_CLOSE_RECENTS_PANEL = 1021; 92 public static final int MSG_SHOW_CHROME = 1030; 93 public static final int MSG_HIDE_CHROME = 1031; 94 public static final int MSG_OPEN_INPUT_METHODS_PANEL = 1040; 95 public static final int MSG_CLOSE_INPUT_METHODS_PANEL = 1041; 96 97 // Fitts' Law assistance for LatinIME; TODO: replace with a more general approach 98 private static final boolean FAKE_SPACE_BAR = true; 99 100 public static final int LIGHTS_ON_DELAY = 5000; 101 102 // The height of the bar, as definied by the build. It may be taller if we're plugged 103 // into hdmi. 104 int mNaturalBarHeight = -1; 105 int mIconSize = -1; 106 int mIconHPadding = -1; 107 108 H mHandler = new H(); 109 110 IWindowManager mWindowManager; 111 112 // tracking all current notifications 113 private NotificationData mNotns = new NotificationData(); 114 115 TabletStatusBarView mStatusBarView; 116 View mNotificationArea; 117 View mNotificationTrigger; 118 NotificationIconArea mNotificationIconArea; 119 View mNavigationArea; 120 121 ImageView mBackButton; 122 View mHomeButton; 123 View mMenuButton; 124 View mRecentButton; 125 126 ViewGroup mNotificationAndImeArea; 127 InputMethodButton mInputMethodSwitchButton; 128 129 NotificationPanel mNotificationPanel; 130 NotificationPeekPanel mNotificationPeekWindow; 131 ViewGroup mNotificationPeekRow; 132 int mNotificationPeekIndex; 133 IBinder mNotificationPeekKey; 134 LayoutTransition mNotificationPeekScrubLeft, mNotificationPeekScrubRight; 135 136 int mNotificationPeekTapDuration; 137 int mNotificationFlingVelocity; 138 139 ViewGroup mPile; 140 141 HeightReceiver mHeightReceiver; 142 BatteryController mBatteryController; 143 NetworkController mNetworkController; 144 145 View mBarContents; 146 147 // hide system chrome ("lights out") support 148 View mShadow; 149 150 NotificationIconArea.IconLayout mIconLayout; 151 152 TabletTicker mTicker; 153 154 View mFakeSpaceBar; 155 KeyEvent mSpaceBarKeyEvent = null; 156 157 // for disabling the status bar 158 int mDisabled = 0; 159 160 boolean mNotificationsOn = true; 161 private RecentAppsPanel mRecentsPanel; 162 private InputMethodsPanel mInputMethodsPanel; 163 164 public Context getContext() { return mContext; } 165 166 protected void addPanelWindows() { 167 final Context context = mContext; 168 169 // Notification Panel 170 mNotificationPanel = (NotificationPanel)View.inflate(context, 171 R.layout.status_bar_notification_panel, null); 172 mNotificationPanel.show(false, false); 173 mNotificationPanel.setOnTouchListener( 174 new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PANEL, mNotificationPanel)); 175 176 // the battery and network icons 177 mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery)); 178 mBatteryController.addLabelView( 179 (TextView)mNotificationPanel.findViewById(R.id.battery_text)); 180 mNetworkController.addCombinedSignalIconView( 181 (ImageView)mNotificationPanel.findViewById(R.id.network_signal)); 182 mNetworkController.addDataTypeIconView( 183 (ImageView)mNotificationPanel.findViewById(R.id.network_type)); 184 mNetworkController.addLabelView( 185 (TextView)mNotificationPanel.findViewById(R.id.network_text)); 186 mNetworkController.addLabelView( 187 (TextView)mBarContents.findViewById(R.id.network_text)); 188 189 mStatusBarView.setIgnoreChildren(0, mNotificationTrigger, mNotificationPanel); 190 191 WindowManager.LayoutParams lp = new WindowManager.LayoutParams( 192 512, // ViewGroup.LayoutParams.MATCH_PARENT, 193 ViewGroup.LayoutParams.MATCH_PARENT, 194 WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL, 195 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 196 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM 197 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH 198 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, 199 PixelFormat.TRANSLUCENT); 200 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; 201 lp.setTitle("NotificationPanel"); 202 lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED 203 | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING; 204 lp.windowAnimations = com.android.internal.R.style.Animation; // == no animation 205// lp.windowAnimations = com.android.internal.R.style.Animation_ZoomButtons; // simple fade 206 207 WindowManagerImpl.getDefault().addView(mNotificationPanel, lp); 208 209 // Notification preview window 210 mNotificationPeekWindow = (NotificationPeekPanel) View.inflate(context, 211 R.layout.status_bar_notification_peek, null); 212 mNotificationPeekRow = (ViewGroup) mNotificationPeekWindow.findViewById(R.id.content); 213 mNotificationPeekWindow.setVisibility(View.GONE); 214 mNotificationPeekWindow.setOnTouchListener( 215 new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PEEK, mNotificationPeekWindow)); 216 mNotificationPeekScrubRight = new LayoutTransition(); 217 mNotificationPeekScrubRight.setAnimator(LayoutTransition.APPEARING, 218 ObjectAnimator.ofInt(null, "left", -512, 0)); 219 mNotificationPeekScrubRight.setAnimator(LayoutTransition.DISAPPEARING, 220 ObjectAnimator.ofInt(null, "left", -512, 0)); 221 mNotificationPeekScrubRight.setDuration(500); 222 223 mNotificationPeekScrubLeft = new LayoutTransition(); 224 mNotificationPeekScrubLeft.setAnimator(LayoutTransition.APPEARING, 225 ObjectAnimator.ofInt(null, "left", 512, 0)); 226 mNotificationPeekScrubLeft.setAnimator(LayoutTransition.DISAPPEARING, 227 ObjectAnimator.ofInt(null, "left", 512, 0)); 228 mNotificationPeekScrubLeft.setDuration(500); 229 230 // XXX: setIgnoreChildren? 231 lp = new WindowManager.LayoutParams( 232 512, // ViewGroup.LayoutParams.WRAP_CONTENT, 233 ViewGroup.LayoutParams.WRAP_CONTENT, 234 WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL, 235 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 236 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM 237 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH, 238 PixelFormat.TRANSLUCENT); 239 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; 240 lp.setTitle("NotificationPeekWindow"); 241 lp.windowAnimations = com.android.internal.R.style.Animation_Toast; 242 243 WindowManagerImpl.getDefault().addView(mNotificationPeekWindow, lp); 244 245 // Recents Panel 246 mRecentsPanel = (RecentAppsPanel) View.inflate(context, 247 R.layout.status_bar_recent_panel, null); 248 mRecentsPanel.setVisibility(View.GONE); 249 mRecentsPanel.setOnTouchListener(new TouchOutsideListener(MSG_CLOSE_RECENTS_PANEL, 250 mRecentsPanel)); 251 mStatusBarView.setIgnoreChildren(2, mRecentButton, mRecentsPanel); 252 253 lp = new WindowManager.LayoutParams( 254 ViewGroup.LayoutParams.WRAP_CONTENT, 255 ViewGroup.LayoutParams.WRAP_CONTENT, 256 WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL, 257 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 258 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM 259 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH 260 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, 261 PixelFormat.TRANSLUCENT); 262 lp.gravity = Gravity.BOTTOM | Gravity.LEFT; 263 lp.setTitle("RecentsPanel"); 264 lp.windowAnimations = R.style.Animation_RecentPanel; 265 266 WindowManagerImpl.getDefault().addView(mRecentsPanel, lp); 267 mRecentsPanel.setBar(this); 268 269 // Input methods Panel 270 mInputMethodsPanel = (InputMethodsPanel) View.inflate(context, 271 R.layout.status_bar_input_methods_panel, null); 272 mInputMethodsPanel.setVisibility(View.GONE); 273 mInputMethodsPanel.setOnTouchListener(new TouchOutsideListener( 274 MSG_CLOSE_INPUT_METHODS_PANEL, mInputMethodsPanel)); 275 mInputMethodsPanel.setImeSwitchButton(mInputMethodSwitchButton); 276 mStatusBarView.setIgnoreChildren(3, mInputMethodSwitchButton, mInputMethodsPanel); 277 lp = new WindowManager.LayoutParams( 278 ViewGroup.LayoutParams.WRAP_CONTENT, 279 ViewGroup.LayoutParams.WRAP_CONTENT, 280 WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL, 281 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 282 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM 283 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH 284 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, 285 PixelFormat.TRANSLUCENT); 286 lp.gravity = Gravity.BOTTOM | Gravity.RIGHT; 287 lp.setTitle("InputMethodsPanel"); 288 lp.windowAnimations = R.style.Animation_RecentPanel; 289 290 WindowManagerImpl.getDefault().addView(mInputMethodsPanel, lp); 291 } 292 293 @Override 294 public void start() { 295 super.start(); // will add the main bar view 296 } 297 298 @Override 299 protected void onConfigurationChanged(Configuration newConfig) { 300 loadDimens(); 301 } 302 303 protected void loadDimens() { 304 final Resources res = mContext.getResources(); 305 306 mNaturalBarHeight = res.getDimensionPixelSize( 307 com.android.internal.R.dimen.status_bar_height); 308 309 int newIconSize = res.getDimensionPixelSize( 310 com.android.internal.R.dimen.status_bar_icon_size); 311 int newIconHPadding = res.getDimensionPixelSize( 312 R.dimen.status_bar_icon_padding); 313 314 if (newIconHPadding != mIconHPadding || newIconSize != mIconSize) { 315// Slog.d(TAG, "size=" + newIconSize + " padding=" + newIconHPadding); 316 mIconHPadding = newIconHPadding; 317 mIconSize = newIconSize; 318 reloadAllNotificationIcons(); // reload the tray 319 } 320 } 321 322 protected View makeStatusBarView() { 323 final Context context = mContext; 324 325 mWindowManager = IWindowManager.Stub.asInterface( 326 ServiceManager.getService(Context.WINDOW_SERVICE)); 327 328 // This guy will listen for HDMI plugged broadcasts so we can resize the 329 // status bar as appropriate. 330 mHeightReceiver = new HeightReceiver(mContext); 331 mHeightReceiver.registerReceiver(); 332 loadDimens(); 333 334 final TabletStatusBarView sb = (TabletStatusBarView)View.inflate( 335 context, R.layout.status_bar, null); 336 mStatusBarView = sb; 337 338 sb.setHandler(mHandler); 339 340 mBarContents = sb.findViewById(R.id.bar_contents); 341 342 // the whole right-hand side of the bar 343 mNotificationArea = sb.findViewById(R.id.notificationArea); 344 345 // the button to open the notification area 346 mNotificationTrigger = sb.findViewById(R.id.notificationTrigger); 347 mNotificationTrigger.setOnClickListener(mOnClickListener); 348 349 // the more notifications icon 350 mNotificationIconArea = (NotificationIconArea)sb.findViewById(R.id.notificationIcons); 351 352 // where the icons go 353 mIconLayout = (NotificationIconArea.IconLayout) sb.findViewById(R.id.icons); 354 mIconLayout.setOnTouchListener(new NotificationIconTouchListener()); 355 356 ViewConfiguration vc = ViewConfiguration.get(context); 357 mNotificationPeekTapDuration = vc.getTapTimeout(); 358 mNotificationFlingVelocity = 300; // px/s 359 360 mTicker = new TabletTicker(this); 361 362 // The icons 363 mBatteryController = new BatteryController(mContext); 364 mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery)); 365 mNetworkController = new NetworkController(mContext); 366 mNetworkController.addCombinedSignalIconView( 367 (ImageView)sb.findViewById(R.id.network_signal)); 368 mNetworkController.addDataTypeIconView( 369 (ImageView)sb.findViewById(R.id.network_type)); 370 371 // The navigation buttons 372 mNavigationArea = sb.findViewById(R.id.navigationArea); 373 mBackButton = (ImageView)mNavigationArea.findViewById(R.id.back); 374 mHomeButton = mNavigationArea.findViewById(R.id.home); 375 mMenuButton = mNavigationArea.findViewById(R.id.menu); 376 mRecentButton = mNavigationArea.findViewById(R.id.recent_apps); 377 mRecentButton.setOnClickListener(mOnClickListener); 378 379 // The bar contents buttons 380 mNotificationAndImeArea = (ViewGroup)sb.findViewById(R.id.notificationAndImeArea); 381 mInputMethodSwitchButton = (InputMethodButton) sb.findViewById(R.id.imeSwitchButton); 382 // Overwrite the lister 383 mInputMethodSwitchButton.setOnClickListener(mOnClickListener); 384 385 // for redirecting errant bar taps to the IME 386 mFakeSpaceBar = sb.findViewById(R.id.fake_space_bar); 387 388 // "shadows" of the status bar features, for lights-out mode 389 mShadow = sb.findViewById(R.id.bar_shadow); 390 mShadow.setOnTouchListener( 391 new View.OnTouchListener() { 392 public boolean onTouch(View v, MotionEvent ev) { 393 if (ev.getAction() == MotionEvent.ACTION_DOWN) { 394 mShadow.setVisibility(View.GONE); 395 mBarContents.setVisibility(View.VISIBLE); 396 } 397 return false; 398 } 399 }); 400 401 // tuning parameters 402 final int LIGHTS_GOING_OUT_SYSBAR_DURATION = 600; 403 final int LIGHTS_GOING_OUT_SHADOW_DURATION = 1000; 404 final int LIGHTS_GOING_OUT_SHADOW_DELAY = 500; 405 406 final int LIGHTS_COMING_UP_SYSBAR_DURATION = 200; 407// final int LIGHTS_COMING_UP_SYSBAR_DELAY = 50; 408 final int LIGHTS_COMING_UP_SHADOW_DURATION = 0; 409 410 LayoutTransition xition = new LayoutTransition(); 411 xition.setAnimator(LayoutTransition.APPEARING, 412 ObjectAnimator.ofFloat(null, "alpha", 0.5f, 1f)); 413 xition.setDuration(LayoutTransition.APPEARING, LIGHTS_COMING_UP_SYSBAR_DURATION); 414 xition.setStartDelay(LayoutTransition.APPEARING, 0); 415 xition.setAnimator(LayoutTransition.DISAPPEARING, 416 ObjectAnimator.ofFloat(null, "alpha", 1f, 0f)); 417 xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_GOING_OUT_SYSBAR_DURATION); 418 xition.setStartDelay(LayoutTransition.DISAPPEARING, 0); 419 ((ViewGroup)sb.findViewById(R.id.bar_contents_holder)).setLayoutTransition(xition); 420 421 xition = new LayoutTransition(); 422 xition.setAnimator(LayoutTransition.APPEARING, 423 ObjectAnimator.ofFloat(null, "alpha", 0f, 1f)); 424 xition.setDuration(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DURATION); 425 xition.setStartDelay(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DELAY); 426 xition.setAnimator(LayoutTransition.DISAPPEARING, 427 ObjectAnimator.ofFloat(null, "alpha", 1f, 0f)); 428 xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_COMING_UP_SHADOW_DURATION); 429 xition.setStartDelay(LayoutTransition.DISAPPEARING, 0); 430 ((ViewGroup)sb.findViewById(R.id.bar_shadow_holder)).setLayoutTransition(xition); 431 432 // set the initial view visibility 433 setAreThereNotifications(); 434 435 // Add the windows 436 addPanelWindows(); 437 438 mPile = (ViewGroup)mNotificationPanel.findViewById(R.id.content); 439 mPile.removeAllViews(); 440 441 ScrollView scroller = (ScrollView)mPile.getParent(); 442 scroller.setFillViewport(true); 443 444 mHeightReceiver.addOnBarHeightChangedListener(this); 445 446 return sb; 447 } 448 449 public int getStatusBarHeight() { 450 return mHeightReceiver.getHeight(); 451 } 452 453 protected int getStatusBarGravity() { 454 return Gravity.BOTTOM | Gravity.FILL_HORIZONTAL; 455 } 456 457 public void onBarHeightChanged(int height) { 458 final WindowManager.LayoutParams lp 459 = (WindowManager.LayoutParams)mStatusBarView.getLayoutParams(); 460 if (lp == null) { 461 // haven't been added yet 462 return; 463 } 464 if (lp.height != height) { 465 lp.height = height; 466 final WindowManager wm = WindowManagerImpl.getDefault(); 467 wm.updateViewLayout(mStatusBarView, lp); 468 } 469 } 470 471 private class H extends Handler { 472 public void handleMessage(Message m) { 473 switch (m.what) { 474 case MSG_OPEN_NOTIFICATION_PEEK: 475 if (DEBUG) Slog.d(TAG, "opening notification peek window; arg=" + m.arg1); 476 if (m.arg1 >= 0) { 477 final int N = mNotns.size(); 478 if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) { 479 NotificationData.Entry entry = mNotns.get(N-1-mNotificationPeekIndex); 480 entry.icon.setBackgroundColor(0); 481 mNotificationPeekIndex = -1; 482 mNotificationPeekKey = null; 483 } 484 485 final int peekIndex = m.arg1; 486 if (peekIndex < N) { 487 //Slog.d(TAG, "loading peek: " + peekIndex); 488 NotificationData.Entry entry = mNotns.get(N-1-peekIndex); 489 NotificationData.Entry copy = new NotificationData.Entry( 490 entry.key, 491 entry.notification, 492 entry.icon); 493 inflateViews(copy, mNotificationPeekRow); 494 495 entry.icon.setBackgroundColor(0x20FFFFFF); 496 497// mNotificationPeekRow.setLayoutTransition( 498// peekIndex < mNotificationPeekIndex 499// ? mNotificationPeekScrubLeft 500// : mNotificationPeekScrubRight); 501 502 mNotificationPeekRow.removeAllViews(); 503 mNotificationPeekRow.addView(copy.row); 504 505 mNotificationPeekWindow.setVisibility(View.VISIBLE); 506 mNotificationPanel.show(false, true); 507 508 mNotificationPeekIndex = peekIndex; 509 mNotificationPeekKey = entry.key; 510 } 511 } 512 break; 513 case MSG_CLOSE_NOTIFICATION_PEEK: 514 if (DEBUG) Slog.d(TAG, "closing notification peek window"); 515 mNotificationPeekWindow.setVisibility(View.GONE); 516 mNotificationPeekRow.removeAllViews(); 517 final int N = mNotns.size(); 518 if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) { 519 NotificationData.Entry entry = mNotns.get(N-1-mNotificationPeekIndex); 520 entry.icon.setBackgroundColor(0); 521 } 522 523 mNotificationPeekIndex = -1; 524 mNotificationPeekKey = null; 525 break; 526 case MSG_OPEN_NOTIFICATION_PANEL: 527 if (DEBUG) Slog.d(TAG, "opening notifications panel"); 528 if (!mNotificationPanel.isShowing()) { 529 mNotificationPeekWindow.setVisibility(View.GONE); 530 mNotificationPanel.show(true, true); 531 mNotificationArea.setVisibility(View.GONE); 532 mTicker.halt(); 533 } 534 break; 535 case MSG_CLOSE_NOTIFICATION_PANEL: 536 if (DEBUG) Slog.d(TAG, "closing notifications panel"); 537 if (mNotificationPanel.isShowing()) { 538 mNotificationPanel.show(false, true); 539 mNotificationArea.setVisibility(View.VISIBLE); 540 } 541 break; 542 case MSG_OPEN_RECENTS_PANEL: 543 if (DEBUG) Slog.d(TAG, "opening recents panel"); 544 if (mRecentsPanel != null) { 545 mRecentsPanel.setVisibility(View.VISIBLE); 546 mRecentsPanel.show(true, true); 547 } 548 break; 549 case MSG_CLOSE_RECENTS_PANEL: 550 if (DEBUG) Slog.d(TAG, "closing recents panel"); 551 if (mRecentsPanel != null && mRecentsPanel.isShowing()) { 552 mRecentsPanel.show(false, true); 553 } 554 break; 555 case MSG_OPEN_INPUT_METHODS_PANEL: 556 if (DEBUG) Slog.d(TAG, "opening input methods panel"); 557 if (mInputMethodsPanel != null) mInputMethodsPanel.setVisibility(View.VISIBLE); 558 break; 559 case MSG_CLOSE_INPUT_METHODS_PANEL: 560 if (DEBUG) Slog.d(TAG, "closing input methods panel"); 561 if (mInputMethodsPanel != null) mInputMethodsPanel.setVisibility(View.GONE); 562 break; 563 case MSG_SHOW_CHROME: 564 if (DEBUG) Slog.d(TAG, "hiding shadows (lights on)"); 565 mBarContents.setVisibility(View.VISIBLE); 566 mShadow.setVisibility(View.GONE); 567 notifyLightsChanged(true); 568 break; 569 case MSG_HIDE_CHROME: 570 if (DEBUG) Slog.d(TAG, "showing shadows (lights out)"); 571 animateCollapse(); 572 mBarContents.setVisibility(View.GONE); 573 mShadow.setVisibility(View.VISIBLE); 574 notifyLightsChanged(false); 575 break; 576 } 577 } 578 } 579 580 private void notifyLightsChanged(boolean shown) { 581 try { 582 Slog.d(TAG, "lights " + (shown?"on":"out")); 583 mWindowManager.statusBarVisibilityChanged( 584 shown ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN); 585 } catch (RemoteException ex) { 586 } 587 } 588 589 public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon) { 590 if (DEBUG) Slog.d(TAG, "addIcon(" + slot + ") -> " + icon); 591 } 592 593 public void updateIcon(String slot, int index, int viewIndex, 594 StatusBarIcon old, StatusBarIcon icon) { 595 if (DEBUG) Slog.d(TAG, "updateIcon(" + slot + ") -> " + icon); 596 } 597 598 public void removeIcon(String slot, int index, int viewIndex) { 599 if (DEBUG) Slog.d(TAG, "removeIcon(" + slot + ")"); 600 } 601 602 public void addNotification(IBinder key, StatusBarNotification notification) { 603 if (DEBUG) Slog.d(TAG, "addNotification(" + key + " -> " + notification + ")"); 604 addNotificationViews(key, notification); 605 606 final boolean immersive = isImmersive(); 607 if (false && immersive) { 608 // TODO: immersive mode popups for tablet 609 } else if (notification.notification.fullScreenIntent != null) { 610 // not immersive & a full-screen alert should be shown 611 Slog.w(TAG, "Notification has fullScreenIntent and activity is not immersive;" 612 + " sending fullScreenIntent"); 613 try { 614 notification.notification.fullScreenIntent.send(); 615 } catch (PendingIntent.CanceledException e) { 616 } 617 } else { 618 tick(key, notification, true); 619 } 620 621 setAreThereNotifications(); 622 } 623 624 public void updateNotification(IBinder key, StatusBarNotification notification) { 625 if (DEBUG) Slog.d(TAG, "updateNotification(" + key + " -> " + notification + ") // TODO"); 626 627 final NotificationData.Entry oldEntry = mNotns.findByKey(key); 628 if (oldEntry == null) { 629 Slog.w(TAG, "updateNotification for unknown key: " + key); 630 return; 631 } 632 633 final StatusBarNotification oldNotification = oldEntry.notification; 634 final RemoteViews oldContentView = oldNotification.notification.contentView; 635 636 final RemoteViews contentView = notification.notification.contentView; 637 638 if (DEBUG) { 639 Slog.d(TAG, "old notification: when=" + oldNotification.notification.when 640 + " ongoing=" + oldNotification.isOngoing() 641 + " expanded=" + oldEntry.expanded 642 + " contentView=" + oldContentView); 643 Slog.d(TAG, "new notification: when=" + notification.notification.when 644 + " ongoing=" + oldNotification.isOngoing() 645 + " contentView=" + contentView); 646 } 647 648 // Can we just reapply the RemoteViews in place? If when didn't change, the order 649 // didn't change. 650 boolean contentsUnchanged = oldEntry.expanded != null 651 && contentView != null && oldContentView != null 652 && contentView.getPackage() != null 653 && oldContentView.getPackage() != null 654 && oldContentView.getPackage().equals(contentView.getPackage()) 655 && oldContentView.getLayoutId() == contentView.getLayoutId(); 656 ViewGroup rowParent = (ViewGroup) oldEntry.row.getParent(); 657 boolean orderUnchanged = notification.notification.when==oldNotification.notification.when 658 && notification.isOngoing() == oldNotification.isOngoing(); 659 boolean isLastAnyway = rowParent.indexOfChild(oldEntry.row) == rowParent.getChildCount()-1; 660 if (contentsUnchanged && (orderUnchanged || isLastAnyway)) { 661 if (DEBUG) Slog.d(TAG, "reusing notification for key: " + key); 662 oldEntry.notification = notification; 663 try { 664 // Reapply the RemoteViews 665 contentView.reapply(mContext, oldEntry.content); 666 // update the contentIntent 667 final PendingIntent contentIntent = notification.notification.contentIntent; 668 if (contentIntent != null) { 669 oldEntry.content.setOnClickListener(new NotificationClicker(contentIntent, 670 notification.pkg, notification.tag, notification.id)); 671 } else { 672 oldEntry.content.setOnClickListener(null); 673 } 674 // Update the icon. 675 final StatusBarIcon ic = new StatusBarIcon(notification.pkg, 676 notification.notification.icon, notification.notification.iconLevel, 677 notification.notification.number); 678 if (!oldEntry.icon.set(ic)) { 679 handleNotificationError(key, notification, "Couldn't update icon: " + ic); 680 return; 681 } 682 // Update the large icon 683 if (notification.notification.largeIcon != null) { 684 oldEntry.largeIcon.setImageBitmap(notification.notification.largeIcon); 685 } else { 686 oldEntry.largeIcon.getLayoutParams().width = 0; 687 oldEntry.largeIcon.setVisibility(View.INVISIBLE); 688 } 689 690 if (key == mNotificationPeekKey) { 691 // must update the peek window 692 Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK); 693 peekMsg.arg1 = mNotificationPeekIndex; 694 mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK); 695 mHandler.sendMessage(peekMsg); 696 } 697 } 698 catch (RuntimeException e) { 699 // It failed to add cleanly. Log, and remove the view from the panel. 700 Slog.w(TAG, "Couldn't reapply views for package " + contentView.getPackage(), e); 701 removeNotificationViews(key); 702 addNotificationViews(key, notification); 703 } 704 } else { 705 if (DEBUG) Slog.d(TAG, "not reusing notification for key: " + key); 706 removeNotificationViews(key); 707 addNotificationViews(key, notification); 708 } 709 // fullScreenIntent doesn't happen on updates. You need to clear & repost a new 710 // notification. 711 final boolean immersive = isImmersive(); 712 if (false && immersive) { 713 // TODO: immersive mode 714 } else { 715 tick(key, notification, false); 716 } 717 718 setAreThereNotifications(); 719 } 720 721 public void removeNotification(IBinder key) { 722 if (DEBUG) Slog.d(TAG, "removeNotification(" + key + ") // TODO"); 723 removeNotificationViews(key); 724 mTicker.remove(key); 725 setAreThereNotifications(); 726 } 727 728 public void showClock(boolean show) { 729 View clock = mBarContents.findViewById(R.id.clock); 730 View network_text = mBarContents.findViewById(R.id.network_text); 731 if (clock != null) { 732 clock.setVisibility(show ? View.VISIBLE : View.GONE); 733 } 734 if (network_text != null) { 735 network_text.setVisibility((!show) ? View.VISIBLE : View.GONE); 736 } 737 } 738 739 public void disable(int state) { 740 int old = mDisabled; 741 int diff = state ^ old; 742 mDisabled = state; 743 744 // act accordingly 745 if ((diff & StatusBarManager.DISABLE_CLOCK) != 0) { 746 boolean show = (state & StatusBarManager.DISABLE_CLOCK) == 0; 747 Slog.i(TAG, "DISABLE_CLOCK: " + (show ? "no" : "yes")); 748 showClock(show); 749 } 750 if ((diff & StatusBarManager.DISABLE_SYSTEM_INFO) != 0) { 751 boolean show = (state & StatusBarManager.DISABLE_SYSTEM_INFO) == 0; 752 Slog.i(TAG, "DISABLE_SYSTEM_INFO: " + (show ? "no" : "yes")); 753 mNotificationTrigger.setVisibility(show ? View.VISIBLE : View.GONE); 754 } 755 if ((diff & StatusBarManager.DISABLE_EXPAND) != 0) { 756 if ((state & StatusBarManager.DISABLE_EXPAND) != 0) { 757 Slog.i(TAG, "DISABLE_EXPAND: yes"); 758 animateCollapse(); 759 } 760 } 761 if ((diff & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) { 762 if ((state & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) { 763 Slog.i(TAG, "DISABLE_NOTIFICATION_ICONS: yes"); 764 // synchronize with current shadow state 765 mNotificationIconArea.setVisibility(View.GONE); 766 mTicker.halt(); 767 } else { 768 Slog.i(TAG, "DISABLE_NOTIFICATION_ICONS: no"); 769 // synchronize with current shadow state 770 mNotificationIconArea.setVisibility(View.VISIBLE); 771 } 772 } else if ((diff & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) { 773 if ((state & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) { 774 mTicker.halt(); 775 } 776 } 777 if ((diff & StatusBarManager.DISABLE_NAVIGATION) != 0) { 778 if ((state & StatusBarManager.DISABLE_NAVIGATION) != 0) { 779 Slog.i(TAG, "DISABLE_NAVIGATION: yes"); 780 mNavigationArea.setVisibility(View.GONE); 781 mInputMethodSwitchButton.setScreenLocked(true); 782 } else { 783 Slog.i(TAG, "DISABLE_NAVIGATION: no"); 784 mNavigationArea.setVisibility(View.VISIBLE); 785 mInputMethodSwitchButton.setScreenLocked(false); 786 } 787 } 788 } 789 790 private boolean hasTicker(Notification n) { 791 return n.tickerView != null || !TextUtils.isEmpty(n.tickerText); 792 } 793 794 private void tick(IBinder key, StatusBarNotification n, boolean firstTime) { 795 // Don't show the ticker when the windowshade is open. 796 if (mNotificationPanel.isShowing()) { 797 return; 798 } 799 // If they asked for FLAG_ONLY_ALERT_ONCE, then only show this notification 800 // if it's a new notification. 801 if (!firstTime && (n.notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { 802 return; 803 } 804 // Show the ticker if one is requested. Also don't do this 805 // until status bar window is attached to the window manager, 806 // because... well, what's the point otherwise? And trying to 807 // run a ticker without being attached will crash! 808 if (hasTicker(n.notification) && mStatusBarView.getWindowToken() != null) { 809 if (0 == (mDisabled & (StatusBarManager.DISABLE_NOTIFICATION_ICONS 810 | StatusBarManager.DISABLE_NOTIFICATION_TICKER))) { 811 mTicker.add(key, n); 812 mNotificationAndImeArea.setVisibility(View.GONE); 813 } 814 } 815 } 816 817 // called by TabletTicker when it's done with all queued ticks 818 public void doneTicking() { 819 mNotificationAndImeArea.setVisibility(View.VISIBLE); 820 } 821 822 public void animateExpand() { 823 mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL); 824 mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL); 825 } 826 827 public void animateCollapse() { 828 mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PANEL); 829 mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PANEL); 830 mHandler.removeMessages(MSG_CLOSE_RECENTS_PANEL); 831 mHandler.sendEmptyMessage(MSG_CLOSE_RECENTS_PANEL); 832 mHandler.removeMessages(MSG_CLOSE_INPUT_METHODS_PANEL); 833 mHandler.sendEmptyMessage(MSG_CLOSE_INPUT_METHODS_PANEL); 834 } 835 836 // called by StatusBar 837 @Override 838 public void setLightsOn(boolean on) { 839 // Policy note: if the frontmost activity needs the menu key, we assume it is a legacy app 840 // that can't handle lights-out mode. 841 if (mMenuButton.getVisibility() == View.VISIBLE) { 842 on = true; 843 } 844 mHandler.removeMessages(MSG_HIDE_CHROME); 845 mHandler.removeMessages(MSG_SHOW_CHROME); 846 mHandler.sendEmptyMessage(on ? MSG_SHOW_CHROME : MSG_HIDE_CHROME); 847 } 848 849 public void setMenuKeyVisible(boolean visible) { 850 if (DEBUG) { 851 Slog.d(TAG, (visible?"showing":"hiding") + " the MENU button"); 852 } 853 mMenuButton.setVisibility(visible ? View.VISIBLE : View.GONE); 854 855 // See above re: lights-out policy for legacy apps. 856 if (visible) setLightsOn(true); 857 } 858 859 public void setIMEButtonVisible(IBinder token, boolean visible) { 860 if (DEBUG) { 861 Slog.d(TAG, (visible?"showing":"hiding") + " the IME button"); 862 } 863 mInputMethodSwitchButton.setIMEButtonVisible(token, visible); 864 updateNotificationIcons(); 865 mInputMethodsPanel.setImeToken(token); 866 mBackButton.setImageResource( 867 visible ? R.drawable.ic_sysbar_back_ime : R.drawable.ic_sysbar_back); 868 if (FAKE_SPACE_BAR) { 869 mFakeSpaceBar.setVisibility(visible ? View.VISIBLE : View.GONE); 870 } 871 } 872 873 private boolean isImmersive() { 874 try { 875 return ActivityManagerNative.getDefault().isTopActivityImmersive(); 876 //Slog.d(TAG, "Top activity is " + (immersive?"immersive":"not immersive")); 877 } catch (RemoteException ex) { 878 // the end is nigh 879 return false; 880 } 881 } 882 883 private void setAreThereNotifications() { 884 final boolean hasClearable = mNotns.hasClearableItems(); 885 } 886 887 /** 888 * Cancel this notification and tell the status bar service about the failure. Hold no locks. 889 */ 890 void handleNotificationError(IBinder key, StatusBarNotification n, String message) { 891 removeNotification(key); 892 try { 893 mBarService.onNotificationError(n.pkg, n.tag, n.id, n.uid, n.initialPid, message); 894 } catch (RemoteException ex) { 895 // The end is nigh. 896 } 897 } 898 899 private void sendKey(KeyEvent key) { 900 try { 901 if (DEBUG) Slog.d(TAG, "injecting key event: " + key); 902 mWindowManager.injectInputEventNoWait(key); 903 } catch (RemoteException ex) { 904 } 905 } 906 907 private View.OnClickListener mOnClickListener = new View.OnClickListener() { 908 public void onClick(View v) { 909 if (v == mNotificationTrigger) { 910 onClickNotificationTrigger(); 911 } else if (v == mRecentButton) { 912 onClickRecentButton(); 913 } else if (v == mInputMethodSwitchButton) { 914 onClickInputMethodSwitchButton(); 915 } 916 } 917 }; 918 919 public void onClickNotificationTrigger() { 920 if (DEBUG) Slog.d(TAG, "clicked notification icons; disabled=" + mDisabled); 921 if ((mDisabled & StatusBarManager.DISABLE_EXPAND) == 0) { 922 if (!mNotificationsOn) { 923 mNotificationsOn = true; 924 mIconLayout.setVisibility(View.VISIBLE); // TODO: animation 925 } else { 926 int msg = !mNotificationPanel.isShowing() 927 ? MSG_OPEN_NOTIFICATION_PANEL 928 : MSG_CLOSE_NOTIFICATION_PANEL; 929 mHandler.removeMessages(msg); 930 mHandler.sendEmptyMessage(msg); 931 } 932 } 933 } 934 935 public void onClickRecentButton() { 936 if (DEBUG) Slog.d(TAG, "clicked recent apps; disabled=" + mDisabled); 937 if (mRecentsPanel == null) { 938 Intent intent = new Intent(); 939 intent.setClass(mContext, RecentApplicationsActivity.class); 940 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 941 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 942 mContext.startActivity(intent); 943 } else { 944 if ((mDisabled & StatusBarManager.DISABLE_EXPAND) == 0) { 945 int msg = (mRecentsPanel.getVisibility() == View.GONE) 946 ? MSG_OPEN_RECENTS_PANEL 947 : MSG_CLOSE_RECENTS_PANEL; 948 mHandler.removeMessages(msg); 949 mHandler.sendEmptyMessage(msg); 950 } 951 } 952 } 953 954 public void onClickInputMethodSwitchButton() { 955 if (DEBUG) Slog.d(TAG, "clicked input methods panel; disabled=" + mDisabled); 956 int msg = (mInputMethodsPanel.getVisibility() == View.GONE) ? 957 MSG_OPEN_INPUT_METHODS_PANEL : MSG_CLOSE_INPUT_METHODS_PANEL; 958 mHandler.removeMessages(msg); 959 mHandler.sendEmptyMessage(msg); 960 } 961 962 public NotificationClicker makeClicker(PendingIntent intent, String pkg, String tag, int id) { 963 return new NotificationClicker(intent, pkg, tag, id); 964 } 965 966 private class NotificationClicker implements View.OnClickListener { 967 private PendingIntent mIntent; 968 private String mPkg; 969 private String mTag; 970 private int mId; 971 972 NotificationClicker(PendingIntent intent, String pkg, String tag, int id) { 973 mIntent = intent; 974 mPkg = pkg; 975 mTag = tag; 976 mId = id; 977 } 978 979 public void onClick(View v) { 980 try { 981 // The intent we are sending is for the application, which 982 // won't have permission to immediately start an activity after 983 // the user switches to home. We know it is safe to do at this 984 // point, so make sure new activity switches are now allowed. 985 ActivityManagerNative.getDefault().resumeAppSwitches(); 986 } catch (RemoteException e) { 987 } 988 989 if (mIntent != null) { 990 int[] pos = new int[2]; 991 v.getLocationOnScreen(pos); 992 Intent overlay = new Intent(); 993 overlay.setSourceBounds( 994 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight())); 995 try { 996 mIntent.send(mContext, 0, overlay); 997 } catch (PendingIntent.CanceledException e) { 998 // the stack trace isn't very helpful here. Just log the exception message. 999 Slog.w(TAG, "Sending contentIntent failed: " + e); 1000 } 1001 } 1002 1003 try { 1004 mBarService.onNotificationClick(mPkg, mTag, mId); 1005 } catch (RemoteException ex) { 1006 // system process is dead if we're here. 1007 } 1008 1009 // close the shade if it was open 1010 animateCollapse(); 1011 1012 // If this click was on the intruder alert, hide that instead 1013// mHandler.sendEmptyMessage(MSG_HIDE_INTRUDER); 1014 } 1015 } 1016 1017 StatusBarNotification removeNotificationViews(IBinder key) { 1018 NotificationData.Entry entry = mNotns.remove(key); 1019 if (entry == null) { 1020 Slog.w(TAG, "removeNotification for unknown key: " + key); 1021 return null; 1022 } 1023 // Remove the expanded view. 1024 ViewGroup rowParent = (ViewGroup)entry.row.getParent(); 1025 if (rowParent != null) rowParent.removeView(entry.row); 1026 1027 if (key == mNotificationPeekKey) { 1028 // must close the peek as well, since it's gone 1029 mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK); 1030 } 1031 // Remove the icon. 1032// ViewGroup iconParent = (ViewGroup)entry.icon.getParent(); 1033// if (iconParent != null) iconParent.removeView(entry.icon); 1034 updateNotificationIcons(); 1035 1036 return entry.notification; 1037 } 1038 1039 private class NotificationIconTouchListener implements View.OnTouchListener { 1040 VelocityTracker mVT; 1041 1042 public NotificationIconTouchListener() { 1043 } 1044 1045 public boolean onTouch(View v, MotionEvent event) { 1046 boolean peeking = mNotificationPeekWindow.getVisibility() != View.GONE; 1047 boolean panelShowing = mNotificationPanel.isShowing(); 1048 if (panelShowing) return false; 1049 1050 switch (event.getAction()) { 1051 case MotionEvent.ACTION_DOWN: 1052 mVT = VelocityTracker.obtain(); 1053 1054 // fall through 1055 case MotionEvent.ACTION_OUTSIDE: 1056 case MotionEvent.ACTION_MOVE: 1057 // peek and switch icons if necessary 1058 int numIcons = mIconLayout.getChildCount(); 1059 int peekIndex = (int)((float)event.getX() * numIcons / mIconLayout.getWidth()); 1060 if (peekIndex > numIcons - 1) peekIndex = numIcons - 1; 1061 else if (peekIndex < 0) peekIndex = 0; 1062 1063 if (!peeking || mNotificationPeekIndex != peekIndex) { 1064 if (DEBUG) Slog.d(TAG, "will peek at notification #" + peekIndex); 1065 Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK); 1066 peekMsg.arg1 = peekIndex; 1067 1068 mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK); 1069 1070 // no delay if we're scrubbing left-right 1071 mHandler.sendMessage(peekMsg); 1072 } 1073 1074 // check for fling 1075 if (mVT != null) { 1076 mVT.addMovement(event); 1077 mVT.computeCurrentVelocity(1000); 1078 // require a little more oomph once we're already in peekaboo mode 1079 if (!panelShowing && ( 1080 (peeking && mVT.getYVelocity() < -mNotificationFlingVelocity*3) 1081 || (mVT.getYVelocity() < -mNotificationFlingVelocity))) { 1082 mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK); 1083 mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL); 1084 mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK); 1085 mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL); 1086 } 1087 } 1088 return true; 1089 case MotionEvent.ACTION_UP: 1090 case MotionEvent.ACTION_CANCEL: 1091 mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK); 1092 if (peeking) { 1093 mHandler.sendEmptyMessageDelayed(MSG_CLOSE_NOTIFICATION_PEEK, 5000); 1094 } 1095 mVT.recycle(); 1096 mVT = null; 1097 return true; 1098 } 1099 return false; 1100 } 1101 } 1102 1103 StatusBarIconView addNotificationViews(IBinder key, StatusBarNotification notification) { 1104 if (DEBUG) { 1105 Slog.d(TAG, "addNotificationViews(key=" + key + ", notification=" + notification); 1106 } 1107 // Construct the icon. 1108 final StatusBarIconView iconView = new StatusBarIconView(mContext, 1109 notification.pkg + "/0x" + Integer.toHexString(notification.id)); 1110 iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 1111 1112 final StatusBarIcon ic = new StatusBarIcon(notification.pkg, 1113 notification.notification.icon, 1114 notification.notification.iconLevel, 1115 notification.notification.number); 1116 if (!iconView.set(ic)) { 1117 handleNotificationError(key, notification, "Couldn't attach StatusBarIcon: " + ic); 1118 return null; 1119 } 1120 // Construct the expanded view. 1121 NotificationData.Entry entry = new NotificationData.Entry(key, notification, iconView); 1122 if (!inflateViews(entry, mPile)) { 1123 handleNotificationError(key, notification, "Couldn't expand RemoteViews for: " 1124 + notification); 1125 return null; 1126 } 1127 1128 // Add the icon. 1129 mNotns.add(entry); 1130 updateNotificationIcons(); 1131 1132 return iconView; 1133 } 1134 1135 private void reloadAllNotificationIcons() { 1136 if (mIconLayout == null) return; 1137 mIconLayout.removeAllViews(); 1138 updateNotificationIcons(); 1139 } 1140 1141 private void updateNotificationIcons() { 1142 // XXX: need to implement a new limited linear layout class 1143 // to avoid removing & readding everything 1144 1145 if (mIconLayout == null) return; 1146 1147 final LinearLayout.LayoutParams params 1148 = new LinearLayout.LayoutParams(mIconSize + 2*mIconHPadding, mNaturalBarHeight); 1149 1150 int N = mNotns.size(); 1151 1152 if (DEBUG) { 1153 Slog.d(TAG, "refreshing icons: " + N + " notifications, mIconLayout=" + mIconLayout); 1154 } 1155 1156 ArrayList<View> toShow = new ArrayList<View>(); 1157 1158 // When IME button is visible, the number of notification icons should be decremented 1159 // to fit the upper limit. 1160 final int maxNotificationIconsCount = 1161 (mInputMethodSwitchButton.getVisibility() != View.GONE) ? 1162 MAX_NOTIFICATION_ICONS_IME_BUTTON_VISIBLE : MAX_NOTIFICATION_ICONS; 1163 for (int i=0; i< maxNotificationIconsCount; i++) { 1164 if (i>=N) break; 1165 toShow.add(mNotns.get(N-i-1).icon); 1166 } 1167 1168 ArrayList<View> toRemove = new ArrayList<View>(); 1169 for (int i=0; i<mIconLayout.getChildCount(); i++) { 1170 View child = mIconLayout.getChildAt(i); 1171 if (!toShow.contains(child)) { 1172 toRemove.add(child); 1173 } 1174 } 1175 1176 for (View remove : toRemove) { 1177 mIconLayout.removeView(remove); 1178 } 1179 1180 for (int i=0; i<toShow.size(); i++) { 1181 View v = toShow.get(i); 1182 v.setPadding(mIconHPadding, 0, mIconHPadding, 0); 1183 if (v.getParent() == null) { 1184 mIconLayout.addView(v, i, params); 1185 } 1186 } 1187 1188 loadNotificationPanel(); 1189 } 1190 1191 private void loadNotificationPanel() { 1192 int N = mNotns.size(); 1193 1194 ArrayList<View> toShow = new ArrayList<View>(); 1195 1196 for (int i=0; i<N; i++) { 1197 View row = mNotns.get(N-i-1).row; 1198 toShow.add(row); 1199 } 1200 1201 ArrayList<View> toRemove = new ArrayList<View>(); 1202 for (int i=0; i<mPile.getChildCount(); i++) { 1203 View child = mPile.getChildAt(i); 1204 if (!toShow.contains(child)) { 1205 toRemove.add(child); 1206 } 1207 } 1208 1209 for (View remove : toRemove) { 1210 mPile.removeView(remove); 1211 } 1212 1213 for (int i=0; i<toShow.size(); i++) { 1214 View v = toShow.get(i); 1215 if (v.getParent() == null) { 1216 mPile.addView(toShow.get(i)); 1217 } 1218 } 1219 1220 mNotificationPanel.setNotificationCount(N); 1221 } 1222 1223 void workAroundBadLayerDrawableOpacity(View v) { 1224 LayerDrawable d = (LayerDrawable)v.getBackground(); 1225 v.setBackgroundDrawable(null); 1226 d.setOpacity(PixelFormat.TRANSLUCENT); 1227 v.setBackgroundDrawable(d); 1228 } 1229 1230 private boolean inflateViews(NotificationData.Entry entry, ViewGroup parent) { 1231 StatusBarNotification sbn = entry.notification; 1232 RemoteViews remoteViews = sbn.notification.contentView; 1233 if (remoteViews == null) { 1234 return false; 1235 } 1236 1237 // create the row view 1238 LayoutInflater inflater = (LayoutInflater)mContext.getSystemService( 1239 Context.LAYOUT_INFLATER_SERVICE); 1240 View row = inflater.inflate(R.layout.status_bar_notification_row, parent, false); 1241 workAroundBadLayerDrawableOpacity(row); 1242 View vetoButton = row.findViewById(R.id.veto); 1243 if (entry.notification.isClearable()) { 1244 final String _pkg = sbn.pkg; 1245 final String _tag = sbn.tag; 1246 final int _id = sbn.id; 1247 vetoButton.setOnClickListener(new View.OnClickListener() { 1248 public void onClick(View v) { 1249 try { 1250 mBarService.onNotificationClear(_pkg, _tag, _id); 1251 } catch (RemoteException ex) { 1252 // system process is dead if we're here. 1253 } 1254 } 1255 }); 1256 } else { 1257 if ((sbn.notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) { 1258 vetoButton.setVisibility(View.INVISIBLE); 1259 } else { 1260 vetoButton.setVisibility(View.GONE); 1261 } 1262 } 1263 1264 // the large icon 1265 ImageView largeIcon = (ImageView)row.findViewById(R.id.large_icon); 1266 if (sbn.notification.largeIcon != null) { 1267 largeIcon.setImageBitmap(sbn.notification.largeIcon); 1268 } else { 1269 largeIcon.getLayoutParams().width = 0; 1270 largeIcon.setVisibility(View.INVISIBLE); 1271 } 1272 1273 // bind the click event to the content area 1274 ViewGroup content = (ViewGroup)row.findViewById(R.id.content); 1275 // XXX: update to allow controls within notification views 1276 content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); 1277// content.setOnFocusChangeListener(mFocusChangeListener); 1278 PendingIntent contentIntent = sbn.notification.contentIntent; 1279 if (contentIntent != null) { 1280 content.setOnClickListener(new NotificationClicker(contentIntent, 1281 sbn.pkg, sbn.tag, sbn.id)); 1282 } else { 1283 content.setOnClickListener(null); 1284 } 1285 1286 View expanded = null; 1287 Exception exception = null; 1288 try { 1289 expanded = remoteViews.apply(mContext, content); 1290 } 1291 catch (RuntimeException e) { 1292 exception = e; 1293 } 1294 if (expanded == null) { 1295 final String ident = sbn.pkg + "/0x" + Integer.toHexString(sbn.id); 1296 Slog.e(TAG, "couldn't inflate view for notification " + ident, exception); 1297 return false; 1298 } else { 1299 content.addView(expanded); 1300 row.setDrawingCacheEnabled(true); 1301 } 1302 1303 entry.row = row; 1304 entry.content = content; 1305 entry.expanded = expanded; 1306 entry.largeIcon = largeIcon; 1307 1308 return true; 1309 } 1310 1311/* 1312 public class ShadowController { 1313 boolean mShowShadows; 1314 Map<View, View> mShadowsForElements = new IdentityHashMap<View, View>(7); 1315 Map<View, View> mElementsForShadows = new IdentityHashMap<View, View>(7); 1316 LayoutTransition mElementTransition, mShadowTransition; 1317 1318 View mTouchTarget; 1319 1320 ShadowController(boolean showShadows) { 1321 mShowShadows = showShadows; 1322 mTouchTarget = null; 1323 1324 mElementTransition = new LayoutTransition(); 1325// AnimatorSet s = new AnimatorSet(); 1326// s.play(ObjectAnimator.ofInt(null, "top", 48, 0)) 1327// .with(ObjectAnimator.ofFloat(null, "scaleY", 0.5f, 1f)) 1328// .with(ObjectAnimator.ofFloat(null, "alpha", 0.5f, 1f)) 1329// ; 1330 mElementTransition.setAnimator(LayoutTransition.APPEARING, //s); 1331 ObjectAnimator.ofInt(null, "top", 48, 0)); 1332 mElementTransition.setDuration(LayoutTransition.APPEARING, 100); 1333 mElementTransition.setStartDelay(LayoutTransition.APPEARING, 0); 1334 1335// s = new AnimatorSet(); 1336// s.play(ObjectAnimator.ofInt(null, "top", 0, 48)) 1337// .with(ObjectAnimator.ofFloat(null, "scaleY", 1f, 0.5f)) 1338// .with(ObjectAnimator.ofFloat(null, "alpha", 1f, 0.5f)) 1339// ; 1340 mElementTransition.setAnimator(LayoutTransition.DISAPPEARING, //s); 1341 ObjectAnimator.ofInt(null, "top", 0, 48)); 1342 mElementTransition.setDuration(LayoutTransition.DISAPPEARING, 400); 1343 1344 mShadowTransition = new LayoutTransition(); 1345 mShadowTransition.setAnimator(LayoutTransition.APPEARING, 1346 ObjectAnimator.ofFloat(null, "alpha", 0f, 1f)); 1347 mShadowTransition.setDuration(LayoutTransition.APPEARING, 200); 1348 mShadowTransition.setStartDelay(LayoutTransition.APPEARING, 100); 1349 mShadowTransition.setAnimator(LayoutTransition.DISAPPEARING, 1350 ObjectAnimator.ofFloat(null, "alpha", 1f, 0f)); 1351 mShadowTransition.setDuration(LayoutTransition.DISAPPEARING, 100); 1352 1353 ViewGroup bar = (ViewGroup) TabletStatusBar.this.mBarContents; 1354 bar.setLayoutTransition(mElementTransition); 1355 ViewGroup nav = (ViewGroup) TabletStatusBar.this.mNavigationArea; 1356 nav.setLayoutTransition(mElementTransition); 1357 ViewGroup shadowGroup = (ViewGroup) bar.findViewById(R.id.shadows); 1358 shadowGroup.setLayoutTransition(mShadowTransition); 1359 } 1360 1361 public void add(View element, View shadow) { 1362 shadow.setOnTouchListener(makeTouchListener()); 1363 mShadowsForElements.put(element, shadow); 1364 mElementsForShadows.put(shadow, element); 1365 } 1366 1367 public boolean getShadowState() { 1368 return mShowShadows; 1369 } 1370 1371 public View.OnTouchListener makeTouchListener() { 1372 return new View.OnTouchListener() { 1373 public boolean onTouch(View v, MotionEvent ev) { 1374 final int action = ev.getAction(); 1375 1376 if (DEBUG) Slog.d(TAG, "ShadowController: v=" + v + ", ev=" + ev); 1377 1378 // currently redirecting events? 1379 if (mTouchTarget == null) { 1380 mTouchTarget = mElementsForShadows.get(v); 1381 } 1382 1383 if (mTouchTarget != null && mTouchTarget.getVisibility() != View.GONE) { 1384 boolean last = false; 1385 switch (action) { 1386 case MotionEvent.ACTION_CANCEL: 1387 case MotionEvent.ACTION_UP: 1388 mHandler.removeMessages(MSG_RESTORE_SHADOWS); 1389 if (mShowShadows) { 1390 mHandler.sendEmptyMessageDelayed(MSG_RESTORE_SHADOWS, 1391 v == mNotificationShadow ? 5000 : 500); 1392 } 1393 last = true; 1394 break; 1395 case MotionEvent.ACTION_DOWN: 1396 mHandler.removeMessages(MSG_RESTORE_SHADOWS); 1397 setElementShadow(mTouchTarget, false); 1398 break; 1399 } 1400 mTouchTarget.dispatchTouchEvent(ev); 1401 if (last) mTouchTarget = null; 1402 return true; 1403 } 1404 1405 return false; 1406 } 1407 }; 1408 } 1409 1410 public void refresh() { 1411 for (View element : mShadowsForElements.keySet()) { 1412 setElementShadow(element, mShowShadows); 1413 } 1414 } 1415 1416 public void showAllShadows() { 1417 mShowShadows = true; 1418 refresh(); 1419 } 1420 1421 public void hideAllShadows() { 1422 mShowShadows = false; 1423 refresh(); 1424 } 1425 1426 // Use View.INVISIBLE for things hidden due to shadowing, and View.GONE for things that are 1427 // disabled (and should not be shadowed or re-shown) 1428 public void setElementShadow(View button, boolean shade) { 1429 View shadow = mShadowsForElements.get(button); 1430 if (shadow != null) { 1431 if (button.getVisibility() != View.GONE) { 1432 shadow.setVisibility(shade ? View.VISIBLE : View.INVISIBLE); 1433 button.setVisibility(shade ? View.INVISIBLE : View.VISIBLE); 1434 } 1435 } 1436 } 1437 1438 // Hide both element and shadow, using default layout animations. 1439 public void hideElement(View button) { 1440 Slog.d(TAG, "hiding: " + button); 1441 View shadow = mShadowsForElements.get(button); 1442 if (shadow != null) { 1443 shadow.setVisibility(View.GONE); 1444 } 1445 button.setVisibility(View.GONE); 1446 } 1447 1448 // Honoring the current shadow state. 1449 public void showElement(View button) { 1450 Slog.d(TAG, "showing: " + button); 1451 View shadow = mShadowsForElements.get(button); 1452 if (shadow != null) { 1453 shadow.setVisibility(mShowShadows ? View.VISIBLE : View.INVISIBLE); 1454 } 1455 button.setVisibility(mShowShadows ? View.INVISIBLE : View.VISIBLE); 1456 } 1457 } 1458 */ 1459 1460 public class TouchOutsideListener implements View.OnTouchListener { 1461 private int mMsg; 1462 private StatusBarPanel mPanel; 1463 1464 public TouchOutsideListener(int msg, StatusBarPanel panel) { 1465 mMsg = msg; 1466 mPanel = panel; 1467 } 1468 1469 public boolean onTouch(View v, MotionEvent ev) { 1470 final int action = ev.getAction(); 1471 if (action == MotionEvent.ACTION_OUTSIDE 1472 || (action == MotionEvent.ACTION_DOWN 1473 && !mPanel.isInContentArea((int)ev.getX(), (int)ev.getY()))) { 1474 mHandler.removeMessages(mMsg); 1475 mHandler.sendEmptyMessage(mMsg); 1476 return true; 1477 } 1478 return false; 1479 } 1480 } 1481 1482 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1483 pw.print("mDisabled=0x"); 1484 pw.println(Integer.toHexString(mDisabled)); 1485 pw.println("mNetworkController:"); 1486 mNetworkController.dump(fd, pw, args); 1487 } 1488} 1489 1490 1491