美文网首页Launcher
Android Launcher3分析

Android Launcher3分析

作者: Exception_Cui | 来源:发表于2016-12-14 19:02 被阅读1182次

本文主要是对自己分析MTK 6.0 Launcher3源码的笔记,如有发现不对,请及时指正

启动流程

app启动都是由application开始启动的。

LauncherApplication.java
    public void onCreate() {
        super.onCreate();
     ...
        LauncherAppState.setApplicationContext(this);
        LauncherAppState.getInstance().setLauncehrApplication(this);    
     ..

    }

上面最主要的就是获取了 LauncherAppState 这个实例,然后将自己传到LauncherAppState的LauncherApplication对象 让

LauncherAppState是用于存储全局变量,比如缓存(各种cache),维护内存数据的类(LauncherModel),下面是LauncherAppState的类结构:

    private final AppFilter mAppFilter;
    private final LauncherModel mModel;
    private final IconCache mIconCache;
    private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
    private static WeakReference<LauncherProvider> sLauncherProvider;

mAppFilter:用于存储app文件夹的一些信息
mModel:用于维护Launcher在内存中的数据,比如app信息列表和widget信息列表,同时提供了更新数据库的操作
mIconCache:应用程序icon和title的缓存
mWidgetPreviewCacheDb:存储widget预览信息的数据库
sLauncherProvide:r app和widget的ContentProvider,用数据库存储信息

private LauncherAppState() {
       ...
        mIsScreenLarge = isScreenLarge(sContext.getResources());
        mScreenDensity = sContext.getResources().getDisplayMetrics().density;

        recreateWidgetPreviewDb();
        mIconCache = new IconCache(sContext);

        mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
        mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
        mModel = new LauncherModel(this, mIconCache, mAppFilter);
        final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
        launcherApps.addOnAppsChangedCallback(mModel);

        // Register intent receivers
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        sContext.registerReceiver(mModel, filter);
        filter = new IntentFilter();
        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
        sContext.registerReceiver(mModel, filter);
        filter = new IntentFilter();
        filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
        sContext.registerReceiver(mModel, filter);
      .....      
      // Register for changes to the favorites
        ContentResolver resolver = sContext.getContentResolver();
        resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
                mFavoritesObserver);

        mPowerManager = (PowerManager)sContext.getSystemService(Context.POWER_SERVICE);

LauncherAppState.getInstance()方法实例化了以上的数据,同时对Launcher中使用到的Receiver和Observer进行了注册。
在执行完Application的onCreate方法后,接下来就开始执行Launcher(Main Activity)的onCreate()方法。

Launcher.java

看onCreate()方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
     ...此处省略部分代码
        super.onCreate(savedInstanceState);                      
     ...
        LauncherAppState.setApplicationContext(getApplicationContext());
        LauncherAppState app = LauncherAppState.getInstance();
        LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);  //监听      LauncherAppState 里面的provider
     ...
        DeviceProfile grid = app.initDynamicGrid(this, isDatabaseIdChanged);   //设备的配置文件

     ...
        /**@}**/
        mIconCache = app.getIconCache();   /*获取Icon的缓存之类*/
        mIconCache.flushInvalidIcons(grid);
        mDragController = new DragController(this);
        mInflater = getLayoutInflater();
      ...
        mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);  //获取Widget的实例
   ...
        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
        mAppWidgetHost.startListening();     
        checkForLocaleChange();  //检测语言的变化 随之变化
       
         setContentView(R.layout.launcher);  

        setupViews();  //实例化Luncher.XML 全部引用进来
        grid.layout(this); //配置网格
       ...
       // 数据加载  
         if (!mRestoring) {
            /// M: Add for smart book feature. Reset load state if database changed before.
            if (isDatabaseIdChanged) { //false
                mModel.resetLoadedState(true, true);
            } else {
                /**M: Added to reset the loader state, to resolve the timing state issue.@{*/
                mModel.resetLoadedState(false, false);                 
                /**@}**/
            }

            if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {  //false
                // If the user leaves launcher, then we should just load items asynchronously when
                // they return.
                mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
            } else {
                // We only load the page synchronously if the user rotates (or triggers a
                // configuration change) while launcher is in the foreground
                mModel.startLoader(true, mWorkspace.getRestorePage());
            }
        }

        registerContentObservers();

        lockAllApps();

       ......
    }
mModel.startLoader(true, mWorkspace.getRestorePage()); 这句话很重要 开始进行加载数据模型等
在分析加载之前我们先看看luncher.xml都有什么

luncher.xml###

<com.android.launcher3.LauncherRootView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res-auto/com.android.launcher3"

    android:id="@+id/launcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/workspace_bg"
    android:fitsSystemWindows="true">

    <com.android.launcher3.DragLayer
        android:id="@+id/drag_layer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.android.launcher3.FocusIndicatorView
            android:id="@+id/focus_indicator"
            android:layout_width="52dp"
            android:layout_height="52dp" />

        <!-- The workspace contains 5 screens of cells -->
        <com.android.launcher3.Workspace
            android:id="@+id/workspace"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            launcher:defaultScreen="@integer/config_workspaceDefaultScreen" />

        <include layout="@layout/hotseat"
            android:id="@+id/hotseat"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end" />

        <include
            android:id="@+id/search_drop_target_bar"
            layout="@layout/search_drop_target_bar" />

        <include layout="@layout/overview_panel"
            android:id="@+id/overview_panel"
            android:visibility="gone" />

        <include layout="@layout/apps_customize_pane"
            android:id="@+id/apps_customize_pane"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="invisible" />
    </com.android.launcher3.DragLayer>

    <ViewStub
        android:id="@+id/launcher_overlay_stub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/launcher_overlay"
        android:layout="@layout/launcher_overlay" />
</com.android.launcher3.LauncherRootView>

DragLayer :这是个对Launcher的所有事件处理
Workspace:就是可以左右滑动的区域
![Paste_Image.png](https://img.haomeiwen.com/i2687780/2437085bb3712940.png?
imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
FocusIndicatorView:和Icon焦点变化有关
hotseat:

Paste_Image.png

overview_panel:就是长按显示下面的图标


Paste_Image.png

apps_customize_pane:


Paste_Image.png

下面贴两张Launcher常用的类


Paste_Image.png Paste_Image.png

到此 launcher3的启动流程已经完成了
接下来会分析Launcher3的数据加载流程

数据加载

刚看到一篇总结比较好的流程图

Paste_Image.png

数据加载 load和bindWorkspace()


Paste_Image.png

数据加载 获取和bind apps


Paste_Image.png

相关文章

网友评论

    本文标题:Android Launcher3分析

    本文链接:https://www.haomeiwen.com/subject/nqoimttx.html