Android 4.0 ICS SystemUI浅析——StatusBar加载流程之Notification

来源:互联网 发布:数据库故障应急预案 编辑:程序博客网 时间:2024/05/01 18:51

        前面三篇文章《Android 4.0 ICS SystemUI浅析——SystemUI启动流程》、《Android 4.0 ICS SystemUI浅析——StatusBar结构分析》、《Android 4.0 ICS SystemUI浅析——StatusBar加载流程分析》逐步分析了SystemUI中StatusBar的启动以及加载流程,本文主要分析StatusBar上的Notification的加载,如有不正之处还恳请各位帮忙指正。

        本文来自:http://blog.csdn.net/yihongyuelan 欢迎转载 请务必注明出处!

        在上一篇文章《Android 4.0 ICS SystemUI浅析——StatusBar加载流程分析》中,我们主要分析了StatusBar上的系统Icons加载的过程,包括了耳机图标、蓝牙图标、禁音图标等等,此文是紧接着上文分析的,因此我们首先看到/SourceCode/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/StatusBar.java的start():

[java] view plaincopy
  1.      public void start() {  
  2.     // First set up our views and stuff.首先准备我们需要显示的view以及原材料  
  3.     //我们先跟踪这里的makeStatusBarView  
  4.     View sb = makeStatusBarView();  
  5.   
  6.     // Connect in to the status bar manager service  
  7.     //初始化各个存储器,用于存储各类信息,这些信息通过StatusBarManagerService获取  
  8.     //iconsList用于存放icons  
  9.     StatusBarIconList iconList = new StatusBarIconList();  
  10.     //nodificationKeys保存以Binder为Key的notification  
  11.     ArrayList<IBinder> notificationKeys = new ArrayList<IBinder>();  
  12.     //保存StatusBarNotification类型的notifications  
  13.     ArrayList<StatusBarNotification> notifications = new ArrayList<StatusBarNotification>();  
  14.     //mCommandQueue是和IStatusBarService进行交互的IBinder  
  15.     mCommandQueue = new CommandQueue(this, iconList);  
  16.     //这里实际上获取的是StatusBarManagerService  
  17.     mBarService = IStatusBarService.Stub.asInterface(  
  18.             ServiceManager.getService(Context.STATUS_BAR_SERVICE));  
  19.     int[] switches = new int[7];  
  20.     ArrayList<IBinder> binders = new ArrayList<IBinder>();  
  21.     try {  
  22.         //通过StatusBarManagerService中的registerStatusBar来获取初始设置  
  23.         mBarService.registerStatusBar(mCommandQueue, iconList, notificationKeys, notifications,  
  24.                 switches, binders);  
  25.     } catch (RemoteException ex) {  
  26.         // If the system process isn't there we're doomed anyway.  
  27.     }  
  28.     ... ...  
  29.   
  30.     // Set up the initial notification state  
  31.     //加载notifications,本文的分析主要从这里开始!  
  32.     N = notificationKeys.size();  
  33.     if (N == notifications.size()) {  
  34.         for (int i=0; i<N; i++) {  
  35.             addNotification(notificationKeys.get(i), notifications.get(i));  
  36.         }  
  37.     } else {  
  38.         Log.wtf(TAG, "Notification list length mismatch: keys=" + N  
  39.                 + " notifications=" + notifications.size());  
  40.     }  
  41.   
  42.     ... ...  
  43.     lp.gravity = getStatusBarGravity();  
  44.     lp.setTitle("StatusBar");  
  45.     lp.packageName = mContext.getPackageName();  
  46.     lp.windowAnimations = R.style.Animation_StatusBar;  
  47.     //在Window上显示StatusBar界面  
  48.     WindowManagerImpl.getDefault().addView(sb, lp);  
  49.     mDoNotDisturb = new DoNotDisturb(mContext);  
  50. }  

    我们可以看到addNotification()方法主要完成Notification图标的加载。跟进去看看(因为我们分析的是Phone因此选择PhoneStatusBar),代码如下:

