1 /*
2  * Copyright (C) 2015 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 package com.android.settings.applications;
17 
18 import static android.app.AppOpsManager.OP_GET_USAGE_STATS;
19 import static android.app.AppOpsManager.OP_LOADER_USAGE_STATS;
20 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_DISABLE_USAGE_ACCESS_WARNING;
21 
22 import android.Manifest;
23 import android.app.AppOpsManager;
24 import android.app.admin.DevicePolicyManager;
25 import android.app.settings.SettingsEnums;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.pm.PackageManager;
30 import android.content.pm.ResolveInfo;
31 import android.os.Bundle;
32 import android.provider.Settings;
33 import android.text.TextUtils;
34 import android.util.Log;
35 
36 import androidx.annotation.VisibleForTesting;
37 import androidx.appcompat.app.AlertDialog;
38 import androidx.preference.Preference;
39 import androidx.preference.Preference.OnPreferenceChangeListener;
40 import androidx.preference.Preference.OnPreferenceClickListener;
41 
42 import com.android.settings.R;
43 import com.android.settings.applications.AppStateUsageBridge.UsageState;
44 import com.android.settings.overlay.FeatureFactory;
45 import com.android.settingslib.RestrictedSwitchPreference;
46 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
47 
48 public class UsageAccessDetails extends AppInfoWithHeader implements OnPreferenceChangeListener,
49         OnPreferenceClickListener {
50 
51     private static final String TAG = UsageAccessDetails.class.getSimpleName();
52     private static final String KEY_APP_OPS_PREFERENCE_SCREEN = "app_ops_preference_screen";
53     private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
54     private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description";
55 
56     // Use a bridge to get the usage stats but don't initialize it to connect with all state.
57     // TODO: Break out this functionality into its own class.
58     private AppStateUsageBridge mUsageBridge;
59     private AppOpsManager mAppOpsManager;
60     private RestrictedSwitchPreference mSwitchPref;
61     private Preference mUsageDesc;
62     private Intent mSettingsIntent;
63     private UsageState mUsageState;
64     private DevicePolicyManager mDpm;
65 
66     @Override
onCreate(Bundle savedInstanceState)67     public void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69 
70         Context context = getActivity();
71         if (TextUtils.equals(mPackageName, context.getPackageName())) {
72             Log.w(TAG, "Unsupported app package.");
73             finish();
74         }
75 
76         mUsageBridge = new AppStateUsageBridge(context, mState, null);
77         mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
78         mDpm = context.getSystemService(DevicePolicyManager.class);
79 
80         addPreferencesFromResource(R.xml.app_ops_permissions_details);
81         mSwitchPref = (RestrictedSwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
82         mUsageDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC);
83 
84         getPreferenceScreen().setTitle(R.string.usage_access);
85         mSwitchPref.setTitle(R.string.permit_usage_access);
86         mUsageDesc.setTitle(R.string.usage_access_description);
87 
88         mSwitchPref.setOnPreferenceChangeListener(this);
89 
90         mSettingsIntent = new Intent(Intent.ACTION_MAIN)
91                 .addCategory(Settings.INTENT_CATEGORY_USAGE_ACCESS_CONFIG)
92                 .setPackage(mPackageName);
93     }
94 
95     @Override
onPreferenceClick(Preference preference)96     public boolean onPreferenceClick(Preference preference) {
97         return false;
98     }
99 
100     @Override
onPreferenceChange(Preference preference, Object newValue)101     public boolean onPreferenceChange(Preference preference, Object newValue) {
102         if (preference == mSwitchPref) {
103             if (mUsageState != null && (Boolean) newValue != mUsageState.isPermissible()) {
104                 if (mUsageState.isPermissible() && mDpm.isProfileOwnerApp(mPackageName)) {
105                     new AlertDialog.Builder(getContext())
106                             .setIcon(com.android.internal.R.drawable.ic_dialog_alert_material)
107                             .setTitle(android.R.string.dialog_alert_title)
108                             .setMessage(mDpm.getResources().getString(
109                                     WORK_PROFILE_DISABLE_USAGE_ACCESS_WARNING,
110                                     () -> getString(R.string.work_profile_usage_access_warning)))
111                             .setPositiveButton(R.string.okay, null)
112                             .show();
113                 }
114                 setHasAccess(!mUsageState.isPermissible());
115                 refreshUi();
116             }
117             return true;
118         }
119         return false;
120     }
121 
doesAnyPermissionMatch(String permissionToMatch, String[] permissions)122     private static boolean doesAnyPermissionMatch(String permissionToMatch, String[] permissions) {
123         for (String permission : permissions) {
124             if (permissionToMatch.equals(permission)) {
125                 return true;
126             }
127         }
128         return false;
129     }
130 
setHasAccess(boolean newState)131     private void setHasAccess(boolean newState) {
132         logSpecialPermissionChange(newState, mPackageName);
133 
134         final int newAppOpMode = newState ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED;
135         final int uid = mPackageInfo.applicationInfo.uid;
136         if (doesAnyPermissionMatch(Manifest.permission.PACKAGE_USAGE_STATS,
137                 mUsageState.packageInfo.requestedPermissions)) {
138             mAppOpsManager.setMode(OP_GET_USAGE_STATS, uid, mPackageName, newAppOpMode);
139         }
140         if (doesAnyPermissionMatch(Manifest.permission.LOADER_USAGE_STATS,
141                 mUsageState.packageInfo.requestedPermissions)) {
142             mAppOpsManager.setMode(OP_LOADER_USAGE_STATS, uid, mPackageName, newAppOpMode);
143         }
144     }
145 
146     @VisibleForTesting
logSpecialPermissionChange(boolean newState, String packageName)147     void logSpecialPermissionChange(boolean newState, String packageName) {
148         int logCategory = newState ? SettingsEnums.APP_SPECIAL_PERMISSION_USAGE_VIEW_ALLOW
149                 : SettingsEnums.APP_SPECIAL_PERMISSION_USAGE_VIEW_DENY;
150         final MetricsFeatureProvider metricsFeatureProvider =
151                 FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
152         metricsFeatureProvider.action(
153                 metricsFeatureProvider.getAttribution(getActivity()),
154                 logCategory,
155                 getMetricsCategory(),
156                 packageName,
157                 0);
158     }
159 
160     @Override
refreshUi()161     protected boolean refreshUi() {
162         retrieveAppEntry();
163         if (mAppEntry == null) {
164             return false;
165         }
166         if (mPackageInfo == null) {
167             return false; // onCreate must have failed, make sure to exit
168         }
169         mUsageState = mUsageBridge.getUsageInfo(mPackageName,
170                 mPackageInfo.applicationInfo.uid);
171 
172         boolean hasAccess = mUsageState.isPermissible();
173         boolean shouldEnable = mUsageState.permissionDeclared;
174 
175         if (shouldEnable && !hasAccess) {
176             mSwitchPref.checkEcmRestrictionAndSetDisabled(AppOpsManager.OPSTR_GET_USAGE_STATS,
177                     mPackageName);
178             shouldEnable = !mSwitchPref.isDisabledByEcm();
179         }
180 
181         mSwitchPref.setChecked(hasAccess);
182         mSwitchPref.setEnabled(shouldEnable);
183 
184         ResolveInfo resolveInfo = mPm.resolveActivityAsUser(mSettingsIntent,
185                 PackageManager.GET_META_DATA, mUserId);
186         if (resolveInfo != null) {
187             Bundle metaData = resolveInfo.activityInfo.metaData;
188             mSettingsIntent.setComponent(new ComponentName(resolveInfo.activityInfo.packageName,
189                     resolveInfo.activityInfo.name));
190             if (metaData != null
191                     && metaData.containsKey(Settings.METADATA_USAGE_ACCESS_REASON)) {
192                 mSwitchPref.setSummary(
193                         metaData.getString(Settings.METADATA_USAGE_ACCESS_REASON));
194             }
195         }
196 
197         return true;
198     }
199 
200     @Override
createDialog(int id, int errorCode)201     protected AlertDialog createDialog(int id, int errorCode) {
202         return null;
203     }
204 
205     @Override
getMetricsCategory()206     public int getMetricsCategory() {
207         return SettingsEnums.APPLICATIONS_USAGE_ACCESS_DETAIL;
208     }
209 
210 }
211