当前位置:   article > 正文

android8.1.0中launcher3开发(2)_android8.1 launcher3负一层的开发

android8.1 launcher3负一层的开发

同时制作了下载的资源,资源是按照我我这篇文章的内容针对原生的launcher3代码做了大部分修改,横竖屏都有适配,我自己使用时直接删除了hotseat相关的部分,不过下载资源中做了保留以满足有需要的朋友,下载资源地址:https://download.csdn.net/download/weixue9/12691528

前篇文章已经能够让我们能够成功打包apk了,本人在做自定义修改的时候也借鉴了一些前辈的佳作,在文章结尾处有链接感兴趣的可以自行查看,这篇文章大部分内容与链接中的内容有重复,由于launcher3的开发时公司项目所以不便详细说明,只能总结一些大家熟知的,不过这篇文章的内容也足够满足我们初始的定制化目标,更为个性化的内容需自行摸索了,这篇主要是介绍如何修改以满足我们的需求,具体如下:

1、去掉第一屏google搜索框

修改位置

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\Workspace.java

注释 bindAndInitFirstWorkspaceScreen() 中 if (qsb == null) 开始到结尾
 

  1. public void bindAndInitFirstWorkspaceScreen(View qsb) {
  2. if (!FeatureFlags.QSB_ON_FIRST_SCREEN) {
  3. return;
  4. }
  5. // Add the first page
  6. CellLayout firstPage = insertNewWorkspaceScreen(Workspace.FIRST_SCREEN_ID, 0);
  7. if (FeatureFlags.PULLDOWN_SEARCH) {
  8. .....
  9. }
  10. //add don't show google quick search box[qsb]
  11. // Always add a QSB on the first screen.
  12. /*if (qsb == null) {
  13. // In transposed layout, we add the QSB in the Grid. As workspace does not touch the
  14. // edges, we do not need a full width QSB.
  15. qsb = LayoutInflater.from(getContext())
  16. .inflate(R.layout.search_container_workspace,firstPage, false);
  17. }
  18. CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, firstPage.getCountX(), 1);
  19. lp.canReorder = false;
  20. if (!firstPage.addViewToCellLayout(qsb, 0, R.id.search_container_workspace, lp, true)) {
  21. Log.e(TAG, "Failed to add to item at (0, 0) to CellLayout");
  22. }*/
  23. //add don't show google quick search box[qsb]
  24. }

2、去掉上滑后显示所有 app 界面搜索应用栏

修改位置

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\allapps\AllAppsContainerView.java

在 onFinishInflate() 中添加一行 mSearchContainer.setVisibility(View.GONE);

  1. @Override
  2. protected void onFinishInflate() {
  3. super.onFinishInflate();
  4. ....
  5. mSearchContainer = findViewById(R.id.search_container_all_apps);
  6. mSearchUiManager = (SearchUiManager) mSearchContainer;
  7. mSearchUiManager.initialize(mApps, mAppsRecyclerView);
  8. /// add this code don't show all app quick search box
  9. mSearchContainer.setVisibility(View.GONE);
  10. ......
  11. }

3、隐藏hotseat中添加老版本的所有应用图标,并去除上滑箭头和屏蔽 Drag 动画

修改位置

  • (1)、 添加所有 app 按钮

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\Hotseat.java

在 resetLayout() 中将 FeatureFlags.NO_ALL_APPS_ICON 写成 true 则默认加载六个点按钮

  1. void resetLayout() {
  2. mContent.removeAllViewsInLayout();
  3. //change true default add all app button
  4. if (/*!FeatureFlags.NO_ALL_APPS_ICON*/true) {
  5. // Add the Apps button
  6. Context context = getContext();
  7. DeviceProfile grid = mLauncher.getDeviceProfile();
  8. int allAppsButtonRank = grid.inv.getAllAppsButtonRank();
  9. LayoutInflater inflater = LayoutInflater.from(context);
  10. TextView allAppsButton = (TextView)
  11. inflater.inflate(R.layout.all_apps_button, mContent, false);
  12. Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
  13. d.setBounds(0, 0, grid.iconSizePx, grid.iconSizePx);
  14. ...
  15. }
  16. }
  • (2)、去除上滑箭头

