Java Code Examples for com.google.android.gms.common.api.GoogleApiClient#ConnectionCallbacks

The following examples show how to use com.google.android.gms.common.api.GoogleApiClient#ConnectionCallbacks . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleApiClientBridge.java    From friendspell with Apache License 2.0 6 votes vote down vote up
public String init(
    Activity activity,
    GoogleApiClient.ConnectionCallbacks connectedListener,
    GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
  // Configure sign-in to request the user's ID, email address, and basic profile. ID and
  // basic profile are included in DEFAULT_SIGN_IN.
  GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
          .requestEmail()
          .requestId()
          .requestProfile()
          .build();

  GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity)
      .addConnectionCallbacks(connectedListener)
      .addOnConnectionFailedListener(connectionFailedListener)
      .addApi(Plus.API)
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
      .addApi(Nearby.MESSAGES_API)
      .build();
  String token = UUID.randomUUID().toString();
  clients.put(token, googleApiClient);
  return token;
}
 
Example 2
Source File: UARTConfigurationSynchronizer.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Initializes the synchronizer.
 * @param context the activity context
 * @param listener the connection callbacks listener
 */
public static UARTConfigurationSynchronizer from(final Context context, final GoogleApiClient.ConnectionCallbacks listener) {
	if (instance == null)
		instance = new UARTConfigurationSynchronizer();

	instance.init(context, listener);
	return instance;
}
 
Example 3
Source File: LocationServicesApiBuilder.java    From android_external_GmsLib with Apache License 2.0 5 votes vote down vote up
@Override
public ApiConnection build(Context context, Looper looper,
        Api.ApiOptions.NoOptions options,
        AccountInfo accountInfo, GoogleApiClient.ConnectionCallbacks callbacks,
        GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    return new LocationClientImpl(context, callbacks, connectionFailedListener);
}
 
Example 4
Source File: GmsClient.java    From android_external_GmsLib with Apache License 2.0 5 votes vote down vote up
public GmsClient(Context context, GoogleApiClient.ConnectionCallbacks callbacks,
                 GoogleApiClient.OnConnectionFailedListener connectionFailedListener, String actionString) {
    this.context = context;
    this.callbacks = callbacks;
    this.connectionFailedListener = connectionFailedListener;
    this.actionString = actionString;
}
 
Example 5
Source File: PlayServiceManager.java    From android-sdk with MIT License 5 votes vote down vote up
public void removeListener(GoogleApiClient.ConnectionCallbacks listener) {
    Iterator<GoogleApiClient.ConnectionCallbacks> iterator = listeners.iterator();
    while (iterator.hasNext()) {
        GoogleApiClient.ConnectionCallbacks existing = iterator.next();
        if (existing == listener) {
            iterator.remove();
            return;
        }
    }
}
 
Example 6
Source File: UARTConfigurationSynchronizer.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void init(final Context context, final GoogleApiClient.ConnectionCallbacks listener) {
	if (googleApiClient != null)
		return;

	googleApiClient = new GoogleApiClient.Builder(context)
			.addApiIfAvailable(Wearable.API)
			.addConnectionCallbacks(listener)
			.build();
	googleApiClient.connect();
}
 
Example 7
Source File: PlayServiceManager.java    From android-sdk with MIT License 5 votes vote down vote up
public void addListener(GoogleApiClient.ConnectionCallbacks listener) {
    for (GoogleApiClient.ConnectionCallbacks previous : listeners) {
        if (previous == listener) {
            return;
        }
    }
    listeners.add(listener);
}
 
