Merge "Misc tidy up before adding geolocation classes" into rvc-dev

This commit is contained in:
Neil Fuller
2020-03-31 14:52:09 +00:00
committed by Android (Google) Code Review
6 changed files with 79 additions and 76 deletions

View File

@@ -30,7 +30,7 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
/** /**
* A time signal from a manual (user provided) source. * A time zone suggestion from a manual (user provided) source.
* *
* <p>{@code zoneId} contains the suggested time zone ID, e.g. "America/Los_Angeles". * <p>{@code zoneId} contains the suggested time zone ID, e.g. "America/Los_Angeles".
* *
@@ -124,7 +124,7 @@ public final class ManualTimeZoneSuggestion implements Parcelable {
@Override @Override
public String toString() { public String toString() {
return "ManualTimeSuggestion{" return "ManualTimeZoneSuggestion{"
+ "mZoneId=" + mZoneId + "mZoneId=" + mZoneId
+ ", mDebugInfo=" + mDebugInfo + ", mDebugInfo=" + mDebugInfo
+ '}'; + '}';

View File

@@ -40,6 +40,15 @@ public interface TimeZoneDetector {
return suggestion; return suggestion;
} }
/**
* Suggests the current time zone, determined using the user's manually entered information, to
* the detector. The detector may ignore the signal based on system settings.
*
* @hide
*/
@RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE)
void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion);
/** /**
* Suggests the current time zone, determined using telephony signals, to the detector. The * Suggests the current time zone, determined using telephony signals, to the detector. The
* detector may ignore the signal based on system settings, whether better information is * detector may ignore the signal based on system settings, whether better information is
@@ -49,13 +58,4 @@ public interface TimeZoneDetector {
*/ */
@RequiresPermission(android.Manifest.permission.SUGGEST_TELEPHONY_TIME_AND_ZONE) @RequiresPermission(android.Manifest.permission.SUGGEST_TELEPHONY_TIME_AND_ZONE)
void suggestTelephonyTimeZone(@NonNull TelephonyTimeZoneSuggestion timeZoneSuggestion); void suggestTelephonyTimeZone(@NonNull TelephonyTimeZoneSuggestion timeZoneSuggestion);
/**
* Suggests the current time zone, determined for the user's manually information, to the
* detector. The detector may ignore the signal based on system settings.
*
* @hide
*/
@RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE)
void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion);
} }

View File

