Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Unregister callbacks #208

Merged
merged 4 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -74,6 +74,7 @@ public void onConnectionSuspended() {
LocationServices.FusedLocationApi.removeLocationUpdates(lostApiClient,
MultipleLocationListenerMultipleClientsActivity.this);
lostApiClient.disconnect();
lostApiClient.unregisterConnectionCallbacks(this);
}

private void initLocationTracking() {
Expand Down Expand Up @@ -109,26 +110,27 @@ 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;

LostApiClient fragmentClient;

@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() {
Expand All @@ -140,6 +142,7 @@ public void onConnectionSuspended() {
super.onStop();
LocationServices.FusedLocationApi.removeLocationUpdates(fragmentClient, this);
fragmentClient.disconnect();
fragmentClient.unregisterConnectionCallbacks(this);
}

private void fragmentInitLocationTracking() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
15 changes: 1 addition & 14 deletions lost-sample/src/main/res/layout/activity_geofencing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@
android:padding="8dp"
>

<Button
android:id="@+id/button_connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connect"
/>

<Button
android:id="@+id/button_disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/disconnect"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -77,6 +63,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/create"
android:enabled="false"
/>

</LinearLayout>
14 changes: 11 additions & 3 deletions lost/src/main/java/com/mapzen/android/lost/api/LostApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import android.content.Context;

import java.lang.ref.WeakReference;

public interface LostApiClient {

interface ConnectionCallbacks {
Expand All @@ -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> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down