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";
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
}

View File

@ -23,7 +23,7 @@ public abstract class GmsClient<I extends IInterface> 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<I extends IInterface> 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<I extends IInterface> 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<I extends IInterface> 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<I extends IInterface> 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<I extends IInterface> 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<I extends IInterface> implements ApiConnection {
@Override
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
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);
}
}

View File

@ -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<C extends ApiConnection, R extends Result, O extends Api.ApiOptions> {
private static final String TAG = "GmsConnector";
private final GoogleApiClientImpl apiClient;
private final Api<O> api;
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() {
Log.d(TAG, "connect()");
Looper looper = apiClient.getLooper();
final AbstractPendingResult<R> result = new AbstractPendingResult<>(looper);
Message msg = new Message();
@ -60,11 +64,10 @@ public class GmsConnector<C extends ApiConnection, R extends Result, O extends A
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "Handler : onClientAvailable");
AbstractPendingResult<R> result = (AbstractPendingResult<R>) 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) {
}

View File

@ -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;
}

View File

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

View File

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

View File

@ -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();
}

View File

@ -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<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_TIMEOUT = 2;
public CallbackHandler(Looper looper) {
public ResultCallbackHandler(Looper looper) {
super(looper);
}
@ -19,6 +22,7 @@ class CallbackHandler<R extends Result> extends Handler {
switch (msg.what) {
case CALLBACK_ON_COMPLETE:
OnCompleteObject<R> o = (OnCompleteObject<R>) 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<R extends Result> extends Handler {
}
public void sendTimeoutResultCallback(AbstractPendingResult pendingResult, long millis) {
}
public static class OnCompleteObject<R extends Result> {

View File

@ -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);

View File

@ -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<IGoogleLocationManagerService> {
public abstract class GoogleLocationManagerClient extends GmsClient<IGoogleLocationManagerService> {
public GoogleLocationManagerClient(Context context, GoogleApiClient.ConnectionCallbacks
callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
super(context, callbacks, connectionFailedListener);

View File

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