1 /* 2 * Copyright (C) 2021 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 17 package com.android.car.settings.applications; 18 19 import android.app.Application; 20 import android.app.usage.UsageStats; 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.content.Context; 23 import android.os.UserHandle; 24 import android.text.format.DateUtils; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceCategory; 29 30 import com.android.car.settings.R; 31 import com.android.car.settings.common.FragmentController; 32 import com.android.car.settings.common.PreferenceController; 33 import com.android.car.ui.preference.CarUiPreference; 34 import com.android.settingslib.Utils; 35 import com.android.settingslib.applications.ApplicationsState; 36 37 import java.util.ArrayList; 38 import java.util.List; 39 40 /** 41 * Class responsible for displaying recently used apps. 42 */ 43 public class RecentAppsListPreferenceController extends PreferenceController<PreferenceCategory> 44 implements RecentAppsItemManager.RecentAppStatsListener { 45 46 private ApplicationsState mApplicationsState; 47 private int mUserId; 48 private List<UsageStats> mRecentAppStats; 49 private int mMaxRecentAppsCount; 50 RecentAppsListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51 public RecentAppsListPreferenceController(Context context, String preferenceKey, 52 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 53 this(context, preferenceKey, fragmentController, uxRestrictions, ApplicationsState 54 .getInstance((Application) context.getApplicationContext())); 55 } 56 57 @VisibleForTesting RecentAppsListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, ApplicationsState applicationsState)58 RecentAppsListPreferenceController(Context context, String preferenceKey, 59 FragmentController fragmentController, CarUxRestrictions uxRestrictions, 60 ApplicationsState applicationsState) { 61 super(context, preferenceKey, fragmentController, uxRestrictions); 62 mApplicationsState = applicationsState; 63 mUserId = UserHandle.myUserId(); 64 mRecentAppStats = new ArrayList<>(); 65 mMaxRecentAppsCount = getContext().getResources().getInteger( 66 R.integer.recent_apps_max_count); 67 } 68 69 @Override onRecentAppStatsLoaded(List<UsageStats> recentAppStats)70 public void onRecentAppStatsLoaded(List<UsageStats> recentAppStats) { 71 mRecentAppStats = recentAppStats; 72 refreshUi(); 73 } 74 75 @Override getPreferenceType()76 protected Class<PreferenceCategory> getPreferenceType() { 77 return PreferenceCategory.class; 78 } 79 80 @Override updateState(PreferenceCategory preferenceCategory)81 protected void updateState(PreferenceCategory preferenceCategory) { 82 preferenceCategory.setVisible(!mRecentAppStats.isEmpty()); 83 preferenceCategory.removeAll(); 84 85 int prefCount = 0; 86 for (UsageStats usageStats : mRecentAppStats) { 87 Preference pref = createPreference(getContext(), usageStats); 88 89 if (pref != null) { 90 getPreference().addPreference(pref); 91 prefCount++; 92 if (prefCount == mMaxRecentAppsCount) { 93 break; 94 } 95 } 96 } 97 } 98 createPreference(Context context, UsageStats usageStats)99 private Preference createPreference(Context context, UsageStats usageStats) { 100 String pkgName = usageStats.getPackageName(); 101 ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(pkgName, mUserId); 102 103 if (appEntry == null) { 104 return null; 105 } 106 107 Preference pref = new CarUiPreference(context); 108 pref.setTitle(appEntry.label); 109 if (appEntry.icon == null) { 110 pref.setIcon(Utils.getBadgedIcon(context, appEntry.info)); 111 } else { 112 pref.setIcon(appEntry.icon); 113 } 114 pref.setSummary(DateUtils.getRelativeTimeSpanString(usageStats.getLastTimeStamp(), 115 System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS)); 116 pref.setOnPreferenceClickListener(p -> { 117 getFragmentController().launchFragment( 118 ApplicationDetailsFragment.getInstance(pkgName)); 119 return true; 120 }); 121 122 return pref; 123 } 124 } 125