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.getFactory(context) 57 .getApplicationFeatureProvider(context); 58 } 59 setPackageName(String packageName)60 public void setPackageName(String packageName) { 61 mPackageName = packageName; 62 mIntent = new Intent(SEE_TIME_IN_APP_TEMPLATE) 63 .putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName); 64 } 65 66 /** 67 * Set a parent fragment for this controller. 68 */ setParentFragment(AppInfoDashboardFragment parent)69 public void setParentFragment(AppInfoDashboardFragment parent) { 70 mParent = parent; 71 mAppEntry = mParent.getAppEntry(); 72 } 73 74 @Override getAvailabilityStatus()75 public int getAvailabilityStatus() { 76 if (TextUtils.isEmpty(mPackageName)) { 77 return UNSUPPORTED_ON_DEVICE; 78 } 79 final List<ResolveInfo> resolved = mPackageManager.queryIntentActivities(mIntent, 80 0 /* flags */); 81 if (resolved == null || resolved.isEmpty()) { 82 return UNSUPPORTED_ON_DEVICE; 83 } 84 for (ResolveInfo info : resolved) { 85 if (isSystemApp(info)) { 86 return AVAILABLE; 87 } 88 } 89 return UNSUPPORTED_ON_DEVICE; 90 } 91 92 @Override displayPreference(PreferenceScreen screen)93 public void displayPreference(PreferenceScreen screen) { 94 super.displayPreference(screen); 95 final Preference pref = screen.findPreference(getPreferenceKey()); 96 if (pref != null) { 97 pref.setIntent(mIntent); 98 } 99 pref.setEnabled(AppUtils.isAppInstalled(mAppEntry)); 100 } 101 102 @Override getSummaryTextInBackground()103 protected CharSequence getSummaryTextInBackground() { 104 return mAppFeatureProvider.getTimeSpentInApp(mPackageName); 105 } 106 isSystemApp(ResolveInfo info)107 private boolean isSystemApp(ResolveInfo info) { 108 return info != null 109 && info.activityInfo != null 110 && info.activityInfo.applicationInfo != null 111 && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; 112 } 113 } 114