Launcher3源码分析 — 将Workspace的数据与界面绑定

来源:互联网 发布:安卓慢动作拍摄软件 编辑:程序博客网 时间:2024/06/07 03:18

在数据从数据库加载到内存之后,接下来的工作就是把这些数据与launcher的UI视图绑定。绑定的过程在LauncherModel.bindWorkspace()方法中完成,在这个方法中会调用回调接口Callback里相应的回调方法。


Callback接口的定义如下:

public interface Callbacks {        public boolean setLoadOnResume();        public int getCurrentWorkspaceScreen();        public void startBinding();        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,                              boolean forceAnimateIcons);        public void bindScreens(ArrayList<Long> orderedScreenIds);        public void bindAddScreens(ArrayList<Long> orderedScreenIds);        public void bindFolders(HashMap<Long,FolderInfo> folders);        public void finishBindingItems(boolean upgradePath);        public void bindAppWidget(LauncherAppWidgetInfo info);        public void bindAllApplications(ArrayList<AppInfo> apps);        public void bindAppsAdded(ArrayList<Long> newScreens,                                  ArrayList<ItemInfo> addNotAnimated,                                  ArrayList<ItemInfo> addAnimated,                                  ArrayList<AppInfo> addedApps);        public void bindAppsUpdated(ArrayList<AppInfo> apps);        public void bindComponentsRemoved(ArrayList<String> packageNames,                        ArrayList<AppInfo> appInfos,                        boolean matchPackageNamesOnly);        public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);        public void bindSearchablesChanged();        public boolean isAllAppsButtonRank(int rank);        public void onPageBoundSynchronously(int page);        public void dumpLogsToLocalData();    }

通过方法名我们大概可以猜到每个回调方法的作用,因为Launcher类(Main Activity)实现了该接口,并作为参数传递给了LauncherModel的mCallback,所以bindWorkspace的过程就是通过回调,一步步地通知Launcher类进行相应界面和相应数据的绑定。


bindWorkspace的过程如下:




1.将items分为current和other

ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();ArrayList<LauncherAppWidgetInfo> currentAppWidgets =    new ArrayList<LauncherAppWidgetInfo>();ArrayList<LauncherAppWidgetInfo> otherAppWidgets =    new ArrayList<LauncherAppWidgetInfo>();HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();// Separate the items that are on the current screen, and all the other remaining itemsfilterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,    otherWorkspaceItems);filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,    otherAppWidgets);filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,    otherFolders);sortWorkspaceItemsSpatially(currentWorkspaceItems);sortWorkspaceItemsSpatially(otherWorkspaceItems);

先绑定当前页面的数据,再绑定其他页面,从而提供更好的用户体验。


2.调用startBinding()

    // Tell the workspace that we're about to start binding items    r = new Runnable() {        public void run() {            Callbacks callbacks = tryGetCallbacks(oldCallbacks);            if (callbacks != null) {                callbacks.startBinding();            }        }    };    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
完成绑定前的初始化工作。


3.调用bindScreens()

    final Runnable r = new Runnable() {        @Override        public void run() {            Callbacks callbacks = tryGetCallbacks(oldCallbacks);            if (callbacks != null) {                callbacks.bindScreens(orderedScreens);            }        }    };    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
完成screen的初始化。


4.加载当前页面的item

    bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,            currentFolders, null);


5.加载其他页面的item

bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,                    (isLoadingSynchronously ? mDeferredBindRunnables : null));


6.调用finishBindingItems()
    // Tell the workspace that we're done binding items    r = new Runnable() {        public void run() {            Callbacks callbacks = tryGetCallbacks(oldCallbacks);            if (callbacks != null) {                callbacks.finishBindingItems(isUpgradePath);            }            mIsLoadingAndBindingWorkspace = false;        }    };    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);


以上是Workspace数据绑定的步骤,Launcher类通过回调在每个步骤中完成相应的UI渲染工作。

0 0
原创粉丝点击