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

The following examples show how to use com.google.android.gms.common.api.GoogleApiClient#OnConnectionFailedListener . 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: GoogleApiHelper.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public static GoogleApiClient createGoogleApiClient(FragmentActivity fragmentActivity) {
    GoogleApiClient.OnConnectionFailedListener failedListener;

    if (fragmentActivity instanceof GoogleApiClient.OnConnectionFailedListener) {
        failedListener = (GoogleApiClient.OnConnectionFailedListener) fragmentActivity;
    } else {
        throw new IllegalArgumentException(fragmentActivity.getClass().getSimpleName() + " should implement OnConnectionFailedListener");
    }

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(fragmentActivity.getResources().getString(R.string.google_web_client_id))
            .requestEmail()
            .build();

    return new GoogleApiClient.Builder(fragmentActivity)
            .enableAutoManage(fragmentActivity, failedListener)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}
 
Example 2
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 3
Source File: GoogleApiHelper.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public static GoogleApiClient createGoogleApiClient(FragmentActivity fragmentActivity) {
    GoogleApiClient.OnConnectionFailedListener failedListener;

    if (fragmentActivity instanceof GoogleApiClient.OnConnectionFailedListener) {
        failedListener = (GoogleApiClient.OnConnectionFailedListener) fragmentActivity;
    } else {
        throw new IllegalArgumentException(fragmentActivity.getClass().getSimpleName() + " should implement OnConnectionFailedListener");
    }

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(fragmentActivity.getResources().getString(R.string.google_web_client_id))
            .requestEmail()
            .build();

    return new GoogleApiClient.Builder(fragmentActivity)
            .enableAutoManage(fragmentActivity, failedListener)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}
 
Example 4
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 5
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 6
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 7
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 8
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 9
Source File: WearableClientImpl.java    From android_external_GmsLib with Apache License 2.0 5 votes vote down vote up
public WearableClientImpl(Context context, Wearable.WearableOptions options, GoogleApiClient.ConnectionCallbacks callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    super(context, callbacks, connectionFailedListener, GmsService.WEARABLE.ACTION);
    serviceId = GmsService.WEARABLE.SERVICE_ID;
    if (options != null && options.firstPartyMode)
        extras.putBoolean("firstPartyMode", true);
    Log.d(TAG, "<init>");
}
 
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: GoogleClientModule.java    From Nibo with MIT License 5 votes vote down vote up
public GoogleClientModule(AppCompatActivity activity,
                          GoogleApiClient.ConnectionCallbacks connectionCallbacks,
                          GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    this.activity = activity;
    this.connectionCallbacks = connectionCallbacks;
    this.connectionFailedListener = connectionFailedListener;
}
 
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: 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 14
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 15
Source File: ActivityRecognitionApiBuilder.java    From android_external_GmsLib with Apache License 2.0 4 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 ActivityRecognitionClientImpl(context, callbacks, connectionFailedListener);
}
 
Example 16
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 17
Source File: CastClientImpl.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
public CastClientImpl(Context context, Cast.CastOptions options, GoogleApiClient.ConnectionCallbacks callbacks, GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
}
 
Example 18
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 19
Source File: LocationClientImpl.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
public LocationClientImpl(Context context, GoogleApiClient.ConnectionCallbacks callbacks,
                          GoogleApiClient.OnConnectionFailedListener connectionFailedListener) {
    super(context, callbacks, connectionFailedListener);
    Log.d(TAG, "<init>");
}
 
Example 20
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);
}