diff --git a/src/com/google/android/gms/common/api/Api.java b/src/com/google/android/gms/common/api/Api.java index d094fdd0..5a8d3cfc 100644 --- a/src/com/google/android/gms/common/api/Api.java +++ b/src/com/google/android/gms/common/api/Api.java @@ -1,7 +1,6 @@ package com.google.android.gms.common.api; -import android.content.Context; -import android.os.Looper; +import org.microg.gms.common.api.ApiBuilder; /** * Describes a section of the Google Play Services API that should be made available. Instances of @@ -17,13 +16,13 @@ import android.os.Looper; */ public final class Api { - private final Builder builder; + private final ApiBuilder builder; - public Api(Builder builder) { + public Api(ApiBuilder builder) { this.builder = builder; } - public Builder getBuilder() { + public ApiBuilder getBuilder() { return builder; } @@ -57,15 +56,4 @@ public final class Api { } } - public interface Connection { - public void connect(); - public void disconnect(); - public boolean isConnected(); - } - - public interface Builder { - Connection build(Context context, Looper looper, O options, AccountInfo accountInfo, - GoogleApiClient.ConnectionCallbacks callbacks, - GoogleApiClient.OnConnectionFailedListener connectionFailedListener); - } } diff --git a/src/com/google/android/gms/location/LocationServices.java b/src/com/google/android/gms/location/LocationServices.java index ced08aa6..db3f9b5b 100644 --- a/src/com/google/android/gms/location/LocationServices.java +++ b/src/com/google/android/gms/location/LocationServices.java @@ -4,6 +4,9 @@ import android.content.Context; import android.os.Looper; import com.google.android.gms.common.api.AccountInfo; import com.google.android.gms.common.api.Api; +import org.microg.gms.common.api.ApiConnection; + +import org.microg.gms.common.api.ApiBuilder; import com.google.android.gms.common.api.GoogleApiClient; import org.microg.gms.location.FusedLocationProviderApiImpl; import org.microg.gms.location.GeofencingApiImpl; @@ -14,9 +17,9 @@ import org.microg.gms.location.LocationClientImpl; */ public class LocationServices { public static final Api API = new Api<>( - new Api.Builder() { + new ApiBuilder() { @Override - public Api.Connection build(Context context, Looper looper, + public ApiConnection build(Context context, Looper looper, Api.ApiOptions.NoOptions options, AccountInfo accountInfo, GoogleApiClient.ConnectionCallbacks callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) { diff --git a/src/org/microg/gms/common/GmsClient.java b/src/org/microg/gms/common/GmsClient.java index 046c2e36..14e00cd8 100644 --- a/src/org/microg/gms/common/GmsClient.java +++ b/src/org/microg/gms/common/GmsClient.java @@ -9,11 +9,11 @@ import android.os.IInterface; import android.os.RemoteException; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; -import com.google.android.gms.common.api.Api; +import org.microg.gms.common.api.ApiConnection; import com.google.android.gms.common.internal.IGmsCallbacks; import com.google.android.gms.common.internal.IGmsServiceBroker; -public abstract class GmsClient implements Api.Connection { +public abstract class GmsClient implements ApiConnection { private static final String TAG = "GmsClient"; private final Context context; diff --git a/src/org/microg/gms/common/GmsConnector.java b/src/org/microg/gms/common/GmsConnector.java new file mode 100644 index 00000000..af3c8699 --- /dev/null +++ b/src/org/microg/gms/common/GmsConnector.java @@ -0,0 +1,79 @@ +/* + * Copyright 2014-2015 µg Project Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.microg.gms.common; + +import android.os.Looper; +import android.os.Message; +import android.os.RemoteException; + +import com.google.android.gms.common.api.Api; + +import org.microg.gms.common.api.AbstractPendingResult; +import org.microg.gms.common.api.ApiConnection; + +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.Result; + +import org.microg.gms.common.api.GoogleApiClientImpl; + +public class GmsConnector { + public static + AbstractPendingResult connect(GoogleApiClient apiClient, Api api, Callback callback) { + Looper looper = ((GoogleApiClientImpl) apiClient).getLooper(); + final AbstractPendingResult result = new AbstractPendingResult<>(looper); + Message msg = new Message(); + msg.obj = new ConnectRequest((GoogleApiClientImpl) apiClient, api, result, callback); + new Handler(looper).sendMessage(msg); + return result; + } + + public static interface Callback { + public R onClientAvailable(C client) throws RemoteException; + } + + private static class Handler extends android.os.Handler { + private Handler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + ConnectRequest request = (ConnectRequest) msg.obj; + ApiConnection apiConnection = request.apiClient.getApiConnection(request.api); + apiConnection.connect(); + try { + request.result.setResult(request.callback.onClientAvailable((C) apiConnection)); + } catch (RemoteException ignored) { + + } + } + } + + private static class ConnectRequest { + GoogleApiClientImpl apiClient; + Api api; + AbstractPendingResult result; + Callback callback; + + private ConnectRequest(GoogleApiClientImpl apiClient, Api api, AbstractPendingResult result, Callback callback) { + this.apiClient = apiClient; + this.api = api; + this.result = result; + this.callback = callback; + } + } +} diff --git a/src/org/microg/gms/common/api/AbstractPendingResult.java b/src/org/microg/gms/common/api/AbstractPendingResult.java index dfb00a86..2b34ae43 100644 --- a/src/org/microg/gms/common/api/AbstractPendingResult.java +++ b/src/org/microg/gms/common/api/AbstractPendingResult.java @@ -11,13 +11,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 CallbackHandler handler; private boolean canceled; private R result; private ResultCallback resultCallback; public AbstractPendingResult(Looper looper) { - handler = new CallbackHandler(looper); + handler = new CallbackHandler(looper); } private R getResult() { @@ -92,4 +92,7 @@ public class AbstractPendingResult implements PendingResult } + public void setResult(R result) { + this.result = result; + } } diff --git a/src/org/microg/gms/common/api/ApiBuilder.java b/src/org/microg/gms/common/api/ApiBuilder.java new file mode 100644 index 00000000..14aba7d0 --- /dev/null +++ b/src/org/microg/gms/common/api/ApiBuilder.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014-2015 µg Project Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.microg.gms.common.api; + +import android.content.Context; +import android.os.Looper; + +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 org.microg.gms.common.api.ApiConnection; + +public interface ApiBuilder { + ApiConnection build(Context context, Looper looper, O options, AccountInfo accountInfo, + GoogleApiClient.ConnectionCallbacks callbacks, + GoogleApiClient.OnConnectionFailedListener connectionFailedListener); +} diff --git a/src/org/microg/gms/common/api/ApiConnection.java b/src/org/microg/gms/common/api/ApiConnection.java new file mode 100644 index 00000000..8f21c1bf --- /dev/null +++ b/src/org/microg/gms/common/api/ApiConnection.java @@ -0,0 +1,23 @@ +/* + * Copyright 2014-2015 µg Project Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.microg.gms.common.api; + +public interface ApiConnection { + public void connect(); + public void disconnect(); + public boolean isConnected(); +} diff --git a/src/org/microg/gms/common/api/CallbackHandler.java b/src/org/microg/gms/common/api/CallbackHandler.java index 9d573abc..b2250f9e 100644 --- a/src/org/microg/gms/common/api/CallbackHandler.java +++ b/src/org/microg/gms/common/api/CallbackHandler.java @@ -28,7 +28,10 @@ class CallbackHandler extends Handler { } public void sendResultCallback(ResultCallback callback, R result) { - + Message message = new Message(); + message.what = CALLBACK_ON_COMPLETE; + message.obj = new OnCompleteObject(callback, result); + sendMessage(message); } public void sendTimeoutResultCallback(AbstractPendingResult pendingResult, long millis) { @@ -38,5 +41,10 @@ class CallbackHandler extends Handler { public static class OnCompleteObject { public ResultCallback callback; public R result; + + public OnCompleteObject(ResultCallback callback, R result) { + this.callback = callback; + this.result = result; + } } } diff --git a/src/org/microg/gms/common/api/GoogleApiClientImpl.java b/src/org/microg/gms/common/api/GoogleApiClientImpl.java index f77b5243..8a96332c 100644 --- a/src/org/microg/gms/common/api/GoogleApiClientImpl.java +++ b/src/org/microg/gms/common/api/GoogleApiClientImpl.java @@ -18,9 +18,10 @@ public class GoogleApiClientImpl implements GoogleApiClient { private final Looper looper; private final AccountInfo accountInfo; private final Map apis = new HashMap<>(); - private final Map apiConnections = new HashMap<>(); + private final Map apiConnections = new HashMap<>(); private final Set connectionCallbacks = new HashSet<>(); private final Set connectionFailedListeners = new HashSet<>(); + private final int clientId; private final ConnectionCallbacks baseConnectionCallbacks = new ConnectionCallbacks() { @Override public void onConnected(Bundle connectionHint) { @@ -44,7 +45,6 @@ public class GoogleApiClientImpl implements GoogleApiClient { } } }; - private final int clientId; public GoogleApiClientImpl(Context context, Looper looper, AccountInfo accountInfo, Map apis, @@ -64,8 +64,12 @@ public class GoogleApiClientImpl implements GoogleApiClient { baseConnectionFailedListener)); } } - - public Api.Connection getApiConnection(Api api) { + + public Looper getLooper() { + return looper; + } + + public ApiConnection getApiConnection(Api api) { return apiConnections.get(api); } @@ -86,21 +90,21 @@ public class GoogleApiClientImpl implements GoogleApiClient { @Override public void connect() { - for (Api.Connection connection : apiConnections.values()) { + for (ApiConnection connection : apiConnections.values()) { connection.connect(); } } @Override public void disconnect() { - for (Api.Connection connection : apiConnections.values()) { + for (ApiConnection connection : apiConnections.values()) { connection.disconnect(); } } @Override public boolean isConnected() { - for (Api.Connection connection : apiConnections.values()) { + for (ApiConnection connection : apiConnections.values()) { if (!connection.isConnected()) return false; } return true; diff --git a/src/org/microg/gms/location/FusedLocationProviderApiImpl.java b/src/org/microg/gms/location/FusedLocationProviderApiImpl.java index eb4c516f..169b9788 100644 --- a/src/org/microg/gms/location/FusedLocationProviderApiImpl.java +++ b/src/org/microg/gms/location/FusedLocationProviderApiImpl.java @@ -11,6 +11,8 @@ import com.google.android.gms.location.FusedLocationProviderApi; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; +import org.microg.gms.common.GmsConnector; + public class FusedLocationProviderApiImpl implements FusedLocationProviderApi { private static final String TAG = "GmsFusedApiImpl"; @@ -27,6 +29,7 @@ public class FusedLocationProviderApiImpl implements FusedLocationProviderApi { @Override public PendingResult requestLocationUpdates(GoogleApiClient client, LocationRequest request, LocationListener listener) { + //LocationClientImpl.get(client).requestLocationUpdates(request, listener); return null; }