欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android Q SystemUI

程序员文章站 2022-06-18 13:54:27
android Q SystemUISystemUI 启动状态栏的加载Service 中addView用Fragment填充内容信号栏图标的更新锁屏壁纸通知加载过程SystemUI 启动SystemServer.startOtherServices->startSystemUi启动SystemUIService进而通过((SystemUIApplication) getApplication()).startServicesIfNeeded()启动一系列继承SystemUI的类其中Syste...

SystemUI 启动

SystemServer.startOtherServices->startSystemUi
启动SystemUIService进而通过((SystemUIApplication) getApplication()).startServicesIfNeeded()
启动一系列继承SystemUI的类
其中SystemBar.start()中通过反射构造出StatusBar的实列,进一步调用mStatusBar.start()

状态栏的加载

StatusBar.createAndAddWindows(result);
public void createAndAddWindows(@Nullable RegisterStatusBarResult result) {
//初始化mStatusBarWindow
makeStatusBarView(result);
//获取mStatusBarWindowController
mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
//将mStatusBarWindow通过mWindowManager.addView(mStatusBarView, mLp);方式添加到Window中
mStatusBarWindowController.add(mStatusBarWindow, getStatusBarHeight());
}

makeStatusBarView中status_bar_container由CollapsedStatusBarFragment来填充

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


 @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    android.util.Log.e(TAG,"onViewCreated");
    mStatusBar = (PhoneStatusBarView) view;
    if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_PANEL_STATE)) {
        mStatusBar.restoreHierarchyState(
                savedInstanceState.getSparseParcelableArray(EXTRA_PANEL_STATE));
    }
	//构造DarkIconManager
    mDarkIconManager = new DarkIconManager(view.findViewById(R.id.statusIcons));
    mDarkIconManager.setShouldLog(true);
	//通过StatusBarIconController.addIconGroup将wifi,mobile等图标加载到状态栏中
    Dependency.get(StatusBarIconController.class).addIconGroup(mDarkIconManager);
    mSystemIconArea = mStatusBar.findViewById(R.id.system_icon_area);
    mClockView = mStatusBar.findViewById(R.id.clock);
    showSystemIconArea(false);
    showClock(false);
    initEmergencyCryptkeeperText();
    initOperatorName();
}

Service 中addView用Fragment填充内容

具体在StatusBar.createAndAddWindows中实现
关键点在于FragmentManager的获取

1,初始化 FragmentController mFragments
private void createFragmentHost(Parcelable savedState) {

	//获取FragmentController,最重要的就是HostCallbacks的自定义,其中onFindViewById必须复写
    mFragments = FragmentController.createController(new HostCallbacks());
	//关联父Fragment,不需要关联故attachHost(null)
    mFragments.attachHost(null);
    mLifecycleCallbacks = new FragmentLifecycleCallbacks() {
        @Override
        public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v,
                Bundle savedInstanceState) {
            FragmentHostManager.this.onFragmentViewCreated(f);
            Log.e("FragmentHostManager","onFragmentViewCreated f="+f+",view="+v);
        }

        @Override
        public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {
            FragmentHostManager.this.onFragmentViewDestroyed(f);
        }

        @Override
        public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
            Dependency.get(LeakDetector.class).trackGarbage(f);
        }
    };
	//管理Fragment生命周期
    mFragments.getFragmentManager().registerFragmentLifecycleCallbacks(mLifecycleCallbacks,
            true);
    if (savedState != null) {
        mFragments.restoreAllState(savedState, (FragmentManagerNonConfig) null);
    }
	
    // For now just keep all fragments in the resumed state.
    mFragments.dispatchCreate();
    mFragments.dispatchStart();
    mFragments.dispatchResume();
}

class HostCallbacks extends FragmentHostCallback<FragmentHostManager> {
    public HostCallbacks() {
        super(mContext, FragmentHostManager.this.mHandler, 0);
    }

    @Override
    public FragmentHostManager onGetHost() {
        return FragmentHostManager.this;
    }

    @Override
    public void onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
        FragmentHostManager.this.dump(prefix, fd, writer, args);
    }

    @Override
    public Fragment instantiate(Context context, String className, Bundle arguments) {
        return mPlugins.instantiate(context, className, arguments);
    }

    @Override
    public boolean onShouldSaveFragmentState(Fragment fragment) {
        return true; // True for now.
    }

    @Override
    public LayoutInflater onGetLayoutInflater() {
        return LayoutInflater.from(mContext);
    }

    @Override
    public boolean onUseFragmentManagerInflaterFactory() {
        return true;
    }

    @Override
    public boolean onHasWindowAnimations() {
        return false;
    }

    @Override
    public int onGetWindowAnimations() {
        return 0;
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
    }


	///此方法必须覆写,否则返回null,因为没有view而不会显示Fragment内容
    @Override
    @Nullable
    public <T extends View> T onFindViewById(int id) {
        return FragmentHostManager.this.findViewById(id);
    }

    @Override
    public boolean onHasView() {
        return true;
    }
}

