Search in sources :

Example 1 with ConnectionCallbacks

use of com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks in project braintree_android by braintree.

the class BraintreeFragment method getGoogleApiClient.

protected GoogleApiClient getGoogleApiClient() {
    if (getActivity() == null) {
        postCallback(new GoogleApiClientException(ErrorType.NotAttachedToActivity, 1));
        return null;
    }
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addApi(Wallet.API, new Wallet.WalletOptions.Builder().setEnvironment(GooglePayment.getEnvironment(getConfiguration().getAndroidPay())).setTheme(WalletConstants.THEME_LIGHT).build()).build();
    }
    if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
        mGoogleApiClient.registerConnectionCallbacks(new ConnectionCallbacks() {

            @Override
            public void onConnected(Bundle bundle) {
            }

            @Override
            public void onConnectionSuspended(int i) {
                postCallback(new GoogleApiClientException(ErrorType.ConnectionSuspended, i));
            }
        });
        mGoogleApiClient.registerConnectionFailedListener(new OnConnectionFailedListener() {

            @Override
            public void onConnectionFailed(ConnectionResult connectionResult) {
                postCallback(new GoogleApiClientException(ErrorType.ConnectionFailed, connectionResult.getErrorCode()));
            }
        });
        mGoogleApiClient.connect();
    }
    return mGoogleApiClient;
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) GoogleApiClientException(com.braintreepayments.api.exceptions.GoogleApiClientException) Wallet(com.google.android.gms.wallet.Wallet) Bundle(android.os.Bundle) ConnectionCallbacks(com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks) ConnectionResult(com.google.android.gms.common.ConnectionResult) OnConnectionFailedListener(com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener)

Example 2 with ConnectionCallbacks

use of com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks in project iNaturalistAndroid by inaturalist.

the class INaturalistService method getLocation.

private void getLocation(final IOnLocation callback) {
    if (!mApp.isLocationEnabled(null)) {
        Log.e(TAG, "getLocation: Location not enabled");
        // Location not enabled
        new Thread(new Runnable() {

            @Override
            public void run() {
                callback.onLocation(null);
            }
        }).start();
        return;
    }
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    Log.e(TAG, "getLocation: resultCode = " + resultCode);
    if (ConnectionResult.SUCCESS == resultCode) {
        // User Google Play services if available
        if ((mLocationClient != null) && (mLocationClient.isConnected())) {
            // Location client already initialized and connected - use it
            new Thread(new Runnable() {

                @Override
                public void run() {
                    callback.onLocation(getLastKnownLocationFromClient());
                }
            }).start();
        } else {
            // Connect to the place services
            mLocationClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(new ConnectionCallbacks() {

                @Override
                public void onConnected(Bundle bundle) {
                    // Connected successfully
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            callback.onLocation(getLastKnownLocationFromClient());
                            mLocationClient.disconnect();
                        }
                    }).start();
                }

                @Override
                public void onConnectionSuspended(int i) {
                }
            }).addOnConnectionFailedListener(new OnConnectionFailedListener() {

                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    // Couldn't connect
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            callback.onLocation(null);
                            mLocationClient.disconnect();
                        }
                    }).start();
                }
            }).build();
            mLocationClient.connect();
        }
    } else {
        // Use GPS alone for place
        new Thread(new Runnable() {

            @Override
            public void run() {
                callback.onLocation(getLocationFromGPS());
            }
        }).start();
    }
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) Bundle(android.os.Bundle) ConnectionCallbacks(com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks) ConnectionResult(com.google.android.gms.common.ConnectionResult) OnConnectionFailedListener(com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener) SuppressLint(android.annotation.SuppressLint)

Aggregations

Bundle (android.os.Bundle)2 ConnectionResult (com.google.android.gms.common.ConnectionResult)2 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)2 ConnectionCallbacks (com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks)2 OnConnectionFailedListener (com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener)2 SuppressLint (android.annotation.SuppressLint)1 GoogleApiClientException (com.braintreepayments.api.exceptions.GoogleApiClientException)1 Wallet (com.google.android.gms.wallet.Wallet)1