[java] view plaincopy
  1.    
  2. public void addNotification(IBinder key, StatusBarNotification notification) {  
  3.      //该方法主要构造Notification Icons以及Expaned View  
  4.      StatusBarIconView iconView = addNotificationViews(key, notification);  
  5.      if (iconView == nullreturn;  
  6.   
  7.      boolean immersive = false;  
  8.      try {  
  9.          //判断当前栈顶Activity是否具有android:immersive属性。该属性在Android 4.0中新加入的属性,如果该属性为true则该Activity不能被其他Activity或者Notification所打断。  
  10.          immersive = ActivityManagerNative.getDefault().isTopActivityImmersive();  
  11.          if (DEBUG) {  
  12.              Slog.d(TAG, "Top activity is " + (immersive?"immersive":"not immersive"));  
  13.          }  
  14.      } catch (RemoteException ex) {  
  15.      }  
  16.      //因为这里我们返回的是false,所以不会执行  
  17.      if (immersive) {  
  18.          if ((notification.notification.flags & Notification.FLAG_HIGH_PRIORITY) != 0) {  
  19.              Slog.d(TAG, "Presenting high-priority notification in immersive activity");  
  20.              // special new transient ticker mode  
  21.              // 1. Populate mIntruderAlertView  
  22.   
  23.              ImageView alertIcon = (ImageView) mIntruderAlertView.findViewById(R.id.alertIcon);  
  24.              TextView alertText = (TextView) mIntruderAlertView.findViewById(R.id.alertText);  
  25.              alertIcon.setImageDrawable(StatusBarIconView.getIcon(  
  26.                  alertIcon.getContext(),  
  27.                  iconView.getStatusBarIcon()));  
  28.              alertText.setText(notification.notification.tickerText);  
  29.   
  30.              View button = mIntruderAlertView.findViewById(R.id.intruder_alert_content);  
  31.              button.setOnClickListener(  
  32.                  new NotificationClicker(notification.notification.contentIntent,  
  33.                      notification.pkg, notification.tag, notification.id));  
  34.   
  35.              // 2. Animate mIntruderAlertView in  
  36.              mHandler.sendEmptyMessage(MSG_SHOW_INTRUDER);  
  37.   
  38.              // 3. Set alarm to age the notification off (TODO)  
  39.              mHandler.removeMessages(MSG_HIDE_INTRUDER);  
  40.              mHandler.sendEmptyMessageDelayed(MSG_HIDE_INTRUDER, INTRUDER_ALERT_DECAY_MS);  
  41.          }  
  42.      //这里的fullScreenIntent=null因此也不执行  
  43.      } else if (notification.notification.fullScreenIntent != null) {  
  44.          // not immersive & a full-screen alert should be shown  
  45.          Slog.d(TAG, "Notification has fullScreenIntent; sending fullScreenIntent");  
  46.          try {  
  47.              notification.notification.fullScreenIntent.send();  
  48.          } catch (PendingIntent.CanceledException e) {  
  49.          }  
  50.      } else {  
  51.          // usual case: status bar visible & not immersive  
  52.   
  53.          // show the ticker  
  54.          //因此StatusBar可见同时不具有immersive属性,因此显示tiker  
  55.          tick(notification);  
  56.      }  
  57.   
  58.      // Recalculate the position of the sliding windows and the titles.  
  59.      // 重新计算滑动窗口的位置和标题  
  60.      setAreThereNotifications();  
  61.      // 更新ExpanedView  
  62.      updateExpandedViewPos(EXPANDED_LEAVE_ALONE);  
  63.  }  

通过对以上代码的分析,我们可以大致知道,Notification的加载主要分为三步:

1.addNotificationViews(key, notification);

2.tick(notification);

3.setAreThereNotifications()和updateExpandedViewPos(EXPANDED_LEAVE_ALONE);

那么接下来我们就通过这三个方法来分析Notification的加载。

 (1). addNotificationViews(key, notification);

 跟踪查看代码如下:

[java] view plaincopy
  1.    
  2. StatusBarIconView addNotificationViews(IBinder key, StatusBarNotification notification) {  
  3.      if (DEBUG) {  
  4.          Slog.d(TAG, "addNotificationViews(key=" + key + ", notification=" + notification);  
  5.      }  
  6.      // Construct the icon.  
  7.      // 初始化iconView  
  8.      final StatusBarIconView iconView = new StatusBarIconView(mContext,  
  9.              notification.pkg + "/0x" + Integer.toHexString(notification.id),  
  10.              notification.notification);  
  11.      //设置icons按照什么方式显示  
  12.      iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);  
  13.      // 对全局变量赋值  
  14.      final StatusBarIcon ic = new StatusBarIcon(notification.pkg,  
  15.                  notification.notification.icon,  
  16.                  notification.notification.iconLevel,  
  17.                  notification.notification.number,  
  18.                  notification.notification.tickerText);  
  19.      // 设置显示icons 和上一篇文章提到的系统icons图标设置是一样的 如果返回true则表示设置成功  
  20.      if (!iconView.set(ic)) {  
  21.          handleNotificationError(key, notification, "Couldn't create icon: " + ic);  
  22.          return null;  
  23.      }  
  24.      // Construct the expanded view.  
  25.      // 将Notification在ExpandedView上显示出来  
  26.      NotificationData.Entry entry = new NotificationData.Entry(key, notification, iconView);  
  27.      if (!inflateViews(entry, mPile)) {  
  28.          handleNotificationError(key, notification, "Couldn't expand RemoteViews for: "  
  29.                  + notification);  
  30.          return null;  
  31.      }  
  32.   
  33.      // Add the expanded view and icon.  
  34.      //mNotificationData中保存着当前显示的Notification的数量及其属性  
  35.      int pos = mNotificationData.add(entry);  
  36.      if (DEBUG) {  
  37.          Slog.d(TAG, "addNotificationViews: added at " + pos);  
  38.      }  
  39.      //更新图标  
  40.      updateNotificationIcons();  
  41.   
  42.      return iconView;  
  43.  }  

