blob: 1fd737f3de20b127a5d4efb2f12f1b388cddfb36 [file] [log] [blame]
Nick Pellye0fd6932012-07-11 10:26:13 -07001/*
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
18package com.android.server.location;
19
Nick Pelly6fa9ad42012-07-16 12:18:23 -070020import android.app.PendingIntent;
21import android.location.Geofence;
Nick Pellye0fd6932012-07-11 10:26:13 -070022import android.location.Location;
23
24/**
Nick Pelly6fa9ad42012-07-16 12:18:23 -070025 * Represents state associated with a geofence
Nick Pellye0fd6932012-07-11 10:26:13 -070026 */
Nick Pelly6fa9ad42012-07-16 12:18:23 -070027public class GeofenceState {
Nick Pellye0fd6932012-07-11 10:26:13 -070028 public final static int FLAG_ENTER = 0x01;
29 public final static int FLAG_EXIT = 0x02;
30
Nick Pelly6fa9ad42012-07-16 12:18:23 -070031 private static final int STATE_UNKNOWN = 0;
32 private static final int STATE_INSIDE = 1;
33 private static final int STATE_OUTSIDE = 2;
Nick Pellye0fd6932012-07-11 10:26:13 -070034
Nick Pelly6fa9ad42012-07-16 12:18:23 -070035 public final Geofence mFence;
36 private final Location mLocation;
37 public final long mExpireAt;
38 public final String mPackageName;
39 public final PendingIntent mIntent;
Nick Pellye0fd6932012-07-11 10:26:13 -070040
41 int mState; // current state
42 double mDistance; // current distance to center of fence
43
Nick Pelly6fa9ad42012-07-16 12:18:23 -070044 public GeofenceState(Geofence fence, Location prevLocation, long expireAt,
45 String packageName, PendingIntent intent) {
Nick Pellye0fd6932012-07-11 10:26:13 -070046 mState = STATE_UNKNOWN;
47
Nick Pelly6fa9ad42012-07-16 12:18:23 -070048 mFence = fence;
49 mExpireAt = expireAt;
50 mPackageName = packageName;
51 mIntent = intent;
Nick Pellye0fd6932012-07-11 10:26:13 -070052
53 mLocation = new Location("");
Nick Pelly6fa9ad42012-07-16 12:18:23 -070054 mLocation.setLatitude(fence.getLatitude());
55 mLocation.setLongitude(fence.getLongitude());
Nick Pellye0fd6932012-07-11 10:26:13 -070056
57 if (prevLocation != null) {
58 processLocation(prevLocation);
59 }
60 }
61
62 /**
63 * Process a new location.
64 * @return FLAG_ENTER or FLAG_EXIT if the fence was crossed, 0 otherwise
65 */
66 public int processLocation(Location location) {
67 mDistance = mLocation.distanceTo(location);
68
69 int prevState = mState;
70 //TODO: inside/outside detection could be made more rigorous
Nick Pelly6fa9ad42012-07-16 12:18:23 -070071 boolean inside = mDistance <= Math.max(mFence.getRadius(), location.getAccuracy());
Nick Pellye0fd6932012-07-11 10:26:13 -070072 if (inside) {
73 mState = STATE_INSIDE;
74 } else {
75 mState = STATE_OUTSIDE;
76 }
77
78 if (prevState != 0 && mState != prevState) {
79 if (mState == STATE_INSIDE) return FLAG_ENTER;
80 if (mState == STATE_OUTSIDE) return FLAG_EXIT;
81 }
82 return 0;
83 }
84
85 public double getDistance() {
86 return mDistance;
87 }
88
89 @Override
90 public String toString() {
91 String state;
92 switch (mState) {
93 case STATE_INSIDE:
94 state = "IN";
95 break;
96 case STATE_OUTSIDE:
97 state = "OUT";
98 break;
99 default:
100 state = "?";
101 }
Nick Pelly6fa9ad42012-07-16 12:18:23 -0700102 return String.format("%s d=%.0f %s", mFence.toString(), mDistance, state);
Nick Pellye0fd6932012-07-11 10:26:13 -0700103 }
104}