Android 状态栏Statusbar的启动与显示信号栏的过程

启动Statusbar并显示信号栏图标的过程

Systemserver在启动系统服务的时候启动了Systemui。

try {
    startSystemUi(context, windowManagerF);
} catch (Throwable e) {
    reportWtf("starting System UI", e);
}
static final void startSystemUi(Context context, WindowManagerService windowManager) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.android.systemui",
                                          "com.android.systemui.SystemUIService"));
    intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
    //Slog.d(TAG, "Starting service: " + intent);
    context.startServiceAsUser(intent, UserHandle.SYSTEM);
    windowManager.onSystemUiStarted();
}

SystemUIService

@Override
public void onCreate() {
    super.onCreate();
    ((SystemUIApplication) getApplication()).startServicesIfNeeded();
}

接着使用反射的方式来启动SystemUIFactory.

    public void startServicesIfNeeded() {
        startServicesIfNeeded(SERVICES);
    }

这里的SERVICES对应的是静态数组

    private final Class<?>[] SERVICES = new Class[] {
            Dependency.class,
            NotificationChannels.class,
            CommandQueue.CommandQueueStart.class,
            KeyguardViewMediator.class,
            Recents.class,
            VolumeUI.class,
            Divider.class,
            SystemBars.class,   /////////////状态栏
            StorageNotification.class,
            PowerUI.class,
            RingtonePlayer.class,
            KeyboardUI.class,
            PipUI.class,
            ShortcutKeyDispatcher.class,
            VendorServices.class,
            GarbageMonitor.Service.class,
            LatencyTester.class,
            GlobalActionsComponent.class,
            RoundedCorners.class,
    };

startServicesIfNeeded

private void startServicesIfNeeded(Class<?>[] services) {
    ...
for (int i = 0; i < N; i++) {
    Class<?> cl = services[i];
    if (DEBUG) Log.d(TAG, "loading: " + cl);
    log.traceBegin("StartServices" + cl.getSimpleName());
    long ti = System.currentTimeMillis();
    try {

        Object newService = SystemUIFactory.getInstance().createInstance(cl);
        mServices[i] = (SystemUI) ((newService == null) ? cl.newInstance() : newService);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    } catch (InstantiationException ex) {
        throw new RuntimeException(ex);
    }

上面的newService返回的是null,所以会去调用SERVICES的类名去初始化。

其中SystemBars就是包含了顶部状态栏Statusbar和底部导航栏Navigationbar。

SystemBars

public class SystemBars extends SystemUI
@Override
public void start() {
    if (DEBUG) Log.d(TAG, "start");
    createStatusBarFromConfig();
}
private void createStatusBarFromConfig() {
    if (DEBUG) Log.d(TAG, "createStatusBarFromConfig");
    final String clsName = mContext.getString(R.string.config_statusBarComponent);
    if (clsName == null || clsName.length() == 0) {
        throw andLog("No status bar component configured", null);
    }
    Class<?> cls = null;
    try {
        cls = mContext.getClassLoader().loadClass(clsName);
    } catch (Throwable t) {
        throw andLog("Error loading status bar component: " + clsName, t);
    }
    try {
        mStatusBar = (SystemUI) cls.newInstance();
    } catch (Throwable t) {
        throw andLog("Error creating status bar component: " + clsName, t);
    }
    mStatusBar.mContext = mContext;
    mStatusBar.mComponents = mComponents;
    mStatusBar.start();
    if (DEBUG) Log.d(TAG, "started " + mStatusBar.getClass().getSimpleName());
}

然后这里也是使用反射的方式来启动statusbar,

R.string.config_statusBarComponent对应的内容是

<string name="config_statusBarComponent" translatable="false">com.android.systemui.statusbar.phone.StatusBar</string>

也就是说,到这里才是正式启动StatusBar。

进入到StatusBar.start();

@Override
public void start() {
    ....
    createAndAddWindows();
    ....
}          

start里做了很多其他初始化处理,但是最关键的是添加view和window的过程。

public void createAndAddWindows() {
    addStatusBarWindow();
}

private void addStatusBarWindow() {
    makeStatusBarView();
    mStatusBarWindowManager = Dependency.get(StatusBarWindowManager.class);
    mRemoteInputController = new RemoteInputController(mHeadsUpManager);
    mStatusBarWindowManager.add(mStatusBarWindow, getStatusBarHeight());
}
protected void makeStatusBarView() {
    final Context context = mContext;
    updateDisplaySize(); // populates mDisplayMetrics
    updateResources();
    updateTheme();

    inflateStatusBarWindow(context);
    ....
        FragmentHostManager.get(mStatusBarWindow)
        .addTagListener(CollapsedStatusBarFragment.TAG, (tag, fragment) -> {
            CollapsedStatusBarFragment statusBarFragment =
                (CollapsedStatusBarFragment) fragment;
            statusBarFragment.initNotificationIconArea(mNotificationIconAreaController);
            mStatusBarView = (PhoneStatusBarView) fragment.getView();
            mStatusBarView.setBar(this);
            mStatusBarView.setPanel(mNotificationPanel);
            mStatusBarView.setScrimController(mScrimController);
            mStatusBarView.setBouncerShowing(mBouncerShowing);
            setAreThereNotifications();
            checkBarModes();
            /// M: add for plmn display feature @{
            attachPlmnPlugin();
            ///@}
        }).getFragmentManager()
        .beginTransaction()
        .replace(R.id.status_bar_container, new CollapsedStatusBarFragment(),
                 CollapsedStatusBarFragment.TAG)
        .commit();
    ....
        try {
            boolean showNav = mWindowManagerService.hasNavigationBar();
            if (DEBUG) Log.v(TAG, "hasNavigationBar=" + showNav);
            if (showNav) {
                createNavigationBar();
            }
        } catch (RemoteException ex) {
            // no window manager? good luck with that
        }
    ....
}

makeStatusBarView里有也有很多view的处理流程。后面还去判断是否启动NavigationBar。

其中有一段是把status_bar_container启动了一个CollapsedStatusBarFragment。

status_bar_container这个view是显示众多statusbar view的地方。

CollapsedStatusBarFragment

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.status_bar, container, false);
}

这里加载了statusbar的layout

R.layout.status_bar这个layout装载了许多的view。

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mStatusBar = (PhoneStatusBarView) view;
    mDarkIconManager = new DarkIconManager(view.findViewById(R.id.statusIcons));
    mSystemIconArea = mStatusBar.findViewById(R.id.system_icon_area);
    carrierText = (CarrierText)mStatusBar.findViewById(R.id.system_carrier_text);
    mSignalClusterView = mStatusBar.findViewById(R.id.signal_cluster);
    // Default to showing until we know otherwise.
    showSystemIconArea(false);
    initEmergencyCryptkeeperText();
}

在onViewCreated里则是几个重要view的初始化。

分别有

mSystemIconArea用来显示状态栏左边图标的区域

carrierText用来显示网络供应商的名称

mSignalClusterView则是用来显示信号相关的信息


以上就是信号栏初始化的流程。

你可能感兴趣的:(SystemUI)