Example 8
Source File: MainActivityTest.java    From friendspell with Apache License 2.0 5 votes vote down vote up
protected static void setupGoogleApiClientBridge(
    GoogleApiClientBridge googleApiClientBridge, final boolean initialStatus) {
  final String token = "token";
  final ArgumentCaptor<GoogleApiClient.ConnectionCallbacks> connectedArgument
      = ArgumentCaptor.forClass(GoogleApiClient.ConnectionCallbacks.class);
  final ArgumentCaptor<GoogleApiClient.OnConnectionFailedListener> failedArgument
      = ArgumentCaptor.forClass(GoogleApiClient.OnConnectionFailedListener.class);
  Mockito.when(googleApiClientBridge.init(Mockito.any(Activity.class),
      connectedArgument.capture(), failedArgument.capture())).thenReturn(token);
  Mockito.doAnswer(new Answer() {
    @Override public Object answer(InvocationOnMock invocation) throws Throwable {
        connectedArgument.getValue().onConnected(null);
        return null;
    }
  }).when(googleApiClientBridge).connect(Mockito.anyString());

  GoogleSignInAccount account = Mockito.mock(GoogleSignInAccount.class);
  Mockito.when(googleApiClientBridge.getCurrentAccount()).thenReturn(account);

  @SuppressWarnings("unchecked") OptionalPendingResult<GoogleSignInResult> mockPendingResult =
          Mockito.mock(OptionalPendingResult.class);
  GoogleSignInResult mockInitialSignInResult = Mockito.mock(GoogleSignInResult.class);
  Mockito.when(mockInitialSignInResult.isSuccess()).thenReturn(initialStatus);
  Mockito.when(mockInitialSignInResult.getSignInAccount()).thenReturn(account);
  GoogleSignInResult mockSuccessfulSignInResult = Mockito.mock(GoogleSignInResult.class);
  Mockito.when(mockSuccessfulSignInResult.isSuccess()).thenReturn(true);
  Mockito.when(mockSuccessfulSignInResult.getSignInAccount()).thenReturn(account);
  Mockito.when(mockPendingResult.isDone()).thenReturn(true);
  Mockito.when(mockPendingResult.get()).thenReturn(mockInitialSignInResult);
  Mockito.when(googleApiClientBridge.silentSignIn(Mockito.anyString()))
      .thenReturn(mockPendingResult);
  Mockito.when(googleApiClientBridge.isConnected(Mockito.anyString())).thenReturn(true);
  Mockito.when(googleApiClientBridge.isSignedIn()).thenReturn(initialStatus);
  Mockito.when(googleApiClientBridge
      .getSignInResultFromIntent(ArgumentMatchers.isNull(Intent.class)))
      .thenReturn(mockSuccessfulSignInResult);
  Mockito.when(googleApiClientBridge.getSignInIntent(Mockito.anyString()))
      .thenReturn(new Intent("com.google.android.gms.auth.GOOGLE_SIGN_IN"));
}
 
Example 9
Source File: AuthHelper.java    From drip-steps with Apache License 2.0 5 votes vote down vote up
public void buildFitnessClient(final Context context, final GoogleApiClient.ConnectionCallbacks connectionCallbacks, final GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
	client = new GoogleApiClient.Builder(context.getApplicationContext()).
			addApi(Fitness.HISTORY_API).
			addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ)).
			addConnectionCallbacks(connectionCallbacks).
			addOnConnectionFailedListener(connectionFailedListener)
			.build();
}
 
Example 10
Source File: LocationAsyncEmitter.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void call(AsyncEmitter<Location> locationAsyncEmitter) {

    LocationListener locationListener = locationAsyncEmitter::onNext;

    GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = connectionResult ->
            locationAsyncEmitter.onError(new LocationUnavailableException("Failed to connect to Google Play Services!"));

    GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(@Nullable Bundle bundle) {
            try {
                LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener);
            } catch (SecurityException e) {
                locationAsyncEmitter.onError(new LocationUnavailableException("Location permission not available!"));
            }
        }

        @Override
        public void onConnectionSuspended(int i) {
            locationAsyncEmitter.onError(new LocationUnavailableException("Connection lost to Google Play Services"));

        }
    };

    googleApiClient.registerConnectionCallbacks(connectionCallbacks);
    googleApiClient.registerConnectionFailedListener(onConnectionFailedListener);
    googleApiClient.connect();

    locationAsyncEmitter.setCancellation(() -> {
        LocationAsyncEmitter.this.locationRequest = null;
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, locationListener);
        googleApiClient.unregisterConnectionCallbacks(connectionCallbacks);
        googleApiClient.unregisterConnectionFailedListener(onConnectionFailedListener);
    });
}
 
