|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2014 Google Inc. All Rights Reserved. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | 
|  | 5 | + * in compliance with the License. You may obtain a copy of the License at | 
|  | 6 | + * | 
|  | 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | + * | 
|  | 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the | 
|  | 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | 
|  | 11 | + * express or implied. See the License for the specific language governing permissions and | 
|  | 12 | + * limitations under the License. | 
|  | 13 | + */ | 
|  | 14 | +package com.google.android.gms.drive.sample.conflict; | 
|  | 15 | + | 
|  | 16 | +import android.app.Activity; | 
|  | 17 | +import android.content.Intent; | 
|  | 18 | +import android.util.Log; | 
|  | 19 | +import android.widget.Toast; | 
|  | 20 | + | 
|  | 21 | +import com.google.android.gms.auth.api.signin.GoogleSignIn; | 
|  | 22 | +import com.google.android.gms.auth.api.signin.GoogleSignInAccount; | 
|  | 23 | +import com.google.android.gms.auth.api.signin.GoogleSignInClient; | 
|  | 24 | +import com.google.android.gms.auth.api.signin.GoogleSignInOptions; | 
|  | 25 | +import com.google.android.gms.drive.Drive; | 
|  | 26 | +import com.google.android.gms.drive.DriveClient; | 
|  | 27 | +import com.google.android.gms.drive.DriveResourceClient; | 
|  | 28 | +import com.google.android.gms.tasks.Task; | 
|  | 29 | + | 
|  | 30 | +/** | 
|  | 31 | + * An abstract activity that handles authorization and connection to the Drive | 
|  | 32 | + * services. | 
|  | 33 | + */ | 
|  | 34 | +public abstract class BaseDemoActivity extends Activity { | 
|  | 35 | +    private static final String TAG = "BaseDemoActivity"; | 
|  | 36 | + | 
|  | 37 | +    /** | 
|  | 38 | +     * Request code for auto Google Play Services error resolution. | 
|  | 39 | +     */ | 
|  | 40 | +    protected static final int REQUEST_CODE_SIGN_IN = 0; | 
|  | 41 | + | 
|  | 42 | +    /** | 
|  | 43 | +     * Handles high-level drive functions like sync. | 
|  | 44 | +     */ | 
|  | 45 | +    private DriveClient mDriveClient; | 
|  | 46 | + | 
|  | 47 | +    /** | 
|  | 48 | +     * Handles access to Drive resources/files. | 
|  | 49 | +     */ | 
|  | 50 | +    private DriveResourceClient mDriveResourceClient; | 
|  | 51 | + | 
|  | 52 | +    @Override | 
|  | 53 | +    protected void onStart() { | 
|  | 54 | +        super.onStart(); | 
|  | 55 | +        signIn(); | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    /** | 
|  | 59 | +     * Handles resolution callbacks. | 
|  | 60 | +     */ | 
|  | 61 | +    @Override | 
|  | 62 | +    public void onActivityResult(int requestCode, int resultCode, Intent data) { | 
|  | 63 | +        super.onActivityResult(requestCode, resultCode, data); | 
|  | 64 | +        if (requestCode == REQUEST_CODE_SIGN_IN) { | 
|  | 65 | +            if (resultCode != RESULT_OK) { | 
|  | 66 | +                // Sign-in may fail or be cancelled by the user. For this sample, sign-in is | 
|  | 67 | +                // required and is fatal. For apps where sign-in is optional, handle appropriately. | 
|  | 68 | +                Log.e(TAG, "Sign-in failed."); | 
|  | 69 | +                finish(); | 
|  | 70 | +                return; | 
|  | 71 | +            } | 
|  | 72 | + | 
|  | 73 | +            // We can use last signed in account here because we know the account has Drive scopes. | 
|  | 74 | +            initializeDriveClient(GoogleSignIn.getLastSignedInAccount(this)); | 
|  | 75 | +        } | 
|  | 76 | +    } | 
|  | 77 | + | 
|  | 78 | +    /** | 
|  | 79 | +     * Starts the sign-in process and initializes the Drive client. | 
|  | 80 | +     */ | 
|  | 81 | +    protected void signIn() { | 
|  | 82 | +        GoogleSignInOptions signInOptions = | 
|  | 83 | +                new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | 
|  | 84 | +                        .requestScopes(Drive.SCOPE_FILE) | 
|  | 85 | +                        .requestScopes(Drive.SCOPE_APPFOLDER) | 
|  | 86 | +                        .build(); | 
|  | 87 | +        GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions); | 
|  | 88 | +        startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN); | 
|  | 89 | +    } | 
|  | 90 | + | 
|  | 91 | +    /** | 
|  | 92 | +     * Continues the sign-in process, initializing the DriveResourceClient with the current | 
|  | 93 | +     * user's account. | 
|  | 94 | +     */ | 
|  | 95 | +    private void initializeDriveClient(GoogleSignInAccount signInAccount) { | 
|  | 96 | +        mDriveClient = Drive.getDriveClient(getApplicationContext(), signInAccount); | 
|  | 97 | +        mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount); | 
|  | 98 | +        onDriveClientReady(); | 
|  | 99 | +    } | 
|  | 100 | + | 
|  | 101 | +    /** | 
|  | 102 | +     * Shows a toast message. | 
|  | 103 | +     */ | 
|  | 104 | +    protected void showMessage(String message) { | 
|  | 105 | +        Toast.makeText(this, message, Toast.LENGTH_LONG).show(); | 
|  | 106 | +    } | 
|  | 107 | + | 
|  | 108 | +    /** | 
|  | 109 | +     * Called after the user has signed in and the Drive client has been initialized. | 
|  | 110 | +     */ | 
|  | 111 | +    protected abstract void onDriveClientReady(); | 
|  | 112 | + | 
|  | 113 | +    protected DriveClient getDriveClient() { | 
|  | 114 | +        return mDriveClient; | 
|  | 115 | +    } | 
|  | 116 | + | 
|  | 117 | +    protected DriveResourceClient getDriveResourceClient() { | 
|  | 118 | +        return mDriveResourceClient; | 
|  | 119 | +    } | 
|  | 120 | +} | 
0 commit comments