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.BasePreferenceController; 33 import com.android.settings.overlay.FeatureFactory; 34 35 import java.util.List; 36 37 public class TimeSpentInAppPreferenceController extends BasePreferenceController { 38 39 @VisibleForTesting 40 static final Intent SEE_TIME_IN_APP_TEMPLATE = new Intent(Settings.ACTION_APP_USAGE_SETTINGS); 41 42 private final PackageManager mPackageManager; 43 private final ApplicationFeatureProvider mAppFeatureProvider; 44 private Intent mIntent; 45 private String mPackageName; 46 TimeSpentInAppPreferenceController(Context context, String preferenceKey)47 public TimeSpentInAppPreferenceController(Context context, String preferenceKey) { 48 super(context, preferenceKey); 49 mPackageManager = context.getPackageManager(); 50 mAppFeatureProvider = FeatureFactory.getFactory(context) 51 .getApplicationFeatureProvider(context); 52 } 53 setPackageName(String packageName)54 public void setPackageName(String packageName) { 55 mPackageName = packageName; 56 mIntent = new Intent(SEE_TIME_IN_APP_TEMPLATE) 57 .putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName); 58 } 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 if (TextUtils.isEmpty(mPackageName)) { 63 return UNSUPPORTED_ON_DEVICE; 64 } 65 final List<ResolveInfo> resolved = mPackageManager.queryIntentActivities(mIntent, 66 0 /* flags */); 67 if (resolved == null || resolved.isEmpty()) { 68 return UNSUPPORTED_ON_DEVICE; 69 } 70 for (ResolveInfo info : resolved) { 71 if (isSystemApp(info)) { 72 return AVAILABLE; 73 } 74 } 75 return UNSUPPORTED_ON_DEVICE; 76 } 77 78 @Override displayPreference(PreferenceScreen screen)79 public void displayPreference(PreferenceScreen screen) { 80 super.displayPreference(screen); 81 final Preference pref = screen.findPreference(getPreferenceKey()); 82 if (pref != null) { 83 pref.setIntent(mIntent); 84 } 85 } 86 87 @Override getSummary()88 public CharSequence getSummary() { 89 return mAppFeatureProvider.getTimeSpentInApp(mPackageName); 90 } 91 isSystemApp(ResolveInfo info)92 private boolean isSystemApp(ResolveInfo info) { 93 return info != null 94 && info.activityInfo != null 95 && info.activityInfo.applicationInfo != null 96 && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; 97 } 98 } 99