设为首页 收藏本站 662p开源网
开启辅助访问 切换到宽版

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

切换风格 立即注册 找回密码

安卓教程网

发表于 2019-5-21 17:24:11 | 显示全部楼层 |阅读模式
更多

systemui/recents/model/RecentsTaskLoadPlan.java

找到 preloadPlan方法

  1. /*
  2. * Copyright (C) 2014 The android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */

  16. package com.android.systemui.recents.model;

  17. import android.app.ActivityManager;
  18. import android.content.Context;
  19. import android.content.pm.ActivityInfo;
  20. import android.content.pm.ApplicationInfo;
  21. import android.content.pm.UserInfo;
  22. import android.content.res.Resources;
  23. import android.graphics.BitMap;
  24. import android.graphics.drawable.Drawable;
  25. import android.os.UserHandle;
  26. import android.os.UserManager;
  27. import android.provider.Settings;
  28. import android.provider.Settings.Secure;
  29. import android.util.ArraySet;
  30. import android.util.SparseArray;
  31. import android.util.SparseIntArray;

  32. import com.android.systemui.Prefs;
  33. import com.android.systemui.R;
  34. import com.android.systemui.recents.Recents;
  35. import com.android.systemui.recents.RecentsConfiguration;
  36. import com.android.systemui.recents.RecentsDebugFlags;
  37. import com.android.systemui.recents.misc.SystemServicesProxy;

  38. import com.android.systemui.recents.views.grid.TaskGridLayoutAlgorithm;
  39. import java.util.ArrayList;
  40. import java.util.Collections;
  41. import java.util.List;


  42. /**
  43. * This class stores the loading state as it goes through multiple stages of loading:
  44. *   1) preloadRawTasks() will load the raw set of recents tasks from the system
  45. *   2) preloadPlan() will construct a new task stack with all metadata and only icons and
  46. *      thumbnails that are currently in the cache
  47. *   3) executePlan() will actually load and fill in the icons and thumbnails according to the load
  48. *      options specified, such that we can transition into the Recents activity seamlessly
  49. */
  50. public class RecentsTaskLoadPlan {

  51.     private static int MIN_NUM_TASKS = 5;
  52.     private static int SESSION_BEGIN_TIME = 1000 /* ms/s */ * 60 /* s/min */ * 60 /* min/hr */ *
  53.             6 /* hrs */;

  54.     /** The set of conditions to load tasks. */
  55.     public static class Options {
  56.         public int runningTaskId = -1;
  57.         public boolean loadIcons = true;
  58.         public boolean loadThumbnails = true;
  59.         public boolean onlyLoadForCache = false;
  60.         public boolean onlyLoadPausedActivities = false;
  61.         public int numVisibleTasks = 0;
  62.         public int numVisibleTaskThumbnails = 0;
  63.     }

  64.     Context mContext;

  65.     List<ActivityManager.RecentTaskInfo> mRawTasks;
  66.     TaskStack mStack;
  67.     ArraySet<Integer> mCurrentQuietProfiles = new ArraySet<Integer>();

  68.     /** Package level ctor */
  69.     RecentsTaskLoadPlan(Context context) {
  70.         mContext = context;
  71.     }

  72.     private void updateCurrentQuietProfilesCache(int currentUserId) {
  73.         mCurrentQuietProfiles.clear();

  74.         if (currentUserId == UserHandle.USER_CURRENT) {
  75.             currentUserId = ActivityManager.getCurrentUser();
  76.         }
  77.         UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
  78.         List<UserInfo> profiles = userManager.getProfiles(currentUserId);
  79.         if (profiles != null) {
  80.             for (int i = 0; i < profiles.size(); i++) {
  81.                 UserInfo user  = profiles.get(i);
  82.                 if (user.isManagedProfile() && user.isQuietModeEnabled()) {
  83.                     mCurrentQuietProfiles.add(user.id);
  84.                 }
  85.             }
  86.         }
  87.     }

  88.     /**
  89.      * An optimization to preload the raw list of tasks. The raw tasks are saved in least-recent
  90.      * to most-recent order.
  91.      */
  92.     public synchronized void preloadRawTasks(boolean includeFrontMostExcludedTask) {
  93.         int currentUserId = UserHandle.USER_CURRENT;
  94.         updateCurrentQuietProfilesCache(currentUserId);
  95.         SystemServicesProxy ssp = Recents.getSystemServices();
  96.         mRawTasks = ssp.getRecentTasks(ActivityManager.getMaxRecentTasksStatic(),
  97.                 currentUserId, includeFrontMostExcludedTask, mCurrentQuietProfiles);

  98.         // Since the raw tasks are given in most-recent to least-recent order, we need to reverse it
  99.         Collections.reverse(mRawTasks);
  100.     }

  101.     /**
  102.      * Preloads the list of recent tasks from the system. After this call, the TaskStack will
  103.      * have a list of all the recent tasks with their metadata, not including icons or
  104.      * thumbnails which were not cached and have to be loaded.
  105.      *
  106.      * The tasks will be ordered by:
  107.      * - least-recent to most-recent stack tasks
  108.      * - least-recent to most-recent freeform tasks
  109.      */
  110.     public synchronized void preloadPlan(RecentsTaskLoader loader, int runningTaskId,
  111.             boolean includeFrontMostExcludedTask) {
  112.         Resources res = mContext.getResources();
  113.         ArrayList<Task> allTasks = new ArrayList<>();
  114.         if (mRawTasks == null) {
  115.             preloadRawTasks(includeFrontMostExcludedTask);
  116.         }

  117.         SystemServicesProxy ssp = SystemServicesProxy.getInstance(mContext);
  118.         SparseArray<Task.TaskKey> affiliatedTasks = new SparseArray<>();
  119.         SparseIntArray affiliatedTaskCounts = new SparseIntArray();
  120.         String dismissDescFormat = mContext.getString(
  121.                 R.string.accessibility_recents_item_will_be_dismissed);
  122.         String appInfoDescFormat = mContext.getString(
  123.                 R.string.accessibility_recents_item_open_app_info);
  124.         int currentUserId = ssp.getCurrentUser();
  125.         long legacyLastStackActiveTime = migrateLegacyLastStackActiveTime(currentUserId);
  126.         long lastStackActiveTime = Settings.Secure.getLongForUser(mContext.getContentResolver(),
  127.                 Secure.OVERVIEW_LAST_STACK_ACTIVE_TIME, legacyLastStackActiveTime, currentUserId);
  128.         if (RecentsDebugFlags.Static.EnableMockTasks) {
  129.             lastStackActiveTime = 0;
  130.         }
  131.         long newLastStackActiveTime = -1;
  132.         int taskCount = mRawTasks.size();
  133.                 //wfy
  134.         for (int i = 0; i < taskCount; i++) {
  135.             ActivityManager.RecentTaskInfo t = mRawTasks.get(i);

  136.             // Compose the task key
  137.             Task.TaskKey taskKey = new Task.TaskKey(t.persistentId, t.stackId, t.baseIntent,
  138.                     t.userId, t.firstActiveTime, t.lastActiveTime);

  139.             // This task is only shown in the stack if it satisfies the historical time or min
  140.             // number of tasks constraints. Freeform tasks are also always shown.
  141.             boolean isFreeformTask = SystemServicesProxy.isFreeformStack(t.stackId);
  142.             boolean isStackTask;
  143.             if (Recents.getConfiguration().isGridEnabled) {
  144.                 // When grid layout is enabled, we only show the first
  145.                 // TaskGridLayoutAlgorithm.MAX_LAYOUT_TASK_COUNT} tasks.
  146.                 isStackTask = t.lastActiveTime >= lastStackActiveTime &&
  147.                     i >= taskCount - TaskGridLayoutAlgorithm.MAX_LAYOUT_TASK_COUNT;
  148.             } else {
  149.                 isStackTask = isFreeformTask || !isHistoricalTask(t) ||
  150.                     (t.lastActiveTime >= lastStackActiveTime && i >= (taskCount - MIN_NUM_TASKS));
  151.             }
  152.             boolean isLaunchTarget = taskKey.id == runningTaskId;

  153.             // The last stack active time is the baseline for which we show visible tasks.  Since
  154.             // the system will store all the tasks, we don't want to show the tasks prior to the
  155.             // last visible ones, otherwise, as you dismiss them, the previous tasks may satisfy
  156.             // the other stack-task constraints.
  157.             if (isStackTask && newLastStackActiveTime < 0) {
  158.                 newLastStackActiveTime = t.lastActiveTime;
  159.             }

  160.             // Load the title, icon, and color
  161.             ActivityInfo info = loader.getAndUpdateActivityInfo(taskKey);
  162.             String title = loader.getAndUpdateActivityTitle(taskKey, t.taskDescription);
  163.             String titleDescription = loader.getAndUpdateContentDescription(taskKey, res);
  164.             String dismissDescription = String.format(dismissDescFormat, titleDescription);
  165.             String appInfoDescription = String.format(appInfoDescFormat, titleDescription);
  166.             Drawable icon = isStackTask
  167.                     ? loader.getAndUpdateActivityIcon(taskKey, t.taskDescription, res, false)
  168.                     : null;
  169.             Bitmap thumbnail = loader.getAndUpdateThumbnail(taskKey, false /* loadIfNotCached */);
  170.             int activityColor = loader.getActivityPrimaryColor(t.taskDescription);
  171.             int backgroundColor = loader.getActivityBackgroundColor(t.taskDescription);
  172.             boolean isSystemApp = (info != null) &&
  173.                     ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);

  174.             // Add the task to the stack
  175.             Task task = new Task(taskKey, t.affiliatedTaskId, t.affiliatedTaskColor, icon,
  176.                     thumbnail, title, titleDescription, dismissDescription, appInfoDescription,
  177.                     activityColor, backgroundColor, isLaunchTarget, isStackTask, isSystemApp,
  178.                     t.isDockable, t.bounds, t.taskDescription, t.resizeMode, t.topActivity);
  179.                        
  180.                         if(info.packageName.equals("com.funduemobile.enpad")){
  181.                                
  182.                         }else{
  183.                                 allTasks.add(task);
  184.                                 affiliatedTaskCounts.put(taskKey.id, affiliatedTaskCounts.get(taskKey.id, 0) + 1);
  185.                                 affiliatedTasks.put(taskKey.id, taskKey);
  186.                         }
  187.             
  188.         }
  189.         if (newLastStackActiveTime != -1) {
  190.             Settings.Secure.putLongForUser(mContext.getContentResolver(),
  191.                     Secure.OVERVIEW_LAST_STACK_ACTIVE_TIME, newLastStackActiveTime, currentUserId);
  192.         }

  193.         // Initialize the stacks
  194.         mStack = new TaskStack();
  195.         mStack.setTasks(mContext, allTasks, false /* notifyStackChanges */);
  196.     }

  197.     /**
  198.      * Called to apply the actual loading based on the specified conditions.
  199.      */
  200.     public synchronized void executePlan(Options opts, RecentsTaskLoader loader,
  201.             TaskResourceLoadQueue loadQueue) {
  202.         RecentsConfiguration config = Recents.getConfiguration();
  203.         Resources res = mContext.getResources();

  204.         // Iterate through each of the tasks and load them according to the load conditions.
  205.         ArrayList<Task> tasks = mStack.getStackTasks();
  206.         int taskCount = tasks.size();
  207.         for (int i = 0; i < taskCount; i++) {
  208.             Task task = tasks.get(i);
  209.             Task.TaskKey taskKey = task.key;

  210.             boolean isRunningTask = (task.key.id == opts.runningTaskId);
  211.             boolean isVisibleTask = i >= (taskCount - opts.numVisibleTasks);
  212.             boolean isVisibleThumbnail = i >= (taskCount - opts.numVisibleTaskThumbnails);

  213.             // If requested, skip the running task
  214.             if (opts.onlyLoadPausedActivities && isRunningTask) {
  215.                 continue;
  216.             }

  217.             if (opts.loadIcons && (isRunningTask || isVisibleTask)) {
  218.                 if (task.icon == null) {
  219.                     task.icon = loader.getAndUpdateActivityIcon(taskKey, task.taskDescription, res,
  220.                             true);
  221.                 }
  222.             }
  223.             if (opts.loadThumbnails && (isRunningTask || isVisibleThumbnail)) {
  224.                 if (task.thumbnail == null || isRunningTask) {
  225.                     if (config.svelteLevel <= RecentsConfiguration.SVELTE_LIMIT_CACHE) {
  226.                         task.thumbnail = loader.getAndUpdateThumbnail(taskKey,
  227.                                 true /* loadIfNotCached */);
  228.                     } else if (config.svelteLevel == RecentsConfiguration.SVELTE_DISABLE_CACHE) {
  229.                         loadQueue.addTask(task);
  230.                     }
  231.                 }
  232.             }
  233.         }
  234.     }

  235.     /**
  236.      * Returns the TaskStack from the preloaded list of recent tasks.
  237.      */
  238.     public TaskStack getTaskStack() {
  239.         return mStack;
  240.     }

  241.     /**
  242.      * Returns the raw list of recent tasks.
  243.      */
  244.     public List<ActivityManager.RecentTaskInfo> getRawTasks() {
  245.         return mRawTasks;
  246.     }

  247.     /** Returns whether there are any tasks in any stacks. */
  248.     public boolean hasTasks() {
  249.         if (mStack != null) {
  250.             return mStack.getTaskCount() > 0;
  251.         }
  252.         return false;
  253.     }

  254.     /**
  255.      * Returns whether this task is too old to be shown.
  256.      */
  257.     private boolean isHistoricalTask(ActivityManager.RecentTaskInfo t) {
  258.         return t.lastActiveTime < (System.currentTimeMillis() - SESSION_BEGIN_TIME);
  259.     }

  260.     /**
  261.      * Migrate the last active time from the prefs to the secure settings.
  262.      *
  263.      * The first time this runs, it will:
  264.      * 1) fetch the last stack active time from the prefs
  265.      * 2) set the prefs to the last stack active time for all users
  266.      * 3) clear the pref
  267.      * 4) return the last stack active time
  268.      *
  269.      * Subsequent calls to this will return zero.
  270.      */
  271.     private long migrateLegacyLastStackActiveTime(int currentUserId) {
  272.         long legacyLastStackActiveTime = Prefs.getLong(mContext,
  273.                 Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME, -1);
  274.         if (legacyLastStackActiveTime != -1) {
  275.             Prefs.remove(mContext, Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME);
  276.             UserManager userMgr = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
  277.             List<UserInfo> users = userMgr.getUsers();
  278.             for (int i = 0; i < users.size(); i++) {
  279.                 int userId = users.get(i).id;
  280.                 if (userId != currentUserId) {
  281.                     Settings.Secure.putLongForUser(mContext.getContentResolver(),
  282.                             Secure.OVERVIEW_LAST_STACK_ACTIVE_TIME, legacyLastStackActiveTime,
  283.                             userId);
  284.                 }
  285.             }
  286.             return legacyLastStackActiveTime;
  287.         }
  288.         return 0;
  289.     }
  290. }
复制代码



您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Archiver|手机版|小黑屋|安卓教程网 ( 粤ICP备12089784号  

GMT+8, 2019-12-16 03:01 , Processed in 0.362763 second(s), 44 queries .

Powered by Discuz X3.1

© 2001-2013 IOS教程网

快速回复 返回顶部 返回列表