• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.appinfo;
17 
18 import android.app.AppOpsManager;
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.os.Bundle;
22 
23 import androidx.appcompat.app.AlertDialog;
24 import androidx.preference.Preference;
25 import androidx.preference.Preference.OnPreferenceChangeListener;
26 import androidx.preference.SwitchPreference;
27 
28 import com.android.settings.R;
29 import com.android.settings.applications.AppInfoWithHeader;
30 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState;
31 import com.android.settings.applications.AppStateMediaManagementAppsBridge;
32 import com.android.settingslib.applications.ApplicationsState.AppEntry;
33 
34 /**
35  * Class for displaying app info related to {@link AppOpsManager#OP_MANAGE_MEDIA}.
36  */
37 public class MediaManagementAppsDetails extends AppInfoWithHeader implements
38         OnPreferenceChangeListener {
39 
40     private static final String KEY_SWITCH_PREF = "media_management_apps_toggle";
41 
42     private AppStateMediaManagementAppsBridge mAppBridge;
43     private AppOpsManager mAppOpsManager;
44     private SwitchPreference mSwitchPref;
45     private PermissionState mPermissionState;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     public void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         final Context context = getActivity();
52         mAppBridge = new AppStateMediaManagementAppsBridge(context, mState, null /* callback */);
53         mAppOpsManager = context.getSystemService(AppOpsManager.class);
54 
55         // initialize preferences
56         addPreferencesFromResource(R.xml.media_management_apps);
57         mSwitchPref = findPreference(KEY_SWITCH_PREF);
58 
59         // install event listeners
60         mSwitchPref.setOnPreferenceChangeListener(this);
61     }
62 
63     @Override
onPreferenceChange(Preference preference, Object newValue)64     public boolean onPreferenceChange(Preference preference, Object newValue) {
65         final boolean value = (Boolean) newValue;
66         if (preference == mSwitchPref) {
67             if (mPermissionState != null && value != mPermissionState.isPermissible()) {
68                 setCanManageMedia(value);
69                 logPermissionChange(value, mPackageName);
70                 refreshUi();
71             }
72             return true;
73         }
74         return false;
75     }
76 
setCanManageMedia(boolean newState)77     private void setCanManageMedia(boolean newState) {
78         mAppOpsManager.setUidMode(AppOpsManager.OP_MANAGE_MEDIA, mPackageInfo.applicationInfo.uid,
79                 newState ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED);
80     }
81 
logPermissionChange(boolean newState, String packageName)82     private void logPermissionChange(boolean newState, String packageName) {
83         mMetricsFeatureProvider.action(
84                 mMetricsFeatureProvider.getAttribution(getActivity()),
85                 SettingsEnums.ACTION_MEDIA_MANAGEMENT_APPS_TOGGLE,
86                 getMetricsCategory(),
87                 packageName,
88                 newState ? 1 : 0);
89     }
90 
91     @Override
refreshUi()92     protected boolean refreshUi() {
93         if (mPackageInfo == null || mPackageInfo.applicationInfo == null) {
94             return false;
95         }
96 
97         mPermissionState = mAppBridge.createPermissionState(mPackageName,
98                 mPackageInfo.applicationInfo.uid);
99         mSwitchPref.setEnabled(mPermissionState.permissionDeclared);
100         mSwitchPref.setChecked(mPermissionState.isPermissible());
101         return true;
102     }
103 
104     @Override
createDialog(int id, int errorCode)105     protected AlertDialog createDialog(int id, int errorCode) {
106         return null;
107     }
108 
109     @Override
getMetricsCategory()110     public int getMetricsCategory() {
111         return SettingsEnums.MEDIA_MANAGEMENT_APPS;
112     }
113 
114     /**
115      * Returns the string that states whether the app has access to
116      * {@link android.Manifest.permission#MANAGE_MEDIA}.
117      */
getSummary(Context context, AppEntry entry)118     public static int getSummary(Context context, AppEntry entry) {
119         final PermissionState state;
120         if (entry.extraInfo instanceof PermissionState) {
121             state = (PermissionState) entry.extraInfo;
122         } else {
123             state = new AppStateMediaManagementAppsBridge(context, null /* appState */,
124                     null /* callback */).createPermissionState(entry.info.packageName,
125                     entry.info.uid);
126         }
127 
128         return state.isPermissible() ? R.string.app_permission_summary_allowed
129                 : R.string.app_permission_summary_not_allowed;
130     }
131 }
132