//mRootView是mWindowManager.addView(mStatusBarView, mLp) 中的mStatusBarView.
private <T extends View> T findViewById(int id) {
    return mRootView.findViewById(id);
}

2,获取FragmentManager mFragments.getFragmentManager()
如此便可以在Service中使用Fragment了

信号栏图标的更新

NetworkControllerImpl 监控网络状态的变化(wifi,telephony)
mWifiSignalController 负责wifi
mMobileSignalControllers 负责mobileSignal

public class StatusBarSignalPolicy implements NetworkControllerImpl.SignalCallback

mNetworkController.addCallback(this);将NetworkControllerImpl与StatusBarSignalPolicy绑定起来
进而通过CallbackHandler将SignalController与StatusBarSignalPolicy关联起来 ------------mCallbackHandler.setListening

当NetworkControllerImpl接到wifi状态发生变化时,通过mWifiSignalController更新wifi状态,然后通过callback.setWifiIndicators来更新UI
即StatusBarSignalPolicy.setWifiIndicators–>IconController来真正更新UI

StatusBarSignalPolicy.SignalIconState
WifiIconState extends SignalIconState
MobileIconState extends SignalIconState
StatusBarMobileView------------>sim信号
StatusBarWifiView-------------->wifi信号
根据SignalIconState来设置当前的UI

NetworkControllerImpl可以监控网络状态的变化
mCallbackHandler负责将SignalController与StatusBarSignalPolicy关联起来
StatusBarSignalPolicy负责逻辑处理并通过IconController来实现UI的更新
StatusBarIconControllerImpl 管理更新signalIcon

以wifi状态的改变逻辑如下:
NetworkControllerImpl.updateConnectivity->pushConnectivityToSignals
mWifiSignalController.updateConnectivity 此处保存旧的WifiIconState并通知是否更新->notifyListenersIfNecessary->notifyListeners
callback.setWifiIndicators即StatusBarSignalPolicy.setWifiIndicators->updateWifiIconWithState
mIconController.setSignalIcon即StatusBarIconControllerImpl.setSignalIcon->setIcon->handleSet
IconManager.onSetIconHolder->onSetSignalIcon
StatusBarWifiView.applyWifiState(state)

锁屏壁纸

NotificationMediaManager 负责绘制锁屏壁纸

public void setup(BackDropView backdrop, ImageView backdropFront, ImageView backdropBack,
        ScrimController scrimController, LockscreenWallpaper lockscreenWallpaper) {
    mBackdrop = backdrop;
    mBackdropFront = backdropFront;
    mBackdropBack = backdropBack;
    mScrimController = scrimController;
    mLockscreenWallpaper = lockscreenWallpaper;
}

mBackdrop是展现壁纸的父View,壁纸设置在mBackdropBack上

finishUpdateMediaMetaData中完成壁纸的获取和设置
final boolean hasArtwork = artworkDrawable != null;即表示是否有独立的锁屏壁纸

mScrimController控制ScrimView的显示

ScrimState中的AOD和PULSING会根据hasArtWork来设置mCurrentBehindAlpha即mScrimBehind的alpha,决定是否遮挡住锁屏壁纸的显示

通知加载过程

NotificationEntryManager
NotificationListController
StatusBarNotificationPresenter

import android.service.notification.NotificationListenerService;
NotificationListenerWithPlugins extends NotificationListenerService
NotificationListener extends NotificationListenerWithPlugins
onNotificationPosted
mEntryManager.addNotification(sbn, rankingMap);
addNotificationInternal
requireBinder().inflateViews
NotificationRowBinderImpl.inflateViews->updateNotification
ExpandableNotificationRow.inflateViews()
NotificationInflater.inflateNotificationViews
AsyncInflationTask.execute
NotificationInflater.apply->applyRemoteView->onViewApplied->finishIfDone
NotificationEntryManager.onAsyncInflationFinished
StatusBarNotificationPresenter-1,onEntryInflated;2,onBeforeNotificationAdded;3,onNotificationAdded
NotificationEntryManager.updateNotifications
StatusBarNotificationPresenter.updateNotificationViews();
mViewHierarchyManager.updateNotificationViews();
mNotificationPanel.updateNotificationViews();

本文地址:https://blog.csdn.net/lsz876/article/details/108164857

相关标签: android