vendor\mediatek\proprietary\packages\apps\Launcher3\res\layout-land\launcher.xml

给 PageIndicatorCaretLandscape 控件添加 gone 属性,注意我此处修改的是横屏的版本,若你是竖屏则修改

layout 文件夹下 launcher.xml

  1. <com.android.launcher3.pageindicators.PageIndicatorCaretLandscape
  2. android:id="@+id/page_indicator"
  3. android:theme="@style/HomeScreenElementTheme"
  4. android:layout_width="@dimen/dynamic_grid_min_page_indicator_size"
  5. android:layout_height="@dimen/dynamic_grid_min_page_indicator_size"
  6. android:layout_gravity="bottom|left"
  7. android:visibility="gone"/>

此处添加完成后小箭头确实不显示了,但是当你在 workspace 长按时,小箭头又出现了,坑爹的找了半天,最终终

于搞定了。

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\pageindicators\PageIndicatorCaretLandscape.java

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. //annotaion for longclick don't show up arrow
  4. /*Rect drawableBounds = getCaretDrawable().getBounds();
  5. int count = canvas.save();
  6. canvas.translate((getWidth() - drawableBounds.width()) / 2,
  7. getHeight() - drawableBounds.height());
  8. getCaretDrawable().draw(canvas);
  9. canvas.restoreToCount(count);*/
  10. }

注释 onDraw() 的方法体就好了,因为当你长按 workSpace 时,触发长按事件,最终调用了 Launcher

中的 showOverviewMode(),通过 mWorkspace.setVisibility(View.VISIBLE);

PageIndicatorCaretLandscape 和 Workspace 是绑定的,导致 onDraw 执行,重新绘制。

  • (3)、屏蔽 Drag 动画

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\allapps\AllAppsTransitionController.java

  1. @Override
  2. public boolean onDrag(float displacement, float velocity) {
  3. Log.e("Launcher3", "onDrag()");
  4. if (true) return true;
  5. //add for forbidden workspace drag change GradientView alph
  6. if (mAppsView == null) {
  7. return false; // early termination.
  8. }
  9. mContainerVelocity = velocity;
  10. float shift = Math.min(Math.max(0, mShiftStart + displacement), mShiftRange);
  11. setProgress(shift / mShiftRange);
  12. return true;
  13. }
  14. @Override
  15. public void onDragEnd(float velocity, boolean fling) {
  16. Log.e("Launcher3", "onDragEnd()");
  17. //add for forbidden workspace drag change GradientView alph
  18. if (true){
  19. if (velocity < 0) {
  20. mLauncher.showWorkspace(true);
  21. }
  22. return;
  23. }
  24. //add for forbidden workspace drag change GradientView alph
  25. if (mAppsView == null) {
  26. return; // early termination.
  27. }
  28. final int containerType = mTouchEventStartedOnHotseat
  29. ? ContainerType.HOTSEAT : ContainerType.WORKSPACE;
  30. if (fling) {
  31. if (velocity < 0) {
  32. calculateDuration(velocity, mAppsView.getTranslationY());
  33. if (!mLauncher.isAllAppsVisible()) {
  34. mLauncher.getUserEventDispatcher().logActionOnContainer(
  35. Action.Touch.FLING,
  36. Action.Direction.UP,
  37. containerType);
  38. }
  39. mLauncher.showAppsView(true /* animated */, false /* updatePredictedApps */);
  40. if (hasSpringAnimationHandler()) {
  41. mSpringAnimationHandler.add(mSearchSpring, true /* setDefaultValues */);
  42. // The icons are moving upwards, so we go to 0 from 1. (y-axis 1 is below 0.)
  43. mSpringAnimationHandler.animateToFinalPosition(0 /* pos */, 1 /* startValue */);
  44. }
  45. } else {
  46. calculateDuration(velocity, Math.abs(mShiftRange - mAppsView.getTranslationY()));
  47. mLauncher.showWorkspace(true);
  48. }
  49. // snap to top or bottom using the release velocity
  50. }
  51. .....
  52. }

