Launcher移植clearall按钮及显示内存信息

238 阅读5分钟

需要修改的代码块:

modified:   go/quickstep/res/layout/overview_actions_container.xml
        modified:   go/quickstep/src/com/android/quickstep/views/GoOverviewActionsView.java
        modified:   quickstep/res/layout/overview_actions_container.xml
        modified:   quickstep/res/layout/overview_clear_all_button.xml
        modified:   quickstep/res/values-zh-rCN/strings.xml
        modified:   quickstep/res/values/colors.xml
        modified:   quickstep/res/values/dimens.xml
        modified:   quickstep/res/values/strings.xml
        modified:   quickstep/src/com/android/launcher3/uioverrides/touchcontrollers/TaskViewTouchController.java
        modified:   quickstep/src/com/android/quickstep/views/OverviewActionsView.java
        modified:   quickstep/src/com/android/quickstep/views/RecentsView.java
        modified:   src/com/android/launcher3/CellLayout.java
        modified:   src/com/android/launcher3/PagedView.java
        quickstep/res/drawable/clear_all_button_ext.xml
        quickstep/src/com/android/quickstep/views/MeminfoHelper.java

一、首先先对于添加用于内存显示的类方法:

package com.android.quickstep.views;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.format.Formatter;
import android.widget.Toast;
import com.android.launcher3.R;
import com.android.launcher3.util.MainThreadInitializedObject;
import com.sprd.ext.LogUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.FileReader;
public class MeminfoHelper {
    private static final String TAG="MeminfoHelper";
    private static final long INVALID_MEM_SIZE=-1L;
    private static final long   PROCESS_REMOVETASKS_DELAY_MS=200L;
    private static final String DEFAULT_CONFIG="unconfig";
    private static final MainThreadInitializedObject<MeminfoHelper> INSTANCE =
            new MainThreadInitializedObject<>(MeminfoHelper::new);
    private long mTotalMem=INVALID_MEM_SIZE;
    private long mAvailMem=INVALID_MEM_SIZE;
    private static final  int SI_KUNIT=1000;
    private static final int KUINT=1024;
    private MeminfoHelper(Context context){

    }
    public static MeminfoHelper getInstance(final Context context){
        return INSTANCE.get(context.getApplicationContext());
    }
    void showReleaseMemoryToast(Context context,boolean isRemoveView){
        long availSizeOriginal=mAvailMem!=INVALID_MEM_SIZE ? mAvailMem :getSystemAvailMemory(context);
        new Handler(Looper.getMainLooper()).postDelayed(()->{
            mAvailMem=getSystemAvailMemory(context);
            long releaseMem=isRemoveView ? mAvailMem-availSizeOriginal :0;
            if (releaseMem <= 0){
                android.widget.Toast.makeText(context, context.getString(R.string.recents_nothing_to_clear), Toast.LENGTH_SHORT).show();
            }else {
                String release=Formatter.formatShortFileSize(context,releaseMem);
                String avail=Formatter.formatShortFileSize(context,mAvailMem);
                 android.widget.Toast.makeText(context,
                       context.getString(R.string.recents_clean_finished_toast,
                                release.toUpperCase(), avail.toUpperCase()), Toast.LENGTH_SHORT)
                        .show();
            }
        },PROCESS_REMOVETASKS_DELAY_MS);
    }
    void updateTotalMemory(){
        mTotalMem=getSystemTotalMemory();
    }
    void updateAvailMemory(Context context){
        mAvailMem=getSystemAvailMemory(context);
    }
    String getTotalMemString(Context context){
        if (mTotalMem==INVALID_MEM_SIZE){
            updateTotalMemory();
        }
        String totalMemory=Formatter.formatShortFileSize(context,mTotalMem);
        if (totalMemory.contains(".")){
            String total=totalMemory.split("\\.")[0];
            String size=totalMemory.split(" ")[1];
            totalMemory=total+".0"+size;
            android.util.Log.d(TAG, "getTotalMemString: total="+total);
            android.util.Log.d(TAG, "getTotalMemString: size="+size);
            android.util.Log.d(TAG, "getTotalMemString: totalMemory="+totalMemory);
        }
        return totalMemory;
    }
    String getAvailMemString(Context context){
        if (mAvailMem==INVALID_MEM_SIZE){
            updateAvailMemory(context);
        }
        return Formatter.formatShortFileSize(context,mAvailMem);
    }
    private long getSystemAvailMemory(Context context){
        ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memoryInfo=new ActivityManager.MemoryInfo();
        if (activityManager!=null){
            activityManager.getMemoryInfo(memoryInfo);
        }
        android.util.Log.d(TAG, "getSystemAvailMemory: "+memoryInfo.availMem);
        return memoryInfo.availMem;
    }
    private long getSystemTotalMemory(){
        String str1="/proc/meminfo";
        String str2;
        String[] arrayOfString;
        long ramSize=0;
        try {
            FileReader localFileReader=new FileReader(str1);
            BufferedReader localBufferedReader=new BufferedReader(localFileReader,8192);
            str2=localBufferedReader.readLine();
            arrayOfString=str2.split("\\s+");
            int i=Integer.valueOf(arrayOfString[1]).intValue();
            ramSize=new Long((long) i*1024);
            localBufferedReader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        android.util.Log.d(TAG, "getSystemTotalMemory: "+ramSize);
        return ramSize;
    }
    private String getConfig(String key){
        return android.os.SystemProperties.get(key,DEFAULT_CONFIG);
    }
}

二、在OverviewActionsView方法中添加当最近任务有显示的时候的全部清理及其点击提醒事件:

import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
-
+import android.text.TextUtils;
+import android.widget.TextView;
 /**
  * View for showing action buttons in Overview
  */
@@ -109,6 +110,7 @@ public class OverviewActionsView<T extends OverlayUICallbacks> extends FrameLayo
         super(context, attrs, defStyleAttr, 0);
         mMultiValueAlpha = new MultiValueAlpha(this, 5);
         mMultiValueAlpha.setUpdateVisibility(true);
+        MeminfoHelper.getInstance(context).updateTotalMemory();
     }

     @Override
