-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathBackgroundLocationBroadcastReceiver.java
156 lines (138 loc) · 6.61 KB
/
BackgroundLocationBroadcastReceiver.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
package com.codename1.location;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.util.Log;
import com.codename1.impl.android.AndroidImplementation;
import com.codename1.ui.Display;
import com.google.android.gms.location.GeofenceStatusCodes;
import com.google.android.gms.location.GeofencingEvent;
import com.google.android.gms.location.LocationResult;
import java.util.List;
/**
* Starting with Android API 26, there are serious background execution limits. We can
* no longer start a background service from the background. Therefore, instead of
* using a service to receive the Background Location Notifications, we use a broadcast
* receiver.
* @author shannah
*/
public class BackgroundLocationBroadcastReceiver extends BroadcastReceiver {
static final String ACTION_PROCESS_UPDATES =
"com.codename1.location.backgroundlocationbroadcastreceiver.action" +
".PROCESS_UPDATES";
static final String ACTION_PROCESS_GEOFENCE_TRANSITIONS = "com.codename1.location.backgroundlocationbroadcastreceiver.action.ACTION_RECEIVE_GEOFENCE";
private static final String TAG ="BackgroundLocationBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
LocationResult result = LocationResult.extractResult(intent);
Location lastLocation = null;
if (result != null) {
List<Location> locations = result.getLocations();
for (Location loc : locations){
lastLocation = loc;
}
} else {
return;
}
if (lastLocation == null) {
return;
}
String dataString = intent.getDataString();
if (dataString == null) {
return;
}
String[] params = dataString.split("[?]");
if (params.length < 2) {
return;
}
Class locationListenerClass;
try {
locationListenerClass = Class.forName(params[1]);
} catch (Throwable t) {
return;
}
boolean shouldStopWhenDone = false;
if (!Display.isInitialized()) {
shouldStopWhenDone = true;
AndroidImplementation.startContext(context);
}
try {
//the 2nd parameter is the class name we need to create
LocationListener l = (LocationListener)locationListenerClass.newInstance();
l.locationUpdated(AndroidLocationManager.convert(lastLocation));
} catch (Throwable e) {
Log.e("Codename One", "background location error", e);
} finally {
if (shouldStopWhenDone) {
AndroidImplementation.stopContext(context);
}
}
}
if (ACTION_PROCESS_GEOFENCE_TRANSITIONS.equals(action)) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if(geofencingEvent == null) {
// Attempt to workaround this issue: https://stackoverflow.com/questions/78497339/geofence-bug-in-codename-one-on-android
Log.e(TAG, "Null geofencingEvent!");
return;
}
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceStatusCodes
.getStatusCodeString(geofencingEvent.getErrorCode());
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_EXIT) {
String dataString = intent.getDataString();
if (dataString == null) {
if (intent.getExtras() != null && intent.getExtras().get("geofenceListenerClass") != null) {
dataString = "-?" + intent.getExtras().get("geofenceListenerClass");
} else {
return;
}
}
String[] params = dataString.split("[?]");
if (params.length < 2) {
return;
}
Class locationListenerClass;
try {
locationListenerClass = Class.forName(params[1]);
} catch (Throwable t) {
return;
}
boolean shouldStopWhenDone = false;
if (!Display.isInitialized()) {
shouldStopWhenDone = true;
AndroidImplementation.startContext(context);
}
try {
//the 2nd parameter is the class name we need to create
GeofenceListener l = (GeofenceListener) locationListenerClass.newInstance();
List<com.google.android.gms.location.Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
for (com.google.android.gms.location.Geofence gf : triggeringGeofences) {
if (geofenceTransition == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_ENTER) {
l.onEntered(gf.getRequestId());
} else {
l.onExit(gf.getRequestId());
}
}
} catch (Throwable e) {
Log.e("Codename One", "background location error", e);
} finally {
if (shouldStopWhenDone) {
AndroidImplementation.stopContext(context);
}
}
}
}
}
}
}