分别在 onDrag() 和 onDragEnd() 回调方法中直接 return,需要注意的是在 onDragEnd 中需要将

workSpace 的状态复原,不然下一次点击所有APP 按钮时,会出现白屏现象。

4、workSpace 中应用名称文字显示不全或不显示的问题

(1)、显示不全修改如下:

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\BubbleTextView.java

  1. public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
  2. super(context, attrs, defStyle);
  3. mLauncher = Launcher.getLauncher(context);
  4. DeviceProfile grid = mLauncher.getDeviceProfile();
  5. mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
  6. TypedArray a = context.obtainStyledAttributes(attrs,
  7. R.styleable.BubbleTextView, defStyle, 0);
  8. mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
  9. mDeferShadowGenerationOnTouch =
  10. a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
  11. int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
  12. int defaultIconSize = grid.iconSizePx;
  13. if (display == DISPLAY_WORKSPACE) {
  14. setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
  15. //cczheng add -11 for text show half
  16. setCompoundDrawablePadding(grid.iconDrawablePaddingPx - 11);
  17. } else if (display == DISPLAY_ALL_APPS) {
  18. setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
  19. setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
  20. defaultIconSize = grid.allAppsIconSizePx;
  21. } else if (display == DISPLAY_FOLDER) {
  22. setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
  23. setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
  24. defaultIconSize = grid.folderChildIconSizePx;
  25. }
  26. .......

setCompoundDrawablePadding()方法 设置图片和 text 之间的间距 ,我们的问题是间距太大导致的,所以在 DISPLAY_WORKSPACE 语句块中,将间距减少 11,同样的当两个图标放置到一块成为文件夹时,对应修改的地方如下,而不是上面的 DISPLAY_FOLDER 语句块。

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\folder\FolderIcon.java

  1. public static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
  2. FolderInfo folderInfo) {
  3. @SuppressWarnings("all") // suppress dead code warning
  4. final boolean error = INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION;
  5. if (error) {
  6. throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
  7. "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
  8. "is dependent on this");
  9. }
  10. DeviceProfile grid = launcher.getDeviceProfile();
  11. FolderIcon icon = (FolderIcon) LayoutInflater.from(group.getContext())
  12. .inflate(resId, group, false);
  13. icon.setClipToPadding(false);
  14. icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
  15. icon.mFolderName.setText(folderInfo.title);
  16. icon.mFolderName.setCompoundDrawablePadding(0);
  17. FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) icon.mFolderName.getLayoutParams();
  18. //cczheng add -11 for folder text show half
  19. lp.topMargin = grid.iconSizePx + grid.iconDrawablePaddingPx - 11;
  20. icon.setTag(folderInfo);
  21. icon.setOnClickListener(launcher);
  22. icon.mInfo = folderInfo;
  23. icon.mLauncher = launcher;
  24. ....

mFolderName 的 setCompoundDrawablePadding 已经是 0, 下面又增加了单独的 marginTop ,同理减去 11.

(2)、不显示的问题

应用名称的的显示主要是通过下面文件中的如下方法的返回值进行控制的,所以这个可以自行灵活控制

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\DeviceProfile.java

  1. /**
  2. * When {@code true}, the device is in landscape mode and the hotseat is on the right column.
  3. * When {@code false}, either device is in portrait mode or the device is in landscape mode and
  4. * the hotseat is on the bottom row.
  5. */
  6. public boolean isVerticalBarLayout() {
  7. return isLandscape && transposeLayoutWithOrientation;
  8. }

5、去掉抽屉式的打开方式

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\model\LoaderTask.java

