Android_Q_Launcher調整為橫向滑動
以下內容為原創,其中有參考部分同行的blog,
需求說明:
1.Android Q Launcher去掉抽屜功能,屏蔽從下往上的滑動功能;
2.將系統中apk及新安裝的apk全部顯示在workspace中;
3.workspace中,不可卸載的應用長按滑動后,只能有cancel功能,可卸載應用,長按滑動后,有卸載功能;
4.Launcher橫屏顯示時,屏蔽從下往上的滑動功能;
需求分解:備注:以下相關代碼注釋請搜索“by lj”
1.將 AllAppsContainerView 中的圖示加載到 Workspace,需要在LoaderTask中,將所有APP都Load并且通過addAndBindAddedWorkspaceItems,添加顯示到workspace,packages\apps\Launcher3\src\com\android\launcher3\model\LoaderTask.java檔案中,在run()方法中,
// second step
TraceHelper.partitionSection(TAG, "step 2.1: loading all apps");
List<LauncherActivityInfo> allActivityList = loadAllApps();
/*add start by lj on 20200530*/
verifyApplications();
/*add end by lj on 20200530*/
需要添加的方法如下:
/*add start by lj on 20200530*/
private void verifyApplications() {
final Context context = mApp.getContext();
ArrayList<Pair<ItemInfo, Object>> installQueue = new ArrayList<>();
final List<UserHandle> profiles = mUserManager.getUserProfiles();
for (UserHandle user : profiles) {
final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
ArrayList<InstallShortcutReceiver.PendingInstallShortcutInfo> added =
new ArrayList<InstallShortcutReceiver.PendingInstallShortcutInfo>();
synchronized (this) {
for (LauncherActivityInfo app : apps) {
InstallShortcutReceiver.PendingInstallShortcutInfo pendingInstallShortcutInfo =
new InstallShortcutReceiver.PendingInstallShortcutInfo(app, context);
added.add(pendingInstallShortcutInfo);
installQueue.add(pendingInstallShortcutInfo.getItemInfo());
}
}
if (!added.isEmpty()) {
mApp.getModel().addAndBindAddedWorkspaceItems(installQueue);
}
}
}
/*add end by lj on 20200530*/
2.packages\apps\Launcher3\src\com\android\launcher3\InstallShortcutReceiver.java檔案中,將PendingInstallShortcutInfo方法從private調整為public;
原先代碼需要判定后,再決定是否需要重新load app串列,packages\apps\Launcher3\src\com\android\launcher3\model\BaseModelUpdateTask.java檔案中,run()方法,將return陳述句屏蔽,保證每次都能執行execte方法,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.
//return; /*delete by lj on 20200530*/
}
execute(mApp, mDataModel, mAllAppsList);
}
3.新安裝的 app 自動添加圖示到 Workspace,packages\apps\Launcher3\src\com\android\launcher3\model\PackageUpdatedTask.java檔案中,execute方法添加內容如下:
final ArrayList<AppInfo> addedOrModified = new ArrayList<>();
addedOrModified.addAll(appsList.added);
/*add start by lj on 20200530*/
updateToWorkSpace(context, app, appsList);
/*add end by lj on 20200530*/
appsList.added.clear();
addedOrModified.addAll(appsList.modified);
appsList.modified.clear();
需要添加的方法如下:
/*add start by lj on 20200530*/
public void updateToWorkSpace(Context context, LauncherAppState app , AllAppsList appsList) {
ArrayList<Pair<ItemInfo, Object>> installQueue = new ArrayList<>();
final List<UserHandle> profiles = UserManagerCompat.getInstance(context).getUserProfiles();
ArrayList<InstallShortcutReceiver.PendingInstallShortcutInfo> added =
new ArrayList<InstallShortcutReceiver.PendingInstallShortcutInfo>();
for (UserHandle user : profiles) {
final List<LauncherActivityInfo> apps = LauncherAppsCompat.getInstance(context).getActivityList(null, user);
synchronized (this) {
for (LauncherActivityInfo info : apps) {
for (AppInfo appInfo : appsList.added) {
if (info.getComponentName().equals(appInfo.componentName)) {
InstallShortcutReceiver.PendingInstallShortcutInfo mPendingInstallShortcutInfo =
new InstallShortcutReceiver.PendingInstallShortcutInfo(info,context);
added.add(mPendingInstallShortcutInfo);
installQueue.add(mPendingInstallShortcutInfo.getItemInfo());
}
}
}
}
}
if (!added.isEmpty()) {
app.getModel().addAndBindAddedWorkspaceItems(installQueue);
}
}
/*add end by lj on 20200530*/
4.Workspace 圖示長按洗掉選項調整為取消,packages\apps\Launcher3\src\com\android\launcher3\DeleteDropTarget.java檔案中,setTextBasedOnDragSource方法中mText,在非可卸載應用使用時,顯示為取消,
/*add start by lj on 20200530*/
mText = getResources().getString(isCanDrop(item)
? R.string.remove_drop_target_label
: android.R.string.cancel);
/*add end by lj on 20200530*/
需要添加的方法如下:
/*add start by lj on 20200530*/
private boolean isCanDrop(ItemInfo item) {
return !(item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER);
}
/*add end by lj on 20200530*/
5.在workspace拖拽icon結束后,增加判斷,是否取消當前拖拽操作,packages\apps\Launcher3\src\com\android\launcher3\dragndrop\DragController.java檔案中,drop()方法,
// Drop onto the target.
boolean accepted = false;
if (dropTarget != null) {
dropTarget.onDragExit(mDragObject);
if (dropTarget.acceptDrop(mDragObject)) {
if (flingAnimation != null) {
flingAnimation.run();
} else {
dropTarget.onDrop(mDragObject, mOptions);
}
accepted = true;
/*add start by lj on 20200530*/
if (dropTarget instanceof DeleteDropTarget && isNeedCancelDrag(mDragObject.dragInfo)) {
cancelDrag();
}
/*add end by lj on 20200530*/
}
}
需要添加的方法如下:
/*add start by lj on 20200530*/
private boolean isNeedCancelDrag(ItemInfo item) {
return (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER);
}
/*add end by lj on 20200530*/
6.屏蔽豎屏狀態下,從下往上的抽屜滑動效果,packages\apps\Launcher3\quickstep\src\com\android\launcher3\uioverrides\touchcontrollers\PortraitStatesTouchController.java檔案中,canInterceptTouch方法,
else if (mLauncher.isInState(OVERVIEW)) {
if (!mOverviewPortraitStateTouchHelper.canInterceptTouch(ev)) {
return false;
}
/*add start by lj on 20200530*/
return false;
/*add end by lj on 20200530*/
} else {
// If we are swiping to all apps instead of overview, allow it from anywhere.
boolean interceptAnywhere = mLauncher.isInState(NORMAL) && !mAllowDragToOverview;
// For all other states, only listen if the event originated below the hotseat height
if (!interceptAnywhere && !isTouchOverHotseat(mLauncher, ev)) {
return false;
}
/*add start by lj on 20200530*/
return false;
/*add end by lj on 20200530*/
}
7.屏蔽橫屏狀態下,從下往上的抽屜滑動效果,packages\apps\launcher3\quickstep\recents_ui_overrides\src\com\android\launcher3\uioverrides\touchcontrollers\OverviewToAllAppsTouchController.java檔案中,canInterceptTouch方法,
if (mLauncher.isInState(ALL_APPS)) {
/*add start by lj on 20200530*/
if (true) {
return false;
}
/*add end by lj on 20200530*/
// In all-apps only listen if the container cannot scroll itself
return mLauncher.getAppsView().shouldContainerScroll(ev);
} else if (mLauncher.isInState(NORMAL)) {
/*add start by lj on 20200530*/
if (true) {
return false;
}
/*add end by lj on 20200530*/
return (ev.getEdgeFlags() & Utilities.EDGE_NAV_BAR) == 0;
} else if (mLauncher.isInState(OVERVIEW)) {
/*add start by lj on 20200530*/
if (true) {
return false;
}
/*add end by lj on 20200530*/
RecentsView rv = mLauncher.getOverviewPanel();
return ev.getY() > (rv.getBottom() - rv.getPaddingBottom());
} else {
return false;
}
packages\apps\launcher3\src\com\android\launcher3\views\ScrimView.java檔案中,updateDragHandleVisibility方法,
//boolean visible = mLauncher.getDeviceProfile().isVerticalBarLayout() || mAM.isEnabled();/*delete by lj on 20200530*/
/*add start by lj on 20200530*/
boolean visible = false;
/*add start by lj on 20200530*/
boolean wasVisible = mDragHandle != null;
完成以上代碼的修改后,即可達到上述期望的效果,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/76433.html
標籤:其他
上一篇:Windows10 安裝 Android Studio 并運行一個專案
下一篇:Flutter安裝