根据以上代码,我们可以知道在addNotificationViews()中,又可以细分为三步:设置icons,设置ExpanedView,更新图标。其中,设置icons实际上和上一篇文章中设置系统Icons类似。主要区别在设置ExpandedView和更新图标。那跟踪inflateViews()方法可以看到:

[java] view plaincopy
  1.    private boolean inflateViews(NotificationData.Entry entry, ViewGroup parent) {  
  2.         StatusBarNotification sbn = entry.notification;  
  3.         //初始化remoteViews(如果有过自定义Notification经验的朋友肯定对这个很熟悉,不了解的朋友可以自己去试试)  
  4.         RemoteViews remoteViews = sbn.notification.contentView;  
  5.         if (remoteViews == null) {  
  6.             return false;  
  7.         }  
  8.   
  9.         // create the row view  
  10.         LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(  
  11.                 Context.LAYOUT_INFLATER_SERVICE);  
  12.         // 加载布局文件,默认的通知信息在ExpandedView中是以一行来显示的,左侧是图标,右侧是通知标题和内容  
  13.         View row = inflater.inflate(R.layout.status_bar_notification_row, parent, false);  
  14.         //这个所谓的button实际上是在清除单个通知信息时调用的  
  15.         View vetoButton = updateNotificationVetoButton(row, sbn);  
  16.         //设置vetoButton的备注说明,作为一种辅助功能提供,为一些没有文字描述的View提供说明。这在界面上不会有效果,可临时放一点字符串数据  
  17.         vetoButton.setContentDescription(mContext.getString(  
  18.                 R.string.accessibility_remove_notification));  
  19.   
  20.         // the large icon  
  21.         //如果有largeIcon则进行设置。这里提到的largeIcon我也不知道具体用处是什么  
  22.         ImageView largeIcon = (ImageView)row.findViewById(R.id.large_icon);  
  23.         if (sbn.notification.largeIcon != null) {  
  24.             largeIcon.setImageBitmap(sbn.notification.largeIcon);  
  25.             largeIcon.setContentDescription(sbn.notification.tickerText);  
  26.         } else {  
  27.             largeIcon.getLayoutParams().width = 0;  
  28.             largeIcon.setVisibility(View.INVISIBLE);  
  29.         }  
  30.         largeIcon.setContentDescription(sbn.notification.tickerText);  
  31.   
  32.         // bind the click event to the content area  
  33.         ViewGroup content = (ViewGroup)row.findViewById(R.id.content);  
  34.         // XXX: update to allow controls within notification views  
  35.         content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);  
  36. //        content.setOnFocusChangeListener(mFocusChangeListener);  
  37.         PendingIntent contentIntent = sbn.notification.contentIntent;  
  38.         if (contentIntent != null) {  
  39.             //绑定largIcons和content区域的点击事件  
  40.             final View.OnClickListener listener = new NotificationClicker(contentIntent,  
  41.                     sbn.pkg, sbn.tag, sbn.id);  
  42.             largeIcon.setOnClickListener(listener);  
  43.             content.setOnClickListener(listener);  
  44.         } else {  
  45.             largeIcon.setOnClickListener(null);  
  46.             content.setOnClickListener(null);  
  47.         }  
  48.   
  49.         View expanded = null;  
  50.         Exception exception = null;  
  51.         try {  
  52.             //  Inflates视图对象并且应用到所有的动作中  
  53.             expanded = remoteViews.apply(mContext, content);  
  54.         }  
  55.         catch (RuntimeException e) {  
  56.             exception = e;  
  57.         }  
  58.         if (expanded == null) {  
  59.             final String ident = sbn.pkg + "/0x" + Integer.toHexString(sbn.id);  
  60.             Slog.e(TAG, "couldn't inflate view for notification " + ident, exception);  
  61.             return false;  
  62.         } else {  
  63.             //content 添加显示view  
  64.             content.addView(expanded);  
  65.             // 获取view中的图像前需要设为true  
  66.             row.setDrawingCacheEnabled(true);  
  67.         }  
  68.         // 设置这些通知信息原始背景  
  69.         applyLegacyRowBackground(sbn, content);  
  70.         // 将设置好的属性回传给entry  
  71.         entry.row = row;  
  72.         entry.content = content;  
  73.         entry.expanded = expanded;  
  74.         entry.largeIcon = largeIcon;  
  75.   
  76.         return true;  
  77.     }  

