/
Location.java
executable file
·548 lines (434 loc) · 22.3 KB
/
Location.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package com.audacious_software.passive_data_kit.generators.device;
import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.SwitchCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.audacious_software.passive_data_kit.DeviceInformation;
import com.audacious_software.passive_data_kit.PassiveDataKit;
import com.audacious_software.passive_data_kit.activities.generators.DataPointViewHolder;
import com.audacious_software.passive_data_kit.activities.generators.RequestPermissionActivity;
import com.audacious_software.passive_data_kit.diagnostics.DiagnosticAction;
import com.audacious_software.passive_data_kit.generators.Generator;
import com.audacious_software.passive_data_kit.generators.Generators;
import com.audacious_software.pdk.passivedatakit.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.ui.IconGenerator;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unused")
public class Location extends Generator implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final String GENERATOR_IDENTIFIER = "pdk-location";
private static final String DATABASE_PATH = "pdk-location.sqlite";
private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.device.Location.ENABLED";
private static final boolean ENABLED_DEFAULT = true;
private static final String USE_GOOGLE_SERVICES = "com.audacious_software.passive_data_kit.generators.device.Location.USE_GOOGLE_SERVICES";
private static final boolean USE_GOOGLE_SERVICES_DEFAULT = true;
private static final String ACCURACY_KEY = "accuracy";
private static final String LATITUDE_KEY = "latitude";
private static final String LONGITUDE_KEY = "longitude";
private static final String FIX_TIMESTAMP_KEY = "fix-timestamp";
private static final String PROVIDER_KEY = "provider";
private static final String ALTITUDE_KEY = "altitude";
private static final String BEARING_KEY = "bearing";
private static final String SPEED_KEY = "speed";
private static final String EXTRAS_KEY = "extras";
private static final String SETTING_DISPLAY_HYBRID_MAP = "com.audacious_software.passive_data_kit.generators.device.Location.SETTING_DISPLAY_HYBRID_MAP";
private static final boolean SETTING_DISPLAY_HYBRID_MAP_DEFAULT = true;
private static Location sInstance = null;
private GoogleApiClient mGoogleApiClient = null;
private android.location.Location mLastLocation = null;
private long mUpdateInterval = 60000;
private SQLiteDatabase mDatabase = null;
private static int DATABASE_VERSION = 1;
private static final String TABLE_HISTORY = "history";
private static final String HISTORY_OBSERVED = "observed";
private static final String HISTORY_LATITUDE = "latitude";
private static final String HISTORY_LONGITUDE = "longitude";
private static final String HISTORY_ALTITUDE = "altitude";
private static final String HISTORY_BEARING = "bearing";
private static final String HISTORY_SPEED = "speed";
private static final String HISTORY_PROVIDER = "provider";
private static final String HISTORY_LOCATION_TIMESTAMP = "location_timestamp";
private static final String HISTORY_ACCURACY = "accuracy";
public static Location getInstance(Context context) {
if (Location.sInstance == null) {
Location.sInstance = new Location(context.getApplicationContext());
}
return Location.sInstance;
}
public Location(Context context) {
super(context);
}
public static void start(final Context context) {
Location.getInstance(context).startGenerator();
}
private void startGenerator() {
final Location me = this;
Runnable r = new Runnable()
{
@Override
public void run() {
if (Location.useKindleLocationServices())
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement Kindle support.");
}
else if (Location.useGoogleLocationServices(me.mContext))
{
if (me.mGoogleApiClient == null) {
GoogleApiClient.Builder builder = new GoogleApiClient.Builder(me.mContext);
builder.addConnectionCallbacks(me);
builder.addOnConnectionFailedListener(me);
builder.addApi(LocationServices.API);
me.mGoogleApiClient = builder.build();
me.mGoogleApiClient.connect();
}
}
else
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement generic location support.");
}
}
};
Thread t = new Thread(r);
t.start();
Generators.getInstance(this.mContext).registerCustomViewClass(Location.GENERATOR_IDENTIFIER, Location.class);
File path = PassiveDataKit.getGeneratorsStorage(this.mContext);
path = new File(path, Location.DATABASE_PATH);
this.mDatabase = SQLiteDatabase.openOrCreateDatabase(path, null);
int version = this.getDatabaseVersion(this.mDatabase);
switch (version) {
case 0:
this.mDatabase.execSQL(this.mContext.getString(R.string.pdk_generator_location_create_history_table));
}
this.setDatabaseVersion(this.mDatabase, Location.DATABASE_VERSION);
}
private void stopGenerator() {
if (this.mGoogleApiClient != null) {
this.mGoogleApiClient.disconnect();
this.mGoogleApiClient = null;
}
this.mDatabase.close();
this.mDatabase = null;
}
public static boolean useGoogleLocationServices(Context context) {
SharedPreferences prefs = Generators.getInstance(context).getSharedPreferences(context);
return prefs.getBoolean(Location.USE_GOOGLE_SERVICES, Location.USE_GOOGLE_SERVICES_DEFAULT);
}
public static boolean useKindleLocationServices() {
return DeviceInformation.isKindleFire();
}
public static boolean isEnabled(Context context) {
SharedPreferences prefs = Generators.getInstance(context).getSharedPreferences(context);
return prefs.getBoolean(Location.ENABLED, Location.ENABLED_DEFAULT);
}
public static boolean isRunning(Context context) {
if (Location.sInstance == null) {
return false;
}
if (Location.useKindleLocationServices())
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement Kindle support.");
}
else if (Location.useGoogleLocationServices(context))
{
return (Location.sInstance.mGoogleApiClient != null);
}
else
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement generic location support.");
}
}
public static ArrayList<DiagnosticAction> diagnostics(Context context)
{
return Location.getInstance(context).runDiagostics();
}
private ArrayList<DiagnosticAction> runDiagostics() {
ArrayList<DiagnosticAction> actions = new ArrayList<>();
final Location me = this;
final Handler handler = new Handler(Looper.getMainLooper());
if (Location.isEnabled(this.mContext)) {
int permissionCheck = ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
actions.add(new DiagnosticAction(me.mContext.getString(R.string.diagnostic_missing_location_permission_title), me.mContext.getString(R.string.diagnostic_missing_location_permission), new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(me.mContext, RequestPermissionActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RequestPermissionActivity.PERMISSION, Manifest.permission.ACCESS_FINE_LOCATION);
me.mContext.startActivity(intent);
}
});
}
}));
}
}
return actions;
}
@Override
public void onConnected(Bundle bundle) {
final LocationRequest request = new LocationRequest();
request.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
request.setFastestInterval(this.mUpdateInterval);
request.setInterval(this.mUpdateInterval);
if (this.mGoogleApiClient != null && this.mGoogleApiClient.isConnected()) {
if (ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, request, this, this.mContext.getMainLooper());
}
}
}
@Override
public void onConnectionSuspended(int i) {
if (this.mGoogleApiClient != null && this.mGoogleApiClient.isConnected())
LocationServices.FusedLocationApi.removeLocationUpdates(this.mGoogleApiClient, this);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
this.mGoogleApiClient = null;
}
@Override
public void onLocationChanged(android.location.Location location) {
Log.e("PDK", "LOCATION CHANGED");
if (location == null)
return;
long now = System.currentTimeMillis();
ContentValues values = new ContentValues();
values.put(Location.HISTORY_OBSERVED, System.currentTimeMillis());
values.put(Location.HISTORY_LATITUDE, location.getLatitude());
values.put(Location.HISTORY_LONGITUDE, location.getLongitude());
values.put(Location.HISTORY_PROVIDER, location.getProvider());
values.put(Location.HISTORY_LOCATION_TIMESTAMP, location.getTime());
Bundle updated = new Bundle();
updated.putLong(Location.HISTORY_OBSERVED, System.currentTimeMillis());
updated.putDouble(Location.HISTORY_LATITUDE, location.getLatitude());
updated.putDouble(Location.HISTORY_LONGITUDE, location.getLongitude());
updated.putString(Location.HISTORY_PROVIDER, location.getProvider());
updated.putLong(Location.HISTORY_LOCATION_TIMESTAMP, location.getTime());
Bundle metadata = new Bundle();
metadata.putDouble(Generator.LATITUDE, location.getLatitude());
metadata.putDouble(Generator.LONGITUDE, location.getLongitude());
updated.putBundle(Generator.PDK_METADATA, metadata);
if (location.hasAltitude()) {
values.put(Location.HISTORY_ALTITUDE, location.getAltitude());
updated.putDouble(Location.HISTORY_ALTITUDE, location.getAltitude());
}
if (location.hasBearing()) {
values.put(Location.HISTORY_BEARING, location.getBearing());
updated.putDouble(Location.HISTORY_BEARING, location.getBearing());
}
if (location.hasSpeed()) {
values.put(Location.HISTORY_SPEED, location.getBearing());
updated.putDouble(Location.HISTORY_SPEED, location.getBearing());
}
if (location.hasAccuracy()) {
values.put(Location.HISTORY_ACCURACY, location.getAccuracy());
updated.putDouble(Location.HISTORY_ACCURACY, location.getAccuracy());
}
this.mDatabase.insert(Location.TABLE_HISTORY, null, values);
Generators.getInstance(this.mContext).notifyGeneratorUpdated(Location.GENERATOR_IDENTIFIER, updated);
}
public static long latestPointGenerated(Context context) {
long timestamp = 0;
Location me = Location.getInstance(context);
Cursor c = me.mDatabase.query(Location.TABLE_HISTORY, null, null, null, null, null, Location.HISTORY_OBSERVED + " DESC");
if (c.moveToNext()) {
timestamp = c.getLong(c.getColumnIndex(Location.HISTORY_OBSERVED));
}
c.close();
return timestamp;
}
public static void bindViewHolder(DataPointViewHolder holder) {
final Context context = holder.itemView.getContext();
Location me = Location.getInstance(context);
double lastLatitude = 0.0;
double lastLongitude = 0.0;
long timestamp = 0;
final List<LatLng> locations = new ArrayList<>();
String where = Location.HISTORY_OBSERVED + " > ?";
String[] args = { "" + (System.currentTimeMillis() - (1000 * 60 * 60 * 24)) };
Cursor c = me.mDatabase.query(Location.TABLE_HISTORY, null, where, args, null, null, Location.HISTORY_OBSERVED);
while (c.moveToNext()) {
lastLatitude = c.getDouble(c.getColumnIndex(Location.HISTORY_LATITUDE));
lastLongitude = c.getDouble(c.getColumnIndex(Location.HISTORY_LONGITUDE));
timestamp = c.getLong(c.getColumnIndex(Location.HISTORY_OBSERVED));
LatLng location = new LatLng(lastLatitude, lastLongitude);
locations.add(location);
}
c.close();
TextView dateLabel = (TextView) holder.itemView.findViewById(R.id.generator_data_point_date);
dateLabel.setText(Generator.formatTimestamp(context, timestamp / 1000));
final double finalLatitude = lastLatitude;
final double finalLongitude = lastLongitude;
final DisplayMetrics metrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (Location.useKindleLocationServices())
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement Kindle support.");
}
else if (Location.useGoogleLocationServices(holder.itemView.getContext()))
{
final MapView mapView = (MapView) holder.itemView.findViewById(R.id.map_view);
mapView.onCreate(null);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final boolean useHybrid = prefs.getBoolean(Location.SETTING_DISPLAY_HYBRID_MAP, Location.SETTING_DISPLAY_HYBRID_MAP_DEFAULT);
SwitchCompat hybridSwitch = (SwitchCompat) holder.itemView.findViewById(R.id.pdk_google_location_map_type_hybrid);
hybridSwitch.setChecked(useHybrid);
hybridSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, final boolean checked) {
SharedPreferences.Editor e = prefs.edit();
e.putBoolean(Location.SETTING_DISPLAY_HYBRID_MAP, checked);
e.apply();
mapView.getMapAsync(new OnMapReadyCallback() {
public void onMapReady(GoogleMap googleMap) {
if (checked) {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
} else {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
});
}
});
ColorStateList buttonStates = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{-android.R.attr.state_enabled},
new int[]{}
},
new int[]{
0xfff1f1f1,
0x1c000000,
0xff33691E
}
);
DrawableCompat.setTintList(hybridSwitch.getThumbDrawable(), buttonStates);
IconGenerator iconGen = new IconGenerator(context);
Drawable shapeDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_location_heatmap_marker, null);
iconGen.setBackground(shapeDrawable);
View view = new View(context);
view.setLayoutParams(new ViewGroup.LayoutParams(8, 8));
iconGen.setContentView(view);
final Bitmap bitmap = iconGen.makeIcon();
mapView.getMapAsync(new OnMapReadyCallback() {
public void onMapReady(GoogleMap googleMap) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
}
if (useHybrid) {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
} else {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setMapToolbarEnabled(false);
googleMap.getUiSettings().setAllGesturesEnabled(false);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latlng : locations) {
builder.include(latlng);
}
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), (int) (16 * metrics.density)));
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
for (LatLng latLng : locations) {
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap)));
}
mapView.onResume();
}
});
}
else
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement generic location support.");
}
}
@Override
public List<Bundle> fetchPayloads() {
return new ArrayList<Bundle>();
}
public static View fetchView(ViewGroup parent)
{
if (Location.useKindleLocationServices())
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement Kindle support.");
}
else if (Location.useGoogleLocationServices(parent.getContext()))
{
return LayoutInflater.from(parent.getContext()).inflate(R.layout.card_generator_location_google, parent, false);
}
else
{
// TODO
throw new RuntimeException("Throw rocks at developer to implement generic location support.");
}
}
public android.location.Location getLastKnownLocation() {
if (this.mLastLocation != null) {
return this.mLastLocation;
}
LocationManager locations = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
android.location.Location last = null;
if (ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
last = locations.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (last == null) {
last = locations.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
return last;
}
public void setUpdateInterval(long interval) {
this.mUpdateInterval = interval;
this.stopGenerator();
this.startGenerator();
}
}