Skip to content

Commit a90bb94

Browse files
asrivas-devrelGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "Migrate and update the Android Drive API Conflict sample app."
2 parents 074a2ca + b56bfc7 commit a90bb94

30 files changed

+1258
-0
lines changed

drive/conflict/.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Android Studio excludes
2+
.idea/
3+
*.iml
4+
5+
# Gradle build excludes
6+
.gradle/
7+
build/
8+
local.properties
9+
10+
# Misc
11+
.DS_Store

drive/conflict/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Google Drive Android API Conflict Sample
2+
3+
This application demonstrates how to resolve remote [conflicts](https://developers.google.com/drive/android/completion#conflict) using
4+
the [Google Android Drive API](https://developers.google.com/drive/android/intro). Keep in mind the default behavior is to
5+
overwrite the server content with the local content when a conflict arises. Please read the
6+
[conflict strategy documentation](https://developers.google.com/android/reference/com/google/android/gms/drive/ExecutionOptions.Builder#setConflictStrategy(int)) when
7+
choosing to override this capability. This application uses the [CONFLICT_STRATEGY_KEEP_REMOTE](https://developers.google.com/android/reference/com/google/android/gms/drive/ExecutionOptions#CONFLICT_STRATEGY_KEEP_REMOTE)
8+
option to allow the remote app (not the server) to manage the conflict.
9+
10+
## Set Up
11+
1. Install the [Android SDK](https://developer.android.com/sdk/index.html).
12+
1. Download and configure the
13+
[Google Play services SDK](https://developer.android.com/google/play-services/setup.html),
14+
which includes the Google Drive Android API.
15+
1. Create [Google API Console](https://console.developers.google.com/projectselector/apis/dashboard)
16+
project and/or enable the Drive API for an existing project.
17+
1. Register an OAuth 2.0 client for the package 'com.google.android.gms.drive.sample.conflict'
18+
with your own [debug keys](https://developers.google.com/drive/android/auth).
19+
See full instructions in the [Getting Started guide](https://developers.google.com/drive/android/get-started).
20+
1. Build and install the app on 2 devices or emulators. If using Android Studio, install the app
21+
individually on each emulator then use the AVD Manager to run both emulators simultaneously. Be
22+
sure to select Android images that have the Play Store enabled.
23+
24+
## Run the Demo
25+
Launch the application on both devices. Both apps should like this with no items listed.
26+
![Home Screen](images/conflict_home.png)
27+
28+
Add an item to the first device's list and hit 'Update List'.
29+
![Item Screen](images/conflict_first_item.png)
30+
31+
The second device should then show the item. Now add items on the second device's list.
32+
After updating the list, both devices should show the merge lists.
33+
![Resolved Screen](images/conflict_resolved.png)

drive/conflict/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

drive/conflict/app/build.gradle

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.2"
6+
7+
defaultConfig {
8+
applicationId "com.google.android.gms.drive.sample.conflict"
9+
minSdkVersion 21
10+
targetSdkVersion 26
11+
versionCode 2
12+
versionName "1.1"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile 'com.android.support:support-v4:26.1.0'
24+
compile 'com.google.android.gms:play-services-auth:11.6.0'
25+
compile 'com.google.android.gms:play-services-drive:11.6.0'
26+
}

drive/conflict/app/proguard-rules.pro

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /opt/android-studio/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2014 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.google.android.gms.drive.sample.conflict" >
20+
<!-- [START add_service] -->
21+
<application
22+
android:allowBackup="true"
23+
android:icon="@drawable/ic_launcher"
24+
android:label="@string/app_name"
25+
android:theme="@style/AppTheme" >
26+
<!-- [START_EXCLUDE silent] -->
27+
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
28+
<!-- [END_EXCLUDE] -->
29+
<service android:name=".MyDriveEventService"
30+
android:exported="true" >
31+
<intent-filter>
32+
<action android:name="com.google.android.gms.drive.events.HANDLE_EVENT" />
33+
</intent-filter>
34+
</service>
35+
<!-- [START_EXCLUDE silent] -->
36+
<activity
37+
android:name=".MainActivity"
38+
android:label="@string/app_name" >
39+
<intent-filter>
40+
<action android:name="android.intent.action.MAIN" />
41+
42+
<category android:name="android.intent.category.LAUNCHER" />
43+
</intent-filter>
44+
</activity>
45+
<!-- [END_EXCLUDE] -->
46+
</application>
47+
<!-- [END add_service] -->
48+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)