diff --git a/src/com/google/android/gms/common/GooglePlayServicesUtil.java b/src/com/google/android/gms/common/GooglePlayServicesUtil.java index 40cc045f..f1dc70c6 100644 --- a/src/com/google/android/gms/common/GooglePlayServicesUtil.java +++ b/src/com/google/android/gms/common/GooglePlayServicesUtil.java @@ -22,7 +22,7 @@ public class GooglePlayServicesUtil { private static final String TAG = "GooglePlayServicesUtil"; public static final String GMS_ERROR_DIALOG = "GooglePlayServicesErrorDialog"; - public static final String GOOGLE_PLAY_SERVICES_PACKAGE = "com.google.android.gms"; + public static final String GOOGLE_PLAY_SERVICES_PACKAGE = Constants.GMS_PACKAGE_NAME; public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE = Constants.MAX_REFERENCE_VERSION; public static final String GOOGLE_PLAY_STORE_PACKAGE = "com.android.vending"; @@ -31,12 +31,12 @@ public class GooglePlayServicesUtil { } public static Dialog getErrorDialog(int errorCode, Activity activity, int requestCode, - DialogInterface.OnCancelListener cancelListener) { + DialogInterface.OnCancelListener cancelListener) { return null; // TODO } public static PendingIntent getErrorPendingIntent(int errorCode, Activity activity, - int requestCode) { + int requestCode) { return null; // TODO } @@ -67,18 +67,17 @@ public class GooglePlayServicesUtil { } public static boolean showErrorDialogFragment(int errorCode, Activity activity, - int requestCode) { + int requestCode) { return false; // TODO } public static boolean showErrorDialogFragment(int errorCode, Activity activity, - Fragment fragment, int requestCode, - DialogInterface.OnCancelListener cancelListener) { + Fragment fragment, int requestCode, DialogInterface.OnCancelListener cancelListener) { return false; // TODO } public static boolean showErrorDialogFragment(int errorCode, Activity activity, int requestCode, - DialogInterface.OnCancelListener cancelListener) { + DialogInterface.OnCancelListener cancelListener) { return false; // TODO } diff --git a/src/org/microg/gms/common/GmsClient.java b/src/org/microg/gms/common/GmsClient.java index e990ab27..c9b726f7 100644 --- a/src/org/microg/gms/common/GmsClient.java +++ b/src/org/microg/gms/common/GmsClient.java @@ -23,7 +23,7 @@ public abstract class GmsClient implements ApiConnection { private final Context context; private final GoogleApiClient.ConnectionCallbacks callbacks; private final GoogleApiClient.OnConnectionFailedListener connectionFailedListener; - private ConnectionState state = ConnectionState.CONNECTED; + private ConnectionState state = ConnectionState.NOT_CONNECTED; private ServiceConnection serviceConnection; private I serviceInterface; @@ -43,6 +43,8 @@ public abstract class GmsClient implements ApiConnection { @Override public void connect() { + Log.d(TAG, "connect()"); + if (state == ConnectionState.CONNECTED || state == ConnectionState.CONNECTING) return; state = ConnectionState.CONNECTING; if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) != ConnectionResult.SUCCESS) { @@ -60,6 +62,12 @@ public abstract class GmsClient implements ApiConnection { @Override public void disconnect() { + Log.d(TAG, "disconnect()"); + if (state == ConnectionState.DISCONNECTING) return; + if (state == ConnectionState.CONNECTING) { + state = ConnectionState.DISCONNECTING; + return; + } serviceInterface = null; if (serviceConnection != null) { MultiConnectionKeeper.getInstance(context).unbind(getActionString(), serviceConnection); @@ -73,6 +81,11 @@ public abstract class GmsClient implements ApiConnection { return state == ConnectionState.CONNECTED; } + @Override + public boolean isConnecting() { + return state == ConnectionState.CONNECTING; + } + public Context getContext() { return context; } @@ -82,7 +95,7 @@ public abstract class GmsClient implements ApiConnection { } private enum ConnectionState { - NOT_CONNECTED, CONNECTING, CONNECTED, ERROR + NOT_CONNECTED, CONNECTING, CONNECTED, DISCONNECTING, ERROR } private class GmsServiceConnection implements ServiceConnection { @@ -90,7 +103,7 @@ public abstract class GmsClient implements ApiConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { try { - Log.d(TAG, "Connecting to broker for " + componentName); + Log.d(TAG, "ServiceConnection : onServiceConnected(" + componentName + ")"); onConnectedToBroker(IGmsServiceBroker.Stub.asInterface(iBinder), new GmsCallbacks()); } catch (RemoteException e) { @@ -100,7 +113,7 @@ public abstract class GmsClient implements ApiConnection { @Override public void onServiceDisconnected(ComponentName componentName) { - state = ConnectionState.ERROR; + state = ConnectionState.NOT_CONNECTED; } } @@ -109,7 +122,14 @@ public abstract class GmsClient implements ApiConnection { @Override public void onPostInitComplete(int statusCode, IBinder binder, Bundle params) throws RemoteException { + if (state == ConnectionState.DISCONNECTING) { + state = ConnectionState.CONNECTED; + disconnect(); + return; + } + state = ConnectionState.CONNECTED; serviceInterface = interfaceFromBinder(binder); + Log.d(TAG, "GmsCallbacks : onPostInitComplete(" + serviceInterface + ")"); callbacks.onConnected(params); } } diff --git a/src/org/microg/gms/common/GmsConnector.java b/src/org/microg/gms/common/GmsConnector.java index 3510c956..fbd4e01b 100644 --- a/src/org/microg/gms/common/GmsConnector.java +++ b/src/org/microg/gms/common/GmsConnector.java @@ -19,6 +19,7 @@ package org.microg.gms.common; import android.os.Looper; import android.os.Message; import android.os.RemoteException; +import android.util.Log; import com.google.android.gms.common.api.Api; import com.google.android.gms.common.api.GoogleApiClient; @@ -29,6 +30,8 @@ import org.microg.gms.common.api.ApiConnection; import org.microg.gms.common.api.GoogleApiClientImpl; public class GmsConnector { + private static final String TAG = "GmsConnector"; + private final GoogleApiClientImpl apiClient; private final Api api; private final Callback callback; @@ -41,6 +44,7 @@ public class GmsConnector connect() { + Log.d(TAG, "connect()"); Looper looper = apiClient.getLooper(); final AbstractPendingResult result = new AbstractPendingResult<>(looper); Message msg = new Message(); @@ -60,11 +64,10 @@ public class GmsConnector result = (AbstractPendingResult) msg.obj; - ApiConnection apiConnection = apiClient.getApiConnection(api); - apiConnection.connect(); try { - result.setResult(callback.onClientAvailable((C) apiConnection)); + result.deliverResult(callback.onClientAvailable((C) apiClient.getApiConnection(api))); } catch (RemoteException ignored) { } diff --git a/src/org/microg/gms/common/MultiConnectionKeeper.java b/src/org/microg/gms/common/MultiConnectionKeeper.java index e7414f71..2d4fe832 100644 --- a/src/org/microg/gms/common/MultiConnectionKeeper.java +++ b/src/org/microg/gms/common/MultiConnectionKeeper.java @@ -26,13 +26,14 @@ public class MultiConnectionKeeper { this.context = context; } - public static MultiConnectionKeeper getInstance(Context context) { + public synchronized static MultiConnectionKeeper getInstance(Context context) { if (INSTANCE == null) INSTANCE = new MultiConnectionKeeper(context); return INSTANCE; } public boolean bind(String action, ServiceConnection connection) { + Log.d(TAG, "bind(" + action + ", " + connection + ")"); Connection con = connections.get(action); if (con != null) { if (!con.forwardsConnection(connection)) { @@ -50,11 +51,13 @@ public class MultiConnectionKeeper { } public void unbind(String action, ServiceConnection connection) { + Log.d(TAG, "unbind(" + action + ", " + connection + ")"); Connection con = connections.get(action); if (con != null) { con.removeConnectionForward(connection); if (!con.hasForwards() && con.isBound()) { con.unbind(); + connections.remove(action); } } } @@ -69,9 +72,10 @@ public class MultiConnectionKeeper { private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { + Log.d(TAG, "Connection(" + actionString + ") : ServiceConnection : " + + "onServiceConnected("+componentName+")"); binder = iBinder; component = componentName; - Log.d(TAG, "bound to " + actionString); for (ServiceConnection connection : connectionForwards) { connection.onServiceConnected(componentName, iBinder); } @@ -80,6 +84,8 @@ public class MultiConnectionKeeper { @Override public void onServiceDisconnected(ComponentName componentName) { + Log.d(TAG, "Connection(" + actionString + ") : ServiceConnection : " + + "onServiceDisconnected("+componentName+")"); binder = null; component = componentName; for (ServiceConnection connection : connectionForwards) { @@ -94,10 +100,10 @@ public class MultiConnectionKeeper { } public void bind() { + Log.d(TAG, "Connection(" + actionString + ") : bind()"); Intent intent = new Intent(actionString).setPackage(GMS_PACKAGE_NAME); bound = context.bindService(intent, serviceConnection, Context.BIND_ADJUST_WITH_ACTIVITY | Context.BIND_AUTO_CREATE); - Log.d(TAG, "binding to " + actionString + ": " + bound); if (!bound) { context.unbindService(serviceConnection); } @@ -112,8 +118,8 @@ public class MultiConnectionKeeper { } public void unbind() { + Log.d(TAG, "Connection(" + actionString + ") : unbind()"); context.unbindService(serviceConnection); - Log.d(TAG, "unbinding from " + actionString); bound = false; } diff --git a/src/org/microg/gms/common/api/AbstractPendingResult.java b/src/org/microg/gms/common/api/AbstractPendingResult.java index e52fb847..7e42142c 100644 --- a/src/org/microg/gms/common/api/AbstractPendingResult.java +++ b/src/org/microg/gms/common/api/AbstractPendingResult.java @@ -12,13 +12,13 @@ import java.util.concurrent.TimeUnit; public class AbstractPendingResult implements PendingResult { private final Object lock = new Object(); private final CountDownLatch countDownLatch = new CountDownLatch(1); - private final CallbackHandler handler; + private final ResultCallbackHandler handler; private boolean canceled; private R result; private ResultCallback resultCallback; public AbstractPendingResult(Looper looper) { - handler = new CallbackHandler(looper); + handler = new ResultCallbackHandler(looper); } private R getResult() { @@ -87,13 +87,9 @@ public class AbstractPendingResult implements PendingResult } } - private void deliverResult(R result) { + public void deliverResult(R result) { this.result = result; countDownLatch.countDown(); } - - public void setResult(R result) { - this.result = result; - } } diff --git a/src/org/microg/gms/common/api/ApiConnection.java b/src/org/microg/gms/common/api/ApiConnection.java index 24a80b4a..d5697e4a 100644 --- a/src/org/microg/gms/common/api/ApiConnection.java +++ b/src/org/microg/gms/common/api/ApiConnection.java @@ -22,4 +22,6 @@ public interface ApiConnection { public void disconnect(); public boolean isConnected(); + + boolean isConnecting(); } diff --git a/src/org/microg/gms/common/api/GoogleApiClientImpl.java b/src/org/microg/gms/common/api/GoogleApiClientImpl.java index ed67ad31..1884b2f1 100644 --- a/src/org/microg/gms/common/api/GoogleApiClientImpl.java +++ b/src/org/microg/gms/common/api/GoogleApiClientImpl.java @@ -2,12 +2,17 @@ package org.microg.gms.common.api; import android.content.Context; import android.os.Bundle; -import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.v4.app.FragmentActivity; +import android.util.Log; + import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.api.*; +import com.google.android.gms.common.api.AccountInfo; +import com.google.android.gms.common.api.Api; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.PendingResult; +import com.google.android.gms.common.api.Status; import java.util.HashMap; import java.util.HashSet; @@ -16,6 +21,8 @@ import java.util.Set; import java.util.concurrent.TimeUnit; public class GoogleApiClientImpl implements GoogleApiClient { + private static final String TAG = "GmsApiClientImpl"; + private final Context context; private final Looper looper; private final AccountInfo accountInfo; @@ -28,6 +35,7 @@ public class GoogleApiClientImpl implements GoogleApiClient { private final ConnectionCallbacks baseConnectionCallbacks = new ConnectionCallbacks() { @Override public void onConnected(Bundle connectionHint) { + Log.d(TAG, "ConnectionCallbacks : onConnected()"); for (ConnectionCallbacks callback : connectionCallbacks) { callback.onConnected(connectionHint); } @@ -35,14 +43,17 @@ public class GoogleApiClientImpl implements GoogleApiClient { @Override public void onConnectionSuspended(int cause) { + Log.d(TAG, "ConnectionCallbacks : onConnectionSuspended()"); for (ConnectionCallbacks callback : connectionCallbacks) { callback.onConnectionSuspended(cause); } } }; - private final OnConnectionFailedListener baseConnectionFailedListener = new OnConnectionFailedListener() { + private final OnConnectionFailedListener baseConnectionFailedListener = new + OnConnectionFailedListener() { @Override public void onConnectionFailed(ConnectionResult result) { + Log.d(TAG, "OnConnectionFailedListener : onConnectionFailed()"); for (OnConnectionFailedListener listener : connectionFailedListeners) { listener.onConnectionFailed(result); } @@ -61,7 +72,7 @@ public class GoogleApiClientImpl implements GoogleApiClient { this.connectionCallbacks.addAll(connectionCallbacks); this.connectionFailedListeners.addAll(connectionFailedListeners); this.clientId = clientId; - + for (Api api : apis.keySet()) { apiConnections.put(api, api.getBuilder().build(context, looper, apis.get(api), accountInfo, baseConnectionCallbacks, @@ -94,15 +105,21 @@ public class GoogleApiClientImpl implements GoogleApiClient { @Override public void connect() { + Log.d(TAG, "connect()"); for (ApiConnection connection : apiConnections.values()) { - connection.connect(); + if (!connection.isConnected()) { + connection.connect(); + } } } @Override public void disconnect() { + Log.d(TAG, "disconnect()"); for (ApiConnection connection : apiConnections.values()) { - connection.disconnect(); + if (connection.isConnected()) { + connection.disconnect(); + } } } @@ -116,7 +133,10 @@ public class GoogleApiClientImpl implements GoogleApiClient { @Override public boolean isConnecting() { - return false; // TODO + for (ApiConnection connection : apiConnections.values()) { + if (connection.isConnecting()) return true; + } + return false; } @Override @@ -132,6 +152,7 @@ public class GoogleApiClientImpl implements GoogleApiClient { @Override public void reconnect() { + Log.d(TAG, "reconnect()"); disconnect(); connect(); } diff --git a/src/org/microg/gms/common/api/CallbackHandler.java b/src/org/microg/gms/common/api/ResultCallbackHandler.java similarity index 82% rename from src/org/microg/gms/common/api/CallbackHandler.java rename to src/org/microg/gms/common/api/ResultCallbackHandler.java index b2250f9e..0d5dbe98 100644 --- a/src/org/microg/gms/common/api/CallbackHandler.java +++ b/src/org/microg/gms/common/api/ResultCallbackHandler.java @@ -3,14 +3,17 @@ package org.microg.gms.common.api; import android.os.Handler; import android.os.Looper; import android.os.Message; +import android.util.Log; + import com.google.android.gms.common.api.Result; import com.google.android.gms.common.api.ResultCallback; -class CallbackHandler extends Handler { +class ResultCallbackHandler extends Handler { + private static final String TAG = "GmsResultCallbackHandler"; public static final int CALLBACK_ON_COMPLETE = 1; public static final int CALLBACK_ON_TIMEOUT = 2; - - public CallbackHandler(Looper looper) { + + public ResultCallbackHandler(Looper looper) { super(looper); } @@ -19,6 +22,7 @@ class CallbackHandler extends Handler { switch (msg.what) { case CALLBACK_ON_COMPLETE: OnCompleteObject o = (OnCompleteObject) msg.obj; + Log.d(TAG, "handleMessage() : onResult(" + o.result + ")"); o.callback.onResult(o.result); break; case CALLBACK_ON_TIMEOUT: @@ -35,7 +39,7 @@ class CallbackHandler extends Handler { } public void sendTimeoutResultCallback(AbstractPendingResult pendingResult, long millis) { - + } public static class OnCompleteObject { diff --git a/src/org/microg/gms/location/FusedLocationProviderApiImpl.java b/src/org/microg/gms/location/FusedLocationProviderApiImpl.java index 708f4263..ee2bcd25 100644 --- a/src/org/microg/gms/location/FusedLocationProviderApiImpl.java +++ b/src/org/microg/gms/location/FusedLocationProviderApiImpl.java @@ -23,6 +23,7 @@ public class FusedLocationProviderApiImpl implements FusedLocationProviderApi { @Override public Location getLastLocation(GoogleApiClient client) { try { + Log.d(TAG, "getLastLocation(" + client + ")"); return LocationClientImpl.get(client).getLastLocation(); } catch (RemoteException e) { Log.w(TAG, e); diff --git a/src/org/microg/gms/location/GoogleLocationManagerClient.java b/src/org/microg/gms/location/GoogleLocationManagerClient.java index 93f7fba1..63ed578b 100644 --- a/src/org/microg/gms/location/GoogleLocationManagerClient.java +++ b/src/org/microg/gms/location/GoogleLocationManagerClient.java @@ -12,7 +12,7 @@ import com.google.android.gms.location.internal.IGoogleLocationManagerService; import org.microg.gms.Constants; import org.microg.gms.common.GmsClient; -public class GoogleLocationManagerClient extends GmsClient { +public abstract class GoogleLocationManagerClient extends GmsClient { public GoogleLocationManagerClient(Context context, GoogleApiClient.ConnectionCallbacks callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) { super(context, callbacks, connectionFailedListener); diff --git a/src/org/microg/gms/location/LocationClientImpl.java b/src/org/microg/gms/location/LocationClientImpl.java index 07aac0f3..4ce3a12a 100644 --- a/src/org/microg/gms/location/LocationClientImpl.java +++ b/src/org/microg/gms/location/LocationClientImpl.java @@ -38,6 +38,7 @@ public class LocationClientImpl extends GoogleLocationManagerClient { private Map listenerMap = new HashMap<>(); public Location getLastLocation() throws RemoteException { + Log.d(TAG, "getLastLocation()"); return getServiceInterface().getLastLocation(); }