对于ExpanedView中的Notification设置,可能这里有点模糊,那请看以下图1和图2:

图 1

图2

通过图1和图2我们可以看到largeIcon以及vetoButton触发时间。对于ExpandedView,后面会有较为详细的分析。

       上面分析了ExpandedView中的Notification的设置,在addNotificationViews(key, notification);中就还剩下最后一个步骤了,即更新图标,那么查看addNotificationView()中的updateNotificationIcons()方法,代码如下:

[java] view plaincopy
  1. private void updateNotificationIcons() {  
  2.     // 该方法主要用于将通知信息在ExpandedView中显示 如果注释掉则通知将不会在ExpandedView中显示  
  3.     loadNotificationShade();  
  4.     // 下面的操作主要完成在StatusBar添加通知提示图标  
  5.     final LinearLayout.LayoutParams params  
  6.         = new LinearLayout.LayoutParams(mIconSize + 2*mIconHPadding, mNaturalBarHeight);  
  7.   
  8.     int N = mNotificationData.size();  
  9.   
  10.     if (DEBUG) {  
  11.         Slog.d(TAG, "refreshing icons: " + N + " notifications, mNotificationIcons=" + mNotificationIcons);  
  12.     }  
  13.   
  14.     ArrayList<View> toShow = new ArrayList<View>();  
  15.   
  16.     for (int i=0; i<N; i++) {  
  17.         toShow.add(mNotificationData.get(N-i-1).icon);  
  18.     }  
  19.   
  20.     ArrayList<View> toRemove = new ArrayList<View>();  
  21.     for (int i=0; i<mNotificationIcons.getChildCount(); i++) {  
  22.         View child = mNotificationIcons.getChildAt(i);  
  23.         if (!toShow.contains(child)) {  
  24.             toRemove.add(child);  
  25.         }  
  26.     }  
  27.   
  28.     for (View remove : toRemove) {  
  29.         mNotificationIcons.removeView(remove);  
  30.     }  
  31.   
  32.     for (int i=0; i<toShow.size(); i++) {  
  33.         View v = toShow.get(i);  
  34.         if (v.getParent() == null) {  
  35.             mNotificationIcons.addView(v, i, params);  
  36.         }  
  37.     }  
  38. }  

通过以上代码的分析,我们可以知道updateNotificationIcons()主要做了两件事:更新ExpanddeView上的通知信息;更新StatusBar上的通知图标。更新方法都类似,先查通知看是否有效,如果不是则删除,如果是则添加。以上完成了加载Notification的第一步,那么我来看第二步tick(notification)

          (2).tick(notification),跟踪代码如下:

[java] view plaincopy
  1. private void tick(StatusBarNotification n) {  
  2.     // Show the ticker if one is requested. Also don't do this  
  3.     // until status bar window is attached to the window manager,  
  4.     // because...  well, what's the point otherwise?  And trying to  
  5.     // run a ticker without being attached will crash!  
  6.     if (n.notification.tickerText != null && mStatusBarView.getWindowToken() != null) {  
  7.         if (0 == (mDisabled & (StatusBarManager.DISABLE_NOTIFICATION_ICONS  
  8.                         | StatusBarManager.DISABLE_NOTIFICATION_TICKER))) {  
  9.             //ticker执行方法  
  10.             mTicker.addEntry(n);  
  11.         }  
  12.     }  
  13. }  