@@ -118,6 +120,8 @@ public class OverviewActionsView<T extends OverlayUICallbacks> extends FrameLayo

         mSplitButton = findViewById(R.id.action_split);
         mSplitButton.setOnClickListener(this);
+        //add by clear all
+        findViewById(R.id.clear_all_button).setOnClickListener(this);
     }

     /**
@@ -131,6 +135,7 @@ public class OverviewActionsView<T extends OverlayUICallbacks> extends FrameLayo

     @Override
     public void onClick(View view) {
+        android.util.Log.d("HYT", "onClick: ");
         if (mCallbacks == null) {
             return;
         }
@@ -140,7 +145,27 @@ public class OverviewActionsView<T extends OverlayUICallbacks> extends FrameLayo
         } else if (id == R.id.action_split) {
             mCallbacks.onSplit();
         }
+        else if (id==R.id.clear_all_button){
+            android.util.Log.d("HYT",
+                    "onClick: clear");
+            mRecentsView.clearAllTasks(mRecentsView);
+        }
+    }
+    private RecentsView mRecentsView;
+    public void setRecentsView(RecentsView recentsView){
+        mRecentsView=recentsView;
     }
+    private void setMemoryinfoText(String avialStr,String totalStr){
+        if (TextUtils.isEmpty(avialStr)||TextUtils.isEmpty(totalStr)){
+            return;
+        }
+        String text = getContext().getString(R.string.recents_memory_avail,avialStr) + " | " + totalStr;
+        android.util.Log.d("HYT", "setMemoryinfoText: "+text);
+        if (text!=null){
+            ((TextView)findViewById(R.id.recents_memoryinfo)).setText(text);
+        }
+    }
+    int lastVisibility =View.INVISIBLE;

     @Override
     protected void onConfigurationChanged(Configuration newConfig) {
@@ -163,6 +188,16 @@ public class OverviewActionsView<T extends OverlayUICallbacks> extends FrameLayo
         }
         boolean isHidden = mHiddenFlags != 0;
         mMultiValueAlpha.getProperty(INDEX_HIDDEN_FLAGS_ALPHA).setValue(isHidden ? 0 : 1);
+        android.util.Log.d("HYT", "updateHiddenFlags: isHidden=="+isHidden);
+        int visibility=getVisibility();
+        if (visibility!=lastVisibility){
+            android.util.Log.d("HYT", "updateHiddenFlags: lastVisibility=="+lastVisibility);
+            MeminfoHelper meminfoHelper=MeminfoHelper.getInstance(mContext);
+            meminfoHelper.updateAvailMemory(getContext());
+            setMemoryinfoText(meminfoHelper.getAvailMemString(getContext()),meminfoHelper.getTotalMemString(getContext()));
+
+        }
+        lastVisibility=visibility;
     }

     /**
@@ -175,11 +210,14 @@ public class OverviewActionsView<T extends OverlayUICallbacks> extends FrameLayo
      */
     public void updateDisabledFlags(@ActionsDisabledFlags int disabledFlags, boolean enable) {
         if (enable) {
+            android.util.Log.d("shortcut", "updateDisabledFlags:if enable===="+enable);
             mDisabledFlags |= disabledFlags;
         } else {
+            android.util.Log.d("shortcut", "updateDisabledFlags:else enable===="+enable);
             mDisabledFlags &= ~disabledFlags;
         }
         boolean isEnabled = (mDisabledFlags & ~DISABLED_ROTATED) == 0;
+        android.util.Log.d("shortcut", "updateDisabledFlags: isEnabled=="+isEnabled);
         LayoutUtils.setViewEnabled(this, isEnabled);
     }

