1 /*
2  * Copyright (C) 2018 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.settings.applications.appinfo;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.provider.Settings;
25 import android.text.TextUtils;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settings.applications.ApplicationFeatureProvider;
32 import com.android.settings.core.LiveDataController;
33 import com.android.settings.overlay.FeatureFactory;
34 
35 import java.util.List;
36 
37 /**
38  * To Retrieve the time consumption of the application.
39  */
40 public class TimeSpentInAppPreferenceController extends LiveDataController {
41     @VisibleForTesting
42     static final Intent SEE_TIME_IN_APP_TEMPLATE = new Intent(Settings.ACTION_APP_USAGE_SETTINGS);
43 
44     private final PackageManager mPackageManager;
45     private final ApplicationFeatureProvider mAppFeatureProvider;
46     private Intent mIntent;
47     private String mPackageName;
48 
TimeSpentInAppPreferenceController(Context context, String preferenceKey)49     public TimeSpentInAppPreferenceController(Context context, String preferenceKey) {
50         super(context, preferenceKey);
51         mPackageManager = context.getPackageManager();
52         mAppFeatureProvider = FeatureFactory.getFactory(context)
53                 .getApplicationFeatureProvider(context);
54     }
55 
setPackageName(String packageName)56     public void setPackageName(String packageName) {
57         mPackageName = packageName;
58         mIntent = new Intent(SEE_TIME_IN_APP_TEMPLATE)
59                 .putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName);
60     }
61 
62     @Override
getAvailabilityStatus()63     public int getAvailabilityStatus() {
64         if (TextUtils.isEmpty(mPackageName)) {
65             return UNSUPPORTED_ON_DEVICE;
66         }
67         final List<ResolveInfo> resolved = mPackageManager.queryIntentActivities(mIntent,
68                 0 /* flags */);
69         if (resolved == null || resolved.isEmpty()) {
70             return UNSUPPORTED_ON_DEVICE;
71         }
72         for (ResolveInfo info : resolved) {
73             if (isSystemApp(info)) {
74                 return AVAILABLE;
75             }
76         }
77         return UNSUPPORTED_ON_DEVICE;
78     }
79 
80     @Override
displayPreference(PreferenceScreen screen)81     public void displayPreference(PreferenceScreen screen) {
82         super.displayPreference(screen);
83         final Preference pref = screen.findPreference(getPreferenceKey());
84         if (pref != null) {
85             pref.setIntent(mIntent);
86         }
87     }
88 
89     @Override
getSummaryTextInBackground()90     protected CharSequence getSummaryTextInBackground() {
91         return mAppFeatureProvider.getTimeSpentInApp(mPackageName);
92     }
93 
isSystemApp(ResolveInfo info)94     private boolean isSystemApp(ResolveInfo info) {
95         return info != null
96                 && info.activityInfo != null
97                 && info.activityInfo.applicationInfo != null
98                 && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
99     }
100 }
101