Add more debug logging, fix some bugs

This commit is contained in:
mar-v-in 2015-01-24 19:06:41 +01:00
parent 863c9693ac
commit a2b5181128
11 changed files with 90 additions and 37 deletions

View File

@ -22,7 +22,7 @@ public class GooglePlayServicesUtil {
private static final String TAG = "GooglePlayServicesUtil"; private static final String TAG = "GooglePlayServicesUtil";
public static final String GMS_ERROR_DIALOG = "GooglePlayServicesErrorDialog"; 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 int GOOGLE_PLAY_SERVICES_VERSION_CODE = Constants.MAX_REFERENCE_VERSION;
public static final String GOOGLE_PLAY_STORE_PACKAGE = "com.android.vending"; 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, public static Dialog getErrorDialog(int errorCode, Activity activity, int requestCode,
DialogInterface.OnCancelListener cancelListener) { DialogInterface.OnCancelListener cancelListener) {
return null; // TODO return null; // TODO
} }
public static PendingIntent getErrorPendingIntent(int errorCode, Activity activity, public static PendingIntent getErrorPendingIntent(int errorCode, Activity activity,
int requestCode) { int requestCode) {
return null; // TODO return null; // TODO
} }
@ -67,18 +67,17 @@ public class GooglePlayServicesUtil {
} }
public static boolean showErrorDialogFragment(int errorCode, Activity activity, public static boolean showErrorDialogFragment(int errorCode, Activity activity,
int requestCode) { int requestCode) {
return false; // TODO return false; // TODO
} }
public static boolean showErrorDialogFragment(int errorCode, Activity activity, public static boolean showErrorDialogFragment(int errorCode, Activity activity,
Fragment fragment, int requestCode, Fragment fragment, int requestCode, DialogInterface.OnCancelListener cancelListener) {
DialogInterface.OnCancelListener cancelListener) {
return false; // TODO return false; // TODO
} }
public static boolean showErrorDialogFragment(int errorCode, Activity activity, int requestCode, public static boolean showErrorDialogFragment(int errorCode, Activity activity, int requestCode,
DialogInterface.OnCancelListener cancelListener) { DialogInterface.OnCancelListener cancelListener) {
return false; // TODO return false; // TODO
} }

View File

@ -23,7 +23,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
private final Context context; private final Context context;
private final GoogleApiClient.ConnectionCallbacks callbacks; private final GoogleApiClient.ConnectionCallbacks callbacks;
private final GoogleApiClient.OnConnectionFailedListener connectionFailedListener; private final GoogleApiClient.OnConnectionFailedListener connectionFailedListener;
private ConnectionState state = ConnectionState.CONNECTED; private ConnectionState state = ConnectionState.NOT_CONNECTED;
private ServiceConnection serviceConnection; private ServiceConnection serviceConnection;
private I serviceInterface; private I serviceInterface;
@ -43,6 +43,8 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
@Override @Override
public void connect() { public void connect() {
Log.d(TAG, "connect()");
if (state == ConnectionState.CONNECTED || state == ConnectionState.CONNECTING) return;
state = ConnectionState.CONNECTING; state = ConnectionState.CONNECTING;
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) != if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) !=
ConnectionResult.SUCCESS) { ConnectionResult.SUCCESS) {
@ -60,6 +62,12 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
@Override @Override
public void disconnect() { public void disconnect() {
Log.d(TAG, "disconnect()");
if (state == ConnectionState.DISCONNECTING) return;
if (state == ConnectionState.CONNECTING) {
state = ConnectionState.DISCONNECTING;
return;
}
serviceInterface = null; serviceInterface = null;
if (serviceConnection != null) { if (serviceConnection != null) {
MultiConnectionKeeper.getInstance(context).unbind(getActionString(), serviceConnection); MultiConnectionKeeper.getInstance(context).unbind(getActionString(), serviceConnection);
@ -73,6 +81,11 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
return state == ConnectionState.CONNECTED; return state == ConnectionState.CONNECTED;
} }
@Override
public boolean isConnecting() {
return state == ConnectionState.CONNECTING;
}
public Context getContext() { public Context getContext() {
return context; return context;
} }
@ -82,7 +95,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
} }
private enum ConnectionState { private enum ConnectionState {
NOT_CONNECTED, CONNECTING, CONNECTED, ERROR NOT_CONNECTED, CONNECTING, CONNECTED, DISCONNECTING, ERROR
} }
private class GmsServiceConnection implements ServiceConnection { private class GmsServiceConnection implements ServiceConnection {
@ -90,7 +103,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
@Override @Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
try { try {
Log.d(TAG, "Connecting to broker for " + componentName); Log.d(TAG, "ServiceConnection : onServiceConnected(" + componentName + ")");
onConnectedToBroker(IGmsServiceBroker.Stub.asInterface(iBinder), onConnectedToBroker(IGmsServiceBroker.Stub.asInterface(iBinder),
new GmsCallbacks()); new GmsCallbacks());
} catch (RemoteException e) { } catch (RemoteException e) {
@ -100,7 +113,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiConnection {
@Override @Override
public void onServiceDisconnected(ComponentName componentName) { public void onServiceDisconnected(ComponentName componentName) {
state = ConnectionState.ERROR; state = ConnectionState.NOT_CONNECTED;
} }
} }
@ -109,7 +122,14 @@ 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) {
state = ConnectionState.CONNECTED;
disconnect();
return;
}
state = ConnectionState.CONNECTED;
serviceInterface = interfaceFromBinder(binder); serviceInterface = interfaceFromBinder(binder);
Log.d(TAG, "GmsCallbacks : onPostInitComplete(" + serviceInterface + ")");
callbacks.onConnected(params); callbacks.onConnected(params);
} }
} }

