Home | History | Annotate | Download | only in applications
      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 android.app.AlertDialog;
     19 import android.app.AppOpsManager;
     20 import android.content.ActivityNotFoundException;
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.pm.ApplicationInfo;
     25 import android.content.pm.PackageManager;
     26 import android.os.Bundle;
     27 import android.os.UserHandle;
     28 import android.preference.Preference;
     29 import android.preference.Preference.OnPreferenceChangeListener;
     30 import android.preference.Preference.OnPreferenceClickListener;
     31 import android.preference.SwitchPreference;
     32 import android.provider.Settings;
     33 import android.util.Log;
     34 
     35 import com.android.internal.logging.MetricsLogger;
     36 import com.android.settings.InstrumentedFragment;
     37 import com.android.settings.R;
     38 import com.android.settings.applications.AppStateAppOpsBridge.PermissionState;
     39 import com.android.settings.applications.AppStateWriteSettingsBridge.WriteSettingsState;
     40 import com.android.settingslib.applications.ApplicationsState;
     41 import com.android.settingslib.applications.ApplicationsState.AppEntry;
     42 
     43 import java.util.List;
     44 
     45 public class WriteSettingsDetails extends AppInfoWithHeader implements OnPreferenceChangeListener,
     46         OnPreferenceClickListener {
     47 
     48     private static final String KEY_APP_OPS_PREFERENCE_SCREEN = "app_ops_preference_screen";
     49     private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
     50     private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference";
     51     private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description";
     52     private static final String LOG_TAG = "WriteSettingsDetails";
     53 
     54     private static final int [] APP_OPS_OP_CODE = {
     55             AppOpsManager.OP_WRITE_SETTINGS
     56     };
     57 
     58     // Use a bridge to get the overlay details but don't initialize it to connect with all state.
     59     // TODO: Break out this functionality into its own class.
     60     private AppStateWriteSettingsBridge mAppBridge;
     61     private AppOpsManager mAppOpsManager;
     62     private SwitchPreference mSwitchPref;
     63     private Preference mWriteSettingsPrefs;
     64     private Preference mWriteSettingsDesc;
     65     private Intent mSettingsIntent;
     66     private WriteSettingsState mWriteSettingsState;
     67 
     68     @Override
     69     public void onCreate(Bundle savedInstanceState) {
     70         super.onCreate(savedInstanceState);
     71 
     72         Context context = getActivity();
     73         mAppBridge = new AppStateWriteSettingsBridge(context, mState, null);
     74         mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
     75 
     76         addPreferencesFromResource(R.xml.app_ops_permissions_details);
     77         mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
     78         mWriteSettingsPrefs = findPreference(KEY_APP_OPS_SETTINGS_PREFS);
     79         mWriteSettingsDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC);
     80 
     81         getPreferenceScreen().setTitle(R.string.write_settings);
     82         mSwitchPref.setTitle(R.string.permit_write_settings);
     83         mWriteSettingsPrefs.setTitle(R.string.write_settings_preference);
     84         mWriteSettingsDesc.setSummary(R.string.write_settings_description);
     85 
     86         mSwitchPref.setOnPreferenceChangeListener(this);
     87         mWriteSettingsPrefs.setOnPreferenceClickListener(this);
     88 
     89         mSettingsIntent = new Intent(Intent.ACTION_MAIN)
     90                 .addCategory(Settings.INTENT_CATEGORY_USAGE_ACCESS_CONFIG)
     91                 .setPackage(mPackageName);
     92     }
     93 
     94     @Override
     95     public boolean onPreferenceClick(Preference preference) {
     96         if (preference == mWriteSettingsPrefs) {
     97             if (mSettingsIntent != null) {
     98                 try {
     99                     getActivity().startActivityAsUser(mSettingsIntent, new UserHandle(mUserId));
    100                 } catch (ActivityNotFoundException e) {
    101                     Log.w(LOG_TAG, "Unable to launch write system settings " + mSettingsIntent, e);
    102                 }
    103             }
    104             return true;
    105         }
    106         return false;
    107     }
    108 
    109     @Override
    110     public boolean onPreferenceChange(Preference preference, Object newValue) {
    111         if (preference == mSwitchPref) {
    112             if (mWriteSettingsState != null && (Boolean) newValue != mWriteSettingsState
    113                     .isPermissible()) {
    114                 setCanWriteSettings(!mWriteSettingsState.isPermissible());
    115                 refreshUi();
    116             }
    117             return true;
    118         }
    119         return false;
    120     }
    121 
    122     private void setCanWriteSettings(boolean newState) {
    123         mAppOpsManager.setMode(AppOpsManager.OP_WRITE_SETTINGS,
    124                 mPackageInfo.applicationInfo.uid, mPackageName, newState
    125                 ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED);
    126     }
    127 
    128     private boolean canWriteSettings(String pkgName) {
    129         int result = mAppOpsManager.noteOpNoThrow(AppOpsManager.OP_WRITE_SETTINGS,
    130                 mPackageInfo.applicationInfo.uid, pkgName);
    131         if (result == AppOpsManager.MODE_ALLOWED) {
    132             return true;
    133         }
    134 
    135         return false;
    136     }
    137 
    138     @Override
    139     protected boolean refreshUi() {
    140         mWriteSettingsState = mAppBridge.getWriteSettingsInfo(mPackageName,
    141                 mPackageInfo.applicationInfo.uid);
    142 
    143         boolean canWrite = mWriteSettingsState.isPermissible();
    144         mSwitchPref.setChecked(canWrite);
    145         // you can't ask a user for a permission you didn't even declare!
    146         mSwitchPref.setEnabled(mWriteSettingsState.permissionDeclared);
    147         mWriteSettingsPrefs.setEnabled(canWrite);
    148         getPreferenceScreen().removePreference(mWriteSettingsPrefs);
    149 
    150         return true;
    151     }
    152 
    153     @Override
    154     protected AlertDialog createDialog(int id, int errorCode) {
    155         return null;
    156     }
    157 
    158     @Override
    159     protected int getMetricsCategory() {
    160         return MetricsLogger.SYSTEM_ALERT_WINDOW_APPS;
    161     }
    162 
    163     public static CharSequence getSummary(Context context, AppEntry entry) {
    164         if (entry.extraInfo != null) {
    165             return getSummary(context, new WriteSettingsState((PermissionState)entry
    166                     .extraInfo));
    167         }
    168 
    169         // fallback if entry.extrainfo is null - although this should not happen
    170         return getSummary(context, entry.info.packageName);
    171     }
    172 
    173     public static CharSequence getSummary(Context context, WriteSettingsState writeSettingsState) {
    174         return context.getString(writeSettingsState.isPermissible() ? R.string.write_settings_on :
    175                 R.string.write_settings_off);
    176     }
    177 
    178     public static CharSequence getSummary(Context context, String pkg) {
    179         // first check if pkg is a system pkg
    180         boolean isSystem = false;
    181         PackageManager packageManager = context.getPackageManager();
    182         try {
    183             ApplicationInfo appInfo = packageManager.getApplicationInfo(pkg, 0);
    184             if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
    185                 isSystem = true;
    186             }
    187         } catch (PackageManager.NameNotFoundException e) {
    188             // pkg doesn't even exist?
    189             Log.w(LOG_TAG, "Package " + pkg + " not found", e);
    190             return context.getString(R.string.write_settings_off);
    191         }
    192 
    193         AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context
    194                 .APP_OPS_SERVICE);
    195         List<AppOpsManager.PackageOps> packageOps = appOpsManager.getPackagesForOps(
    196                 APP_OPS_OP_CODE);
    197         if (packageOps == null) {
    198             return context.getString(R.string.write_settings_off);
    199         }
    200 
    201         int uid = isSystem ? 0 : -1;
    202         for (AppOpsManager.PackageOps packageOp : packageOps) {
    203             if (pkg.equals(packageOp.getPackageName())) {
    204                 uid = packageOp.getUid();
    205                 break;
    206             }
    207         }
    208 
    209         if (uid == -1) {
    210             return context.getString(R.string.write_settings_off);
    211         }
    212 
    213         int mode = appOpsManager.noteOpNoThrow(AppOpsManager.OP_WRITE_SETTINGS, uid, pkg);
    214         return context.getString((mode == AppOpsManager.MODE_ALLOWED) ?
    215                 R.string.write_settings_on : R.string.write_settings_off);
    216     }
    217 }
    218