-
Notifications
You must be signed in to change notification settings - Fork 9
/
CloudBackup.java
216 lines (190 loc) · 8.56 KB
/
CloudBackup.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.example.intuition.humanity;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
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.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.MetadataChangeSet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import static com.example.intuition.humanity.MainActivity.DATABASE_NAME;
import static com.example.intuition.humanity.MainActivity.PACKAGE_NAME;
public class CloudBackup extends Activity
implements ConnectionCallbacks, OnConnectionFailedListener {
private static final String TAG = "drive_Humanity";
private static final int REQUEST_CODE_RESOLUTION = 3;
public static DriveFile mfile;
private static GoogleApiClient mGoogleApiClient;
private static final String DATABASE_PATH = "/data/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME;
private static final File DATA_DIRECTORY_DATABASE =
new File(Environment.getDataDirectory() + "/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME);
private static final String MIME_TYPE = "application/x-sqlite-3";
/*
* Create a new file and save it to Drive.
*/
public void saveFileToDrive() {
// Start by creating a new contents, and setting a callback.
Log.i(TAG, "Creating new contents.");
Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(@NonNull DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Toast.makeText(MainCloudBackup.this, "Error while trying to create new file contents", Toast.LENGTH_LONG).show();
return;
}
String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(MIME_TYPE);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(DATABASE_NAME) // Google Drive File name
.setMimeType(mimeType)
.setStarred(true).build();
// create a file on root folder
Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, result.getDriveContents()).setResultCallback(backupFileCallback);
}
});
}
public static void doGDriveBackup() {
Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(backupContentsCallback);
}
static final private ResultCallback<DriveApi.DriveContentsResult> backupContentsCallback = new
ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
return;
}
String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(MIME_TYPE);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(DATABASE_NAME) // Google Drive File name
.setMimeType(mimeType)
.setStarred(true).build();
// create a file on root folder
Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, result.getDriveContents())
.setResultCallback(backupFileCallback);
}
};
static final private ResultCallback<DriveFolder.DriveFileResult> backupFileCallback = new
ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(DriveFolder.DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
return;
}
mfile = result.getDriveFile();
mfile.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, new DriveFile.DownloadProgressListener() {
@Override
public void onProgress(long bytesDownloaded, long bytesExpected) {
}
}).setResultCallback(backupContentsOpenedCallback);
}
};
static final private ResultCallback<DriveApi.DriveContentsResult> backupContentsOpenedCallback = new
ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
return;
}
// DialogFragment_Sync.setProgressText("Backing up..");
DriveContents contents = result.getDriveContents();
BufferedOutputStream bos = new BufferedOutputStream(contents.getOutputStream());
byte[] buffer = new byte[1024];
int n;
try {
FileInputStream is = new FileInputStream(DATA_DIRECTORY_DATABASE);
BufferedInputStream bis = new BufferedInputStream(is);
while ((n = bis.read(buffer)) > 0) {
bos.write(buffer, 0, n);
// DialogFragment_Sync.setProgressText("Backing up...");
}
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
contents.commit(mGoogleApiClient, null).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
// DialogFragment_Sync.setProgressText("Backup completed!");
// mToast(act.getResources().getString(R.string.backupComplete));
// DialogFragment_Sync.dismissDialog();
}
});
}
};
@Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(MainCloudBackup.this, MainActivity.class);
startActivity(i);
}
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API).addScope(Drive.SCOPE_FILE).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
}
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (resultCode == Activity.RESULT_OK) {
saveFileToDrive();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
saveFileToDrive();
// doDriveBackup();
}
@Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
}