From 8b57ec0ede1362cec0d3d8bc973f86a779d64da2 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Fri, 8 Jan 2021 16:16:19 +0000 Subject: [PATCH] Fix shell commands to use "current" user To ensure a host test that uses the shell command can run as a secondary user, the user ID associated with the shell app (which runs as user 0 / system user) cannot be used to obtain the userId. This commit switches some commands which are user-sensitive over to using the current user. Bug: 172934905 Test: atest CtsLocationTimeZoneManagerHostTest (when current user is not system user) Change-Id: Ia0711d32986f6733c26aa220f83dd299c04320b6 --- .../TimeZoneDetectorService.java | 35 +++++++++++++++---- .../TimeZoneDetectorShellCommand.java | 16 ++++++--- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java index 865571e90338f..d0c632350270c 100644 --- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java +++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorService.java @@ -18,6 +18,8 @@ package com.android.server.timezonedetector; import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.UserIdInt; +import android.app.ActivityManager; import android.app.time.ITimeZoneDetectorListener; import android.app.time.TimeZoneCapabilitiesAndConfig; import android.app.time.TimeZoneConfiguration; @@ -26,12 +28,14 @@ import android.app.timezonedetector.ManualTimeZoneSuggestion; import android.app.timezonedetector.TelephonyTimeZoneSuggestion; import android.content.Context; import android.location.LocationManager; +import android.os.Binder; import android.os.Handler; import android.os.IBinder; import android.os.RemoteException; import android.os.ResultReceiver; import android.os.ShellCallback; import android.os.SystemProperties; +import android.os.UserHandle; import android.util.ArrayMap; import android.util.IndentingPrintWriter; import android.util.Slog; @@ -163,9 +167,13 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub @Override @NonNull public TimeZoneCapabilitiesAndConfig getCapabilitiesAndConfig() { + int userId = mCallerIdentityInjector.getCallingUserId(); + return getCapabilitiesAndConfig(userId); + } + + TimeZoneCapabilitiesAndConfig getCapabilitiesAndConfig(@UserIdInt int userId) { enforceManageTimeZoneDetectorPermission(); - int userId = mCallerIdentityInjector.getCallingUserId(); final long token = mCallerIdentityInjector.clearCallingIdentity(); try { ConfigurationInternal configurationInternal = @@ -178,13 +186,22 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub @Override public boolean updateConfiguration(@NonNull TimeZoneConfiguration configuration) { + int callingUserId = mCallerIdentityInjector.getCallingUserId(); + return updateConfiguration(callingUserId, configuration); + } + + boolean updateConfiguration( + @UserIdInt int userId, @NonNull TimeZoneConfiguration configuration) { + userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), + userId, false, false, "updateConfiguration", null); + enforceManageTimeZoneDetectorPermission(); + Objects.requireNonNull(configuration); - int callingUserId = mCallerIdentityInjector.getCallingUserId(); final long token = mCallerIdentityInjector.clearCallingIdentity(); try { - return mTimeZoneDetectorStrategy.updateConfiguration(callingUserId, configuration); + return mTimeZoneDetectorStrategy.updateConfiguration(userId, configuration); } finally { mCallerIdentityInjector.restoreCallingIdentity(token); } @@ -318,11 +335,17 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub return isGeoLocationTimeZoneDetectionEnabled(mContext); } - boolean isLocationEnabled() { + boolean isLocationEnabled(@UserIdInt int userId) { enforceManageTimeZoneDetectorPermission(); - return mContext.getSystemService(LocationManager.class) - .isLocationEnabledForUser(mContext.getUser()); + final long token = mCallerIdentityInjector.clearCallingIdentity(); + try { + UserHandle user = UserHandle.of(userId); + LocationManager locationManager = mContext.getSystemService(LocationManager.class); + return locationManager.isLocationEnabledForUser(user); + } finally { + mCallerIdentityInjector.restoreCallingIdentity(token); + } } @Override diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java index b2630300a6aa3..e965f55e49d76 100644 --- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java +++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java @@ -29,6 +29,7 @@ import android.app.time.TimeZoneConfiguration; import android.app.timezonedetector.ManualTimeZoneSuggestion; import android.app.timezonedetector.TelephonyTimeZoneSuggestion; import android.os.ShellCommand; +import android.os.UserHandle; import java.io.PrintWriter; import java.util.function.Consumer; @@ -76,7 +77,8 @@ class TimeZoneDetectorShellCommand extends ShellCommand { private int runIsAutoDetectionEnabled() { final PrintWriter pw = getOutPrintWriter(); - boolean enabled = mInterface.getCapabilitiesAndConfig() + int userId = UserHandle.USER_CURRENT; + boolean enabled = mInterface.getCapabilitiesAndConfig(userId) .getConfiguration() .isAutoDetectionEnabled(); pw.println(enabled); @@ -92,14 +94,16 @@ class TimeZoneDetectorShellCommand extends ShellCommand { private int runIsLocationEnabled() { final PrintWriter pw = getOutPrintWriter(); - boolean enabled = mInterface.isLocationEnabled(); + int userId = UserHandle.USER_CURRENT; + boolean enabled = mInterface.isLocationEnabled(userId); pw.println(enabled); return 0; } private int runIsGeoDetectionEnabled() { final PrintWriter pw = getOutPrintWriter(); - boolean enabled = mInterface.getCapabilitiesAndConfig() + int userId = UserHandle.USER_CURRENT; + boolean enabled = mInterface.getCapabilitiesAndConfig(userId) .getConfiguration() .isGeoDetectionEnabled(); pw.println(enabled); @@ -108,18 +112,20 @@ class TimeZoneDetectorShellCommand extends ShellCommand { private int runSetAutoDetectionEnabled() { boolean enabled = Boolean.parseBoolean(getNextArgRequired()); + int userId = UserHandle.USER_CURRENT; TimeZoneConfiguration configuration = new TimeZoneConfiguration.Builder() .setAutoDetectionEnabled(enabled) .build(); - return mInterface.updateConfiguration(configuration) ? 0 : 1; + return mInterface.updateConfiguration(userId, configuration) ? 0 : 1; } private int runSetGeoDetectionEnabled() { boolean enabled = Boolean.parseBoolean(getNextArgRequired()); + int userId = UserHandle.USER_CURRENT; TimeZoneConfiguration configuration = new TimeZoneConfiguration.Builder() .setGeoDetectionEnabled(enabled) .build(); - return mInterface.updateConfiguration(configuration) ? 0 : 1; + return mInterface.updateConfiguration(userId, configuration) ? 0 : 1; } private int runSuggestGeolocationTimeZone() {