Example 11
Source File: SafetyNetUtils.java    From SecuritySample with Apache License 2.0 5 votes vote down vote up
public SafetyNetUtils(Context ctx, Callback callback) {
    this.ctx = ctx;
    this.callback = callback;

    GoogleApiClient.OnConnectionFailedListener googleApiConnectionFailedListener = connectionResult -> Log.e(TAG, "onConnectionFailed:" + connectionResult.toString());
    GoogleApiClient.ConnectionCallbacks googleApiConnectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(@Nullable Bundle bundle) {
            String logs = bundle == null ? "" : bundle.toString();
            callback.onResponse("GoogleApiClient onConnected " + logs);
        }

        @Override
        public void onConnectionSuspended(int i) {
            Log.d(TAG, "onConnectionSuspended" + i);
        }
    };


    Handler handler = new Handler(MyApplication.INSTANCE.safetyNetLooper.getLooper());
    googleApiClient = new GoogleApiClient.Builder(ctx)
            .addApi(SafetyNet.API)
            .addConnectionCallbacks(googleApiConnectionCallbacks)
            .addOnConnectionFailedListener(googleApiConnectionFailedListener)
            .setHandler(handler) //Run on a new thread
            .build();
    googleApiClient.connect();
    secureRandom = new SecureRandom();
}
 
Example 12
Source File: DriveApiFactory.java    From Drive-Database-Sync with Apache License 2.0 4 votes vote down vote up
/**
 * GoogleApiClient builder method. This is called by the Factory method {@link #getClient(Context, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, boolean)}.
 * That might seem redundant, but it's for future-proofing the API.
 * @param context the Activity Context
 * @param connectionCallbacks the {@link GoogleApiClient.ConnectionCallbacks}
 * @param connectionFailedListener the {@link GoogleApiClient.OnConnectionFailedListener}
 * @param debug whether to log debug messages
 * @return a fully constructed, Drive equipped GoogleApiClient
 */
private static GoogleApiClient buildDriveClient(Context context,
                                                final GoogleApiClient.ConnectionCallbacks connectionCallbacks,
                                                final GoogleApiClient.OnConnectionFailedListener connectionFailedListener,
                                                final boolean debug) {
    return new GoogleApiClient.Builder(context)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_APPFOLDER)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    if (debug) {
                        Log.d("DriveApiClient", "Connected");
                    }

                    if (connectionCallbacks != null) {
                        connectionCallbacks.onConnected(bundle);
                    }
                }

                @Override
                public void onConnectionSuspended(int i) {
                    if (debug) {
                        Log.d("DriveApiClient", "Suspended");
                    }

                    if (connectionCallbacks != null) {
                        connectionCallbacks.onConnectionSuspended(i);
                    }
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    if (debug) {
                        Log.d("DriveApiClient", "Connection failed. Cause: " + connectionResult.toString());
                    }

                    if (connectionFailedListener != null) {
                        connectionFailedListener.onConnectionFailed(connectionResult);
                    }
                }
            })
            .build();
}
 