首先在run方法中添加一个loadAllApplications()方法的调用,主要是循环遍历LauncherActivityInfo并通过 LauncherModel的addAndBindAddedWorkspaceItems()方法添加到桌面上,其中主要涉及的知识就是循环遍历,代码如下:

  1. private void loadAllApplications() {
  2. final Context context = mApp.getContext();
  3. final List<UserHandle> profiles = mUserManager.getUserProfiles();
  4. for (UserHandle user : profiles) {
  5. final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
  6. ArrayList<PendingInstallShortcutInfo> added = new ArrayList<PendingInstallShortcutInfo>();
  7. synchronized (this) {
  8. for (LauncherActivityInfo app : apps) {
  9. PendingInstallShortcutInfo pendingInstallShortcutInfo = new PendingInstallShortcutInfo(app,context);
  10. added.add(pendingInstallShortcutInfo);
  11. }
  12. }
  13. if (!added.isEmpty()) {
  14. mLauncherModel.addAndBindAddedWorkspaceItems(new LazyShortcutsProvider(context.getApplicationContext(), added));
  15. }
  16. }
  17. }

当时在加上这段代码之后,桌面的iocn还是一直加载不出来,检查了很多遍代码之后,苦思不得其解,只好加log将加载流程一个个打印看看哪里出了问题。接下来修改

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\model\BaseModelUpdateTask.java

的run方法中代码,修改如下:

  1. @Override
  2. public final void run() {
  3. if (!mModel.isModelLoaded()) {
  4. if (DEBUG_TASKS) {
  5. Log.d(TAG, "Ignoring model task since loader is pending=" + this);
  6. }
  7. // Loader has not yet run.
  8. // return;
  9. }
  10. execute(mApp, mDataModel, mAllAppsList);
  11. }

6、禁止 Workspace 图标长按删除选项

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\DeleteDropTarget.java

在 setTextBasedOnDragSource() 、setControlTypeBasedOnDragSource()、onAccessibilityDrop() 中分别增加判断是否需要删除图标

方法一、直接将 supportsDrop() 返回 false

  1. @Override
  2. protected boolean supportsDrop(DragSource source, ItemInfo info) {
  3. //return true;
  4. return false;// change false
  5. }

7、安装新应用时,要把图标加载到workspace中

vendor\mediatek\proprietary\packages\apps\Launcher3\src\com\android\launcher3\model\PackageUpdatedTask.java

不过要注意加在added这个ArrayList之前被clear之前

  1. addedOrModified.addAll(appsList.added);
  2. //add by gaoshifeng start
  3. final List<UserHandle> profiles = UserManagerCompat.getInstance(context).getUserProfiles();
  4. ArrayList<PendingInstallShortcutInfo> added = new ArrayList<PendingInstallShortcutInfo>();
  5. for (UserHandle user : profiles) {
  6. final List<LauncherActivityInfo> apps = LauncherAppsCompat.getInstance(context).getActivityList(null, user);
  7. synchronized (this) {
  8. for (LauncherActivityInfo info : apps) {
  9. for (AppInfo appInfo : appsList.added) {
  10. if(info.getComponentName().equalsappInfo.componentName)){
  11. PendingInstallShortcutInfomPendingInstallShortcutInfo= new PendingInstallShortcutInfo(info,context);
  12. added.add(mPendingInstallShortcutInfo);
  13. }
  14. }
  15. }
  16. }
  17. }
  18. if (!added.isEmpty()) {
  19. app.getModel().addAndBindAddedWorkspaceItems(new LazyShortcutsProvider(context.getApplicationContext(), added));
  20. }
  21. //add by gaoshifeng end
  22. appsList.added.clear();

以上基本上就能完成launcher3的初步自定义了,其他的一些细节的处理请自行debug,我在此基础上做了其他的自定义修改就不一一写出,有什么不妥之处请见谅,以上内容主要借鉴如下几位博主的文章:

https://blog.csdn.net/u012932409/article/details/102941114
https://blog.csdn.net/u012932409/article/details/93221912
https://blog.csdn.net/illailla/article/details/80972830

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/296549
推荐阅读
  

闽ICP备14008679号