/** * Attempts to sign-in first via silent sign-in, then with a sign-in {@link Intent}. */ private void signIn() { Log.i(TAG, "Start silent sign-in."); final GoogleSignInClient signInClient = buildGoogleSignInClient(); signInClient.silentSignIn() .addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>() { @Override public void onSuccess(GoogleSignInAccount googleSignInAccount) { onSignInSuccess(googleSignInAccount); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Silent sign-in failed, display account selection prompt startActivityForResult(signInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN); } }); }
private void signInSilently() { if (isConnected()) { return; } GoogleSignInClient signInClient = GoogleSignIn.getClient(activity, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN); signInClient.silentSignIn().addOnCompleteListener(activity, new OnCompleteListener<GoogleSignInAccount>() { @Override public void onComplete(@NonNull Task<GoogleSignInAccount> task) { if (task.isSuccessful()) { // The signed in account is stored in the task's result. try { mAccount = task.getResult(ApiException.class); succeedSignIn(); } catch (ApiException e) { Log.w(TAG, "SignInResult::Failed code=" + e.getStatusCode() + ", Message: " + e.getStatusMessage()); } } else { // Player will need to sign-in explicitly using via UI Log.d(TAG, "Silent::Login::Failed"); } } }); }
private void loadDriveApiClients() { Set<Scope> requiredScopes = new HashSet<>(2); requiredScopes.add(Drive.SCOPE_FILE); requiredScopes.add(Drive.SCOPE_APPFOLDER); GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(getContext()); if (signInAccount != null && signInAccount.getGrantedScopes().containsAll(requiredScopes)) { initializeDriveClient(signInAccount); } else { GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_FILE) .requestScopes(Drive.SCOPE_APPFOLDER) .build(); GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(getActivity(), signInOptions); startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_RESOLVE_ERROR); } }
/** * Starts the sign-in process and initializes the Drive client. */ protected void signIn() { Set<Scope> requiredScopes = new HashSet<>(2); requiredScopes.add(Drive.SCOPE_FILE); requiredScopes.add(Drive.SCOPE_APPFOLDER); GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this); if (signInAccount != null && signInAccount.getGrantedScopes().containsAll(requiredScopes)) { initializeDriveClient(signInAccount); } else { GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_FILE) .requestScopes(Drive.SCOPE_APPFOLDER) .build(); GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions); startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN); } }
public static void signIn(AppCompatActivity activity) { String defaultWebClientId = activity.getString(R.string.default_web_client_id); GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(defaultWebClientId) .requestEmail() .build(); GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(activity, googleSignInOptions); Intent signInIntent = googleSignInClient.getSignInIntent(); activity.startActivityForResult(signInIntent, AppConstants.GOOGLE_SIGN_IN); }
/** * Set up the Drive API client. * * @param prefs The SharedPreferences * @return Whether the setup was successful */ private boolean setupClient(@NonNull SharedPreferences prefs) { final GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_APPFOLDER) .build(); final GoogleSignInClient signInClient = GoogleSignIn.getClient(mContext, signInOptions); try { final GoogleSignInAccount signInAccount = Tasks.await(signInClient.silentSignIn()); mClient = Drive.getDriveClient(mContext, signInAccount); mResourceClient = Drive.getDriveResourceClient(mContext, signInAccount); Log.d(TAG, "Connection successful. sync: " + mShouldSync + " media: " + mMediaMounted); return true; } catch(ExecutionException e) { final ApiException result = (ApiException)e.getCause(); switch(result.getStatusCode()) { case GoogleSignInStatusCodes.SIGN_IN_REQUIRED: case GoogleSignInStatusCodes.INVALID_ACCOUNT: Log.i(TAG, "User not signed in. Disabling photo syncing."); prefs.edit().putBoolean(FlavordexApp.PREF_SYNC_PHOTOS, false).apply(); break; case GoogleSignInStatusCodes.API_NOT_CONNECTED: case GoogleSignInStatusCodes.NETWORK_ERROR: case GoogleSignInStatusCodes.INTERNAL_ERROR: Log.i(TAG, "Google Drive service unavailable. Disabling photo syncing."); prefs.edit().putBoolean(FlavordexApp.PREF_SYNC_PHOTOS, false).apply(); } Log.w(TAG, "Connection failed! Reason: " + result.getMessage()); } catch(InterruptedException ignored) { } return false; }
/** * Sign in to the Google Drive service. */ private void signInDrive() { final Context context = getContext(); if(context == null) { return; } final GoogleSignInOptions options = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_APPFOLDER) .build(); final GoogleSignInClient client = GoogleSignIn.getClient(context, options); startActivityForResult(client.getSignInIntent(), REQUEST_DRIVE_SIGNIN); }
/** * Starts the sign in process to get the current user and authorize access to Drive. */ private void signIn() { GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_FILE) .build(); GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions); startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN); }
/** * Builds the {@link GoogleSignInClient} that will be used to sign-in. */ private GoogleSignInClient buildGoogleSignInClient() { GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_APPFOLDER) .build(); return GoogleSignIn.getClient(this, signInOptions); }
/** * Starts the sign-in process and initializes the Drive client. */ protected void signIn() { GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_FILE) .requestScopes(Drive.SCOPE_APPFOLDER) .build(); GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions); startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN); }
/** * Builds a Google sign-in client. */ private GoogleSignInClient getGoogleSignInClient() { GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(Drive.SCOPE_FILE) .build(); return GoogleSignIn.getClient(this, signInOptions); }