Geofence.java revision b17223b6f5c4d894530c6412fc5dac2d6be403dc
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.location;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * Represents a geographical boundary, also known as a geofence.
24 *
25 * <p>Currently only circular geofences are supported.
26 */
27public final class Geofence implements Parcelable {
28    /** @hide */
29    public static final int TYPE_HORIZONTAL_CIRCLE = 1;
30
31    private final int mType;
32    private final double mLatitude;
33    private final double mLongitude;
34    private final float mRadius;
35
36    /**
37     * Create a horizontal, circular geofence.
38     *
39     * @param latitude latitude in degrees
40     * @param longitude longitude in degrees
41     * @param radius radius in meters
42     * @return a new geofence
43     * @throws IllegalArgumentException if any parameters are out of range
44     */
45    public static Geofence createCircle(double latitude, double longitude, float radius) {
46        return new Geofence(latitude, longitude, radius);
47    }
48
49    private Geofence(double latitude, double longitude, float radius) {
50        checkRadius(radius);
51        checkLatLong(latitude, longitude);
52        mType = TYPE_HORIZONTAL_CIRCLE;
53        mLatitude = latitude;
54        mLongitude = longitude;
55        mRadius = radius;
56    }
57
58    /** @hide */
59    public int getType() {
60        return mType;
61    }
62
63    /** @hide */
64    public double getLatitude() {
65        return mLatitude;
66    }
67
68    /** @hide */
69    public double getLongitude() {
70        return mLongitude;
71    }
72
73    /** @hide */
74    public float getRadius() {
75        return mRadius;
76    }
77
78    private static void checkRadius(float radius) {
79        if (radius <= 0) {
80            throw new IllegalArgumentException("invalid radius: " + radius);
81        }
82    }
83
84    private static void checkLatLong(double latitude, double longitude) {
85        if (latitude > 90.0 || latitude < -90.0) {
86            throw new IllegalArgumentException("invalid latitude: " + latitude);
87        }
88        if (longitude > 180.0 || longitude < -180.0) {
89            throw new IllegalArgumentException("invalid longitude: " + longitude);
90        }
91    }
92
93    private static void checkType(int type) {
94        if (type != TYPE_HORIZONTAL_CIRCLE) {
95            throw new IllegalArgumentException("invalid type: " + type);
96        }
97    }
98
99    public static final Parcelable.Creator<Geofence> CREATOR = new Parcelable.Creator<Geofence>() {
100        @Override
101        public Geofence createFromParcel(Parcel in) {
102            int type = in.readInt();
103            double latitude = in.readDouble();
104            double longitude = in.readDouble();
105            float radius = in.readFloat();
106            checkType(type);
107            return Geofence.createCircle(latitude, longitude, radius);
108        }
109        @Override
110        public Geofence[] newArray(int size) {
111            return new Geofence[size];
112        }
113    };
114
115    @Override
116    public int describeContents() {
117        return 0;
118    }
119
120    @Override
121    public void writeToParcel(Parcel parcel, int flags) {
122        parcel.writeInt(mType);
123        parcel.writeDouble(mLatitude);
124        parcel.writeDouble(mLongitude);
125        parcel.writeFloat(mRadius);
126    }
127
128    private static String typeToString(int type) {
129        switch (type) {
130            case TYPE_HORIZONTAL_CIRCLE:
131                return "CIRCLE";
132            default:
133                checkType(type);
134                return null;
135        }
136    }
137
138    @Override
139    public String toString() {
140        return String.format("Geofence[%s %.6f, %.6f %.0fm]",
141                typeToString(mType), mLatitude, mLongitude, mRadius);
142    }
143
144    @Override
145    public int hashCode() {
146        final int prime = 31;
147        int result = 1;
148        long temp;
149        temp = Double.doubleToLongBits(mLatitude);
150        result = prime * result + (int) (temp ^ (temp >>> 32));
151        temp = Double.doubleToLongBits(mLongitude);
152        result = prime * result + (int) (temp ^ (temp >>> 32));
153        result = prime * result + Float.floatToIntBits(mRadius);
154        result = prime * result + mType;
155        return result;
156    }
157
158    /**
159     * Two geofences are equal if they have identical properties.
160     */
161    @Override
162    public boolean equals(Object obj) {
163        if (this == obj)
164            return true;
165        if (obj == null)
166            return false;
167        if (!(obj instanceof Geofence))
168            return false;
169        Geofence other = (Geofence) obj;
170        if (mRadius != other.mRadius)
171            return false;
172        if (mLatitude != other.mLatitude)
173            return false;
174        if (mLongitude != other.mLongitude)
175            return false;
176        if (mType != other.mType)
177            return false;
178        return true;
179    }
180}
181