/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.location; import android.annotation.NonNull; import android.compat.annotation.UnsupportedAppUsage; import android.os.Parcel; import android.os.Parcelable; import android.os.SystemClock; import android.util.TimeUtils; import com.android.internal.util.Preconditions; import java.util.Objects; /** * Represents a geographical boundary, also known as a geofence. * *
Currently only circular geofences are supported and they do not support altitude changes.
*
* @hide
*/
public final class Geofence implements Parcelable {
private final double mLatitude;
private final double mLongitude;
private final float mRadius;
private long mExpirationRealtimeMs;
/**
* Create a circular geofence (on a flat, horizontal plane).
*
* @param latitude latitude in degrees, between -90 and +90 inclusive
* @param longitude longitude in degrees, between -180 and +180 inclusive
* @param radius radius in meters
* @return a new geofence
* @throws IllegalArgumentException if any parameters are out of range
*/
public static Geofence createCircle(double latitude, double longitude, float radius,
long expirationRealtimeMs) {
return new Geofence(latitude, longitude, radius, expirationRealtimeMs);
}
Geofence(double latitude, double longitude, float radius, long expirationRealtimeMs) {
Preconditions.checkArgumentInRange(latitude, -90.0, 90.0, "latitude");
Preconditions.checkArgumentInRange(longitude, -180.0, 180.0, "latitude");
Preconditions.checkArgument(radius > 0, "invalid radius: %f", radius);
mLatitude = latitude;
mLongitude = longitude;
mRadius = radius;
mExpirationRealtimeMs = expirationRealtimeMs;
}
public double getLatitude() {
return mLatitude;
}
public double getLongitude() {
return mLongitude;
}
public float getRadius() {
return mRadius;
}
public boolean isExpired() {
return isExpired(SystemClock.elapsedRealtime());
}
/**
* Returns true if this geofence is expired with reference to the given realtime.
*/
public boolean isExpired(long referenceRealtimeMs) {
return referenceRealtimeMs >= mExpirationRealtimeMs;
}
@UnsupportedAppUsage
public static final @NonNull Parcelable.Creator