三、将recentview方法进行公开,在点击全部清除时,可以响应到原有的dismissalltasks方法:

--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -881,6 +881,7 @@ public abstract class RecentsView<ACTIVITY_TYPE extends StatefulActivity<STATE_T
         mActionsView = actionsView;
         mActionsView.updateHiddenFlags(HIDDEN_NO_TASKS, getTaskViewCount() == 0);
         mSplitSelectStateController = splitController;
+        mActionsView.setRecentsView(this);
     }

     public SplitSelectStateController getSplitPlaceholder() {
@@ -1207,17 +1208,24 @@ public abstract class RecentsView<ACTIVITY_TYPE extends StatefulActivity<STATE_T
     protected void onPageBeginTransition() {
         super.onPageBeginTransition();
         if (!mActivity.getDeviceProfile().isTablet) {
+            android.util.Log.d("shortcut", "onPageBeginTransition:");
             mActionsView.updateDisabledFlags(OverviewActionsView.DISABLED_SCROLLING, true);
         }
     }

     @Override
     protected void onPageEndTransition() {
+
         super.onPageEndTransition();
-        if (isClearAllHidden() && !mActivity.getDeviceProfile().isTablet) {
+        //isClearAllHidden() &&
+        android.util.Log.d("shortcut", "onPageEndTransition:isClearAllHidden()="+isClearAllHidden());
+        android.util.Log.d("shortcut", "onPageEndTransition:!mActivity.getDeviceProfile().isTablet="+(!mActivity.getDeviceProfile().isTablet));
+        if (!mActivity.getDeviceProfile().isTablet) {
+            android.util.Log.d("shortcut", "onPageEndTransition:isClearAllHidden");
             mActionsView.updateDisabledFlags(OverviewActionsView.DISABLED_SCROLLING, false);
         }
         if (getNextPage() > 0) {
+            android.util.Log.d("shortcut", "onPageEndTransition:getNextPage");
             setSwipeDownShouldLaunchApp(true);
         }
     }
@@ -1460,9 +1469,9 @@ public abstract class RecentsView<ACTIVITY_TYPE extends StatefulActivity<STATE_T
                 taskView.bind(groupTask.task1, mOrientationState);
             }
         }
-        if (!taskGroups.isEmpty()) {
-            addView(mClearAllButton);
-        }
+//        if (!taskGroups.isEmpty()) {
+//            addView(mClearAllButton);
+//        }

         boolean settlingOnNewTask = mNextPage != INVALID_PAGE;
         if (settlingOnNewTask) {
@@ -3459,6 +3468,8 @@ public abstract class RecentsView<ACTIVITY_TYPE extends StatefulActivity<STATE_T
                             ActivityManagerWrapper.getInstance()::removeAllRecentTasks,
                             REMOVE_TASK_WAIT_FOR_APP_STOP_MS);
                     removeTasksViewsAndClearAllButton();
+                    MeminfoHelper.getInstance(getContext()).showReleaseMemoryToast(getContext(),
+                 count > getTaskViewCount());
                     startHome();
                 });
             }
@@ -3506,6 +3517,10 @@ public abstract class RecentsView<ACTIVITY_TYPE extends StatefulActivity<STATE_T
         runDismissAnimation(createAllTasksDismissAnimation(DISMISS_ALL_TASK_DURATION));
         mActivity.getStatsLogManager().logger().log(LAUNCHER_TASK_CLEAR_ALL);
     }
+    public void clearAllTasks(View view){
+        dismissAllTasks(view);
+    }

四、添加全部清除的背景:

+<shape
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle">
+    <solid android:color="@color/clear_all_bg"/>
+    <corners android:radius="@dimen/clear_all_corner_radius"/>
+</shape>

五、添加新的clearall及内存显示信息:

diff --git a/quickstep/res/layout/overview_actions_container.xml b/quickstep/res/layout/overview_actions_container.xml
old mode 100644
new mode 100755
index 0fda0bf8d4..9d24852c1b
--- a/quickstep/res/layout/overview_actions_container.xml
+++ b/quickstep/res/layout/overview_actions_container.xml
@@ -17,14 +17,53 @@
 <com.android.quickstep.views.OverviewActionsView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:layout_gravity="center_horizontal|bottom">
