diff --git a/lost-sample/src/main/java/com/example/lost/GeofencingApiActivity.java b/lost-sample/src/main/java/com/example/lost/GeofencingApiActivity.java
index 6da6db6..fee19c7 100644
--- a/lost-sample/src/main/java/com/example/lost/GeofencingApiActivity.java
+++ b/lost-sample/src/main/java/com/example/lost/GeofencingApiActivity.java
@@ -21,41 +21,39 @@
*/
public class GeofencingApiActivity extends LostApiClientActivity {
+ private Button createButton;
private TextView inputRequestId;
private TextView inputLatitude;
private TextView inputLongitude;
private TextView inputRadius;
+ private PendingIntent pendingIntent;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geofencing);
- initConnectButton();
- initDisconnectButton();
initInputFields();
initCreateButton();
+
+ connect();
}
- private void initConnectButton() {
- Button connectButton = (Button) findViewById(R.id.button_connect);
- if (connectButton != null) {
- connectButton.setOnClickListener(new View.OnClickListener() {
- @Override public void onClick(View v) {
- connect();
- }
- });
- }
+ @Override protected void onDestroy() {
+ disconnect();
+ createButton.setEnabled(false);
+ super.onDestroy();
}
- private void initDisconnectButton() {
- Button disconnectButton = (Button) findViewById(R.id.button_disconnect);
- if (disconnectButton != null) {
- disconnectButton.setOnClickListener(new View.OnClickListener() {
- @Override public void onClick(View v) {
- disconnect();
- }
- });
+ @Override protected void disconnect() {
+ if (pendingIntent != null) {
+ LocationServices.GeofencingApi.removeGeofences(client, pendingIntent);
}
+ super.disconnect();
+ }
+
+ @Override public void onConnected() {
+ super.onConnected();
+ createButton.setEnabled(true);
}
private void initInputFields() {
@@ -66,7 +64,7 @@ private void initInputFields() {
}
private void initCreateButton() {
- Button createButton = (Button) findViewById(R.id.button_create);
+ createButton = (Button) findViewById(R.id.button_create);
if (createButton != null) {
createButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
@@ -94,7 +92,7 @@ private void createGeofence() {
.addGeofence(geofence)
.build();
Intent serviceIntent = new Intent(getApplicationContext(), GeofenceIntentService.class);
- PendingIntent pendingIntent = PendingIntent.getService(this, 0, serviceIntent, 0);
+ pendingIntent = PendingIntent.getService(this, 0, serviceIntent, 0);
LocationServices.GeofencingApi.addGeofences(client, request, pendingIntent);
}
diff --git a/lost-sample/src/main/java/com/example/lost/LocationAvailabilityActivity.java b/lost-sample/src/main/java/com/example/lost/LocationAvailabilityActivity.java
index ace4ec8..2f2b102 100644
--- a/lost-sample/src/main/java/com/example/lost/LocationAvailabilityActivity.java
+++ b/lost-sample/src/main/java/com/example/lost/LocationAvailabilityActivity.java
@@ -43,7 +43,15 @@ public void onLocationResult(LocationResult result) {
setupLocationAvailabilityBtn();
}
- private void setupLocationAvailabilityBtn() {
+ @Override protected void onDestroy() {
+ super.onDestroy();
+ if (client != null && client.isConnected()) {
+ LocationServices.FusedLocationApi.removeLocationUpdates(client, callback);
+ client.disconnect();
+ }
+ }
+
+ private void setupLocationAvailabilityBtn() {
Button connect = (Button) findViewById(R.id.check_availability);
connect.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
diff --git a/lost-sample/src/main/java/com/example/lost/LocationListenerActivity.java b/lost-sample/src/main/java/com/example/lost/LocationListenerActivity.java
index 9fa1726..ec2ab3e 100644
--- a/lost-sample/src/main/java/com/example/lost/LocationListenerActivity.java
+++ b/lost-sample/src/main/java/com/example/lost/LocationListenerActivity.java
@@ -222,7 +222,11 @@ private void setMockLocation() {
private void disconnect() {
Log.d(TAG, "Disconnecting...");
- client.disconnect();
+ if (client.isConnected()) {
+ FusedLocationApi.removeLocationUpdates(client, listener);
+ client.disconnect();
+ }
+ client.unregisterConnectionCallbacks(this);
}
private void reset() {
diff --git a/lost-sample/src/main/java/com/example/lost/LostApiClientActivity.java b/lost-sample/src/main/java/com/example/lost/LostApiClientActivity.java
index 4feabf4..c54ab28 100644
--- a/lost-sample/src/main/java/com/example/lost/LostApiClientActivity.java
+++ b/lost-sample/src/main/java/com/example/lost/LostApiClientActivity.java
@@ -67,6 +67,7 @@ protected void connect() {
protected void disconnect() {
Log.d(TAG, "Disconnecting...");
client.disconnect();
+ client.unregisterConnectionCallbacks(this);
Toast.makeText(this, "LOST client disconnected", LENGTH_SHORT).show();
}
}
diff --git a/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerMultipleClientsActivity.java b/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerMultipleClientsActivity.java
index 993bf07..1a73650 100644
--- a/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerMultipleClientsActivity.java
+++ b/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerMultipleClientsActivity.java
@@ -22,7 +22,7 @@
* Demonstrates two {@link LostApiClient}s receiving location updates at difference intervals
*/
public class MultipleLocationListenerMultipleClientsActivity extends ListActivity
- implements LocationListener {
+ implements LocationListener, LostApiClient.ConnectionCallbacks {
private static final int LOCATION_PERMISSION_REQUEST = 1;
@@ -51,17 +51,17 @@ public class MultipleLocationListenerMultipleClientsActivity extends ListActivit
}
lostApiClient = new LostApiClient.Builder(this).addConnectionCallbacks(
- new LostApiClient.ConnectionCallbacks() {
- @Override
- public void onConnected() {
- initLocationTracking();
- }
+ this).build();
+ }
+
+ @Override
+ public void onConnected() {
+ initLocationTracking();
+ }
- @Override
- public void onConnectionSuspended() {
+ @Override
+ public void onConnectionSuspended() {
- }
- }).build();
}
@Override public void onStart() {
@@ -74,6 +74,7 @@ public void onConnectionSuspended() {
LocationServices.FusedLocationApi.removeLocationUpdates(lostApiClient,
MultipleLocationListenerMultipleClientsActivity.this);
lostApiClient.disconnect();
+ lostApiClient.unregisterConnectionCallbacks(this);
}
private void initLocationTracking() {
@@ -109,7 +110,8 @@ public void addItem(String title) {
adapter.notifyDataSetChanged();
}
- public static class LostClientFragment extends android.app.Fragment implements LocationListener {
+ public static class LostClientFragment extends android.app.Fragment implements LocationListener,
+ LostApiClient.ConnectionCallbacks {
private static final int LOCATION_PERMISSION_REQUEST = 2;
@@ -117,18 +119,18 @@ public static class LostClientFragment extends android.app.Fragment implements L
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- fragmentClient = new LostApiClient.Builder(this.getActivity()).addConnectionCallbacks(
- new LostApiClient.ConnectionCallbacks() {
- @Override
- public void onConnected() {
- fragmentInitLocationTracking();
- }
-
- @Override
- public void onConnectionSuspended() {
-
- }
- }).build();
+ fragmentClient = new LostApiClient.Builder(this.getActivity()).addConnectionCallbacks(this).
+ build();
+ }
+
+ @Override
+ public void onConnected() {
+ fragmentInitLocationTracking();
+ }
+
+ @Override
+ public void onConnectionSuspended() {
+
}
@Override public void onStart() {
@@ -140,6 +142,7 @@ public void onConnectionSuspended() {
super.onStop();
LocationServices.FusedLocationApi.removeLocationUpdates(fragmentClient, this);
fragmentClient.disconnect();
+ fragmentClient.unregisterConnectionCallbacks(this);
}
private void fragmentInitLocationTracking() {
diff --git a/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerSingleClientActivity.java b/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerSingleClientActivity.java
index 8a88bc7..f0b20ed 100644
--- a/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerSingleClientActivity.java
+++ b/lost-sample/src/main/java/com/example/lost/MultipleLocationListenerSingleClientActivity.java
@@ -20,7 +20,8 @@
/**
* Demonstrates one {@link LostApiClient}s receiving location updates at difference intervals
*/
-public class MultipleLocationListenerSingleClientActivity extends ListActivity {
+public class MultipleLocationListenerSingleClientActivity extends ListActivity implements
+ LostApiClient.ConnectionCallbacks {
private static final int LOCATION_PERMISSION_REQUEST = 1;
@@ -50,18 +51,17 @@ public class MultipleLocationListenerSingleClientActivity extends ListActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- lostApiClient = new LostApiClient.Builder(this).addConnectionCallbacks(
- new LostApiClient.ConnectionCallbacks() {
- @Override
- public void onConnected() {
- initLocationTracking();
- }
+ lostApiClient = new LostApiClient.Builder(this).addConnectionCallbacks(this).build();
+ }
+
+ @Override
+ public void onConnected() {
+ initLocationTracking();
+ }
- @Override
- public void onConnectionSuspended() {
+ @Override
+ public void onConnectionSuspended() {
- }
- }).build();
}
@Override public void onStart() {
@@ -74,6 +74,7 @@ public void onConnectionSuspended() {
LocationServices.FusedLocationApi.removeLocationUpdates(lostApiClient, listener);
LocationServices.FusedLocationApi.removeLocationUpdates(lostApiClient, otherListener);
lostApiClient.disconnect();
+ lostApiClient.unregisterConnectionCallbacks(this);
}
private void initLocationTracking() {
diff --git a/lost-sample/src/main/res/layout/activity_geofencing.xml b/lost-sample/src/main/res/layout/activity_geofencing.xml
index c4dc577..3819467 100644
--- a/lost-sample/src/main/res/layout/activity_geofencing.xml
+++ b/lost-sample/src/main/res/layout/activity_geofencing.xml
@@ -6,20 +6,6 @@
android:padding="8dp"
>
-
-
-
-
diff --git a/lost/src/main/java/com/mapzen/android/lost/api/LostApiClient.java b/lost/src/main/java/com/mapzen/android/lost/api/LostApiClient.java
index b218e54..7bf94ca 100644
--- a/lost/src/main/java/com/mapzen/android/lost/api/LostApiClient.java
+++ b/lost/src/main/java/com/mapzen/android/lost/api/LostApiClient.java
@@ -5,6 +5,8 @@
import android.content.Context;
+import java.lang.ref.WeakReference;
+
public interface LostApiClient {
interface ConnectionCallbacks {
@@ -18,21 +20,27 @@ interface ConnectionCallbacks {
boolean isConnected();
+ void unregisterConnectionCallbacks(ConnectionCallbacks callbacks);
+
final class Builder {
private final Context context;
- private ConnectionCallbacks connectionCallbacks;
+ private WeakReference connectionCallbacks;
public Builder(Context context) {
this.context = context.getApplicationContext();
}
public Builder addConnectionCallbacks(ConnectionCallbacks callbacks) {
- this.connectionCallbacks = callbacks;
+ this.connectionCallbacks = new WeakReference(callbacks);
return this;
}
public LostApiClient build() {
- return new LostApiClientImpl(context, connectionCallbacks, LostClientManager.shared());
+ ConnectionCallbacks callbacks = null;
+ if (connectionCallbacks != null) {
+ callbacks = connectionCallbacks.get();
+ }
+ return new LostApiClientImpl(context, callbacks, LostClientManager.shared());
}
}
}
diff --git a/lost/src/main/java/com/mapzen/android/lost/internal/LostApiClientImpl.java b/lost/src/main/java/com/mapzen/android/lost/internal/LostApiClientImpl.java
index 915ec97..d51ce5f 100644
--- a/lost/src/main/java/com/mapzen/android/lost/internal/LostApiClientImpl.java
+++ b/lost/src/main/java/com/mapzen/android/lost/internal/LostApiClientImpl.java
@@ -10,7 +10,7 @@
*/
public class LostApiClientImpl implements LostApiClient {
private final Context context;
- private final ConnectionCallbacks connectionCallbacks;
+ private ConnectionCallbacks connectionCallbacks;
private final ClientManager clientManager;
public LostApiClientImpl(Context context, ConnectionCallbacks callbacks,
@@ -66,6 +66,11 @@ public LostApiClientImpl(Context context, ConnectionCallbacks callbacks,
&& getFusedLocationProviderApiImpl().isConnected() && clientManager.containsClient(this);
}
+ @Override public void unregisterConnectionCallbacks(ConnectionCallbacks callbacks) {
+ getFusedLocationProviderApiImpl().removeConnectionCallbacks(connectionCallbacks);
+ connectionCallbacks = null;
+ }
+
private GeofencingApiImpl getGeofencingImpl() {
return (GeofencingApiImpl) LocationServices.GeofencingApi;
}