View File

@ -19,6 +19,7 @@ package org.microg.gms.common;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log;
import com.google.android.gms.common.api.Api; import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient; 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; import org.microg.gms.common.api.GoogleApiClientImpl;
public class GmsConnector<C extends ApiConnection, R extends Result, O extends Api.ApiOptions> { public class GmsConnector<C extends ApiConnection, R extends Result, O extends Api.ApiOptions> {
private static final String TAG = "GmsConnector";
private final GoogleApiClientImpl apiClient; private final GoogleApiClientImpl apiClient;
private final Api<O> api; private final Api<O> api;
private final Callback<C, R> callback; private final Callback<C, R> callback;
@ -41,6 +44,7 @@ public class GmsConnector<C extends ApiConnection, R extends Result, O extends A
public AbstractPendingResult<R> connect() { public AbstractPendingResult<R> connect() {
Log.d(TAG, "connect()");
Looper looper = apiClient.getLooper(); Looper looper = apiClient.getLooper();
final AbstractPendingResult<R> result = new AbstractPendingResult<>(looper); final AbstractPendingResult<R> result = new AbstractPendingResult<>(looper);
Message msg = new Message(); Message msg = new Message();
@ -60,11 +64,10 @@ 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");
AbstractPendingResult<R> result = (AbstractPendingResult<R>) msg.obj; AbstractPendingResult<R> result = (AbstractPendingResult<R>) msg.obj;
ApiConnection apiConnection = apiClient.getApiConnection(api);
apiConnection.connect();
try { try {
result.setResult(callback.onClientAvailable((C) apiConnection)); result.deliverResult(callback.onClientAvailable((C) apiClient.getApiConnection(api)));
} catch (RemoteException ignored) { } catch (RemoteException ignored) {
} }

View File

@ -26,13 +26,14 @@ public class MultiConnectionKeeper {
this.context = context; this.context = context;
} }
public static MultiConnectionKeeper getInstance(Context context) { public synchronized static MultiConnectionKeeper getInstance(Context context) {
if (INSTANCE == null) if (INSTANCE == null)
INSTANCE = new MultiConnectionKeeper(context); INSTANCE = new MultiConnectionKeeper(context);
return INSTANCE; return INSTANCE;
} }
public boolean bind(String action, ServiceConnection connection) { public boolean bind(String action, ServiceConnection connection) {
Log.d(TAG, "bind(" + action + ", " + connection + ")");
Connection con = connections.get(action); Connection con = connections.get(action);
if (con != null) { if (con != null) {
if (!con.forwardsConnection(connection)) { if (!con.forwardsConnection(connection)) {
@ -50,11 +51,13 @@ public class MultiConnectionKeeper {
} }
public void unbind(String action, ServiceConnection connection) { public void unbind(String action, ServiceConnection connection) {
Log.d(TAG, "unbind(" + action + ", " + connection + ")");
Connection con = connections.get(action); Connection con = connections.get(action);
if (con != null) { if (con != null) {
con.removeConnectionForward(connection); con.removeConnectionForward(connection);
if (!con.hasForwards() && con.isBound()) { if (!con.hasForwards() && con.isBound()) {
con.unbind(); con.unbind();
connections.remove(action);
} }
} }
} }
@ -69,9 +72,10 @@ public class MultiConnectionKeeper {
private ServiceConnection serviceConnection = new ServiceConnection() { private ServiceConnection serviceConnection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d(TAG, "Connection(" + actionString + ") : ServiceConnection : " +
"onServiceConnected("+componentName+")");
binder = iBinder; binder = iBinder;
component = componentName; component = componentName;
Log.d(TAG, "bound to " + actionString);
for (ServiceConnection connection : connectionForwards) { for (ServiceConnection connection : connectionForwards) {
connection.onServiceConnected(componentName, iBinder); connection.onServiceConnected(componentName, iBinder);
} }
@ -80,6 +84,8 @@ public class MultiConnectionKeeper {
@Override @Override
public void onServiceDisconnected(ComponentName componentName) { public void onServiceDisconnected(ComponentName componentName) {
Log.d(TAG, "Connection(" + actionString + ") : ServiceConnection : " +
"onServiceDisconnected("+componentName+")");
binder = null; binder = null;
component = componentName; component = componentName;
for (ServiceConnection connection : connectionForwards) { for (ServiceConnection connection : connectionForwards) {
@ -94,10 +100,10 @@ public class MultiConnectionKeeper {
} }
public void bind() { public void bind() {
Log.d(TAG, "Connection(" + actionString + ") : bind()");
Intent intent = new Intent(actionString).setPackage(GMS_PACKAGE_NAME); Intent intent = new Intent(actionString).setPackage(GMS_PACKAGE_NAME);
bound = context.bindService(intent, serviceConnection, bound = context.bindService(intent, serviceConnection,
Context.BIND_ADJUST_WITH_ACTIVITY | Context.BIND_AUTO_CREATE); Context.BIND_ADJUST_WITH_ACTIVITY | Context.BIND_AUTO_CREATE);
Log.d(TAG, "binding to " + actionString + ": " + bound);
if (!bound) { if (!bound) {
context.unbindService(serviceConnection); context.unbindService(serviceConnection);
} }
@ -112,8 +118,8 @@ public class MultiConnectionKeeper {
} }
public void unbind() { public void unbind() {
Log.d(TAG, "Connection(" + actionString + ") : unbind()");
context.unbindService(serviceConnection); context.unbindService(serviceConnection);
Log.d(TAG, "unbinding from " + actionString);
bound = false; bound = false;
} }

View File

@ -12,13 +12,13 @@ import java.util.concurrent.TimeUnit;
public class AbstractPendingResult<R extends Result> implements PendingResult<R> { public class AbstractPendingResult<R extends Result> implements PendingResult<R> {
private final Object lock = new Object(); private final Object lock = new Object();
private final CountDownLatch countDownLatch = new CountDownLatch(1); private final CountDownLatch countDownLatch = new CountDownLatch(1);
private final CallbackHandler<R> handler; private final ResultCallbackHandler<R> handler;
private boolean canceled; private boolean canceled;
private R result; private R result;
private ResultCallback<R> resultCallback; private ResultCallback<R> resultCallback;
public AbstractPendingResult(Looper looper) { public AbstractPendingResult(Looper looper) {
handler = new CallbackHandler<R>(looper); handler = new ResultCallbackHandler<R>(looper);
} }
private R getResult() { private R getResult() {
@ -87,13 +87,9 @@ public class AbstractPendingResult<R extends Result> implements PendingResult<R>
} }
} }
private void deliverResult(R result) { public void deliverResult(R result) {
this.result = result; this.result = result;
countDownLatch.countDown(); countDownLatch.countDown();
} }
public void setResult(R result) {
this.result = result;
}
} }

View File

@ -22,4 +22,6 @@ public interface ApiConnection {
public void disconnect(); public void disconnect();
public boolean isConnected(); public boolean isConnected();
boolean isConnecting();
} }

View File

@ -2,12 +2,17 @@ package org.microg.gms.common.api;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult; 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.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -16,6 +21,8 @@ import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class GoogleApiClientImpl implements GoogleApiClient { public class GoogleApiClientImpl implements GoogleApiClient {
private static final String TAG = "GmsApiClientImpl";
private final Context context; private final Context context;
private final Looper looper; private final Looper looper;
private final AccountInfo accountInfo; private final AccountInfo accountInfo;
@ -28,6 +35,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
private final ConnectionCallbacks baseConnectionCallbacks = new ConnectionCallbacks() { private final ConnectionCallbacks baseConnectionCallbacks = new ConnectionCallbacks() {
@Override @Override
public void onConnected(Bundle connectionHint) { public void onConnected(Bundle connectionHint) {
Log.d(TAG, "ConnectionCallbacks : onConnected()");
for (ConnectionCallbacks callback : connectionCallbacks) { for (ConnectionCallbacks callback : connectionCallbacks) {
callback.onConnected(connectionHint); callback.onConnected(connectionHint);
} }
@ -35,14 +43,17 @@ public class GoogleApiClientImpl implements GoogleApiClient {
@Override @Override
public void onConnectionSuspended(int cause) { public void onConnectionSuspended(int cause) {
Log.d(TAG, "ConnectionCallbacks : onConnectionSuspended()");
for (ConnectionCallbacks callback : connectionCallbacks) { for (ConnectionCallbacks callback : connectionCallbacks) {
callback.onConnectionSuspended(cause); callback.onConnectionSuspended(cause);
} }
} }
}; };
private final OnConnectionFailedListener baseConnectionFailedListener = new OnConnectionFailedListener() { private final OnConnectionFailedListener baseConnectionFailedListener = new
OnConnectionFailedListener() {
@Override @Override
public void onConnectionFailed(ConnectionResult result) { public void onConnectionFailed(ConnectionResult result) {
Log.d(TAG, "OnConnectionFailedListener : onConnectionFailed()");
for (OnConnectionFailedListener listener : connectionFailedListeners) { for (OnConnectionFailedListener listener : connectionFailedListeners) {
listener.onConnectionFailed(result); listener.onConnectionFailed(result);
} }
@ -61,7 +72,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
this.connectionCallbacks.addAll(connectionCallbacks); this.connectionCallbacks.addAll(connectionCallbacks);
this.connectionFailedListeners.addAll(connectionFailedListeners); this.connectionFailedListeners.addAll(connectionFailedListeners);
this.clientId = clientId; this.clientId = clientId;
for (Api api : apis.keySet()) { for (Api api : apis.keySet()) {
apiConnections.put(api, api.getBuilder().build(context, looper, apiConnections.put(api, api.getBuilder().build(context, looper,
apis.get(api), accountInfo, baseConnectionCallbacks, apis.get(api), accountInfo, baseConnectionCallbacks,
@ -94,15 +105,21 @@ public class GoogleApiClientImpl implements GoogleApiClient {
@Override @Override
public void connect() { public void connect() {
Log.d(TAG, "connect()");
for (ApiConnection connection : apiConnections.values()) { for (ApiConnection connection : apiConnections.values()) {
connection.connect(); if (!connection.isConnected()) {
connection.connect();
}
} }
} }
@Override @Override
public void disconnect() { public void disconnect() {
Log.d(TAG, "disconnect()");
for (ApiConnection connection : apiConnections.values()) { for (ApiConnection connection : apiConnections.values()) {
connection.disconnect(); if (connection.isConnected()) {
connection.disconnect();
}
} }
} }
@ -116,7 +133,10 @@ public class GoogleApiClientImpl implements GoogleApiClient {
@Override @Override
public boolean isConnecting() { public boolean isConnecting() {
return false; // TODO for (ApiConnection connection : apiConnections.values()) {
if (connection.isConnecting()) return true;
}
return false;
} }
@Override @Override
@ -132,6 +152,7 @@ public class GoogleApiClientImpl implements GoogleApiClient {
@Override @Override
public void reconnect() { public void reconnect() {
Log.d(TAG, "reconnect()");
disconnect(); disconnect();
connect(); connect();
} }

View File

@ -3,14 +3,17 @@ package org.microg.gms.common.api;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.util.Log;
import com.google.android.gms.common.api.Result; import com.google.android.gms.common.api.Result;
import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.ResultCallback;
class CallbackHandler<R extends Result> extends Handler { class ResultCallbackHandler<R extends Result> extends Handler {
private static final String TAG = "GmsResultCallbackHandler";
public static final int CALLBACK_ON_COMPLETE = 1; public static final int CALLBACK_ON_COMPLETE = 1;
public static final int CALLBACK_ON_TIMEOUT = 2; public static final int CALLBACK_ON_TIMEOUT = 2;
public CallbackHandler(Looper looper) { public ResultCallbackHandler(Looper looper) {
super(looper); super(looper);
} }
@ -19,6 +22,7 @@ class CallbackHandler<R extends Result> extends Handler {
switch (msg.what) { switch (msg.what) {
case CALLBACK_ON_COMPLETE: case CALLBACK_ON_COMPLETE:
OnCompleteObject<R> o = (OnCompleteObject<R>) msg.obj; OnCompleteObject<R> o = (OnCompleteObject<R>) msg.obj;
Log.d(TAG, "handleMessage() : onResult(" + o.result + ")");
o.callback.onResult(o.result); o.callback.onResult(o.result);
break; break;
case CALLBACK_ON_TIMEOUT: case CALLBACK_ON_TIMEOUT:
@ -35,7 +39,7 @@ class CallbackHandler<R extends Result> extends Handler {
} }
public void sendTimeoutResultCallback(AbstractPendingResult pendingResult, long millis) { public void sendTimeoutResultCallback(AbstractPendingResult pendingResult, long millis) {
} }
public static class OnCompleteObject<R extends Result> { public static class OnCompleteObject<R extends Result> {

View File

@ -23,6 +23,7 @@ public class FusedLocationProviderApiImpl implements FusedLocationProviderApi {
@Override @Override
public Location getLastLocation(GoogleApiClient client) { public Location getLastLocation(GoogleApiClient client) {
try { try {
Log.d(TAG, "getLastLocation(" + client + ")");
return LocationClientImpl.get(client).getLastLocation(); return LocationClientImpl.get(client).getLastLocation();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, e); Log.w(TAG, e);

View File

@ -12,7 +12,7 @@ import com.google.android.gms.location.internal.IGoogleLocationManagerService;
import org.microg.gms.Constants; import org.microg.gms.Constants;
import org.microg.gms.common.GmsClient; import org.microg.gms.common.GmsClient;
public class GoogleLocationManagerClient extends GmsClient<IGoogleLocationManagerService> { public abstract class GoogleLocationManagerClient extends GmsClient<IGoogleLocationManagerService> {
public GoogleLocationManagerClient(Context context, GoogleApiClient.ConnectionCallbacks public GoogleLocationManagerClient(Context context, GoogleApiClient.ConnectionCallbacks
callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) { callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
super(context, callbacks, connectionFailedListener); super(context, callbacks, connectionFailedListener);

View File

@ -38,6 +38,7 @@ public class LocationClientImpl extends GoogleLocationManagerClient {
private Map<LocationListener, ILocationListener> listenerMap = new HashMap<>(); private Map<LocationListener, ILocationListener> listenerMap = new HashMap<>();
public Location getLastLocation() throws RemoteException { public Location getLastLocation() throws RemoteException {
Log.d(TAG, "getLastLocation()");
return getServiceInterface().getLastLocation(); return getServiceInterface().getLastLocation();
} }