Synchronize connections

This commit is contained in:
mar-v-in 2015-02-06 13:00:31 +01:00
parent 60d7da6dc6
commit 2a3cdfed4f
5 changed files with 48 additions and 20 deletions

View File

@ -31,41 +31,49 @@ public class LocationClient extends AbstractPlayServicesClient {
}
public Location getLastLocation() {
assertConnected();
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
public PendingResult requestLocationUpdates(LocationRequest request,
LocationListener listener) {
assertConnected();
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
listener);
}
public PendingResult requestLocationUpdates(LocationRequest request,
LocationListener listener, Looper looper) {
assertConnected();
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
listener, looper);
}
public PendingResult requestLocationUpdates(LocationRequest request,
PendingIntent callbackIntent) {
assertConnected();
return LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request,
callbackIntent);
}
public PendingResult removeLocationUpdates(LocationListener listener) {
assertConnected();
return LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, listener);
}
public PendingResult removeLocationUpdates(PendingIntent callbackIntent) {
assertConnected();
return LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,
callbackIntent);
}
public PendingResult setMockMode(boolean isMockMode) {
assertConnected();
return LocationServices.FusedLocationApi.setMockMode(googleApiClient, isMockMode);
}
public PendingResult setMockLocation(Location mockLocation) {
assertConnected();
return LocationServices.FusedLocationApi.setMockLocation(googleApiClient, mockLocation);
}
}

View File

@ -43,7 +43,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
private I serviceInterface;
public GmsClient(Context context, GoogleApiClient.ConnectionCallbacks callbacks,
GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
this.context = context;
this.callbacks = callbacks;
this.connectionFailedListener = connectionFailedListener;
@ -57,9 +57,11 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
protected abstract I interfaceFromBinder(IBinder binder);
@Override
public void connect() {
public synchronized void 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;
if (serviceConnection != null) {
MultiConnectionKeeper.getInstance(context)
@ -79,7 +81,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
}
@Override
public void disconnect() {
public synchronized void disconnect() {
Log.d(TAG, "disconnect()");
if (state == ConnectionState.DISCONNECTING) return;
if (state == ConnectionState.CONNECTING) {
@ -95,16 +97,16 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
}
@Override
public boolean isConnected() {
public synchronized boolean isConnected() {
return state == ConnectionState.CONNECTED || state == ConnectionState.PSEUDO_CONNECTED;
}
@Override
public boolean isConnecting() {
public synchronized boolean isConnecting() {
return state == ConnectionState.CONNECTING;
}
public boolean hasError() {
public synchronized boolean hasError() {
return state == ConnectionState.ERROR;
}
@ -112,7 +114,13 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
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;
}
@ -135,7 +143,9 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
@Override
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
public void onPostInitComplete(int statusCode, IBinder binder, Bundle params)
throws RemoteException {
if (state == ConnectionState.DISCONNECTING) {
synchronized (GmsClient.this) {
if (state == ConnectionState.DISCONNECTING) {
state = ConnectionState.CONNECTED;
disconnect();
return;
}
state = ConnectionState.CONNECTED;
disconnect();
return;
serviceInterface = interfaceFromBinder(binder);
}
state = ConnectionState.CONNECTED;
serviceInterface = interfaceFromBinder(binder);
Log.d(TAG, "GmsCallbacks : onPostInitComplete(" + serviceInterface + ")");
callbacks.onConnected(params);
}

View File

@ -64,7 +64,7 @@ public class GmsConnector<C extends ApiConnection, R extends Result, O extends A
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "Handler : onClientAvailable");
Log.d(TAG, "Handler : handleMessage");
AbstractPendingResult<R> result = (AbstractPendingResult<R>) msg.obj;
try {
result.deliverResult(callback.onClientAvailable((C) apiClient.getApiConnection

View File

@ -30,6 +30,10 @@ public class AbstractPlayServicesClient implements GooglePlayServicesClient {
this.googleApiClient = googleApiClient;
}
public void assertConnected() {
if (!isConnected()) throw new IllegalStateException("Not connected!");
}
@Override
public void connect() {
googleApiClient.connect();

View File

@ -120,8 +120,12 @@ public class GoogleApiClientImpl implements GoogleApiClient {
}
@Override
public void connect() {
public synchronized void connect() {
Log.d(TAG, "connect()");
if (isConnected() || isConnecting()) {
Log.d(TAG, "Already connected/connecting, noting to do");
return;
}
for (ApiConnection connection : apiConnections.values()) {
if (!connection.isConnected()) {
connection.connect();
@ -130,7 +134,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
}
@Override
public void disconnect() {
public synchronized void disconnect() {
Log.d(TAG, "disconnect()");
for (ApiConnection connection : apiConnections.values()) {
if (connection.isConnected()) {
@ -140,7 +144,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
}
@Override
public boolean isConnected() {
public synchronized boolean isConnected() {
for (ApiConnection connection : apiConnections.values()) {
if (!connection.isConnected()) return false;
}
@ -148,7 +152,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
}
@Override
public boolean isConnecting() {
public synchronized boolean isConnecting() {
for (ApiConnection connection : apiConnections.values()) {
if (connection.isConnecting()) return true;
}
@ -167,7 +171,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
}
@Override
public void reconnect() {
public synchronized void reconnect() {
Log.d(TAG, "reconnect()");
disconnect();
connect();