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 import com.android.settingslib.applications.AppUtils; 35 import com.android.settingslib.applications.ApplicationsState; 36 37 import java.util.List; 38 39 /** 40 * To Retrieve the time consumption of the application. 41 */ 42 public class TimeSpentInAppPreferenceController extends LiveDataController { 43 @VisibleForTesting 44 static final Intent SEE_TIME_IN_APP_TEMPLATE = new Intent(Settings.ACTION_APP_USAGE_SETTINGS); 45 46 private final PackageManager mPackageManager; 47 private final ApplicationFeatureProvider mAppFeatureProvider; 48 private Intent mIntent; 49 private String mPackageName; 50 protected AppInfoDashboardFragment mParent; 51 protected ApplicationsState.AppEntry mAppEntry; 52 TimeSpentInAppPreferenceController(Context context, String preferenceKey)53 public TimeSpentInAppPreferenceController(Context context, String preferenceKey) { 54 super(context, preferenceKey); 55 mPackageManager = context.getPackageManager(); 56 mAppFeatureProvider = FeatureFactory.getFeatureFactory() 57 .getApplicationFeatureProvider(); 58 } 59 setPackageName(String packageName)60 public void setPackageName(String packageName) { 61 mPackageName = packageName; 62 mIntent = new Intent(SEE_TIME_IN_APP_TEMPLATE) 63 .setPackage(mPackageManager.getWellbeingPackageName()) 64 .putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName); 65 } 66 67 /** 68 * Set a parent fragment for this controller. 69 */ setParentFragment(AppInfoDashboardFragment parent)70 public void setParentFragment(AppInfoDashboardFragment parent) { 71 mParent = parent; 72 mAppEntry = mParent.getAppEntry(); 73 } 74 75 @Override getAvailabilityStatus()76 public int getAvailabilityStatus() { 77 if (TextUtils.isEmpty(mPackageName)) { 78 return UNSUPPORTED_ON_DEVICE; 79 } 80 final List<ResolveInfo> resolved = mPackageManager.queryIntentActivities(mIntent, 81 0 /* flags */); 82 if (resolved == null || resolved.isEmpty()) { 83 return UNSUPPORTED_ON_DEVICE; 84 } 85 for (ResolveInfo info : resolved) { 86 if (isSystemApp(info)) { 87 return AVAILABLE; 88 } 89 } 90 return UNSUPPORTED_ON_DEVICE; 91 } 92 93 @Override displayPreference(PreferenceScreen screen)94 public void displayPreference(PreferenceScreen screen) { 95 super.displayPreference(screen); 96 final Preference pref = screen.findPreference(getPreferenceKey()); 97 if (pref != null) { 98 pref.setIntent(mIntent); 99 } 100 pref.setEnabled(AppUtils.isAppInstalled(mAppEntry)); 101 } 102 103 @Override getSummaryTextInBackground()104 protected CharSequence getSummaryTextInBackground() { 105 return mAppFeatureProvider.getTimeSpentInApp(mPackageName); 106 } 107 isSystemApp(ResolveInfo info)108 private boolean isSystemApp(ResolveInfo info) { 109 return info != null 110 && info.activityInfo != null 111 && info.activityInfo.applicationInfo != null 112 && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; 113 } 114 } 115