-
Notifications
You must be signed in to change notification settings - Fork 818
/
Copy pathGPSLogger.java
147 lines (131 loc) · 5.35 KB
/
GPSLogger.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
package io.pslab.others;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import com.google.android.material.snackbar.Snackbar;
import io.pslab.R;
import io.pslab.models.PSLabSensor;
/**
* Created by Padmal on 6/29/18.
*/
public class GPSLogger {
public static final int PSLAB_PERMISSION_FOR_MAPS = 102;
private static final int UPDATE_INTERVAL_IN_MILLISECONDS = 400;
private static final int MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
private LocationManager locationManager;
private Context context;
private Location bestLocation;
private PSLabSensor sensorActivity;
private PSLabPermission psLabPermission;
private String provider = LocationManager.GPS_PROVIDER;
public AlertDialog gpsAlert;
public GPSLogger(Context context, LocationManager locationManager) {
this.context = context;
this.locationManager = locationManager;
psLabPermission = PSLabPermission.getInstance();
if (context instanceof PSLabSensor) {
sensorActivity = (PSLabSensor) context;
buildUpGPSAlert();
}
}
private void buildUpGPSAlert() {
gpsAlert = new AlertDialog.Builder(sensorActivity, R.style.AlertDialogStyle)
.setTitle(R.string.allow_gps)
.setMessage(R.string.allow_gps_info)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
sensorActivity.checkGPSOnResume = true;
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (sensorActivity.isRecording) {
CustomSnackBar.showSnackBar(sensorActivity.sensorParentView,
context.getResources().getString(R.string.data_recording_with_gps_off),
null, null, Snackbar.LENGTH_LONG);
} else {
sensorActivity.isRecording = true;
sensorActivity.invalidateOptionsMenu();
CustomSnackBar.showSnackBar(sensorActivity.sensorParentView,
context.getResources().getString(R.string.data_recording_with_nogps),
null, null, Snackbar.LENGTH_LONG);
}
}
})
.create();
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
bestLocation = location;
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {/**/}
@Override
public void onProviderEnabled(String s) { /**/}
@Override
public void onProviderDisabled(String s) {
if (sensorActivity.isRecording && !gpsAlert.isShowing()) {
gpsAlert.show();
}
}
};
public boolean isGPSEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
/**
* Stop requesting updates
*/
public void removeUpdate() {
locationManager.removeUpdates(locationListener);
}
/**
* @return the best location fetched
*/
@SuppressLint("MissingPermission")
public Location getDeviceLocation() {
if (bestLocation == null) {
if (psLabPermission.checkPermissions((Activity) context, PSLabPermission.MAP_PERMISSION)) {
locationManager.requestLocationUpdates(provider,
UPDATE_INTERVAL_IN_MILLISECONDS, MIN_DISTANCE_CHANGE_FOR_UPDATES,
locationListener);
return locationManager.getLastKnownLocation(provider);
} else {
return dummyLocation();
}
} else {
return bestLocation;
}
}
private Location dummyLocation() {
Location l = new Location("");
l.setLatitude(0.0);
l.setLongitude(0.0);
return l;
}
/**
* Set location updates
*/
@SuppressLint("MissingPermission")
public void startCaptureLocation() {
if (psLabPermission.checkPermissions((Activity) context, PSLabPermission.MAP_PERMISSION)) {
locationManager.requestLocationUpdates(provider, UPDATE_INTERVAL_IN_MILLISECONDS, MIN_DISTANCE_CHANGE_FOR_UPDATES,
locationListener);
} else {
CustomSnackBar.showSnackBar(((Activity) context).findViewById(android.R.id.content),
context.getString(R.string.no_permission_for_maps),null,null,Snackbar.LENGTH_LONG);
}
}
}