From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MSe1969 Date: Sat, 14 Nov 2020 15:17:37 +0100 Subject: [PATCH] Special Access: Add an option to administer Sensor access Accesses the added AppOp for OP_OTHER_SENSORS Change-Id: I79c0ed4ab97494434edc6c308a8a54bd123c02ee --- res/values-de/strings.xml | 3 + res/values-fr/strings.xml | 3 + res/values/strings.xml | 5 + res/xml/special_access.xml | 7 + .../specialaccess/sensor/SensorAccess.java | 178 ++++++++++++++++++ 5 files changed, 196 insertions(+) create mode 100644 src/com/android/settings/applications/specialaccess/sensor/SensorAccess.java diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml index 0893b5c318..a81d55df07 100644 --- a/res/values-de/strings.xml +++ b/res/values-de/strings.xml @@ -4671,4 +4671,7 @@ + Sensorzugriff von Benutzer-Apps kontrollieren + Zugriff auf Sensoren + Keine installierte App hat Sensorzugriff angefordert. diff --git a/res/values-fr/strings.xml b/res/values-fr/strings.xml index a1461ea6a2..c7aa02d04f 100644 --- a/res/values-fr/strings.xml +++ b/res/values-fr/strings.xml @@ -4670,4 +4670,7 @@ + Contrôler l\'accès des applications utilisateurs aux capteurs + Access aux Capteurs + Aucune app installée n\'a demandé de l\'accès aux capteurs. diff --git a/res/values/strings.xml b/res/values/strings.xml index 5b4f19c18b..d8a769645e 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -11426,6 +11426,11 @@ + + Control sensor access for user apps + Access to Sensors + No installed apps have requested sensors access. + A device wants to access your messages. Tap for details. diff --git a/res/xml/special_access.xml b/res/xml/special_access.xml index f846298341..bee9d6e2eb 100644 --- a/res/xml/special_access.xml +++ b/res/xml/special_access.xml @@ -145,6 +145,13 @@ android:value="com.android.settings.Settings$ChangeWifiStateActivity" /> + + + apps = new ArrayList<>(); + final List installed = mPackageManager.getInstalledApplications(0); + if (installed != null) { + for (ApplicationInfo app : installed) { + // Skip system apps + if (isUserApp(app.packageName)) { + // Only apps effectively having the Op OTHER_SENSORS + if (mAppOpsManager.getOpsForPackage(getPackageUid(app.packageName), + app.packageName, new int[]{AppOpsManager.OP_OTHER_SENSORS}) != null) + apps.add(app); + } + } + } + Collections.sort(apps, new PackageItemInfo.DisplayNameComparator(mPackageManager)); + for (ApplicationInfo app : apps) { + final String pkg = app.packageName; + final CharSequence label = app.loadLabel(mPackageManager); + final SwitchPreference pref = new SwitchPreference(getPrefContext()); + pref.setPersistent(false); + pref.setIcon(app.loadIcon(mPackageManager)); + pref.setTitle(label); + updateState(pref, pkg); + pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + boolean switchOn = (Boolean) newValue; + mAppOpsManager.setMode(AppOpsManager.OP_OTHER_SENSORS, getPackageUid(pkg), pkg, + switchOn ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); + pref.setChecked(switchOn); + return false; + } + }); + screen.addPreference(pref); + } + } + + public void updateState(SwitchPreference preference, String pkg) { + final int mode = mAppOpsManager + .checkOpNoThrow(AppOpsManager.OP_OTHER_SENSORS, getPackageUid(pkg), pkg); + if (mode == AppOpsManager.MODE_ERRORED) { + preference.setChecked(false); + } else { + final boolean checked = mode != AppOpsManager.MODE_IGNORED; + preference.setChecked(checked); + } + } + + private boolean isUserApp(String pkg) { + ApplicationInfo appInfo; + try { + appInfo = mPackageManager.getApplicationInfo(pkg, + PackageManager.GET_DISABLED_COMPONENTS + | PackageManager.GET_UNINSTALLED_PACKAGES); + } catch (PackageManager.NameNotFoundException e) { + Log.w(TAG, "Unable to find info for package " + pkg); + return false; + } + return ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0); + } + + private int getPackageUid(String pkg) { + int uid; + try { + uid = mPackageManager.getPackageUid(pkg, 0); + } catch (PackageManager.NameNotFoundException e) { + // We shouldn't hit this, ever. What can we even do after this? + uid = -1; + } + return uid; + } + + private final class SettingObserver extends ContentObserver { + public SettingObserver() { + super(new Handler(Looper.getMainLooper())); + } + + @Override + public void onChange(boolean selfChange, Uri uri) { + reloadList(); + } + } +}