+    android:layout_gravity="center_horizontal|bottom"
+    >
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:orientation="vertical"
+        android:layout_marginBottom="60dp"
+
+        >

+        <TextView
+            android:id="@+id/recents_memoryinfo"
+            android:theme="@style/Theme.AppCompat"
+            android:layout_width="wrap_content"
+            android:layout_height="@dimen/recents_meminfo_height"
+            android:layout_gravity="bottom|center_horizontal"
+            android:gravity="center"
+            android:singleLine="true"
+            android:text="11111"
+            android:textColor="?attr/workspaceTextColor"
+            android:textSize="@dimen/recents_meminfo_text_size"/>
+
+        <Space
+            android:layout_width="3dp"
+            android:layout_height="3dp"/>
+
+        <Button
+            android:id="@+id/clear_all_button"
+            style="@android:style/Widget.DeviceDefault.Button.Borderless"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center"
+            android:background="@drawable/clear_all_button_ext"
+            android:minHeight="@dimen/recents_clearall_height"
+            android:paddingHorizontal="@dimen/clear_all_horizontal_paddding"
+            android:text="@string/recents_clear_all"
+            android:textColor="?attr/workspaceTextColor"
+            android:textSize="@dimen/recents_clearall_text_size" />
+    </LinearLayout>
     <LinearLayout
         android:id="@+id/action_buttons"
         android:layout_width="match_parent"
         android:layout_height="@dimen/overview_actions_height"
         android:layout_gravity="bottom|center_horizontal"
-        android:orientation="horizontal">
+        android:visibility="gone"
+        >

六、增加相应的字符等资源显示

+++ b/alps/packages/apps/Launcher3/quickstep/res/values-zh-rCN/strings.xml
@@ -47,4 +47,10 @@
     <string name="action_share" msgid="2648470652637092375">"分享"</string>
     <string name="action_screenshot" msgid="8171125848358142917">"屏幕截图"</string>
     <string name="blocked_by_policy" msgid="2071401072261365546">"该应用或您所在的单位不允许执行此操作"</string>
+
+    <!-- cczheng add for clear task start -->
+    <string name="recents_memory_avail"><xliff:g id="count">%1$s</xliff:g> 可用</string>
+    <string name="recents_clean_finished_toast">释放了 <xliff:g id="count">%1$s</xliff:g> 内存, 现在可用 <xliff:g id="count">%2$s</xliff:g></string>
+    <string name="recents_nothing_to_clear">已经清理至最佳状态</string>
+    <!-- cczheng add for clear task end -->
 </resources>
 
 packages/apps/Launcher3/quickstep/res/values/strings.xml
 +    <!-- cczheng add for clear task start -->
+    <string name="recents_memory_avail"><xliff:g id="count">%1$s</xliff:g> available</string>
+    <string name="recents_clean_finished_toast">Release <xliff:g id="count">%1$s</xliff:g> memory, <xliff:g id="count">%2$s</xliff:g> available now.</string>
+    <string name="recents_nothing_to_clear">Nothing to clear in cache</string>
+     <!-- cczheng add for clear task end -->

+++ b/alps/packages/apps/Launcher3/quickstep/res/values/colors.xml
@@ -16,4 +16,7 @@
 <resources>
     <color name="back_arrow_color_light">#FFFFFFFF</color>
     <color name="back_arrow_color_dark">#99000000</color>
+
+    <color name="recent_lock_icon_bgcolor">#55000000</color>
+    <color name="clear_all_bg">#80808080</color>
 </resources>

+++ b/alps/packages/apps/Launcher3/quickstep/res/values/dimens.xml
@@ -98,4 +98,16 @@
     <dimen name="swipe_edu_circle_size">64dp</dimen>
     <dimen name="swipe_edu_width">80dp</dimen>
     <dimen name="swipe_edu_max_height">184dp</dimen>
+
+
+    <!-- new feature of quick cleaning. -->
+    <dimen name="task_lock_icon_padding">3dp</dimen>
+    <dimen name="recents_tasklock_enddisplacement">26dp</dimen>
+    <dimen name="recents_meminfo_text_size">12sp</dimen>
+    <dimen name="recents_meminfo_height">24dp</dimen>
+    <dimen name="recents_clearall_text_size">14sp</dimen>
+    <dimen name="recents_clearall_height">28dp</dimen>
+    <dimen name="recents_bottom_padding">3dp</dimen>
+    <dimen name="clear_all_corner_radius">4dp</dimen>
+    <dimen name="clear_all_horizontal_paddding">8dp</dimen>
 </resources>