跳转到mTicker.addEntry();方法中,代码路径:SourceCode/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/Ticker.java中,代码如下:

[java] view plaincopy
  1. public void addEntry(StatusBarNotification n) {  
  2.     int initialCount = mSegments.size();  
  3.   
  4.     // If what's being displayed has the same text and icon, just drop it  
  5.     // (which will let the current one finish, this happens when apps do  
  6.     // a notification storm).  
  7.     if (initialCount > 0) {  
  8.         final Segment seg = mSegments.get(0);  
  9.         //判断该Notififacation是不是已经在StatusBar上显示了的(同一个Notification发送两次这里并么有执行,为什么?)  
  10.         if (n.pkg.equals(seg.notification.pkg)  
  11.                 && n.notification.icon == seg.notification.notification.icon  
  12.                 && n.notification.iconLevel == seg.notification.notification.iconLevel  
  13.                 && CharSequences.equals(seg.notification.notification.tickerText,  
  14.                     n.notification.tickerText)) {  
  15.             return;  
  16.         }  
  17.     }  
  18.     // 获取该Notification的icon  
  19.     final Drawable icon = StatusBarIconView.getIcon(mContext,  
  20.             new StatusBarIcon(n.pkg, n.notification.icon, n.notification.iconLevel, 0,  
  21.                     n.notification.tickerText));  
  22.     // 将Notification的一些信息放入对象newSegment中,这里的Segment翻译过来是片段和部分的意思  
  23.     // Segment是Ticker类的内部类,用于存放notification的部分信息以及对信息的一些处理,比如tickerText  
  24.     final Segment newSegment = new Segment(n, icon, n.notification.tickerText);  
  25.   
  26.     // If there's already a notification schedule for this package and id, remove it.  
  27.     // 若果该信息在Segment中已经存在,则删除掉  
  28.     for (int i=0; i<mSegments.size(); i++) {  
  29.         Segment seg = mSegments.get(i);  
  30.         if (n.id == seg.notification.id && n.pkg.equals(seg.notification.pkg)) {  
  31.             // just update that one to use this new data instead(什么时候触发?)  
  32.             mSegments.remove(i--); // restart iteration here  
  33.         }  
  34.     }  
  35.     // 将Notification的部分信息放到mSegments链表中。  
  36.     mSegments.add(newSegment);  
  37.   
  38.     if (initialCount == 0 && mSegments.size() > 0) {  
  39.         Segment seg = mSegments.get(0);  
  40.         seg.first = false;  
  41.         //初始化id/tickerIcon  
  42.         mIconSwitcher.setAnimateFirstView(false);  
  43.         mIconSwitcher.reset();  
  44.         mIconSwitcher.setImageDrawable(seg.icon);  
  45.         //初始化id/tickerText  
  46.         mTextSwitcher.setAnimateFirstView(false);  
  47.         mTextSwitcher.reset();  
  48.         mTextSwitcher.setText(seg.getText());  
  49.         //启动ticker  
  50.         tickerStarting();  
  51.         scheduleAdvance();  
  52.     }  
  53. }  

通过Open Implementation我们跳转到PhoneStatusBar中的tickerStatrting()中,代码如下:

[java] view plaincopy
  1. @Override  
  2. public void tickerStarting() {  
  3.     mTicking = true;  
  4.     //这里的mIcons和ticker组成了整个StatusBar的布局,因此这里要线将它置为GONE  
  5.     mIcons.setVisibility(View.GONE);  
  6.     //显示tickerText  
  7.     mTickerView.setVisibility(View.VISIBLE);  
  8.     //加载ticker弹出时的动画  
  9.     mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.push_up_in, null));  
  10.     mIcons.startAnimation(loadAnim(com.android.internal.R.anim.push_up_out, null));  
  11. }  
这里可以发现ticker开加载动画并显示了,实际上Notification在StatusBar上的显示效果是加载了动画的原因。继续查看scheduleAdvance()方法:
[java] view plaincopy
  1. private void scheduleAdvance() {  
  2.     mHandler.postDelayed(mAdvanceTicker, TICKER_SEGMENT_DELAY);  
  3. }  

