blob: 6f792be774272ac8a0ea8743c6fc6587851d7613 [file] [log] [blame]
saberian984e52f2012-06-05 18:23:53 -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
17package android.bordeaux.services;
18
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070019import android.app.AlarmManager;
20import android.app.PendingIntent;
21import android.content.BroadcastReceiver;
Ruei-sung Linf0f78442012-08-13 19:04:29 -070022import android.content.Context;
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070023import android.content.Intent;
24import android.content.IntentFilter;
Ruei-sung Linf0f78442012-08-13 19:04:29 -070025import android.location.Criteria;
26import android.location.Location;
27import android.location.LocationListener;
28import android.location.LocationManager;
29import android.location.LocationProvider;
30import android.os.Bundle;
31import android.os.Handler;
32import android.os.HandlerThread;
33import android.os.Message;
34import android.os.Process;
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070035import android.os.SystemClock;
36import android.text.format.Time;
saberian984e52f2012-06-05 18:23:53 -070037import android.util.Log;
38import java.util.HashMap;
Wei Hua9c3a7dc2012-08-28 13:46:31 -070039import java.util.List;
saberian984e52f2012-06-05 18:23:53 -070040import java.util.Map;
41
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -070042// TODO: add functionality to detect speed (use GPS) when needed
43// withouth draining the battery quickly
Ruei-sung Linf0f78442012-08-13 19:04:29 -070044public class LocationStatsAggregator extends Aggregator {
saberian984e52f2012-06-05 18:23:53 -070045 final String TAG = "LocationStatsAggregator";
46 public static final String CURRENT_LOCATION = "Current Location";
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -070047 public static final String CURRENT_SPEED = "Current Speed";
Ruei-sung Linc7c9cf12012-08-28 11:35:19 -070048 public static final String UNKNOWN_LOCATION = "Unknown Location";
Ruei-sung Linf0f78442012-08-13 19:04:29 -070049
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070050 private static final long REPEAT_INTERVAL = 120000;
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -070051
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070052 private static final long FRESH_THRESHOLD = 90000;
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -070053
Ruei-sung Linf0f78442012-08-13 19:04:29 -070054 private static final int LOCATION_CHANGE = 1;
55
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -070056 // record time when the location provider is set
Ruei-sung Linf0f78442012-08-13 19:04:29 -070057 private long mProviderSetTime;
58
Ruei-sung Linf0f78442012-08-13 19:04:29 -070059 private Handler mHandler;
60 private HandlerThread mHandlerThread;
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070061 private AlarmManager mAlarmManager;
Ruei-sung Linf0f78442012-08-13 19:04:29 -070062 private LocationManager mLocationManager;
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070063
Ruei-sung Linf0f78442012-08-13 19:04:29 -070064 private ClusterManager mClusterManager;
65
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070066 private Criteria mCriteria = new Criteria();
67
68 private LocationUpdater mLocationUpdater;
69
70 private Context mContext;
71 private PendingIntent mPendingIntent;
72
Wei Hua9c3a7dc2012-08-28 13:46:31 -070073 // Fake location, used for testing.
74 private String mFakeLocation = null;
75
Ruei-sung Linf0f78442012-08-13 19:04:29 -070076 public LocationStatsAggregator(final Context context) {
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -070077 mLocationManager =
78 (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070079 mAlarmManager =
80 (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
81
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -070082 setClusteringThread(context);
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -070083
84 mCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
85 mCriteria.setPowerRequirement(Criteria.POWER_LOW);
86 /*
87 mCriteria.setAltitudeRequired(false);
88 mCriteria.setBearingRequired(false);
89 mCriteria.setSpeedRequired(true);
90 */
91 mCriteria.setCostAllowed(true);
92
93
94 IntentFilter filter = new IntentFilter(LocationUpdater.LOCATION_UPDATE);
95 mLocationUpdater = new LocationUpdater();
96 context.registerReceiver(mLocationUpdater, filter);
97
98 Intent intent = new Intent(LocationUpdater.LOCATION_UPDATE);
99
100 mContext = context;
101 mPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
102
103 mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
104 SystemClock.elapsedRealtime() + 30000, //
105 REPEAT_INTERVAL,
106 mPendingIntent);
107 }
108
109 public void release() {
110 mContext.unregisterReceiver(mLocationUpdater);
111 mAlarmManager.cancel(mPendingIntent);
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700112 }
113
saberian984e52f2012-06-05 18:23:53 -0700114 public String[] getListOfFeatures(){
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -0700115 String[] list = { CURRENT_LOCATION } ;
saberian984e52f2012-06-05 18:23:53 -0700116 return list;
117 }
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700118
saberian984e52f2012-06-05 18:23:53 -0700119 public Map<String,String> getFeatureValue(String featureName) {
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700120 HashMap<String,String> feature = new HashMap<String,String>();
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -0700121
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700122 if (featureName.equals(CURRENT_LOCATION)) {
Ruei-sung Lin78a66d92012-08-29 10:30:03 -0700123 // TODO: check last known location first before sending out location request.
124 /*
125 Location location =
126 mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
127 */
Ruei-sung Linc7c9cf12012-08-28 11:35:19 -0700128 String location = mClusterManager.getSemanticLocation();
129 if (!location.equals(UNKNOWN_LOCATION)) {
Wei Hua9c3a7dc2012-08-28 13:46:31 -0700130 if (mFakeLocation != null) {
131 feature.put(CURRENT_LOCATION, mFakeLocation);
132 } else {
133 feature.put(CURRENT_LOCATION, location);
134 }
Ruei-sung Linc7c9cf12012-08-28 11:35:19 -0700135 }
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700136 }
137 return (Map) feature;
saberian984e52f2012-06-05 18:23:53 -0700138 }
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700139
Wei Hua9c3a7dc2012-08-28 13:46:31 -0700140 public List<String> getClusterNames() {
141 return mClusterManager.getClusterNames();
142 }
143
144 // set a fake location using cluster name.
145 // Set an empty string "" to disable the fake location
146 public void setFakeLocation(String name) {
147 if (name != null && name.length() != 0)
148 mFakeLocation = name;
149 else mFakeLocation = null;
150 }
151
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -0700152 private Location getLastKnownLocation() {
153 List<String> providers = mLocationManager.getAllProviders();
154 Location bestResult = null;
155 float bestAccuracy = Float.MAX_VALUE;
156 long bestTime;
157
158 // get the latest location data
159 long currTime = System.currentTimeMillis();
160 for (String provider : providers) {
161 Location location = mLocationManager.getLastKnownLocation(provider);
162
163 if (location != null) {
164 float accuracy = location.getAccuracy();
165 long time = location.getTime();
166
167 if (currTime - time < FRESH_THRESHOLD && accuracy < bestAccuracy) {
168 bestResult = location;
169 bestAccuracy = accuracy;
170 bestTime = time;
171 }
172 }
173 }
174 if (bestResult != null) {
175 Log.i(TAG, "found location for free: " + bestResult);
176 }
177 return bestResult;
178 }
179
180 private class LocationUpdater extends BroadcastReceiver {
181 String TAG = "LocationUpdater";
182
183 public static final String LOCATION_UPDATE = "android.bordeaux.services.LOCATION_UPDATE";
184
185 @Override
186 public void onReceive(Context context, Intent intent) {
187 Location location = getLastKnownLocation();
188
189 if (location == null) {
190 String provider = mLocationManager.getBestProvider(mCriteria, true);
191 Log.i(TAG, "Best Available Location Provider: " + provider);
192 mLocationManager.requestSingleUpdate(provider, mLocationListener,
193 mHandlerThread.getLooper());
194 } else {
195 mHandler.sendMessage(mHandler.obtainMessage(LOCATION_CHANGE, location));
196 }
197 }
198 }
199
Ruei-sung Lin5d42ffa2012-08-23 16:01:57 -0700200 private void setClusteringThread(Context context) {
201 mClusterManager = new ClusterManager(context);
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700202
203 mHandlerThread = new HandlerThread("Location Handler",
204 Process.THREAD_PRIORITY_BACKGROUND);
205 mHandlerThread.start();
206 mHandler = new Handler(mHandlerThread.getLooper()) {
207
208 @Override
209 public void handleMessage(Message msg) {
210 if (!(msg.obj instanceof Location)) {
211 return;
212 }
213 Location location = (Location) msg.obj;
214 switch(msg.what) {
215 case LOCATION_CHANGE:
216 mClusterManager.addSample(location);
217 break;
218 default:
219 super.handleMessage(msg);
220 }
221 }
222 };
223 }
224
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700225 private final LocationListener mLocationListener = new LocationListener() {
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -0700226 private static final String TAG = "LocationListener";
227
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700228 public void onLocationChanged(Location location) {
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700229 mHandler.sendMessage(mHandler.obtainMessage(LOCATION_CHANGE, location));
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -0700230 mLocationManager.removeUpdates(this);
Ruei-sung Linf0f78442012-08-13 19:04:29 -0700231 }
232
233 public void onStatusChanged(String provider, int status, Bundle extras) { }
234
235 public void onProviderEnabled(String provider) { }
236
237 public void onProviderDisabled(String provider) { }
238 };
saberian984e52f2012-06-05 18:23:53 -0700239}