1
1
package net .sylvek .itracing2 .receivers ;
2
2
3
+ import android .Manifest ;
3
4
import android .app .Notification ;
4
5
import android .app .NotificationManager ;
5
6
import android .app .PendingIntent ;
6
7
import android .content .BroadcastReceiver ;
7
8
import android .content .Context ;
8
9
import android .content .Intent ;
10
+ import android .content .pm .PackageManager ;
9
11
import android .location .Criteria ;
10
12
import android .location .Location ;
11
13
import android .location .LocationManager ;
12
14
import android .net .Uri ;
15
+ import android .support .v4 .app .ActivityCompat ;
16
+
13
17
import net .sylvek .itracing2 .R ;
14
18
import net .sylvek .itracing2 .database .Devices ;
15
19
import net .sylvek .itracing2 .database .Events ;
@@ -27,12 +31,22 @@ public class CapturePosition extends BroadcastReceiver {
27
31
public static final String NAME = "position" ;
28
32
29
33
@ Override
30
- public void onReceive (Context context , Intent intent )
31
- {
34
+ public void onReceive (Context context , Intent intent ) {
32
35
// because some customers don't like Google Play Services…
33
36
Location bestLocation = null ;
34
37
final LocationManager locationManager = (LocationManager ) context .getSystemService (Context .LOCATION_SERVICE );
38
+ assert locationManager != null ;
35
39
for (final String provider : locationManager .getAllProviders ()) {
40
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED && ActivityCompat .checkSelfPermission (context , Manifest .permission .ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
41
+ // TODO: Consider calling
42
+ // ActivityCompat#requestPermissions
43
+ // here to request the missing permissions, and then overriding
44
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
45
+ // int[] grantResults)
46
+ // to handle the case where the user grants the permission. See the documentation
47
+ // for ActivityCompat#requestPermissions for more details.
48
+ return ;
49
+ }
36
50
final Location location = locationManager .getLastKnownLocation (provider );
37
51
final long now = System .currentTimeMillis ();
38
52
if (location != null
@@ -59,6 +73,7 @@ public void onReceive(Context context, Intent intent)
59
73
.setAutoCancel (false )
60
74
.setContentIntent (PendingIntent .getActivity (context , 0 , mapIntent , PendingIntent .FLAG_UPDATE_CURRENT ))
61
75
.build ();
76
+ assert notificationManager != null ;
62
77
notificationManager .notify (NOTIFICATION_ID , notification );
63
78
64
79
final String address = intent .getStringExtra (Devices .ADDRESS );
0 commit comments