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 4edd33d66b..a22b3de82c 100644 --- a/res/values-de/strings.xml +++ b/res/values-de/strings.xml @@ -4945,6 +4945,9 @@ + Sensorzugriff von Benutzer-Apps kontrollieren + Zugriff auf Sensoren + Keine installierte App hat Sensorzugriff angefordert. "Streamen beenden" "VoLTE deaktivieren?" "Dadurch wird auch deine 5G-Verbindung deaktiviert.\nWährend eines Sprachanrufs kannst du das Internet nicht nutzen und manche Apps funktionieren möglicherweise nicht." diff --git a/res/values-fr/strings.xml b/res/values-fr/strings.xml index 005aa05953..7c2ad2eaf3 100644 --- a/res/values-fr/strings.xml +++ b/res/values-fr/strings.xml @@ -4944,6 +4944,9 @@ + 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. "Arrêter la diffusion" "Désactiver VoLTE ?" "Cela désactive également votre connexion 5G.\nLorsque vous effectuez un appel vocal, vous n\'avez pas accès à Internet et certaines applications peuvent ne pas fonctionner." diff --git a/res/values/strings.xml b/res/values/strings.xml index 0c6fe1a541..120e82f4dd 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -12237,4 +12237,9 @@ Don\u2019t connect Connect + + + Control sensor access for user apps + Access to Sensors + No installed apps have requested sensors access. diff --git a/res/xml/special_access.xml b/res/xml/special_access.xml index 6ee87f4664..f65ee68f7e 100644 --- a/res/xml/special_access.xml +++ b/res/xml/special_access.xml @@ -154,6 +154,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(); + } + } +}