Android 9 SystemUI之内部SystemUI服务的创建
一、內部服務配置
在packages/SystemUI/res/values/config.xml 文件中配置服務
<string-array name="config_systemUIServiceComponents" translatable="false"><item>com.android.systemui.Dependency</item><item>com.android.systemui.util.NotificationChannels</item><item>com.android.systemui.statusbar.CommandQueue$CommandQueueStart</item><item>com.android.systemui.keyguard.KeyguardViewMediator</item><item>com.android.systemui.recents.Recents</item><item>com.android.systemui.volume.VolumeUI</item><item>com.android.systemui.stackdivider.Divider</item><item>com.android.systemui.SystemBars</item><item>com.android.systemui.usb.StorageNotification</item><item>com.android.systemui.power.PowerUI</item><item>com.android.systemui.media.RingtonePlayer</item><item>com.android.systemui.keyboard.KeyboardUI</item><item>com.android.systemui.pip.PipUI</item><item>com.android.systemui.shortcut.ShortcutKeyDispatcher</item><item>@string/config_systemUIVendorServiceComponent</item><item>com.android.systemui.util.leak.GarbageMonitor$Service</item><item>com.android.systemui.LatencyTester</item><item>com.android.systemui.globalactions.GlobalActionsComponent</item><item>com.android.systemui.fingerprint.FingerprintDialogImpl</item><item>com.android.systemui.SliceBroadcastRelayHandler</item></string-array>二、SystemUI內部服務的創建
三、SystemUI調用onConfigurationChanged流程
SystemUIApplication.java 為onConfigurationChanged入口
@Overridepublic void onConfigurationChanged(Configuration newConfig) {if (mServicesStarted) {int len = mServices.length;for (int i = 0; i < len; i++) {if (mServices[i] != null) {mServices[i].onConfigurationChanged(newConfig);}}}callPluginConfigurationChanged(newConfig);}1、第一個服務是從Dependency調用onConfigurationChanged方法,
@Overrideprotected synchronized void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);mDependencies.values().stream().filter(obj -> obj instanceof ConfigurationChangedReceiver).forEach(o -> {Log.i(TAG, " o: " + ((ConfigurationChangedReceiver) o).getClass());((ConfigurationChangedReceiver) o).onConfigurationChanged(newConfig);});}上面Log.i是自己加的,輸出的類名依次
01-01 01:18:52.692 4610 4610 I Dependency: o: class com.android.systemui.assist.AssistManager 01-01 01:18:52.711 4610 4610 I Dependency: o: class com.android.systemui.statusbar.policy.NetworkControllerImpl 01-01 01:18:52.712 4610 4610 I Dependency: o: class com.android.systemui.statusbar.phone.ConfigurationControllerImpl 01-01 01:18:52.733 4610 4610 I Dependency: o: class com.android.systemui.fragments.FragmentServiceAssistManager這是輔助管理類
NetworkControllerImpl 移動信號控制類onConfigurationChanged主要處理圖標的重新加載
ConfigurationController 接口需要監聽onConfigurationChanged方法的類,注冊到ConfigurationControllerImpl里。
FragmentService 包含兩個view key: com.android.systemui.statusbar.phone.StatusBarWindowView和com.android.systemui.statusbar.phone.NavigationBarFrame
FragmentService.java
接下來,調用內部類的FragmentHostState-> sendConfigurationChange
private class FragmentHostState {private final View mView;private FragmentHostManager mFragmentHostManager;public FragmentHostState(View view) { //利用上面提供view 創建兩個FragmentHostManagermView = view;mFragmentHostManager = new FragmentHostManager(mContext, FragmentService.this, mView);}public void sendConfigurationChange(Configuration newConfig) {mHandler.post(() -> handleSendConfigurationChange(newConfig));}public FragmentHostManager getFragmentHostManager() {return mFragmentHostManager;}private void handleSendConfigurationChange(Configuration newConfig) {mFragmentHostManager.onConfigurationChanged(newConfig); //調用到具體view的onConfigurationChanged}}FragmentHostManager.java
protected void onConfigurationChanged(Configuration newConfig) {if (mConfigChanges.applyNewConfig(mContext.getResources())) {// Save the old state.Log.i(TAG, " Save the old state. ");Parcelable p = destroyFragmentHost(); //釋放// Generate a new fragment host and restore its state.createFragmentHost(p);//重新創建Fragment} else {mFragments.dispatchConfigurationChanged(newConfig);}}這里的釋放Fragment 會調用到每個view 的fragment監控
public interface FragmentListener {void onFragmentViewCreated(String tag, Fragment fragment);// The facts of lifecycle// When a fragment is destroyed, you should not talk to it any longer.default void onFragmentViewDestroyed(String tag, Fragment fragment) {}}在StatusBarWindowView的創建時沒有重載onFragmentViewDestroyed方法。
2、StatusBar.java 這是StatusBarWindowView的fragment監聽
.... FragmentHostManager.get(mStatusBarWindow).addTagListener(CollapsedStatusBarFragment.TAG, (tag, fragment) -> {CollapsedStatusBarFragment statusBarFragment =(CollapsedStatusBarFragment) fragment; ... });3、StatusBar.java 創建NavigationBar的fragment監聽
protected void createNavigationBar() {mNavigationBarView = NavigationBarFragment.create(mContext, (tag, fragment) -> {Log.i(TAG, " createNavigationBar ");mNavigationBar = (NavigationBarFragment) fragment;if (mLightBarController != null) {mNavigationBar.setLightBarController(mLightBarController);}mNavigationBar.setCurrentSysuiVisibility(mSystemUiVisibility);});上面沒有對onFragmentViewDestroyed方法重寫,沒有進行view的釋放
NavigationBarFragment.java 這是navigationBarView的fragment添加監聽到FragmentHostManager
...View navigationBarView = LayoutInflater.from(context).inflate(R.layout.navigation_bar_window, null); //解析xml布局if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);if (navigationBarView == null) return null;context.getSystemService(WindowManager.class).addView(navigationBarView, lp); //設置view參數FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView); //獲取對應view的FragmentHostState里的包含的FragmentHostManager實例NavigationBarFragment fragment = new NavigationBarFragment();fragmentHost.getFragmentManager().beginTransaction().replace(R.id.navigation_bar_frame, fragment, TAG).commit();fragmentHost.addTagListener(TAG, listener);Log.i(TAG, " listener: " + listener);...接下來就調用每個一個服務的onConfigurationChanged方法,如果用plugin方式插入view,重新創建fragmeng 會出現view重復加載的情況。解決這一問題的方法是將上次創建的view移除或是不重新創建fragment。
總結
以上是生活随笔為你收集整理的Android 9 SystemUI之内部SystemUI服务的创建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#和C++的区别,也就是解释型语言跟编
- 下一篇: 图的建立(邻接矩阵)与其深度优先和广度优