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