Example 13
Source File: LocationAvailableAsyncEmitter.java    From Forage with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void call(AsyncEmitter<Void> locationAsyncEmitter) {

    GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = connectionResult ->
            locationAsyncEmitter.onError(new LocationUnavailableException("Failed to connect to Google Play Services!"));

    ResultCallback<LocationSettingsResult> pendingResultCallback = locationSettingsResult -> {
        Status status = locationSettingsResult.getStatus();
        if (status.getStatusCode() == LocationSettingsStatusCodes.SUCCESS) {
            locationAsyncEmitter.onCompleted();
        } else {
            locationAsyncEmitter.onError(new LocationUnavailableException("Location services not enabled!"));
        }
    };

    GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(@Nullable Bundle bundle) {
            try {
                LocationRequest request = LocationRequest.create()
                        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                        .addLocationRequest(request)
                        .setAlwaysShow(true);

                PendingResult<LocationSettingsResult> result =
                        LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());

                result.setResultCallback(pendingResultCallback);

            } catch (SecurityException e) {
                locationAsyncEmitter.onError(new LocationUnavailableException("Location permission not available?"));
            }
        }

        @Override
        public void onConnectionSuspended(int i) {
            locationAsyncEmitter.onError(new LocationUnavailableException("Connection lost to Google Play Services"));

        }
    };

    googleApiClient.registerConnectionCallbacks(connectionCallbacks);
    googleApiClient.registerConnectionFailedListener(onConnectionFailedListener);
    googleApiClient.connect();

    locationAsyncEmitter.setCancellation(() -> {
        googleApiClient.unregisterConnectionCallbacks(connectionCallbacks);
        googleApiClient.unregisterConnectionFailedListener(onConnectionFailedListener);
    });
}
 
Example 14
Source File: CastRemoteDisplayApiBuilder.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
@Override
public ApiConnection build(Context context, Looper looper, CastRemoteDisplay.CastRemoteDisplayOptions options, AccountInfo accountInfo, GoogleApiClient.ConnectionCallbacks callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    return new DummyApiConnection();
}
 
Example 15
Source File: CastApiBuilder.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
@Override
public ApiConnection build(Context context, Looper looper, Cast.CastOptions options, AccountInfo accountInfo, GoogleApiClient.ConnectionCallbacks callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    return new CastClientImpl(context, options, callbacks, connectionFailedListener);
}
 
Example 16
Source File: ApiBuilder.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
ApiConnection build(Context context, Looper looper, O options, AccountInfo accountInfo,
GoogleApiClient.ConnectionCallbacks callbacks,
GoogleApiClient.OnConnectionFailedListener connectionFailedListener);
 
Example 17
Source File: WearableApiBuilder.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
@Override
public ApiConnection build(Context context, Looper looper, Wearable.WearableOptions options,
        AccountInfo accountInfo, GoogleApiClient.ConnectionCallbacks callbacks,
        GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    return new WearableClientImpl(context, options, callbacks, connectionFailedListener);
}
 
Example 18
Source File: PlayServiceManager.java    From android-sdk with MIT License 4 votes vote down vote up
@Override
public void onConnected(@Nullable Bundle bundle) {
    for (GoogleApiClient.ConnectionCallbacks listener : listeners) {
        listener.onConnected(bundle);
    }
}
 
Example 19
Source File: GoogleLocationManagerClient.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
public GoogleLocationManagerClient(Context context, GoogleApiClient.ConnectionCallbacks
        callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    super(context, callbacks, connectionFailedListener, GmsService.LOCATION_MANAGER.ACTION);
}
 
Example 20
Source File: DriveApiFactory.java    From Drive-Database-Sync with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method that creates and returns a GoogleApiClient
 * @param context the Activity Context
 * @param connectionCallbacks the {@link GoogleApiClient.ConnectionCallbacks}
 * @param connectionFailedListener the {@link GoogleApiClient.OnConnectionFailedListener}
 * @param debug whether to log debug messages
 * @return a fully constructed, Drive equipped GoogleApiClient
 */
public static GoogleApiClient getClient(Context context,
                                        GoogleApiClient.ConnectionCallbacks connectionCallbacks,
                                        GoogleApiClient.OnConnectionFailedListener connectionFailedListener,
                                        boolean debug) {
    return buildDriveClient(context, connectionCallbacks, connectionFailedListener, debug);
}