From cf2927f275c53099781dbf965acb7c7f8ed36e97 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sun, 31 Jul 2016 13:00:23 +0200 Subject: [PATCH] complete implementation of LocationAvailability --- .../gms/location/LocationAvailability.java | 110 ++++++++++++++++-- 1 file changed, 98 insertions(+), 12 deletions(-) diff --git a/play-services-api/src/main/java/com/google/android/gms/location/LocationAvailability.java b/play-services-api/src/main/java/com/google/android/gms/location/LocationAvailability.java index 3ee22ef9..f2f70201 100644 --- a/play-services-api/src/main/java/com/google/android/gms/location/LocationAvailability.java +++ b/play-services-api/src/main/java/com/google/android/gms/location/LocationAvailability.java @@ -16,35 +16,121 @@ package com.google.android.gms.location; +import android.content.Intent; + +import org.microg.gms.common.PublicApi; import org.microg.safeparcel.AutoSafeParcelable; import org.microg.safeparcel.SafeParceled; +import java.util.Arrays; + +/** + * Status on the availability of location data. + *

+ * Delivered from {@link LocationCallback} registered via {@link FusedLocationProviderApi#requestLocationUpdates(com.google.android.gms.common.api.GoogleApiClient, LocationRequest, LocationCallback, android.os.Looper) + * or from a PendingIntent registered via {@link FusedLocationProviderApi#requestLocationUpdates(com.google.android.gms.common.api.GoogleApiClient, LocationRequest, android.app.PendingIntent)}. + * It is also available on demand via {@link FusedLocationProviderApi#getLocationAvailability(com.google.android.gms.common.api.GoogleApiClient)}. + */ +@PublicApi public class LocationAvailability extends AutoSafeParcelable { + private static final String EXTRA_KEY = "com.google.android.gms.location.EXTRA_LOCATION_AVAILABILITY"; + + @PublicApi(exclude = true) + public static final int STATUS_SUCCESSFUL = 0; + @PublicApi(exclude = true) + public static final int STATUS_UNKNOWN = 1; + @PublicApi(exclude = true) + public static final int STATUS_TIMED_OUT_ON_SCAN = 2; + @PublicApi(exclude = true) + public static final int STATUS_NO_INFO_IN_DATABASE = 3; + @PublicApi(exclude = true) + public static final int STATUS_INVALID_SCAN = 4; + @PublicApi(exclude = true) + public static final int STATUS_UNABLE_TO_QUERY_DATABASE = 5; + @PublicApi(exclude = true) + public static final int STATUS_SCANS_DISABLED_IN_SETTINGS = 6; + @PublicApi(exclude = true) + public static final int STATUS_LOCATION_DISABLED_IN_SETTINGS = 7; + @PublicApi(exclude = true) + public static final int STATUS_IN_PROGRESS = 8; @SafeParceled(1000) - private int versionCode = 1; + private int versionCode = 2; @SafeParceled(1) - private int cellStatus; + @PublicApi(exclude = true) + public int cellStatus; @SafeParceled(2) - private int wifiStatus; + @PublicApi(exclude = true) + public int wifiStatus; @SafeParceled(3) - private long elapsedRealtimeNs; + @PublicApi(exclude = true) + public long elapsedRealtimeNs; @SafeParceled(4) - private int locationStatus; + @PublicApi(exclude = true) + public int locationStatus; - private LocationAvailability(int cellStatus, int wifiStatus, int elapsedRealtimeNs, int locationStatus) { - this.cellStatus = cellStatus; - this.wifiStatus = wifiStatus; - this.elapsedRealtimeNs = elapsedRealtimeNs; - this.locationStatus = this.locationStatus; + @PublicApi(exclude = true) + public LocationAvailability() { } - public static LocationAvailability create() { // TODO - return new LocationAvailability(0, 0, 0, 0); + @Override + public boolean equals(Object o) { + if (o instanceof LocationAvailability) { + LocationAvailability other = (LocationAvailability) o; + return other.cellStatus == cellStatus && other.wifiStatus == wifiStatus && other.elapsedRealtimeNs == elapsedRealtimeNs && other.locationStatus == locationStatus; + } + return false; + } + + /** + * Extracts the {@link LocationAvailability} from an Intent. + *

+ * This is a utility function which extracts the {@link LocationAvailability} from the extras + * of an Intent that was sent in response to a location request. + * + * @return a {@link LocationAvailability}, or null if the Intent doesn't contain this data. + */ + public static LocationAvailability extractLocationAvailability(Intent intent) { + if (!hasLocationAvailability(intent)) { + return null; + } + return intent.getParcelableExtra(EXTRA_KEY); + } + + /** + * Returns true if an Intent contains a {@link LocationAvailability}. + *

+ * This is a utility function that can be called from inside an intent receiver to make sure the + * received intent contains location availability data. + * + * @return true if the intent contains a {@link LocationAvailability}, false otherwise. + */ + public static boolean hasLocationAvailability(Intent intent) { + return intent != null && intent.hasExtra(EXTRA_KEY); + } + + @Override + public int hashCode() { + return Arrays.hashCode(new Object[]{locationStatus, cellStatus, wifiStatus, elapsedRealtimeNs}); + } + + /** + * Returns true if the device location is known and reasonably up to date within the hints + * requested by the active {@link LocationRequest}s. Failure to determine location may result + * from a number of causes including disabled location settings or an inability to retrieve + * sensor data in the device's environment. + */ + public boolean isLocationAvailable() { + return locationStatus < 1000; + } + + @Override + public String toString() { + return "LocationAvailability[isLocationAvailable: " + isLocationAvailable() + "]"; } public static final Creator CREATOR = new AutoCreator(LocationAvailability.class);