Android 4.0 statusbar加载icon(3)

三、StatusBarManagerService.java

前面,我们启动PhoneStatusBar时会调用StatusBarstart函数,在这里和StatusbarManagerService

建立联系。

StatusBar.java

mBarService.registerStatusBar(mCommandQueue, iconList, notificationKeys, notifications,

                    switches, binders);

public class StatusBarManagerService extends IStatusBarService.Stub

    implements WindowManagerService.OnHardKeyboardStatusChangeListener

{

public StatusBarManagerService(Context context, WindowManagerService windowManager) {

         mContext = context;

         mWindowManager = windowManager;

         mWindowManager.setOnHardKeyboardStatusChangeListener(this);

         final Resources res = context.getResources();

//这里,获取config_statusBarIcons.xm中定义的显示icon的数组

    mIcons.defineSlots(res.

getStringArray(com.Android.internal.R.array.config_statusBarIcons));

       }

}

public void registerStatusBar(IStatusBar bar, StatusBarIconList iconList,

            List<IBinder> notificationKeys, List<StatusBarNotification> notifications,

            int switches[], List<IBinder> binders) {

//这个bar就是CommandQueuePhoneStatusbar中的实现callbacks

mBar = bar;

synchronized (mIcons) {

//将初始化这个service时获取的config_statusBarIcons数组中字符串保存到iconList

//也就是传递给PhoneStatusBar使用了

    iconList.copyFrom(mIcons);

}

synchronized (mNotifications) {

    for (Map.Entry<IBinder,StatusBarNotification> e: mNotifications.entrySet()) {

         notificationKeys.add(e.getKey());

         notifications.add(e.getValue());

    }

 }

……

}

public void setIcon(String slot, String iconPackage, int iconId, int iconLevel,

            String contentDescription) {

        enforceStatusBar();

        synchronized (mIcons) {

//也就是当前这个slot在数组中的index

            int index = mIcons.getSlotIndex(slot);

//如果没有定义的slot,就报异常

            if (index < 0) {

                throw new SecurityException("invalid status bar icon slot: " + slot);

            }

//这才正式创建StatusBarIconiconPackagePhoneStatusBar的,iconId就是我们设置的

            StatusBarIcon icon = new StatusBarIcon(iconPackage, iconId, iconLevel, 0,

                    contentDescription);

//数组中对应的index设置了icon

            mIcons.setIcon(index, icon);

            if (mBar != null) {

                try {

                    mBar.setIcon(index, icon);

                } catch (RemoteException ex) {

                }

            }

        }

}

CommandQueue.java

 public void setIcon(int index, StatusBarIcon icon) {

        synchronized (mList) {

//mList用的也是StatusBarManagerService中初始化的iconList

            int what = MSG_ICON | index;

            mHandler.removeMessages(what);

            mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();

        }

 }

private final class H extends Handler {

public void handleMessage(Message msg) {

    final int what = msg.what & MSG_MASK;

switch (what) {

        case MSG_ICON: {

              final int index = msg.what & INDEX_MASK;

              final int viewIndex = mList.getViewIndex(index);

              switch (msg.arg1) {

              case OP_SET_ICON: {

                    StatusBarIcon icon = (StatusBarIcon)msg.obj;

//mList

                    StatusBarIcon old = mList.getIcon(index);

                    if (old == null) {

                        mList.setIcon(index, icon);

                        mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);

                    } else {

                        mList.setIcon(index, icon);

                        mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,

                                        old, icon);

                    }

               break;

         }

}

}

最后就是调用PhoneStatusBaraddIcon。至此,就用StatusBar显示了icon

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pszyp.html