Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 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 | |
| 17 | |
| 18 | package com.android.server.location; |
| 19 | |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 22 | import android.app.PendingIntent; |
| 23 | import android.location.Geofence; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 24 | import android.location.Location; |
| 25 | |
| 26 | /** |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 27 | * Represents state associated with a geofence |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 28 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 29 | public class GeofenceState { |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 30 | public final static int FLAG_ENTER = 0x01; |
| 31 | public final static int FLAG_EXIT = 0x02; |
| 32 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 33 | private static final int STATE_UNKNOWN = 0; |
| 34 | private static final int STATE_INSIDE = 1; |
| 35 | private static final int STATE_OUTSIDE = 2; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 36 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 37 | public final Geofence mFence; |
| 38 | private final Location mLocation; |
| 39 | public final long mExpireAt; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 40 | public final int mAllowedResolutionLevel; |
| 41 | public final int mUid; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 42 | public final String mPackageName; |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 43 | public final @Nullable String mFeatureId; |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 44 | public final @NonNull String mListenerIdentifier; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 45 | public final PendingIntent mIntent; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 46 | |
| 47 | int mState; // current state |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 48 | double mDistanceToCenter; // current distance to center of fence |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 49 | |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 50 | public GeofenceState(Geofence fence, long expireAt, int allowedResolutionLevel, int uid, |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 51 | String packageName, @Nullable String featureId, @NonNull String listenerIdentifier, |
| 52 | PendingIntent intent) { |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 53 | mState = STATE_UNKNOWN; |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 54 | mDistanceToCenter = Double.MAX_VALUE; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 55 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 56 | mFence = fence; |
| 57 | mExpireAt = expireAt; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 58 | mAllowedResolutionLevel = allowedResolutionLevel; |
| 59 | mUid = uid; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 60 | mPackageName = packageName; |
Philip P. Moltmann | 6c7377c | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 61 | mFeatureId = featureId; |
Philip P. Moltmann | bc8b48a | 2019-09-27 17:06:25 -0700 | [diff] [blame] | 62 | mListenerIdentifier = listenerIdentifier; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 63 | mIntent = intent; |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 64 | |
| 65 | mLocation = new Location(""); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 66 | mLocation.setLatitude(fence.getLatitude()); |
| 67 | mLocation.setLongitude(fence.getLongitude()); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Process a new location. |
| 72 | * @return FLAG_ENTER or FLAG_EXIT if the fence was crossed, 0 otherwise |
| 73 | */ |
| 74 | public int processLocation(Location location) { |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 75 | mDistanceToCenter = mLocation.distanceTo(location); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 76 | |
| 77 | int prevState = mState; |
| 78 | //TODO: inside/outside detection could be made more rigorous |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 79 | boolean inside = mDistanceToCenter <= Math.max(mFence.getRadius(), location.getAccuracy()); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 80 | if (inside) { |
| 81 | mState = STATE_INSIDE; |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 82 | if (prevState != STATE_INSIDE) { |
| 83 | return FLAG_ENTER; // return enter if previously exited or unknown |
| 84 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 85 | } else { |
| 86 | mState = STATE_OUTSIDE; |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 87 | if (prevState == STATE_INSIDE) { |
| 88 | return FLAG_EXIT; // return exit only if previously entered |
| 89 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Gets the distance from the current location to the fence's boundary. |
| 96 | * @return The distance or {@link Double#MAX_VALUE} if unknown. |
| 97 | */ |
| 98 | public double getDistanceToBoundary() { |
| 99 | if (Double.compare(mDistanceToCenter, Double.MAX_VALUE) == 0) { |
| 100 | return Double.MAX_VALUE; |
| 101 | } else { |
| 102 | return Math.abs(mFence.getRadius() - mDistanceToCenter); |
| 103 | } |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String toString() { |
| 108 | String state; |
| 109 | switch (mState) { |
| 110 | case STATE_INSIDE: |
| 111 | state = "IN"; |
| 112 | break; |
| 113 | case STATE_OUTSIDE: |
| 114 | state = "OUT"; |
| 115 | break; |
| 116 | default: |
| 117 | state = "?"; |
| 118 | } |
Victoria Lease | 4cd0a50 | 2012-11-02 16:24:08 -0700 | [diff] [blame] | 119 | return String.format("%s d=%.0f %s", mFence.toString(), mDistanceToCenter, state); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 120 | } |
| 121 | } |