-
Notifications
You must be signed in to change notification settings - Fork 162
/
GoogleDriveBackup.java
91 lines (75 loc) · 2.85 KB
/
GoogleDriveBackup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.glucosio.android.backup;
import android.app.Activity;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;
import com.google.firebase.crash.FirebaseCrash;
import java.lang.ref.WeakReference;
public class GoogleDriveBackup implements Backup, GoogleApiClient.OnConnectionFailedListener {
@Nullable
private GoogleApiClient googleApiClient;
@Nullable
private WeakReference<Activity> activityRef;
@Override
public void init(@NonNull final Activity activity) {
this.activityRef = new WeakReference<>(activity);
googleApiClient = new GoogleApiClient.Builder(activity)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
// Do nothing
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(this)
.build();
}
@Override
public GoogleApiClient getClient() {
return googleApiClient;
}
@Override
public void start() {
if (googleApiClient != null) {
googleApiClient.connect();
} else {
throw new IllegalStateException("You should call init before start");
}
}
@Override
public void stop() {
if (googleApiClient != null) {
googleApiClient.disconnect();
} else {
throw new IllegalStateException("You should call init before start");
}
}
@Override
public void onConnectionFailed(@NonNull final ConnectionResult result) {
Log.i("Connection Failed", "GoogleApiClient connection failed: " + result.toString());
if (result.hasResolution() && activityRef != null && activityRef.get() != null) {
Activity a = activityRef.get();
// show the localized error dialog.
try {
result.startResolutionForResult(a, 1);
} catch (IntentSender.SendIntentException e) {
FirebaseCrash.log("Drive connection failed");
FirebaseCrash.report(e);
e.printStackTrace();
GoogleApiAvailability.getInstance().getErrorDialog(a, result.getErrorCode(), 0).show();
}
} else {
Log.d("error", "cannot resolve connection issue");
}
}
}