1 /*
2  * Copyright (C) 2017 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.tv.settings.device.apps.specialaccess;
18 
19 import android.Manifest;
20 import android.app.AppOpsManager;
21 import android.os.Bundle;
22 
23 import androidx.annotation.Keep;
24 import androidx.annotation.NonNull;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceGroup;
27 import androidx.preference.SwitchPreference;
28 import androidx.preference.TwoStatePreference;
29 
30 import com.android.internal.logging.nano.MetricsProto;
31 import com.android.settingslib.applications.ApplicationsState;
32 import com.android.tv.settings.R;
33 
34 /**
35  * Fragment for controlling if apps can monitor app usage
36  */
37 @Keep
38 public class AppUsageAccess extends ManageAppOp
39         implements ManageApplicationsController.Callback {
40 
41     private AppOpsManager mAppOpsManager;
42 
43     @Override
getMetricsCategory()44     public int getMetricsCategory() {
45         return MetricsProto.MetricsEvent.USAGE_ACCESS;
46     }
47 
48     @Override
onCreate(Bundle savedInstanceState)49     public void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         mAppOpsManager = getContext().getSystemService(AppOpsManager.class);
52     }
53 
54     @Override
getAppOpsOpCode()55     public int getAppOpsOpCode() {
56         return AppOpsManager.OP_GET_USAGE_STATS;
57     }
58 
59     @Override
getPermission()60     public String getPermission() {
61         return Manifest.permission.PACKAGE_USAGE_STATS;
62     }
63 
64     @NonNull
65     @Override
bindPreference(@onNull Preference preference, ApplicationsState.AppEntry entry)66     public Preference bindPreference(@NonNull Preference preference,
67             ApplicationsState.AppEntry entry) {
68         final TwoStatePreference switchPref = (SwitchPreference) preference;
69         switchPref.setTitle(entry.label);
70         switchPref.setKey(entry.info.packageName);
71         switchPref.setIcon(entry.icon);
72         switchPref.setOnPreferenceChangeListener((pref, newValue) -> {
73             setAppUsageAccess(entry, (Boolean) newValue);
74             return true;
75         });
76 
77         switchPref.setSummary(getPreferenceSummary(entry));
78         switchPref.setChecked(((PermissionState) entry.extraInfo).isAllowed());
79 
80         return switchPref;
81     }
82 
83     @NonNull
84     @Override
createAppPreference()85     public Preference createAppPreference() {
86         return new SwitchPreference(getPreferenceManager().getContext());
87     }
88 
89     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)90     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
91         setPreferencesFromResource(R.xml.app_usage_access, null);
92     }
93 
getPreferenceSummary(ApplicationsState.AppEntry entry)94     private CharSequence getPreferenceSummary(ApplicationsState.AppEntry entry) {
95         if (entry.extraInfo instanceof PermissionState) {
96             return getContext().getText(((PermissionState) entry.extraInfo).isAllowed()
97                     ? R.string.app_permission_summary_allowed
98                     : R.string.app_permission_summary_not_allowed);
99         } else {
100             return null;
101         }
102     }
103 
setAppUsageAccess(ApplicationsState.AppEntry entry, boolean grant)104     private void setAppUsageAccess(ApplicationsState.AppEntry entry, boolean grant) {
105         mAppOpsManager.setMode(AppOpsManager.OP_GET_USAGE_STATS,
106                 entry.info.uid, entry.info.packageName,
107                 grant ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
108         updateAppList();
109     }
110 
111     @NonNull
112     @Override
getAppPreferenceGroup()113     public PreferenceGroup getAppPreferenceGroup() {
114         return getPreferenceScreen();
115     }
116 }
117