Geofence.java revision 6fa9ad4afcd762aea519ff61811386c23d18ddb2
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 Geofence 24 */ 25public final class Geofence implements Parcelable { 26 /** @hide */ 27 public static final int TYPE_HORIZONTAL_CIRCLE = 1; 28 29 private final int mType; 30 private final double mLatitude; 31 private final double mLongitude; 32 private final float mRadius; 33 34 /** 35 * Create a horizontal, circular geofence. 36 * @param latitude latitude in degrees 37 * @param longitude longitude in degrees 38 * @param radius radius in meters 39 * @return a new geofence 40 * @throws IllegalArgumentException if any parameters are out of range 41 */ 42 public static Geofence createCircle(double latitude, double longitude, float radius) { 43 return new Geofence(latitude, longitude, radius); 44 } 45 46 private Geofence(double latitude, double longitude, float radius) { 47 checkRadius(radius); 48 checkLatLong(latitude, longitude); 49 mType = TYPE_HORIZONTAL_CIRCLE; 50 mLatitude = latitude; 51 mLongitude = longitude; 52 mRadius = radius; 53 } 54 55 /** @hide */ 56 public int getType() { 57 return mType; 58 } 59 60 /** @hide */ 61 public double getLatitude() { 62 return mLatitude; 63 } 64 65 /** @hide */ 66 public double getLongitude() { 67 return mLongitude; 68 } 69 70 /** @hide */ 71 public float getRadius() { 72 return mRadius; 73 } 74 75 private static void checkRadius(float radius) { 76 if (radius <= 0) { 77 throw new IllegalArgumentException("invalid radius: " + radius); 78 } 79 } 80 81 private static void checkLatLong(double latitude, double longitude) { 82 if (latitude > 90.0 || latitude < -90.0) { 83 throw new IllegalArgumentException("invalid latitude: " + latitude); 84 } 85 if (longitude > 180.0 || longitude < -180.0) { 86 throw new IllegalArgumentException("invalid longitude: " + longitude); 87 } 88 } 89 90 private static void checkType(int type) { 91 if (type != TYPE_HORIZONTAL_CIRCLE) { 92 throw new IllegalArgumentException("invalid type: " + type); 93 } 94 } 95 96 public static final Parcelable.Creator<Geofence> CREATOR = new Parcelable.Creator<Geofence>() { 97 @Override 98 public Geofence createFromParcel(Parcel in) { 99 int type = in.readInt(); 100 double latitude = in.readDouble(); 101 double longitude = in.readDouble(); 102 float radius = in.readFloat(); 103 checkType(type); 104 return Geofence.createCircle(latitude, longitude, radius); 105 } 106 @Override 107 public Geofence[] newArray(int size) { 108 return new Geofence[size]; 109 } 110 }; 111 112 @Override 113 public int describeContents() { 114 return 0; 115 } 116 117 @Override 118 public void writeToParcel(Parcel parcel, int flags) { 119 parcel.writeInt(mType); 120 parcel.writeDouble(mLatitude); 121 parcel.writeDouble(mLongitude); 122 parcel.writeFloat(mRadius); 123 } 124 125 private static String typeToString(int type) { 126 switch (type) { 127 case TYPE_HORIZONTAL_CIRCLE: 128 return "CIRCLE"; 129 default: 130 checkType(type); 131 return null; 132 } 133 } 134 135 @Override 136 public String toString() { 137 return String.format("Geofence[%s %.6f, %.6f %.0fm]", 138 typeToString(mType), mLatitude, mLongitude, mRadius); 139 } 140 141 @Override 142 public int hashCode() { 143 final int prime = 31; 144 int result = 1; 145 long temp; 146 temp = Double.doubleToLongBits(mLatitude); 147 result = prime * result + (int) (temp ^ (temp >>> 32)); 148 temp = Double.doubleToLongBits(mLongitude); 149 result = prime * result + (int) (temp ^ (temp >>> 32)); 150 result = prime * result + Float.floatToIntBits(mRadius); 151 result = prime * result + mType; 152 return result; 153 } 154 155 @Override 156 public boolean equals(Object obj) { 157 if (this == obj) 158 return true; 159 if (obj == null) 160 return false; 161 if (!(obj instanceof Geofence)) 162 return false; 163 Geofence other = (Geofence) obj; 164 if (mRadius != other.mRadius) 165 return false; 166 if (mLatitude != other.mLatitude) 167 return false; 168 if (mLongitude != other.mLongitude) 169 return false; 170 if (mType != other.mType) 171 return false; 172 return true; 173 } 174} 175