需求:所有应用在workspaces上展示
// Feature to control launcher drawer
public static final boolean REMOVE_DRAWER = true;
2.1 加载所有安装应用到workspace
// second step
if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
loadAllApps();
// add
if (FeatureFlags.REMOVE_DRAWER && (mBgAllAppsList.data.size() > 0)) {
ArrayList<AppInfo> appInfos = new ArrayList<AppInfo>(mBgAllAppsList.data);
mApp.getModel().addAndBindAddedWorkspaceItems(new LzyProvider(appInfos));
}
// end
if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Binding all apps");
// 添加自定义Provider 参考InstallShortcutReceiver中provider写法
// add
private static class LzyProvider extends Provider<List<Pair<ItemInfo, Object>>> {
private ArrayList<AppInfo> mItems;
public LzyProvider(ArrayList<AppInfo> items) {
mItems = items;
}
@Override
public ArrayList<Pair<ItemInfo, Object>> get() {
ArrayList<Pair<ItemInfo, Object>> installQueue = new ArrayList<>();
for (AppInfo info : mItems ) {
ShortcutInfo si = info.makeShortcut();
installQueue.add(Pair.create((ItemInfo) si, null));
}
return installQueue;
}
}
// end
2.2 有app变化时,更新workspace
final ArrayList<AppInfo> addedOrModified = new ArrayList<>();
addedOrModified.addAll(appsList.added);
appsList.added.clear();
addedOrModified.addAll(appsList.modified);
appsList.modified.clear();
// add 在此处按照LoaderTask.java中添加
@Override
public final void run() {
if (!mModel.isModelLoaded()) {
if (DEBUG_TASKS) {
Log.d(TAG, "Ignoring model task since loader is pending=" + this);
}
// Loader has not yet run.
// add
if (!FeatureFlags.REMOVE_DRAWER) {
return;
}
// end
}
execute(mApp, mDataModel, mAllAppsList);
}
注释掉下面这段代码
if(!FeatureFlags.REMOVE_DRAWER){
// Always add a QSB on the first screen.
if (qsb == null) {
// In transposed layout, we add the QSB in the Grid. As workspace does not touch the
// edges, we do not need a full width QSB.
qsb = LayoutInflater.from(getContext())
.inflate(R.layout.search_container_workspace,firstPage, false);
}
CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, firstPage.getCountX(), 1);
lp.canReorder = false;
if (!firstPage.addViewToCellLayout(qsb, 0, R.id.search_container_workspace, lp, true)) {
Log.e(TAG, "Failed to add to item at (0, 0) to CellLayout");
}
}
// add
if (FeatureFlags.REMOVE_DRAWER) {
lp.height = 0;
}
// end
hotseat.setLayoutParams(lp);
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mAllAppsHandle = (ImageView) findViewById(R.id.all_apps_handle);
mAllAppsHandle.setVisibility(View.GONE); // add for hide allapp button
...
}
隐藏了allapp按钮,此时在屏幕上滑还是可以展示allapp
@Override
public boolean onDrag(float displacement, float velocity) {
if (mAppsView == null) {
return false; // early termination.
}
// add by
if (FeatureFlags.REMOVE_DRAWER) {
return false;
}
// end
mContainerVelocity = velocity;
float shift = Math.min(Math.max(0, mShiftStart + displacement), mShiftRange);
setProgress(shift / mShiftRange); // 滑动时设置allapp从底部显示一点
}
@Override
public void onDragEnd(float velocity, boolean fling) {
if (mAppsView == null) {
return; // early termination.
}
// add
if (FeatureFlags.REMOVE_DRAWER) {
return;
}
// end
final int containerType = mTouchEventStartedOnHotseat
? ContainerType.HOTSEAT : ContainerType.WORKSPACE;
if (fling) {
...
mLauncher.showAppsView(true /* animated */, false /* updatePredictedApps */);
...
}
}
手指在屏幕移动,onDrag方法会先设置allapp显示出来一点,在onDragEnd方法中判断if(fling)是滑动,就会调用showAppsView()方法,和点击allapp走一样的流程
修改之后看效果,发现应用icon都是在第二页中显示,除了默认的几个icon在第一页
int screenCount = workspaceScreens.size();
// First check the preferred screen.
// add by
// int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
int preferredScreenIndex = 0;
//end
if (preferredScreenIndex < screenCount) {
所有应用可以再第一页显示,但是发现少了几个应用的icon,开始没注意到是hotseat中的几个应用,后面抓log发现,loadworkspace时会做判断,dw_phone_hotseat.xml中标记-101,不会添加,具体代码在 LoadCursor中
注释掉dw_phone_hotseat.xml中的代码
// add
//numRows = closestProfile.numRows;
//numColumns = closestProfile.numColumns;
numRows = 4;
numColumns = 3;
// end
...
if (shouldShowDiscoveryBounce()) {
//注释 begin
// mAllAppsController.showDiscoveryBounce();
//注释 end
}
if (mLauncherCallbacks != null) {
mLauncherCallbacks.onResume();
}
...
参考博文:Android8.1 Launcher3 去掉抽屉