通过Handler的PostDelay方法执行mAdvanceTicker这个Runnable中的run方法,代码如下:
[java] view plaincopy
  1. private Runnable mAdvanceTicker = new Runnable() {  
  2.     public void run() {  
  3.         while (mSegments.size() > 0) {  
  4.             Segment seg = mSegments.get(0);  
  5.   
  6.             if (seg.first) {  
  7.                 // this makes the icon slide in for the first one for a given  
  8.                 // notification even if there are two notifications with the  
  9.                 // same icon in a row  
  10.                 // 第一次显示时,设置tickerIcon的值,这里就是ticker最前方显示的那个icon  
  11.                 mIconSwitcher.setImageDrawable(seg.icon);  
  12.             }  
  13.             CharSequence text = seg.advance();  
  14.             if (text == null) {  
  15.                 mSegments.remove(0);  
  16.                 continue;  
  17.             }  
  18.             // 显示用户设置的tickerText内容  
  19.             mTextSwitcher.setText(text);  
  20.   
  21.             scheduleAdvance();  
  22.             break;  
  23.         }  
  24.         if (mSegments.size() == 0) {  
  25.             // 完成ticker的一次显示  
  26.             tickerDone();  
  27.         }  
  28.     }  
  29. };  

最后我们再来看看tickerDone()方法,该方法主要对应于tickerStarting()方法,代码如下:

[java] view plaincopy
  1. @Override  
  2. public void tickerDone() {  
  3.     //显示mIcons布局  
  4.     mIcons.setVisibility(View.VISIBLE);  
  5.     //tickerText消失  
  6.     mTickerView.setVisibility(View.GONE);  
  7.     //加载ticker消失的动画  
  8.     mIcons.startAnimation(loadAnim(com.android.internal.R.anim.push_down_in, null));  
  9.     mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.push_down_out,  
  10.                 mTickingDoneListener));  
  11. }  

至此,我们完成了Notification加载的前两步,分别是addNotificationViews(key, notification)和tick(notification)。剩下最后一步,即:setAreThereNotifications()和updateExpandedViewPos(EXPANDED_LEAVE_ALONE);

          (3).setAreThereNotifications()和updateExpandedViewPos().这里先说一下setAreThereNotifications()方法,代码如下:

[java] view plaincopy
  1. private void setAreThereNotifications() {  
  2.     //是否有Notification  
  3.     final boolean any = mNotificationData.size() > 0;  
  4.     //该Notification是否可被清除  
  5.     final boolean clearable = any && mNotificationData.hasClearableItems();  
  6.   
  7.     if (DEBUG) {  
  8.         Slog.d(TAG, "setAreThereNotifications: N=" + mNotificationData.size()  
  9.                 + " any=" + any + " clearable=" + clearable);  
  10.     }  
  11.     //对"清除所有通知"按钮进行设置  
  12.     if (mClearButton.isShown()) {  
  13.         if (clearable != (mClearButton.getAlpha() == 1.0f)) {  
  14.             ObjectAnimator.ofFloat(mClearButton, "alpha",  
  15.                     clearable ? 1.0f : 0.0f)  
  16.                 .setDuration(250)  
  17.                 .start();  
  18.         }  
  19.     } else {  
  20.         mClearButton.setAlpha(clearable ? 1.0f : 0.0f);  
  21.     }  
  22.     mClearButton.setEnabled(clearable);  
  23.   
  24. ...  
  25. }  

继续分析updateExpandedViewPos(EXPANDED_LEAVE_ALONE);方法,代码如下;