@@ -39,18 +39,6 @@ public final class TimeZoneDetectorImpl implements TimeZoneDetector {
ServiceManager.getServiceOrThrow(Context.TIME_ZONE_DETECTOR_SERVICE)); ServiceManager.getServiceOrThrow(Context.TIME_ZONE_DETECTOR_SERVICE));
} }
@Override
public void suggestTelephonyTimeZone(@NonNull TelephonyTimeZoneSuggestion timeZoneSuggestion) {
if (DEBUG) {
Log.d(TAG, "suggestTelephonyTimeZone called: " + timeZoneSuggestion);
}
try {
mITimeZoneDetectorService.suggestTelephonyTimeZone(timeZoneSuggestion);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
@Override @Override
public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) {
if (DEBUG) { if (DEBUG) {
@@ -62,4 +50,16 @@ public final class TimeZoneDetectorImpl implements TimeZoneDetector {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }
} }
@Override
public void suggestTelephonyTimeZone(@NonNull TelephonyTimeZoneSuggestion timeZoneSuggestion) {
if (DEBUG) {
Log.d(TAG, "suggestTelephonyTimeZone called: " + timeZoneSuggestion);
}
try {
mITimeZoneDetectorService.suggestTelephonyTimeZone(timeZoneSuggestion);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
} }

View File

@@ -129,7 +129,7 @@ public class TelephonyTimeZoneSuggestionTest {
} }
@Test(expected = RuntimeException.class) @Test(expected = RuntimeException.class)
public void testBuilderValidates_emptyZone_badMatchType() { public void testBuilderValidates_nullZone_badMatchType() {
TelephonyTimeZoneSuggestion.Builder builder = TelephonyTimeZoneSuggestion.Builder builder =
new TelephonyTimeZoneSuggestion.Builder(SLOT_INDEX); new TelephonyTimeZoneSuggestion.Builder(SLOT_INDEX);
// No zone ID, so match type should be left unset. // No zone ID, so match type should be left unset.

View File

@@ -22,9 +22,12 @@ import android.app.timezonedetector.TelephonyTimeZoneSuggestion;
import java.io.PrintWriter; import java.io.PrintWriter;
/** /**
* The interface for the class that implement the time detection algorithm used by the * The interface for the class that implements the time detection algorithm used by the
* {@link TimeZoneDetectorService}. * {@link TimeZoneDetectorService}.
* *
* <p>The strategy uses suggestions to decide whether to modify the device's time zone setting
* and what to set it to.
*
* <p>Most calls will be handled by a single thread but that is not true for all calls. For example * <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 * {@link #dump(PrintWriter, String[])}) may be called on a different thread so implementations must
* handle thread safety. * handle thread safety.
@@ -33,7 +36,9 @@ import java.io.PrintWriter;
*/ */
public interface TimeZoneDetectorStrategy { public interface TimeZoneDetectorStrategy {
/** Process the suggested manually-entered (i.e. user sourced) time zone. */ /**
* Suggests a time zone for the device using manually-entered (i.e. user sourced) information.
*/
void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion); void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion);
/** /**
@@ -41,8 +46,7 @@ public interface TimeZoneDetectorStrategy {
* {@link TelephonyTimeZoneSuggestion#getZoneId()} is {@code null}. The suggestion is scoped to * {@link TelephonyTimeZoneSuggestion#getZoneId()} is {@code null}. The suggestion is scoped to
* a specific {@link TelephonyTimeZoneSuggestion#getSlotIndex() slotIndex}. * a specific {@link TelephonyTimeZoneSuggestion#getSlotIndex() slotIndex}.
* See {@link TelephonyTimeZoneSuggestion} for an explanation of the metadata associated with a * See {@link TelephonyTimeZoneSuggestion} for an explanation of the metadata associated with a
* suggestion. The strategy uses suggestions to decide whether to modify the device's time zone * suggestion.
* setting and what to set it to.
*/ */
void suggestTelephonyTimeZone(@NonNull TelephonyTimeZoneSuggestion suggestion); void suggestTelephonyTimeZone(@NonNull TelephonyTimeZoneSuggestion suggestion);

View File

@@ -76,39 +76,7 @@ public class TimeZoneDetectorServiceTest {
} }
@Test(expected = SecurityException.class) @Test(expected = SecurityException.class)
public void testSuggestTelephonyTime_withoutPermission() { public void testSuggestManualTimeZone_withoutPermission() {
doThrow(new SecurityException("Mock"))
.when(mMockContext).enforceCallingPermission(anyString(), any());
TelephonyTimeZoneSuggestion timeZoneSuggestion = createTelephonyTimeZoneSuggestion();
try {
mTimeZoneDetectorService.suggestTelephonyTimeZone(timeZoneSuggestion);
fail();
} finally {
verify(mMockContext).enforceCallingPermission(
eq(android.Manifest.permission.SUGGEST_TELEPHONY_TIME_AND_ZONE),
anyString());
}
}
@Test
public void testSuggestTelephonyTimeZone() throws Exception {
doNothing().when(mMockContext).enforceCallingPermission(anyString(), any());
TelephonyTimeZoneSuggestion timeZoneSuggestion = createTelephonyTimeZoneSuggestion();
mTimeZoneDetectorService.suggestTelephonyTimeZone(timeZoneSuggestion);
mTestHandler.assertTotalMessagesEnqueued(1);
verify(mMockContext).enforceCallingPermission(
eq(android.Manifest.permission.SUGGEST_TELEPHONY_TIME_AND_ZONE),
anyString());
mTestHandler.waitForMessagesToBeProcessed();
mStubbedTimeZoneDetectorStrategy.verifySuggestTelephonyTimeZoneCalled(timeZoneSuggestion);
}
@Test(expected = SecurityException.class)
public void testSuggestManualTime_withoutPermission() {
doThrow(new SecurityException("Mock")) doThrow(new SecurityException("Mock"))
.when(mMockContext).enforceCallingOrSelfPermission(anyString(), any()); .when(mMockContext).enforceCallingOrSelfPermission(anyString(), any());
ManualTimeZoneSuggestion timeZoneSuggestion = createManualTimeZoneSuggestion(); ManualTimeZoneSuggestion timeZoneSuggestion = createManualTimeZoneSuggestion();
@@ -139,6 +107,38 @@ public class TimeZoneDetectorServiceTest {
mStubbedTimeZoneDetectorStrategy.verifySuggestManualTimeZoneCalled(timeZoneSuggestion); mStubbedTimeZoneDetectorStrategy.verifySuggestManualTimeZoneCalled(timeZoneSuggestion);
} }
@Test(expected = SecurityException.class)
public void testSuggestTelephonyTimeZone_withoutPermission() {
doThrow(new SecurityException("Mock"))
.when(mMockContext).enforceCallingPermission(anyString(), any());
TelephonyTimeZoneSuggestion timeZoneSuggestion = createTelephonyTimeZoneSuggestion();
try {
mTimeZoneDetectorService.suggestTelephonyTimeZone(timeZoneSuggestion);
fail();
} finally {
verify(mMockContext).enforceCallingPermission(
eq(android.Manifest.permission.SUGGEST_TELEPHONY_TIME_AND_ZONE),
anyString());
}
}
@Test
public void testSuggestTelephonyTimeZone() throws Exception {
doNothing().when(mMockContext).enforceCallingPermission(anyString(), any());
TelephonyTimeZoneSuggestion timeZoneSuggestion = createTelephonyTimeZoneSuggestion();
mTimeZoneDetectorService.suggestTelephonyTimeZone(timeZoneSuggestion);
mTestHandler.assertTotalMessagesEnqueued(1);
verify(mMockContext).enforceCallingPermission(
eq(android.Manifest.permission.SUGGEST_TELEPHONY_TIME_AND_ZONE),
anyString());
mTestHandler.waitForMessagesToBeProcessed();
mStubbedTimeZoneDetectorStrategy.verifySuggestTelephonyTimeZoneCalled(timeZoneSuggestion);
}
@Test @Test
public void testDump() { public void testDump() {
when(mMockContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)) when(mMockContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP))
@@ -165,6 +165,10 @@ public class TimeZoneDetectorServiceTest {
mStubbedTimeZoneDetectorStrategy.verifyHandleAutoTimeZoneDetectionChangedCalled(); mStubbedTimeZoneDetectorStrategy.verifyHandleAutoTimeZoneDetectionChangedCalled();
} }
private static ManualTimeZoneSuggestion createManualTimeZoneSuggestion() {
return new ManualTimeZoneSuggestion("TestZoneId");
}
private static TelephonyTimeZoneSuggestion createTelephonyTimeZoneSuggestion() { private static TelephonyTimeZoneSuggestion createTelephonyTimeZoneSuggestion() {
int slotIndex = 1234; int slotIndex = 1234;
return new TelephonyTimeZoneSuggestion.Builder(slotIndex) return new TelephonyTimeZoneSuggestion.Builder(slotIndex)
@@ -174,26 +178,22 @@ public class TimeZoneDetectorServiceTest {
.build(); .build();
} }
private static ManualTimeZoneSuggestion createManualTimeZoneSuggestion() {
return new ManualTimeZoneSuggestion("TestZoneId");
}
private static class StubbedTimeZoneDetectorStrategy implements TimeZoneDetectorStrategy { private static class StubbedTimeZoneDetectorStrategy implements TimeZoneDetectorStrategy {
// Call tracking. // Call tracking.
private TelephonyTimeZoneSuggestion mLastTelephonySuggestion;
private ManualTimeZoneSuggestion mLastManualSuggestion; private ManualTimeZoneSuggestion mLastManualSuggestion;
private TelephonyTimeZoneSuggestion mLastTelephonySuggestion;
private boolean mHandleAutoTimeZoneDetectionChangedCalled; private boolean mHandleAutoTimeZoneDetectionChangedCalled;
private boolean mDumpCalled; private boolean mDumpCalled;
@Override @Override
public void suggestTelephonyTimeZone(TelephonyTimeZoneSuggestion timeZoneSuggestion) { public void suggestManualTimeZone(ManualTimeZoneSuggestion timeZoneSuggestion) {
mLastTelephonySuggestion = timeZoneSuggestion; mLastManualSuggestion = timeZoneSuggestion;
} }
@Override @Override
public void suggestManualTimeZone(ManualTimeZoneSuggestion timeZoneSuggestion) { public void suggestTelephonyTimeZone(TelephonyTimeZoneSuggestion timeZoneSuggestion) {
mLastManualSuggestion = timeZoneSuggestion; mLastTelephonySuggestion = timeZoneSuggestion;
} }
@Override @Override
@@ -207,18 +207,18 @@ public class TimeZoneDetectorServiceTest {
} }
void resetCallTracking() { void resetCallTracking() {
mLastTelephonySuggestion = null;
mLastManualSuggestion = null; mLastManualSuggestion = null;
mLastTelephonySuggestion = null;
mHandleAutoTimeZoneDetectionChangedCalled = false; mHandleAutoTimeZoneDetectionChangedCalled = false;
mDumpCalled = false; mDumpCalled = false;
} }
void verifySuggestTelephonyTimeZoneCalled(TelephonyTimeZoneSuggestion expectedSuggestion) { void verifySuggestManualTimeZoneCalled(ManualTimeZoneSuggestion expectedSuggestion) {
assertEquals(expectedSuggestion, mLastTelephonySuggestion); assertEquals(expectedSuggestion, mLastManualSuggestion);
} }
public void verifySuggestManualTimeZoneCalled(ManualTimeZoneSuggestion expectedSuggestion) { void verifySuggestTelephonyTimeZoneCalled(TelephonyTimeZoneSuggestion expectedSuggestion) {
assertEquals(expectedSuggestion, mLastManualSuggestion); assertEquals(expectedSuggestion, mLastTelephonySuggestion);
} }
void verifyHandleAutoTimeZoneDetectionChangedCalled() { void verifyHandleAutoTimeZoneDetectionChangedCalled() {
@@ -229,5 +229,4 @@ public class TimeZoneDetectorServiceTest {
assertTrue(mDumpCalled); assertTrue(mDumpCalled);
} }
} }
} }