From 6abae2a070588b4840f2711b42102c3af71f2fd4 Mon Sep 17 00:00:00 2001 From: Evan Severson Date: Thu, 29 Jul 2021 10:03:14 -0700 Subject: [PATCH] Check the current user in the system server for SensorPrivacyManager Instead of querying for the current user application side we can just pass UserHandle.CURRENT_USER, this eliminates an IPC and the need for INTERACT_ACCROSS_USERS permission which should not be needed for these APIs. Test: CtsSensorPrivacyTestCases Bug: 194806109 Change-Id: I3a0aed0c403338aaebdfb4e2791a84b4e7e3cb3a --- .../hardware/SensorPrivacyManager.java | 18 +++++------------- .../android/server/SensorPrivacyService.java | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/core/java/android/hardware/SensorPrivacyManager.java b/core/java/android/hardware/SensorPrivacyManager.java index 4526ab770bc64..fa7ce11c5414b 100644 --- a/core/java/android/hardware/SensorPrivacyManager.java +++ b/core/java/android/hardware/SensorPrivacyManager.java @@ -24,12 +24,12 @@ import android.annotation.SystemApi; import android.annotation.SystemService; import android.annotation.TestApi; import android.annotation.UserIdInt; -import android.app.ActivityManager; import android.content.Context; import android.os.Binder; import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.UserHandle; import android.service.SensorPrivacyIndividualEnabledSensorProto; import android.service.SensorPrivacyToggleSourceProto; import android.util.ArrayMap; @@ -379,7 +379,7 @@ public final class SensorPrivacyManager { @SystemApi @RequiresPermission(Manifest.permission.OBSERVE_SENSOR_PRIVACY) public boolean isSensorPrivacyEnabled(@Sensors.Sensor int sensor) { - return isSensorPrivacyEnabled(sensor, getCurrentUserId()); + return isSensorPrivacyEnabled(sensor, UserHandle.USER_CURRENT); } /** @@ -410,7 +410,7 @@ public final class SensorPrivacyManager { @RequiresPermission(Manifest.permission.MANAGE_SENSOR_PRIVACY) public void setSensorPrivacy(@Sources.Source int source, @Sensors.Sensor int sensor, boolean enable) { - setSensorPrivacy(source, sensor, enable, getCurrentUserId()); + setSensorPrivacy(source, sensor, enable, UserHandle.USER_CURRENT); } /** @@ -446,7 +446,7 @@ public final class SensorPrivacyManager { @RequiresPermission(Manifest.permission.MANAGE_SENSOR_PRIVACY) public void setSensorPrivacyForProfileGroup(@Sources.Source int source, @Sensors.Sensor int sensor, boolean enable) { - setSensorPrivacyForProfileGroup(source , sensor, enable, getCurrentUserId()); + setSensorPrivacyForProfileGroup(source , sensor, enable, UserHandle.USER_CURRENT); } /** @@ -481,7 +481,7 @@ public final class SensorPrivacyManager { @RequiresPermission(Manifest.permission.MANAGE_SENSOR_PRIVACY) public void suppressSensorPrivacyReminders(int sensor, boolean suppress) { - suppressSensorPrivacyReminders(sensor, suppress, getCurrentUserId()); + suppressSensorPrivacyReminders(sensor, suppress, UserHandle.USER_CURRENT); } /** @@ -609,12 +609,4 @@ public final class SensorPrivacyManager { } } - private int getCurrentUserId() { - try { - return ActivityManager.getService().getCurrentUserId(); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); - } - return 0; - } } diff --git a/services/core/java/com/android/server/SensorPrivacyService.java b/services/core/java/com/android/server/SensorPrivacyService.java index 019e4ea207efc..ecd620e78c3c1 100644 --- a/services/core/java/com/android/server/SensorPrivacyService.java +++ b/services/core/java/com/android/server/SensorPrivacyService.java @@ -21,7 +21,6 @@ import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_CAMERA; import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_MICROPHONE; import static android.app.ActivityManager.RunningServiceInfo; import static android.app.ActivityManager.RunningTaskInfo; -import static android.app.ActivityManager.getCurrentUser; import static android.app.AppOpsManager.MODE_ALLOWED; import static android.app.AppOpsManager.MODE_IGNORED; import static android.app.AppOpsManager.OP_CAMERA; @@ -718,6 +717,9 @@ public final class SensorPrivacyService extends SystemService { public void setIndividualSensorPrivacy(@UserIdInt int userId, @SensorPrivacyManager.Sources.Source int source, int sensor, boolean enable) { enforceManageSensorPrivacyPermission(); + if (userId == UserHandle.USER_CURRENT) { + userId = mCurrentUser; + } if (!canChangeIndividualSensorPrivacy(userId, sensor)) { return; } @@ -843,6 +845,9 @@ public final class SensorPrivacyService extends SystemService { public void setIndividualSensorPrivacyForProfileGroup(@UserIdInt int userId, @SensorPrivacyManager.Sources.Source int source, int sensor, boolean enable) { enforceManageSensorPrivacyPermission(); + if (userId == UserHandle.USER_CURRENT) { + userId = mCurrentUser; + } int parentId = mUserManagerInternal.getProfileParentId(userId); forAllUsers(userId2 -> { if (parentId == mUserManagerInternal.getProfileParentId(userId2)) { @@ -896,6 +901,9 @@ public final class SensorPrivacyService extends SystemService { @Override public boolean isIndividualSensorPrivacyEnabled(@UserIdInt int userId, int sensor) { enforceObserveSensorPrivacyPermission(); + if (userId == UserHandle.USER_CURRENT) { + userId = mCurrentUser; + } synchronized (mLock) { return isIndividualSensorPrivacyEnabledLocked(userId, sensor); } @@ -1213,6 +1221,9 @@ public final class SensorPrivacyService extends SystemService { public void suppressIndividualSensorPrivacyReminders(int userId, int sensor, IBinder token, boolean suppress) { enforceManageSensorPrivacyPermission(); + if (userId == UserHandle.USER_CURRENT) { + userId = mCurrentUser; + } Objects.requireNonNull(token); Pair key = new Pair<>(sensor, UserHandle.of(userId)); @@ -1898,9 +1909,9 @@ public final class SensorPrivacyService extends SystemService { if (!mIsInEmergencyCall) { mIsInEmergencyCall = true; if (mSensorPrivacyServiceImpl - .isIndividualSensorPrivacyEnabled(getCurrentUser(), MICROPHONE)) { + .isIndividualSensorPrivacyEnabled(mCurrentUser, MICROPHONE)) { mSensorPrivacyServiceImpl.setIndividualSensorPrivacyUnchecked( - getCurrentUser(), OTHER, MICROPHONE, false); + mCurrentUser, OTHER, MICROPHONE, false); mMicUnmutedForEmergencyCall = true; } else { mMicUnmutedForEmergencyCall = false; @@ -1915,7 +1926,7 @@ public final class SensorPrivacyService extends SystemService { mIsInEmergencyCall = false; if (mMicUnmutedForEmergencyCall) { mSensorPrivacyServiceImpl.setIndividualSensorPrivacyUnchecked( - getCurrentUser(), OTHER, MICROPHONE, true); + mCurrentUser, OTHER, MICROPHONE, true); mMicUnmutedForEmergencyCall = false; } }