1 /*
2  * Copyright (C) 2019 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.developeroptions.applications;
18 
19 import android.app.usage.UsageStats;
20 import android.content.Context;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.car.developeroptions.R;
28 import com.android.car.developeroptions.core.BasePreferenceController;
29 
30 import java.util.List;
31 
32 public class AllAppsInfoPreferenceController extends BasePreferenceController
33         implements RecentAppStatsMixin.RecentAppStatsListener {
34 
35     @VisibleForTesting
36     Preference mPreference;
37 
AllAppsInfoPreferenceController(Context context, String key)38     public AllAppsInfoPreferenceController(Context context, String key) {
39         super(context, key);
40     }
41 
42     @Override
getAvailabilityStatus()43     public int getAvailabilityStatus() {
44         return AVAILABLE;
45     }
46 
47     @Override
displayPreference(PreferenceScreen screen)48     public void displayPreference(PreferenceScreen screen) {
49         super.displayPreference(screen);
50         mPreference = screen.findPreference(getPreferenceKey());
51         // In most cases, device has recently opened apps. So, we hide it by default.
52         mPreference.setVisible(false);
53     }
54 
55     @Override
onReloadDataCompleted(@onNull List<UsageStats> recentApps)56     public void onReloadDataCompleted(@NonNull List<UsageStats> recentApps) {
57         // If device has recently opened apps, we don't show all apps preference.
58         if (!recentApps.isEmpty()) {
59             mPreference.setVisible(false);
60             return;
61         }
62 
63         mPreference.setVisible(true);
64         // Show total number of installed apps as See all's summary.
65         new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON,
66                 mContext.getPackageManager()) {
67             @Override
68             protected void onCountComplete(int num) {
69                 mPreference.setSummary(mContext.getString(R.string.apps_summary, num));
70             }
71         }.execute();
72     }
73 }
74