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
|
package com.mapswithme.maps.geofence;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.annotation.NonNull;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingClient;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationServices;
import com.mapswithme.maps.LightFramework;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.location.LocationPermissionNotGrantedException;
import com.mapswithme.util.PermissionsUtils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class GeofenceRegistryImpl implements GeofenceRegistry
{
private static final int GEOFENCE_MAX_COUNT = 100;
private static final int GEOFENCE_TTL_IN_DAYS = 3;
private static final float PREFERRED_GEOFENCE_RADIUS = 100.0f;
private static final Logger LOG = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
private static final String TAG = GeofenceRegistryImpl.class.getSimpleName();
@NonNull
private final Application mApplication;
@NonNull
private final GeofencingClient mGeofencingClient;
public GeofenceRegistryImpl(@NonNull Application application)
{
mApplication = application;
mGeofencingClient = LocationServices.getGeofencingClient(mApplication);
}
@Override
public void registerGeofences(@NonNull GeofenceLocation location) throws LocationPermissionNotGrantedException
{
checkThread();
checkPermission();
List<GeoFenceFeature> features = LightFramework.getLocalAdsFeatures(
location.getLat(), location.getLon(), location.getRadiusInMeters(), GEOFENCE_MAX_COUNT);
LOG.d(TAG, "GeoFenceFeatures = " + Arrays.toString(features.toArray()));
if (features.isEmpty())
return;
List<Geofence> geofences = new ArrayList<>();
for (GeoFenceFeature each : features)
{
Geofence geofence = new Geofence.Builder()
.setRequestId(each.getId().toFeatureIdString())
.setCircularRegion(each.getLatitude(), each.getLongitude(), PREFERRED_GEOFENCE_RADIUS)
.setExpirationDuration(TimeUnit.DAYS.toMillis(GEOFENCE_TTL_IN_DAYS))
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
geofences.add(geofence);
}
GeofencingRequest geofencingRequest = makeGeofencingRequest(geofences);
PendingIntent intent = makeGeofencePendingIntent();
mGeofencingClient.addGeofences(geofencingRequest, intent)
.addOnSuccessListener(params -> onAddSucceeded())
.addOnFailureListener(params -> onAddFailed());
}
@Override
public void unregisterGeofences() throws LocationPermissionNotGrantedException
{
checkThread();
checkPermission();
mGeofencingClient.removeGeofences(makeGeofencePendingIntent())
.addOnSuccessListener(params -> onRemoveFailed())
.addOnSuccessListener(params -> onRemoveSucceeded());
}
private void onAddSucceeded()
{
LOG.d(TAG, "onAddSucceeded");
}
private void onAddFailed()
{
LOG.d(TAG, "onAddFailed");
}
private void onRemoveSucceeded()
{
LOG.d(TAG, "onRemoveSucceeded");
}
private void onRemoveFailed()
{
LOG.d(TAG, "onRemoveFailed");
}
private void checkPermission() throws LocationPermissionNotGrantedException
{
if (!PermissionsUtils.isLocationGranted(mApplication))
throw new LocationPermissionNotGrantedException();
}
private static void checkThread()
{
if (!UiThread.isUiThread())
throw new IllegalStateException("Must be call from Ui thread");
}
@NonNull
private PendingIntent makeGeofencePendingIntent()
{
Intent intent = new Intent(mApplication, GeofenceReceiver.class);
return PendingIntent.getBroadcast(mApplication, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@NonNull
private GeofencingRequest makeGeofencingRequest(@NonNull List<Geofence> geofences)
{
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
return builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofences(geofences)
.build();
}
@NonNull
public static GeofenceRegistry from(@NonNull Application application)
{
MwmApplication app = (MwmApplication) application;
return app.getGeofenceRegistry();
}
}
|