Merge "Fix shell commands to use "current" user"

This commit is contained in:
Neil Fuller
2021-01-12 10:20:18 +00:00
committed by Android (Google) Code Review
2 changed files with 40 additions and 11 deletions

View File

@@ -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

View File

@@ -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() {