1 /* 2 * Copyright (C) 2013 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 17 package android.hardware.location; 18 19 import android.annotation.SystemApi; 20 21 /** 22 * This class represents the characteristics of the geofence. 23 * 24 * <p> Use this in conjunction with {@link GeofenceHardware} APIs. 25 * 26 * @hide 27 */ 28 @SystemApi 29 public final class GeofenceHardwareRequest { 30 static final int GEOFENCE_TYPE_CIRCLE = 0; 31 private int mType; 32 private double mLatitude; 33 private double mLongitude; 34 private double mRadius; 35 private int mLastTransition = GeofenceHardware.GEOFENCE_UNCERTAIN; 36 private int mUnknownTimer = 30000; // 30 secs 37 private int mMonitorTransitions = GeofenceHardware.GEOFENCE_UNCERTAIN | 38 GeofenceHardware.GEOFENCE_ENTERED | GeofenceHardware.GEOFENCE_EXITED; 39 private int mNotificationResponsiveness = 5000; // 5 secs 40 private int mSourceTechnologies = GeofenceHardware.SOURCE_TECHNOLOGY_GNSS; 41 setCircularGeofence(double latitude, double longitude, double radius)42 private void setCircularGeofence(double latitude, double longitude, double radius) { 43 mLatitude = latitude; 44 mLongitude = longitude; 45 mRadius = radius; 46 mType = GEOFENCE_TYPE_CIRCLE; 47 } 48 49 /** 50 * Create a circular geofence. 51 * 52 * @param latitude Latitude of the geofence 53 * @param longitude Longitude of the geofence 54 * @param radius Radius of the geofence (in meters) 55 */ createCircularGeofence(double latitude, double longitude, double radius)56 public static GeofenceHardwareRequest createCircularGeofence(double latitude, 57 double longitude, double radius) { 58 GeofenceHardwareRequest geofenceRequest = new GeofenceHardwareRequest(); 59 geofenceRequest.setCircularGeofence(latitude, longitude, radius); 60 return geofenceRequest; 61 } 62 63 /** 64 * Set the last known transition of the geofence. 65 * 66 * @param lastTransition The current state of the geofence. Can be one of 67 * {@link GeofenceHardware#GEOFENCE_ENTERED}, {@link GeofenceHardware#GEOFENCE_EXITED}, 68 * {@link GeofenceHardware#GEOFENCE_UNCERTAIN}. 69 */ setLastTransition(int lastTransition)70 public void setLastTransition(int lastTransition) { 71 mLastTransition = lastTransition; 72 } 73 74 /** 75 * Set the unknown timer for this geofence. 76 * 77 * @param unknownTimer The time limit after which the 78 * {@link GeofenceHardware#GEOFENCE_UNCERTAIN} transition 79 * should be triggered. This paramter is defined in milliseconds. 80 */ setUnknownTimer(int unknownTimer)81 public void setUnknownTimer(int unknownTimer) { 82 mUnknownTimer = unknownTimer; 83 } 84 85 /** 86 * Set the transitions to be monitored. 87 * 88 * @param monitorTransitions Bitwise OR of {@link GeofenceHardware#GEOFENCE_ENTERED}, 89 * {@link GeofenceHardware#GEOFENCE_EXITED}, {@link GeofenceHardware#GEOFENCE_UNCERTAIN} 90 */ setMonitorTransitions(int monitorTransitions)91 public void setMonitorTransitions(int monitorTransitions) { 92 mMonitorTransitions = monitorTransitions; 93 } 94 95 /** 96 * Set the notification responsiveness of the geofence. 97 * 98 * @param notificationResponsiveness (milliseconds) Defines the best-effort description 99 * of how soon should the callback be called when the transition 100 * associated with the Geofence is triggered. For instance, if 101 * set to 1000 millseconds with {@link GeofenceHardware#GEOFENCE_ENTERED}, 102 * the callback will be called 1000 milliseconds within entering 103 * the geofence. 104 */ setNotificationResponsiveness(int notificationResponsiveness)105 public void setNotificationResponsiveness(int notificationResponsiveness) { 106 mNotificationResponsiveness = notificationResponsiveness; 107 } 108 109 /** 110 * Set the source technologies to use while tracking the geofence. 111 * The value is the bit-wise of one or several source fields defined in 112 * {@link GeofenceHardware}. 113 * 114 * @param sourceTechnologies The set of source technologies to use. 115 */ setSourceTechnologies(int sourceTechnologies)116 public void setSourceTechnologies(int sourceTechnologies) { 117 int sourceTechnologiesAll = GeofenceHardware.SOURCE_TECHNOLOGY_GNSS 118 | GeofenceHardware.SOURCE_TECHNOLOGY_WIFI 119 | GeofenceHardware.SOURCE_TECHNOLOGY_SENSORS 120 | GeofenceHardware.SOURCE_TECHNOLOGY_CELL 121 | GeofenceHardware.SOURCE_TECHNOLOGY_BLUETOOTH; 122 123 int sanitizedSourceTechnologies = (sourceTechnologies & sourceTechnologiesAll); 124 if (sanitizedSourceTechnologies == 0) { 125 throw new IllegalArgumentException("At least one valid source technology must be set."); 126 } 127 128 mSourceTechnologies = sanitizedSourceTechnologies; 129 } 130 131 /** 132 * Returns the latitude of this geofence. 133 */ getLatitude()134 public double getLatitude() { 135 return mLatitude; 136 } 137 138 /** 139 * Returns the longitude of this geofence. 140 */ getLongitude()141 public double getLongitude() { 142 return mLongitude; 143 } 144 145 /** 146 * Returns the radius of this geofence. 147 */ getRadius()148 public double getRadius() { 149 return mRadius; 150 } 151 152 /** 153 * Returns transitions monitored for this geofence. 154 */ getMonitorTransitions()155 public int getMonitorTransitions() { 156 return mMonitorTransitions; 157 } 158 159 /** 160 * Returns the unknownTimer of this geofence. 161 */ getUnknownTimer()162 public int getUnknownTimer() { 163 return mUnknownTimer; 164 } 165 166 /** 167 * Returns the notification responsiveness of this geofence. 168 */ getNotificationResponsiveness()169 public int getNotificationResponsiveness() { 170 return mNotificationResponsiveness; 171 } 172 173 /** 174 * Returns the last transition of this geofence. 175 */ getLastTransition()176 public int getLastTransition() { 177 return mLastTransition; 178 } 179 180 /** 181 * Returns the source technologies to track this geofence. 182 */ getSourceTechnologies()183 public int getSourceTechnologies() { 184 return mSourceTechnologies; 185 } 186 getType()187 int getType() { 188 return mType; 189 } 190 } 191