[java] view plaincopy
  1. void updateExpandedViewPos(int expandedPosition) {  
  2.     if (SPEW) {  
  3.         Slog.d(TAG, "updateExpandedViewPos before expandedPosition=" + expandedPosition  
  4.                 + " mTrackingParams.y=" + ((mTrackingParams == null) ? "?" : mTrackingParams.y)  
  5.                 + " mTrackingPosition=" + mTrackingPosition);  
  6.     }  
  7.     //获取StatusBar高度  
  8.     int h = mStatusBarView.getHeight();  
  9.     //获取当前分辨率高度  
  10.     int disph = mDisplayMetrics.heightPixels;  
  11.   
  12.     // If the expanded view is not visible, make sure they're still off screen.  
  13.     // Maybe the view was resized.  
  14.     //如果ExpanedView不可见则执行  
  15.     if (!mExpandedVisible) {  
  16.         //更新mTrackingView属性,设置mExpandedDialog属性  
  17.         updateExpandedInvisiblePosition();  
  18.         return;  
  19.     }  
  20.   
  21.     // tracking view...  
  22.     //设置TriackingView的各种属性  
  23.     int pos;  
  24.     if (expandedPosition == EXPANDED_FULL_OPEN) {  
  25.         pos = h;  
  26.     }  
  27.     else if (expandedPosition == EXPANDED_LEAVE_ALONE) {  
  28.         //传递参数为EXPANDED_LEAVE_ALONE  
  29.         pos = mTrackingPosition;  
  30.     }  
  31.     else {  
  32.         if (expandedPosition <= disph) {  
  33.             pos = expandedPosition;  
  34.         } else {  
  35.             pos = disph;  
  36.         }  
  37.         pos -= disph-h;  
  38.     }  
  39.     mTrackingPosition = mTrackingParams.y = pos;  
  40.     mTrackingParams.height = disph-h;  
  41.     WindowManagerImpl.getDefault().updateViewLayout(mTrackingView, mTrackingParams);  
  42.   
  43.     if (mExpandedParams != null) {  
  44.         if (mCloseView.getWindowVisibility() == View.VISIBLE) {  
  45.             mCloseView.getLocationInWindow(mPositionTmp);  
  46.             final int closePos = mPositionTmp[1];  
  47.   
  48.             mExpandedContents.getLocationInWindow(mPositionTmp);  
  49.             final int contentsBottom = mPositionTmp[1] + mExpandedContents.getHeight();  
  50.   
  51.             mExpandedParams.y = pos + mTrackingView.getHeight()  
  52.                     - (mTrackingParams.height-closePos) - contentsBottom;  
  53.   
  54.             if (SPEW) {  
  55.                 Slog.d(PhoneStatusBar.TAG,  
  56.                         "pos=" + pos +  
  57.                         " trackingHeight=" + mTrackingView.getHeight() +  
  58.                         " (trackingParams.height - closePos)=" +  
  59.                             (mTrackingParams.height - closePos) +  
  60.                         " contentsBottom=" + contentsBottom);  
  61.             }  
  62.   
  63.         } else {  
  64.             // If the tracking view is not yet visible, then we can't have  
  65.             // a good value of the close view location.  We need to wait for  
  66.             // it to be visible to do a layout.  
  67.             mExpandedParams.y = -mDisplayMetrics.heightPixels;  
  68.         }  
  69.         int max = h;  
  70.         if (mExpandedParams.y > max) {  
  71.             mExpandedParams.y = max;  
  72.         }  
  73.         int min = mTrackingPosition;  
  74.         if (mExpandedParams.y < min) {  
  75.             mExpandedParams.y = min;  
  76.         }  
  77.   
  78.         boolean visible = (mTrackingPosition + mTrackingView.getHeight()) > h;  
  79.         if (!visible) {  
  80.             // if the contents aren't visible, move the expanded view way off screen  
  81.             // because the window itself extends below the content view.  
  82.             mExpandedParams.y = -disph;  
  83.         }  
  84.         mExpandedDialog.getWindow().setAttributes(mExpandedParams);  
  85.   
  86.         // As long as this isn't just a repositioning that's not supposed to affect  
  87.         // the user's perception of what's showing, call to say that the visibility  
  88.         // has changed. (Otherwise, someone else will call to do that).  
  89.         if (expandedPosition != EXPANDED_LEAVE_ALONE) {  
  90.             if (SPEW) Slog.d(TAG, "updateExpandedViewPos visibilityChanged(" + visible + ")");  
  91.             visibilityChanged(visible);  
  92.         }  
  93.     }  
  94.   
  95.     if (SPEW) {  
  96.         Slog.d(TAG, "updateExpandedViewPos after  expandedPosition=" + expandedPosition  
  97.                 + " mTrackingParams.y=" + mTrackingParams.y  
  98.                 + " mTrackingPosition=" + mTrackingPosition  
  99.                 + " mExpandedParams.y=" + mExpandedParams.y  
  100.                 + " mExpandedParams.height=" + mExpandedParams.height);  
  101.     }  
  102. }  

以上代码主要完成对TrackingView的属性设置,ExpandedView实际上是包裹在TrackingView中的,因此这里也附带进行了设置,最后更新,完成了整个Notification的加载。

       小结

       通过对Notification加载流程的分析,对Notifification工作流程有了大致的了解。针对上文中的分析可能部分地方还有所偏颇,还需要加强自己的代码阅读能力。以下是简单的时序图,如图3:

图3