diff --git a/core/java/android/util/TimeUtils.java b/core/java/android/util/TimeUtils.java index 4b3afbaada646..e8d3459970220 100644 --- a/core/java/android/util/TimeUtils.java +++ b/core/java/android/util/TimeUtils.java @@ -335,7 +335,17 @@ public class TimeUtils { /** @hide Just for debugging; not internationalized. */ public static String formatUptime(long time) { - final long diff = time - SystemClock.uptimeMillis(); + return formatTime(time, SystemClock.uptimeMillis()); + } + + /** @hide Just for debugging; not internationalized. */ + public static String formatRealtime(long time) { + return formatTime(time, SystemClock.elapsedRealtime()); + } + + /** @hide Just for debugging; not internationalized. */ + public static String formatTime(long time, long referenceTime) { + long diff = time - referenceTime; if (diff > 0) { return time + " (in " + diff + " ms)"; } diff --git a/location/java/android/location/LocationRequest.java b/location/java/android/location/LocationRequest.java index f3e4d81285bd7..4dd1a29d85957 100644 --- a/location/java/android/location/LocationRequest.java +++ b/location/java/android/location/LocationRequest.java @@ -71,8 +71,7 @@ import com.android.internal.util.Preconditions; * heavy-weight work after receiving an update - such as using the network. * *
Activities should strongly consider removing all location
- * request when entering the background
- * (for example at {@link android.app.Activity#onPause}), or
+ * request when entering the background, or
* at least swap the request to a larger interval and lower quality.
* Future version of the location manager may automatically perform background
* throttling on behalf of applications.
@@ -146,38 +145,32 @@ public final class LocationRequest implements Parcelable {
*/
public static final int POWER_HIGH = 203;
- /**
- * By default, mFastestInterval = FASTEST_INTERVAL_MULTIPLE * mInterval
- */
+ private static final long DEFAULT_INTERVAL_MS = 60 * 60 * 1000; // 1 hour
private static final double FASTEST_INTERVAL_FACTOR = 6.0; // 6x
- @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private int mQuality = POWER_LOW;
@UnsupportedAppUsage
- private long mInterval = 60 * 60 * 1000; // 60 minutes
+ private String mProvider;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private long mFastestInterval = (long) (mInterval / FASTEST_INTERVAL_FACTOR); // 10 minutes
- @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private boolean mExplicitFastestInterval = false;
- @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private long mExpireAt = Long.MAX_VALUE; // no expiry
- private long mExpireIn = Long.MAX_VALUE; // no expiry
- @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private int mNumUpdates = Integer.MAX_VALUE; // no expiry
- @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private float mSmallestDisplacement = 0.0f; // meters
+ private int mQuality;
@UnsupportedAppUsage
- private WorkSource mWorkSource = null;
+ private long mInterval;
+ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
+ private long mFastestInterval;
+ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
+ private boolean mExplicitFastestInterval;
+ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
+ private long mExpireAt;
+ private long mExpireIn;
+ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
+ private int mNumUpdates;
+ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
+ private float mSmallestDisplacement;
@UnsupportedAppUsage
- private boolean mHideFromAppOps = false; // True if this request shouldn't be counted by AppOps
- private boolean mLocationSettingsIgnored = false;
-
+ private boolean mHideFromAppOps;
+ private boolean mLocationSettingsIgnored;
+ private boolean mLowPowerMode;
@UnsupportedAppUsage
- private String mProvider = LocationManager.FUSED_PROVIDER;
- // for deprecated APIs that explicitly request a provider
-
- /** If true, GNSS chipset will make strong tradeoffs to substantially restrict power use */
- private boolean mLowPowerMode = false;
+ private @Nullable WorkSource mWorkSource;
/**
* Create a location request with default parameters.
@@ -260,23 +253,71 @@ public final class LocationRequest implements Parcelable {
/** @hide */
public LocationRequest() {
+ this(
+ /* provider= */ LocationManager.FUSED_PROVIDER,
+ /* quality= */ POWER_LOW,
+ /* interval= */ DEFAULT_INTERVAL_MS,
+ /* fastestInterval= */ (long) (DEFAULT_INTERVAL_MS / FASTEST_INTERVAL_FACTOR),
+ /* explicitFastestInterval= */ false,
+ /* expireAt= */ Long.MAX_VALUE,
+ /* expireIn= */ Long.MAX_VALUE,
+ /* numUpdates= */ Integer.MAX_VALUE,
+ /* smallestDisplacement= */ 0,
+ /* hideFromAppOps= */ false,
+ /* lowPowerMode= */ false,
+ /* locationSettingsIgnored= */ false,
+ /* workSource= */ null);
}
/** @hide */
public LocationRequest(LocationRequest src) {
- mQuality = src.mQuality;
- mInterval = src.mInterval;
- mFastestInterval = src.mFastestInterval;
- mExplicitFastestInterval = src.mExplicitFastestInterval;
- mExpireAt = src.mExpireAt;
- mExpireIn = src.mExpireIn;
- mNumUpdates = src.mNumUpdates;
- mSmallestDisplacement = src.mSmallestDisplacement;
- mProvider = src.mProvider;
- mWorkSource = src.mWorkSource;
- mHideFromAppOps = src.mHideFromAppOps;
- mLowPowerMode = src.mLowPowerMode;
- mLocationSettingsIgnored = src.mLocationSettingsIgnored;
+ this(
+ src.mProvider,
+ src.mQuality,
+ src.mInterval,
+ src.mFastestInterval,
+ src.mExplicitFastestInterval,
+ src.mExpireAt,
+ src.mExpireIn,
+ src.mNumUpdates,
+ src.mSmallestDisplacement,
+ src.mHideFromAppOps,
+ src.mLowPowerMode,
+ src.mLocationSettingsIgnored,
+ src.mWorkSource);
+ }
+
+ private LocationRequest(
+ @NonNull String provider,
+ int quality,
+ long intervalMs,
+ long fastestIntervalMs,
+ boolean explicitFastestInterval,
+ long expireAt,
+ long expireInMs,
+ int numUpdates,
+ float smallestDisplacementM,
+ boolean hideFromAppOps,
+ boolean locationSettingsIgnored,
+ boolean lowPowerMode,
+ WorkSource workSource) {
+ Preconditions.checkArgument(provider != null, "invalid provider: null");
+ checkQuality(quality);
+
+ mProvider = provider;
+ mQuality = quality;
+ mInterval = intervalMs;
+ mFastestInterval = fastestIntervalMs;
+ mExplicitFastestInterval = explicitFastestInterval;
+ mExpireAt = expireAt;
+ mExpireIn = expireInMs;
+ mNumUpdates = numUpdates;
+ mSmallestDisplacement = Preconditions.checkArgumentInRange(smallestDisplacementM, 0,
+ Float.MAX_VALUE, "smallestDisplacementM");
+ mHideFromAppOps = hideFromAppOps;
+ mLowPowerMode = lowPowerMode;
+ mLocationSettingsIgnored = locationSettingsIgnored;
+ mWorkSource = workSource;
}
/**
@@ -567,7 +608,7 @@ public final class LocationRequest implements Parcelable {
/** Sets the provider to use for this location request. */
public @NonNull LocationRequest setProvider(@NonNull String provider) {
- checkProvider(provider);
+ Preconditions.checkArgument(provider != null, "invalid provider: null");
mProvider = provider;
return this;
}
@@ -580,9 +621,9 @@ public final class LocationRequest implements Parcelable {
/** @hide */
@SystemApi
- public @NonNull LocationRequest setSmallestDisplacement(float meters) {
- checkDisplacement(meters);
- mSmallestDisplacement = meters;
+ public @NonNull LocationRequest setSmallestDisplacement(float smallestDisplacementM) {
+ mSmallestDisplacement = Preconditions.checkArgumentInRange(smallestDisplacementM, 0,
+ Float.MAX_VALUE, "smallestDisplacementM");
return this;
}
@@ -653,40 +694,24 @@ public final class LocationRequest implements Parcelable {
}
}
- @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private static void checkDisplacement(float meters) {
- if (meters < 0.0f) {
- throw new IllegalArgumentException("invalid displacement: " + meters);
- }
- }
-
- @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
- private static void checkProvider(String name) {
- if (name == null) {
- throw new IllegalArgumentException("invalid provider: null");
- }
- }
-
- public static final @android.annotation.NonNull Parcelable.Creator