StatusBarManagerService.java revision 5565cb42f2ac07fcdbe3aab2503de07fbeb39504
1/* 2 * Copyright (C) 2007 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.server.statusbar; 18 19import android.app.StatusBarManager; 20import android.os.Binder; 21import android.os.Handler; 22import android.os.IBinder; 23import android.os.RemoteException; 24import android.os.UserHandle; 25import android.content.Context; 26import android.content.pm.PackageManager; 27import android.content.res.Resources; 28import android.util.Slog; 29import android.view.WindowManager; 30 31import com.android.internal.statusbar.IStatusBar; 32import com.android.internal.statusbar.IStatusBarService; 33import com.android.internal.statusbar.StatusBarIcon; 34import com.android.internal.statusbar.StatusBarIconList; 35import com.android.server.LocalServices; 36import com.android.server.notification.NotificationDelegate; 37import com.android.server.wm.WindowManagerService; 38 39import java.io.FileDescriptor; 40import java.io.PrintWriter; 41import java.util.ArrayList; 42import java.util.List; 43 44 45/** 46 * A note on locking: We rely on the fact that calls onto mBar are oneway or 47 * if they are local, that they just enqueue messages to not deadlock. 48 */ 49public class StatusBarManagerService extends IStatusBarService.Stub { 50 private static final String TAG = "StatusBarManagerService"; 51 private static final boolean SPEW = false; 52 53 private final Context mContext; 54 private final WindowManagerService mWindowManager; 55 private Handler mHandler = new Handler(); 56 private NotificationDelegate mNotificationDelegate; 57 private volatile IStatusBar mBar; 58 private StatusBarIconList mIcons = new StatusBarIconList(); 59 60 // for disabling the status bar 61 private final ArrayList<DisableRecord> mDisableRecords = new ArrayList<DisableRecord>(); 62 private IBinder mSysUiVisToken = new Binder(); 63 private int mDisabled = 0; 64 65 private Object mLock = new Object(); 66 // encompasses lights-out mode and other flags defined on View 67 private int mSystemUiVisibility = 0; 68 private boolean mMenuVisible = false; 69 private int mImeWindowVis = 0; 70 private int mImeBackDisposition; 71 private boolean mShowImeSwitcher; 72 private IBinder mImeToken = null; 73 private int mCurrentUserId; 74 75 private class DisableRecord implements IBinder.DeathRecipient { 76 int userId; 77 String pkg; 78 int what; 79 IBinder token; 80 81 public void binderDied() { 82 Slog.i(TAG, "binder died for pkg=" + pkg); 83 disableInternal(userId, 0, token, pkg); 84 token.unlinkToDeath(this, 0); 85 } 86 } 87 88 /** 89 * Construct the service, add the status bar view to the window manager 90 */ 91 public StatusBarManagerService(Context context, WindowManagerService windowManager) { 92 mContext = context; 93 mWindowManager = windowManager; 94 95 final Resources res = context.getResources(); 96 mIcons.defineSlots(res.getStringArray(com.android.internal.R.array.config_statusBarIcons)); 97 98 LocalServices.addService(StatusBarManagerInternal.class, mInternalService); 99 } 100 101 /** 102 * Private API used by NotificationManagerService. 103 */ 104 private final StatusBarManagerInternal mInternalService = new StatusBarManagerInternal() { 105 private boolean mNotificationLightOn; 106 107 @Override 108 public void setNotificationDelegate(NotificationDelegate delegate) { 109 mNotificationDelegate = delegate; 110 } 111 112 @Override 113 public void buzzBeepBlinked() { 114 if (mBar != null) { 115 try { 116 mBar.buzzBeepBlinked(); 117 } catch (RemoteException ex) { 118 } 119 } 120 } 121 122 @Override 123 public void notificationLightPulse(int argb, int onMillis, int offMillis) { 124 mNotificationLightOn = true; 125 if (mBar != null) { 126 try { 127 mBar.notificationLightPulse(argb, onMillis, offMillis); 128 } catch (RemoteException ex) { 129 } 130 } 131 } 132 133 @Override 134 public void notificationLightOff() { 135 if (mNotificationLightOn) { 136 mNotificationLightOn = false; 137 if (mBar != null) { 138 try { 139 mBar.notificationLightOff(); 140 } catch (RemoteException ex) { 141 } 142 } 143 } 144 } 145 146 @Override 147 public void showScreenPinningRequest() { 148 if (mBar != null) { 149 try { 150 mBar.showScreenPinningRequest(); 151 } catch (RemoteException e) { 152 } 153 } 154 } 155 }; 156 157 // ================================================================================ 158 // From IStatusBarService 159 // ================================================================================ 160 @Override 161 public void expandNotificationsPanel() { 162 enforceExpandStatusBar(); 163 164 if (mBar != null) { 165 try { 166 mBar.animateExpandNotificationsPanel(); 167 } catch (RemoteException ex) { 168 } 169 } 170 } 171 172 @Override 173 public void collapsePanels() { 174 enforceExpandStatusBar(); 175 176 if (mBar != null) { 177 try { 178 mBar.animateCollapsePanels(); 179 } catch (RemoteException ex) { 180 } 181 } 182 } 183 184 @Override 185 public void expandSettingsPanel() { 186 enforceExpandStatusBar(); 187 188 if (mBar != null) { 189 try { 190 mBar.animateExpandSettingsPanel(); 191 } catch (RemoteException ex) { 192 } 193 } 194 } 195 196 @Override 197 public void disable(int what, IBinder token, String pkg) { 198 disableInternal(mCurrentUserId, what, token, pkg); 199 } 200 201 private void disableInternal(int userId, int what, IBinder token, String pkg) { 202 enforceStatusBar(); 203 204 synchronized (mLock) { 205 disableLocked(userId, what, token, pkg); 206 } 207 } 208 209 private void disableLocked(int userId, int what, IBinder token, String pkg) { 210 // It's important that the the callback and the call to mBar get done 211 // in the same order when multiple threads are calling this function 212 // so they are paired correctly. The messages on the handler will be 213 // handled in the order they were enqueued, but will be outside the lock. 214 manageDisableListLocked(userId, what, token, pkg); 215 216 // Ensure state for the current user is applied, even if passed a non-current user. 217 final int net = gatherDisableActionsLocked(mCurrentUserId); 218 if (net != mDisabled) { 219 mDisabled = net; 220 mHandler.post(new Runnable() { 221 public void run() { 222 mNotificationDelegate.onSetDisabled(net); 223 } 224 }); 225 if (mBar != null) { 226 try { 227 mBar.disable(net); 228 } catch (RemoteException ex) { 229 } 230 } 231 } 232 } 233 234 @Override 235 public void setIcon(String slot, String iconPackage, int iconId, int iconLevel, 236 String contentDescription) { 237 enforceStatusBar(); 238 239 synchronized (mIcons) { 240 int index = mIcons.getSlotIndex(slot); 241 if (index < 0) { 242 throw new SecurityException("invalid status bar icon slot: " + slot); 243 } 244 245 StatusBarIcon icon = new StatusBarIcon(iconPackage, UserHandle.OWNER, iconId, 246 iconLevel, 0, 247 contentDescription); 248 //Slog.d(TAG, "setIcon slot=" + slot + " index=" + index + " icon=" + icon); 249 mIcons.setIcon(index, icon); 250 251 if (mBar != null) { 252 try { 253 mBar.setIcon(index, icon); 254 } catch (RemoteException ex) { 255 } 256 } 257 } 258 } 259 260 @Override 261 public void setIconVisibility(String slot, boolean visible) { 262 enforceStatusBar(); 263 264 synchronized (mIcons) { 265 int index = mIcons.getSlotIndex(slot); 266 if (index < 0) { 267 throw new SecurityException("invalid status bar icon slot: " + slot); 268 } 269 270 StatusBarIcon icon = mIcons.getIcon(index); 271 if (icon == null) { 272 return; 273 } 274 275 if (icon.visible != visible) { 276 icon.visible = visible; 277 278 if (mBar != null) { 279 try { 280 mBar.setIcon(index, icon); 281 } catch (RemoteException ex) { 282 } 283 } 284 } 285 } 286 } 287 288 @Override 289 public void removeIcon(String slot) { 290 enforceStatusBar(); 291 292 synchronized (mIcons) { 293 int index = mIcons.getSlotIndex(slot); 294 if (index < 0) { 295 throw new SecurityException("invalid status bar icon slot: " + slot); 296 } 297 298 mIcons.removeIcon(index); 299 300 if (mBar != null) { 301 try { 302 mBar.removeIcon(index); 303 } catch (RemoteException ex) { 304 } 305 } 306 } 307 } 308 309 /** 310 * Hide or show the on-screen Menu key. Only call this from the window manager, typically in 311 * response to a window with {@link android.view.WindowManager.LayoutParams#needsMenuKey} set 312 * to {@link android.view.WindowManager.LayoutParams#NEEDS_MENU_SET_TRUE}. 313 */ 314 @Override 315 public void topAppWindowChanged(final boolean menuVisible) { 316 enforceStatusBar(); 317 318 if (SPEW) Slog.d(TAG, (menuVisible?"showing":"hiding") + " MENU key"); 319 320 synchronized(mLock) { 321 mMenuVisible = menuVisible; 322 mHandler.post(new Runnable() { 323 public void run() { 324 if (mBar != null) { 325 try { 326 mBar.topAppWindowChanged(menuVisible); 327 } catch (RemoteException ex) { 328 } 329 } 330 } 331 }); 332 } 333 } 334 335 @Override 336 public void setImeWindowStatus(final IBinder token, final int vis, final int backDisposition, 337 final boolean showImeSwitcher) { 338 enforceStatusBar(); 339 340 if (SPEW) { 341 Slog.d(TAG, "swetImeWindowStatus vis=" + vis + " backDisposition=" + backDisposition); 342 } 343 344 synchronized(mLock) { 345 // In case of IME change, we need to call up setImeWindowStatus() regardless of 346 // mImeWindowVis because mImeWindowVis may not have been set to false when the 347 // previous IME was destroyed. 348 mImeWindowVis = vis; 349 mImeBackDisposition = backDisposition; 350 mImeToken = token; 351 mShowImeSwitcher = showImeSwitcher; 352 mHandler.post(new Runnable() { 353 public void run() { 354 if (mBar != null) { 355 try { 356 mBar.setImeWindowStatus(token, vis, backDisposition, showImeSwitcher); 357 } catch (RemoteException ex) { 358 } 359 } 360 } 361 }); 362 } 363 } 364 365 @Override 366 public void setSystemUiVisibility(int vis, int mask, String cause) { 367 // also allows calls from window manager which is in this process. 368 enforceStatusBarService(); 369 370 if (SPEW) Slog.d(TAG, "setSystemUiVisibility(0x" + Integer.toHexString(vis) + ")"); 371 372 synchronized (mLock) { 373 updateUiVisibilityLocked(vis, mask); 374 disableLocked( 375 mCurrentUserId, 376 vis & StatusBarManager.DISABLE_MASK, 377 mSysUiVisToken, 378 cause); 379 } 380 } 381 382 private void updateUiVisibilityLocked(final int vis, final int mask) { 383 if (mSystemUiVisibility != vis) { 384 mSystemUiVisibility = vis; 385 mHandler.post(new Runnable() { 386 public void run() { 387 if (mBar != null) { 388 try { 389 mBar.setSystemUiVisibility(vis, mask); 390 } catch (RemoteException ex) { 391 } 392 } 393 } 394 }); 395 } 396 } 397 398 @Override 399 public void toggleRecentApps() { 400 if (mBar != null) { 401 try { 402 mBar.toggleRecentApps(); 403 } catch (RemoteException ex) {} 404 } 405 } 406 407 @Override 408 public void preloadRecentApps() { 409 if (mBar != null) { 410 try { 411 mBar.preloadRecentApps(); 412 } catch (RemoteException ex) {} 413 } 414 } 415 416 @Override 417 public void cancelPreloadRecentApps() { 418 if (mBar != null) { 419 try { 420 mBar.cancelPreloadRecentApps(); 421 } catch (RemoteException ex) {} 422 } 423 } 424 425 @Override 426 public void showRecentApps(boolean triggeredFromAltTab) { 427 if (mBar != null) { 428 try { 429 mBar.showRecentApps(triggeredFromAltTab); 430 } catch (RemoteException ex) {} 431 } 432 } 433 434 @Override 435 public void hideRecentApps(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) { 436 if (mBar != null) { 437 try { 438 mBar.hideRecentApps(triggeredFromAltTab, triggeredFromHomeKey); 439 } catch (RemoteException ex) {} 440 } 441 } 442 443 @Override 444 public void setCurrentUser(int newUserId) { 445 if (SPEW) Slog.d(TAG, "Setting current user to user " + newUserId); 446 mCurrentUserId = newUserId; 447 } 448 449 @Override 450 public void setWindowState(int window, int state) { 451 if (mBar != null) { 452 try { 453 mBar.setWindowState(window, state); 454 } catch (RemoteException ex) {} 455 } 456 } 457 458 private void enforceStatusBar() { 459 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR, 460 "StatusBarManagerService"); 461 } 462 463 private void enforceExpandStatusBar() { 464 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.EXPAND_STATUS_BAR, 465 "StatusBarManagerService"); 466 } 467 468 private void enforceStatusBarService() { 469 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, 470 "StatusBarManagerService"); 471 } 472 473 // ================================================================================ 474 // Callbacks from the status bar service. 475 // ================================================================================ 476 @Override 477 public void registerStatusBar(IStatusBar bar, StatusBarIconList iconList, 478 int switches[], List<IBinder> binders) { 479 enforceStatusBarService(); 480 481 Slog.i(TAG, "registerStatusBar bar=" + bar); 482 mBar = bar; 483 synchronized (mIcons) { 484 iconList.copyFrom(mIcons); 485 } 486 synchronized (mLock) { 487 switches[0] = gatherDisableActionsLocked(mCurrentUserId); 488 switches[1] = mSystemUiVisibility; 489 switches[2] = mMenuVisible ? 1 : 0; 490 switches[3] = mImeWindowVis; 491 switches[4] = mImeBackDisposition; 492 switches[5] = mShowImeSwitcher ? 1 : 0; 493 binders.add(mImeToken); 494 } 495 } 496 497 /** 498 * The status bar service should call this each time the user brings the panel from 499 * invisible to visible in order to clear the notification light. 500 */ 501 @Override 502 public void onPanelRevealed() { 503 enforceStatusBarService(); 504 long identity = Binder.clearCallingIdentity(); 505 try { 506 // tell the notification manager to turn off the lights. 507 mNotificationDelegate.onPanelRevealed(); 508 } finally { 509 Binder.restoreCallingIdentity(identity); 510 } 511 } 512 513 @Override 514 public void onPanelHidden() throws RemoteException { 515 enforceStatusBarService(); 516 long identity = Binder.clearCallingIdentity(); 517 try { 518 mNotificationDelegate.onPanelHidden(); 519 } finally { 520 Binder.restoreCallingIdentity(identity); 521 } 522 } 523 524 @Override 525 public void onNotificationClick(String key) { 526 enforceStatusBarService(); 527 final int callingUid = Binder.getCallingUid(); 528 final int callingPid = Binder.getCallingPid(); 529 long identity = Binder.clearCallingIdentity(); 530 try { 531 mNotificationDelegate.onNotificationClick(callingUid, callingPid, key); 532 } finally { 533 Binder.restoreCallingIdentity(identity); 534 } 535 } 536 537 @Override 538 public void onNotificationActionClick(String key, int actionIndex) { 539 enforceStatusBarService(); 540 final int callingUid = Binder.getCallingUid(); 541 final int callingPid = Binder.getCallingPid(); 542 long identity = Binder.clearCallingIdentity(); 543 try { 544 mNotificationDelegate.onNotificationActionClick(callingUid, callingPid, key, 545 actionIndex); 546 } finally { 547 Binder.restoreCallingIdentity(identity); 548 } 549 } 550 551 @Override 552 public void onNotificationError(String pkg, String tag, int id, 553 int uid, int initialPid, String message, int userId) { 554 enforceStatusBarService(); 555 final int callingUid = Binder.getCallingUid(); 556 final int callingPid = Binder.getCallingPid(); 557 long identity = Binder.clearCallingIdentity(); 558 try { 559 // WARNING: this will call back into us to do the remove. Don't hold any locks. 560 mNotificationDelegate.onNotificationError(callingUid, callingPid, 561 pkg, tag, id, uid, initialPid, message, userId); 562 } finally { 563 Binder.restoreCallingIdentity(identity); 564 } 565 } 566 567 @Override 568 public void onNotificationClear(String pkg, String tag, int id, int userId) { 569 enforceStatusBarService(); 570 final int callingUid = Binder.getCallingUid(); 571 final int callingPid = Binder.getCallingPid(); 572 long identity = Binder.clearCallingIdentity(); 573 try { 574 mNotificationDelegate.onNotificationClear(callingUid, callingPid, pkg, tag, id, userId); 575 } finally { 576 Binder.restoreCallingIdentity(identity); 577 } 578 } 579 580 @Override 581 public void onNotificationVisibilityChanged( 582 String[] newlyVisibleKeys, String[] noLongerVisibleKeys) throws RemoteException { 583 enforceStatusBarService(); 584 long identity = Binder.clearCallingIdentity(); 585 try { 586 mNotificationDelegate.onNotificationVisibilityChanged( 587 newlyVisibleKeys, noLongerVisibleKeys); 588 } finally { 589 Binder.restoreCallingIdentity(identity); 590 } 591 } 592 593 @Override 594 public void onNotificationExpansionChanged(String key, boolean userAction, 595 boolean expanded) throws RemoteException { 596 enforceStatusBarService(); 597 long identity = Binder.clearCallingIdentity(); 598 try { 599 mNotificationDelegate.onNotificationExpansionChanged( 600 key, userAction, expanded); 601 } finally { 602 Binder.restoreCallingIdentity(identity); 603 } 604 } 605 606 @Override 607 public void onClearAllNotifications(int userId) { 608 enforceStatusBarService(); 609 final int callingUid = Binder.getCallingUid(); 610 final int callingPid = Binder.getCallingPid(); 611 long identity = Binder.clearCallingIdentity(); 612 try { 613 mNotificationDelegate.onClearAll(callingUid, callingPid, userId); 614 } finally { 615 Binder.restoreCallingIdentity(identity); 616 } 617 } 618 619 620 // ================================================================================ 621 // Can be called from any thread 622 // ================================================================================ 623 624 // lock on mDisableRecords 625 void manageDisableListLocked(int userId, int what, IBinder token, String pkg) { 626 if (SPEW) { 627 Slog.d(TAG, "manageDisableList userId=" + userId 628 + " what=0x" + Integer.toHexString(what) + " pkg=" + pkg); 629 } 630 // update the list 631 final int N = mDisableRecords.size(); 632 DisableRecord tok = null; 633 int i; 634 for (i=0; i<N; i++) { 635 DisableRecord t = mDisableRecords.get(i); 636 if (t.token == token && t.userId == userId) { 637 tok = t; 638 break; 639 } 640 } 641 if (what == 0 || !token.isBinderAlive()) { 642 if (tok != null) { 643 mDisableRecords.remove(i); 644 tok.token.unlinkToDeath(tok, 0); 645 } 646 } else { 647 if (tok == null) { 648 tok = new DisableRecord(); 649 tok.userId = userId; 650 try { 651 token.linkToDeath(tok, 0); 652 } 653 catch (RemoteException ex) { 654 return; // give up 655 } 656 mDisableRecords.add(tok); 657 } 658 tok.what = what; 659 tok.token = token; 660 tok.pkg = pkg; 661 } 662 } 663 664 // lock on mDisableRecords 665 int gatherDisableActionsLocked(int userId) { 666 final int N = mDisableRecords.size(); 667 // gather the new net flags 668 int net = 0; 669 for (int i=0; i<N; i++) { 670 final DisableRecord rec = mDisableRecords.get(i); 671 if (rec.userId == userId) { 672 net |= rec.what; 673 } 674 } 675 return net; 676 } 677 678 // ================================================================================ 679 // Always called from UI thread 680 // ================================================================================ 681 682 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 683 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 684 != PackageManager.PERMISSION_GRANTED) { 685 pw.println("Permission Denial: can't dump StatusBar from from pid=" 686 + Binder.getCallingPid() 687 + ", uid=" + Binder.getCallingUid()); 688 return; 689 } 690 691 synchronized (mIcons) { 692 mIcons.dump(pw); 693 } 694 695 synchronized (mLock) { 696 pw.println(" mDisabled=0x" + Integer.toHexString(mDisabled)); 697 final int N = mDisableRecords.size(); 698 pw.println(" mDisableRecords.size=" + N); 699 for (int i=0; i<N; i++) { 700 DisableRecord tok = mDisableRecords.get(i); 701 pw.println(" [" + i + "] userId=" + tok.userId 702 + " what=0x" + Integer.toHexString(tok.what) 703 + " pkg=" + tok.pkg 704 + " token=" + tok.token); 705 } 706 } 707 } 708} 709