0

In my app, I give the user an option to backup his/her data (a db and a few audio files) to their Google Drive account in the app's folder. To allow that, the user has to "choose account for MyApp" in the following dialog:

enter image description here

That dialog is shown after I create the connection this way:

mGAPIClient = new GoogleApiClient.Builder(mACA)
                .enableAutoManage(mACA, this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

After that, I start the backup process.

However, if the user taps somewhere outside that dialog, it disappears and the connection is not made - and the dialog is not shown again.

How can I prompt the system to show him/her that dialog again when that happens? Among other things, I've tried seting the GoogleApiClient instance to null and building it up again, but that didn't work.

bernardo.g
  • 826
  • 1
  • 12
  • 27
  • Have you checked [this answer?](https://stackoverflow.com/a/30776419/2910520) – MatPag Dec 08 '17 at 21:11
  • @MatPag Not that one, but I've tried calling `GoogleApiClient.clearDefaultAccountAndReconnect();`. I believe you have to be connected for that to work (which is not my case). Thanks for the help. – bernardo.g Dec 08 '17 at 21:47
  • I will try to help you when i go back home – MatPag Dec 08 '17 at 22:08

1 Answers1

0

I've used the latest GoogleApi client as suggested in this official blog post, and I have used AGP 3.0.1 and Gradle 4.2.1.

  1. Add this classpath to project build.gradle dependencies

    classpath 'com.google.gms:google-services:3.1.2'
    
  2. Add those dependencies to app build.gradle

    implementation 'com.google.android.gms:play-services-auth:11.6.2'
    implementation 'com.google.android.gms:play-services-drive:11.6.2'
    
  3. Add plugin line at the bottom of the app build.gradle file

    apply plugin: 'com.google.gms.google-services'
    

Then in my example Activity i have:

public class MainActivity extends AppCompatActivity {

    private static final int RC_SIGN_IN = 9001;

    private GoogleSignInClient mSignInClient;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //request access to drive
        GoogleSignInOptions options =
                new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestScopes(Drive.SCOPE_FILE)
                        .build();

        mSignInClient = GoogleSignIn.getClient(this, options);
        //button to launch the signIn flow
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> signIn());
    }

    private void signIn() {
        // Launches the sign in flow, the result is returned in onActivityResult
        Intent intent = mSignInClient.getSignInIntent();
        startActivityForResult(intent, RC_SIGN_IN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task =
                    GoogleSignIn.getSignedInAccountFromIntent(data);
            if (task.isSuccessful()) {
                // Sign in succeeded, proceed with account
                GoogleSignInAccount acct = task.getResult();
                // proceed with your logic
            } else {
                // Sign in failed, handle failure and update UI
                // we should probably show a motivation screen to the user before showing
                // the account selection again, but for example purposes we launch the
                // sign-in flow immediately
                signIn();
            }
        }
    }
}

For me this is working fine. Let me know if it works for you too.

MatPag
  • 41,742
  • 14
  • 105
  • 114
  • I was hoping to avoid that new approach, since I'd rather not rewrite the whole backup and restore backup processes currently implemented with `GoogleAPIClient`. I'll weight my options.... Thanks! – bernardo.g Dec 09 '17 at 10:38
  • Consider that the new approach is a lot better than the old one. It has a lot of benefits and is a lot smarter in practically every aspect. (and maybe the conversion is not so time-consuming as you can think) ;) – MatPag Dec 09 '17 at 10:45
  • It's shaping up to be an even bigger hassle than I thought. GoogleSignIn... Services plugin... google-services.json... DriveResourceClient... I'd have to literally start from scratch. FML. Thanks for the help anyway! – bernardo.g Dec 09 '17 at 20:53