Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cce4dcd3 authored by Neil Fuller's avatar Neil Fuller
Browse files

Correct a permission check / add a test

This change corrects a permission check in TimeZoneDetectorService
which is now covered by a test.

A TestHandler common to the timezonedetector and timedetector classes
has been extracted and made less flaky.

There are some changes included to make the equivalent time zone and
time classes consistent and to improve method names.

Bug: 140712361
Test: atest com.android.server.timezonedetector
Test: atest com.android.server.timedetector

Change-Id: Ic380ede6c7276d6b80f0fc74b30bba8c89c07fcc
parent f5f137b9
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -37,6 +37,9 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Objects;

/**
 * The implementation of ITimeDetectorService.aidl.
 */
public final class TimeDetectorService extends ITimeDetectorService.Stub {
    private static final String TAG = "TimeDetectorService";

@@ -75,7 +78,7 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub {
                Settings.Global.getUriFor(Settings.Global.AUTO_TIME), true,
                new ContentObserver(handler) {
                    public void onChange(boolean selfChange) {
                        timeDetectorService.handleAutoTimeDetectionToggle();
                        timeDetectorService.handleAutoTimeDetectionChanged();
                    }
                });

@@ -114,8 +117,9 @@ public final class TimeDetectorService extends ITimeDetectorService.Stub {
        mHandler.post(() -> mTimeDetectorStrategy.suggestNetworkTime(timeSignal));
    }

    /** Internal method for handling the auto time setting being changed. */
    @VisibleForTesting
    public void handleAutoTimeDetectionToggle() {
    public void handleAutoTimeDetectionChanged() {
        mHandler.post(mTimeDetectorStrategy::handleAutoTimeDetectionChanged);
    }

+2 −2
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ import android.os.TimestampedValue;
import java.io.PrintWriter;

/**
 * The interface for classes that implement the time detection algorithm used by the
 * TimeDetectorService.
 * The interface for the class that implements the time detection algorithm used by the
 * {@link TimeDetectorService}.
 *
 * <p>Most calls will be handled by a single thread but that is not true for all calls. For example
 * {@link #dump(PrintWriter, String[])}) may be called on a different thread so implementations must
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * An implementation of TimeDetectorStrategy that passes phone and manual suggestions to
 * An implementation of {@link TimeDetectorStrategy} that passes phone and manual suggestions to
 * {@link AlarmManager}. When there are multiple phone sources, the one with the lowest ID is used
 * unless the data becomes too stale.
 *
+2 −2
Original line number Diff line number Diff line
@@ -24,9 +24,9 @@ import android.os.SystemProperties;
import android.provider.Settings;

/**
 * The real implementation of {@link TimeZoneDetectorStrategy.Callback}.
 * The real implementation of {@link TimeZoneDetectorStrategyImpl.Callback}.
 */
public final class TimeZoneDetectorCallbackImpl implements TimeZoneDetectorStrategy.Callback {
public final class TimeZoneDetectorCallbackImpl implements TimeZoneDetectorStrategyImpl.Callback {

    private static final String TIMEZONE_PROPERTY = "persist.sys.timezone";

+17 −7
Original line number Diff line number Diff line
@@ -67,19 +67,21 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub

    private static TimeZoneDetectorService create(@NonNull Context context) {
        final TimeZoneDetectorStrategy timeZoneDetectorStrategy =
                TimeZoneDetectorStrategy.create(context);
                TimeZoneDetectorStrategyImpl.create(context);

        Handler handler = FgThread.getHandler();
        TimeZoneDetectorService service =
                new TimeZoneDetectorService(context, handler, timeZoneDetectorStrategy);

        ContentResolver contentResolver = context.getContentResolver();
        contentResolver.registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.AUTO_TIME_ZONE), true,
                new ContentObserver(handler) {
                    public void onChange(boolean selfChange) {
                        timeZoneDetectorStrategy.handleAutoTimeZoneDetectionChange();
                        service.handleAutoTimeZoneDetectionChanged();
                    }
                });

        return new TimeZoneDetectorService(context, handler, timeZoneDetectorStrategy);
        return service;
    }

    @VisibleForTesting
@@ -111,17 +113,25 @@ public final class TimeZoneDetectorService extends ITimeZoneDetectorService.Stub
            @Nullable String[] args) {
        if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;

        mTimeZoneDetectorStrategy.dumpState(pw, args);
        mTimeZoneDetectorStrategy.dump(pw, args);
    }

    /** Internal method for handling the auto time zone setting being changed. */
    @VisibleForTesting
    public void handleAutoTimeZoneDetectionChanged() {
        mHandler.post(mTimeZoneDetectorStrategy::handleAutoTimeZoneDetectionChanged);
    }

    private void enforceSuggestPhoneTimeZonePermission() {
        mContext.enforceCallingPermission(
                android.Manifest.permission.SET_TIME_ZONE, "set time zone");
                android.Manifest.permission.SUGGEST_PHONE_TIME_AND_ZONE,
                "suggest phone time and time zone");
    }

    private void enforceSuggestManualTimeZonePermission() {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.SET_TIME_ZONE, "set time zone");
                android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE,
                "suggest manual time and time zone");
    }
}
Loading