Synchronize connections
This commit is contained in:
parent
60d7da6dc6
commit
2a3cdfed4f
|
@ -31,41 +31,49 @@ public class LocationClient extends AbstractPlayServicesClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Location getLastLocation() {
|
public Location getLastLocation() {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
|
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PendingResult requestLocationUpdates(LocationRequest request,
|
public PendingResult requestLocationUpdates(LocationRequest request,
|
||||||
LocationListener listener) {
|
LocationListener listener) {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
|
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
|
||||||
listener);
|
listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PendingResult requestLocationUpdates(LocationRequest request,
|
public PendingResult requestLocationUpdates(LocationRequest request,
|
||||||
LocationListener listener, Looper looper) {
|
LocationListener listener, Looper looper) {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
|
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
|
||||||
listener, looper);
|
listener, looper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PendingResult requestLocationUpdates(LocationRequest request,
|
public PendingResult requestLocationUpdates(LocationRequest request,
|
||||||
PendingIntent callbackIntent) {
|
PendingIntent callbackIntent) {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
|
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
|
||||||
callbackIntent);
|
callbackIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PendingResult removeLocationUpdates(LocationListener listener) {
|
public PendingResult removeLocationUpdates(LocationListener listener) {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, listener);
|
return LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PendingResult removeLocationUpdates(PendingIntent callbackIntent) {
|
public PendingResult removeLocationUpdates(PendingIntent callbackIntent) {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,
|
return LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,
|
||||||
callbackIntent);
|
callbackIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PendingResult setMockMode(boolean isMockMode) {
|
public PendingResult setMockMode(boolean isMockMode) {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.setMockMode(googleApiClient, isMockMode);
|
return LocationServices.FusedLocationApi.setMockMode(googleApiClient, isMockMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PendingResult setMockLocation(Location mockLocation) {
|
public PendingResult setMockLocation(Location mockLocation) {
|
||||||
|
assertConnected();
|
||||||
return LocationServices.FusedLocationApi.setMockLocation(googleApiClient, mockLocation);
|
return LocationServices.FusedLocationApi.setMockLocation(googleApiClient, mockLocation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
|
||||||
private I serviceInterface;
|
private I serviceInterface;
|
||||||
|
|
||||||
public GmsClient(Context context, GoogleApiClient.ConnectionCallbacks callbacks,
|
public GmsClient(Context context, GoogleApiClient.ConnectionCallbacks callbacks,
|
||||||
GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
|
GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.callbacks = callbacks;
|
this.callbacks = callbacks;
|
||||||
this.connectionFailedListener = connectionFailedListener;
|
this.connectionFailedListener = connectionFailedListener;
|
||||||
|
@ -57,9 +57,11 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
|
||||||
protected abstract I interfaceFromBinder(IBinder binder);
|
protected abstract I interfaceFromBinder(IBinder binder);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect() {
|
public synchronized void connect() {
|
||||||
Log.d(TAG, "connect()");
|
Log.d(TAG, "connect()");
|
||||||
if (state == ConnectionState.CONNECTED || state == ConnectionState.CONNECTING) return;
|
if (state == ConnectionState.CONNECTED || state == ConnectionState.CONNECTING) {
|
||||||
|
Log.d(TAG, "Already connected/connecting - nothing to do");
|
||||||
|
}
|
||||||
state = ConnectionState.CONNECTING;
|
state = ConnectionState.CONNECTING;
|
||||||
if (serviceConnection != null) {
|
if (serviceConnection != null) {
|
||||||
MultiConnectionKeeper.getInstance(context)
|
MultiConnectionKeeper.getInstance(context)
|
||||||
|
@ -79,7 +81,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disconnect() {
|
public synchronized void disconnect() {
|
||||||
Log.d(TAG, "disconnect()");
|
Log.d(TAG, "disconnect()");
|
||||||
if (state == ConnectionState.DISCONNECTING) return;
|
if (state == ConnectionState.DISCONNECTING) return;
|
||||||
if (state == ConnectionState.CONNECTING) {
|
if (state == ConnectionState.CONNECTING) {
|
||||||
|
@ -95,16 +97,16 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isConnected() {
|
public synchronized boolean isConnected() {
|
||||||
return state == ConnectionState.CONNECTED || state == ConnectionState.PSEUDO_CONNECTED;
|
return state == ConnectionState.CONNECTED || state == ConnectionState.PSEUDO_CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isConnecting() {
|
public synchronized boolean isConnecting() {
|
||||||
return state == ConnectionState.CONNECTING;
|
return state == ConnectionState.CONNECTING;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasError() {
|
public synchronized boolean hasError() {
|
||||||
return state == ConnectionState.ERROR;
|
return state == ConnectionState.ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +114,13 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public I getServiceInterface() {
|
public synchronized I getServiceInterface() {
|
||||||
|
if (isConnecting()) {
|
||||||
|
// TODO: wait for connection to be established and return afterwards.
|
||||||
|
throw new IllegalStateException("Waiting for connection");
|
||||||
|
} else if (!isConnected()) {
|
||||||
|
throw new IllegalStateException("interface only available once connected!");
|
||||||
|
}
|
||||||
return serviceInterface;
|
return serviceInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +143,9 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceDisconnected(ComponentName componentName) {
|
public void onServiceDisconnected(ComponentName componentName) {
|
||||||
state = ConnectionState.NOT_CONNECTED;
|
synchronized (GmsClient.this) {
|
||||||
|
state = ConnectionState.NOT_CONNECTED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,13 +154,15 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
|
||||||
@Override
|
@Override
|
||||||
public void onPostInitComplete(int statusCode, IBinder binder, Bundle params)
|
public void onPostInitComplete(int statusCode, IBinder binder, Bundle params)
|
||||||
throws RemoteException {
|
throws RemoteException {
|
||||||
if (state == ConnectionState.DISCONNECTING) {
|
synchronized (GmsClient.this) {
|
||||||
|
if (state == ConnectionState.DISCONNECTING) {
|
||||||
|
state = ConnectionState.CONNECTED;
|
||||||
|
disconnect();
|
||||||
|
return;
|
||||||
|
}
|
||||||
state = ConnectionState.CONNECTED;
|
state = ConnectionState.CONNECTED;
|
||||||
disconnect();
|
serviceInterface = interfaceFromBinder(binder);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
state = ConnectionState.CONNECTED;
|
|
||||||
serviceInterface = interfaceFromBinder(binder);
|
|
||||||
Log.d(TAG, "GmsCallbacks : onPostInitComplete(" + serviceInterface + ")");
|
Log.d(TAG, "GmsCallbacks : onPostInitComplete(" + serviceInterface + ")");
|
||||||
callbacks.onConnected(params);
|
callbacks.onConnected(params);
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ public class GmsConnector<C extends ApiConnection, R extends Result, O extends A
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(Message msg) {
|
public void handleMessage(Message msg) {
|
||||||
Log.d(TAG, "Handler : onClientAvailable");
|
Log.d(TAG, "Handler : handleMessage");
|
||||||
AbstractPendingResult<R> result = (AbstractPendingResult<R>) msg.obj;
|
AbstractPendingResult<R> result = (AbstractPendingResult<R>) msg.obj;
|
||||||
try {
|
try {
|
||||||
result.deliverResult(callback.onClientAvailable((C) apiClient.getApiConnection
|
result.deliverResult(callback.onClientAvailable((C) apiClient.getApiConnection
|
||||||
|
|
|
@ -30,6 +30,10 @@ public class AbstractPlayServicesClient implements GooglePlayServicesClient {
|
||||||
this.googleApiClient = googleApiClient;
|
this.googleApiClient = googleApiClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void assertConnected() {
|
||||||
|
if (!isConnected()) throw new IllegalStateException("Not connected!");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect() {
|
public void connect() {
|
||||||
googleApiClient.connect();
|
googleApiClient.connect();
|
||||||
|
|
|
@ -120,8 +120,12 @@ public class GoogleApiClientImpl implements GoogleApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect() {
|
public synchronized void connect() {
|
||||||
Log.d(TAG, "connect()");
|
Log.d(TAG, "connect()");
|
||||||
|
if (isConnected() || isConnecting()) {
|
||||||
|
Log.d(TAG, "Already connected/connecting, noting to do");
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (ApiConnection connection : apiConnections.values()) {
|
for (ApiConnection connection : apiConnections.values()) {
|
||||||
if (!connection.isConnected()) {
|
if (!connection.isConnected()) {
|
||||||
connection.connect();
|
connection.connect();
|
||||||
|
@ -130,7 +134,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disconnect() {
|
public synchronized void disconnect() {
|
||||||
Log.d(TAG, "disconnect()");
|
Log.d(TAG, "disconnect()");
|
||||||
for (ApiConnection connection : apiConnections.values()) {
|
for (ApiConnection connection : apiConnections.values()) {
|
||||||
if (connection.isConnected()) {
|
if (connection.isConnected()) {
|
||||||
|
@ -140,7 +144,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isConnected() {
|
public synchronized boolean isConnected() {
|
||||||
for (ApiConnection connection : apiConnections.values()) {
|
for (ApiConnection connection : apiConnections.values()) {
|
||||||
if (!connection.isConnected()) return false;
|
if (!connection.isConnected()) return false;
|
||||||
}
|
}
|
||||||
|
@ -148,7 +152,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isConnecting() {
|
public synchronized boolean isConnecting() {
|
||||||
for (ApiConnection connection : apiConnections.values()) {
|
for (ApiConnection connection : apiConnections.values()) {
|
||||||
if (connection.isConnecting()) return true;
|
if (connection.isConnecting()) return true;
|
||||||
}
|
}
|
||||||
|
@ -167,7 +171,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reconnect() {
|
public synchronized void reconnect() {
|
||||||
Log.d(TAG, "reconnect()");
|
Log.d(TAG, "reconnect()");
|
||||||
disconnect();
|
disconnect();
|
||||||
connect();
|
connect();
|
||||||
|
|
Loading…
Reference in New Issue