• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.applications.ApplicationsState;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 /**
40  * Class responsible for displaying recently used apps.
41  */
42 public class RecentAppsListPreferenceController extends PreferenceController<PreferenceCategory>
43         implements RecentAppsItemManager.RecentAppStatsListener {
44 
45     private ApplicationsState mApplicationsState;
46     private int mUserId;
47     private List<UsageStats> mRecentAppStats;
48     private int mMaxRecentAppsCount;
49 
RecentAppsListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)50     public RecentAppsListPreferenceController(Context context, String preferenceKey,
51             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
52         this(context, preferenceKey, fragmentController, uxRestrictions, ApplicationsState
53                 .getInstance((Application) context.getApplicationContext()));
54     }
55 
56     @VisibleForTesting
RecentAppsListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, ApplicationsState applicationsState)57     RecentAppsListPreferenceController(Context context, String preferenceKey,
58             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
59             ApplicationsState applicationsState) {
60         super(context, preferenceKey, fragmentController, uxRestrictions);
61         mApplicationsState = applicationsState;
62         mUserId = UserHandle.myUserId();
63         mRecentAppStats = new ArrayList<>();
64         mMaxRecentAppsCount = getContext().getResources().getInteger(
65                 R.integer.recent_apps_max_count);
66     }
67 
68     @Override
onRecentAppStatsLoaded(List<UsageStats> recentAppStats)69     public void onRecentAppStatsLoaded(List<UsageStats> recentAppStats) {
70         mRecentAppStats = recentAppStats;
71         refreshUi();
72     }
73 
74     @Override
getPreferenceType()75     protected Class<PreferenceCategory> getPreferenceType() {
76         return PreferenceCategory.class;
77     }
78 
79     @Override
updateState(PreferenceCategory preferenceCategory)80     protected void updateState(PreferenceCategory preferenceCategory) {
81         preferenceCategory.setVisible(!mRecentAppStats.isEmpty());
82         preferenceCategory.removeAll();
83 
84         int prefCount = 0;
85         for (UsageStats usageStats : mRecentAppStats) {
86             Preference pref = createPreference(getContext(), usageStats);
87 
88             if (pref != null) {
89                 getPreference().addPreference(pref);
90                 prefCount++;
91                 if (prefCount == mMaxRecentAppsCount) {
92                     break;
93                 }
94             }
95         }
96     }
97 
createPreference(Context context, UsageStats usageStats)98     private Preference createPreference(Context context, UsageStats usageStats) {
99         String pkgName = usageStats.getPackageName();
100         ApplicationsState.AppEntry appEntry = mApplicationsState.getEntry(pkgName, mUserId);
101 
102         if (appEntry == null) {
103             return null;
104         }
105 
106         Preference pref = new CarUiPreference(context);
107         pref.setTitle(appEntry.label);
108         pref.setIcon(appEntry.icon);
109         pref.setSummary(DateUtils.getRelativeTimeSpanString(usageStats.getLastTimeStamp(),
110                 System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS));
111         pref.setOnPreferenceClickListener(p -> {
112             getFragmentController().launchFragment(
113                     ApplicationDetailsFragment.getInstance(pkgName));
114             return true;
115         });
116 
117         return pref;
118     }
119 }
120