Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 74bffc1

Browse files
author
Evgenii Kanivets
committed
#141[1h]. Migrate Dropbox API from v1 to v2.
1 parent 01f476b commit 74bffc1

File tree

5 files changed

+50
-59
lines changed

5 files changed

+50
-59
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ dependencies {
7474
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4' // Charts
7575
compile 'com.jakewharton.timber:timber:4.1.2' // Advanced logging tool
7676
compile 'com.android.support.constraint:constraint-layout:1.0.2'
77+
compile 'com.dropbox.core:dropbox-core-sdk:3.0.5' // Dropbox Core API
7778

7879
testCompile 'junit:junit:4.12'
7980
testCompile 'org.mockito:mockito-core:2.0.43-beta'
-164 KB
Binary file not shown.

app/libs/json_simple-1.1.jar

-15.7 KB
Binary file not shown.

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/external/BackupActivity.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import com.blogspot.e_kanivets.moneytracker.controller.BackupController;
1414
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
1515
import com.blogspot.e_kanivets.moneytracker.util.AnswersProxy;
16-
import com.dropbox.client2.DropboxAPI;
17-
import com.dropbox.client2.android.AndroidAuthSession;
18-
import com.dropbox.client2.session.AppKeyPair;
16+
import com.dropbox.core.DbxRequestConfig;
17+
import com.dropbox.core.android.Auth;
18+
import com.dropbox.core.v2.DbxClientV2;
1919

2020
import java.util.List;
2121

@@ -28,14 +28,13 @@
2828

2929
public class BackupActivity extends BaseBackActivity {
3030
private static final String APP_KEY = "5lqugcckdy9y6lj";
31-
private static final String APP_SECRET = "psbu50k9713u68j";
3231

3332
@Inject
3433
PreferenceController preferenceController;
3534
@Inject
3635
BackupController backupController;
3736

38-
private DropboxAPI<AndroidAuthSession> dbApi;
37+
private DbxClientV2 dbClient;
3938

4039
@BindView(R.id.btn_backup_now)
4140
View btnBackupNow;
@@ -51,14 +50,11 @@ protected int getContentViewId() {
5150
protected boolean initData() {
5251
getAppComponent().inject(BackupActivity.this);
5352

54-
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
5553
String accessToken = preferenceController.readDropboxAccessToken();
56-
57-
AndroidAuthSession session = new AndroidAuthSession(appKeys);
58-
dbApi = new DropboxAPI<>(session);
59-
if (accessToken == null) dbApi.getSession().startOAuth2Authentication(BackupActivity.this);
54+
if (accessToken == null) Auth.startOAuth2Authentication(BackupActivity.this, APP_KEY);
6055
else {
61-
dbApi.getSession().setOAuth2AccessToken(accessToken);
56+
DbxRequestConfig config = new DbxRequestConfig("open_money_tracker");
57+
dbClient = new DbxClientV2(config, accessToken);
6258
fetchBackups();
6359
}
6460

@@ -75,11 +71,9 @@ protected void initViews() {
7571
protected void onResume() {
7672
super.onResume();
7773

78-
if (dbApi.getSession().authenticationSuccessful()) {
74+
if (Auth.getOAuth2Token() != null) {
7975
try {
80-
// Required to complete auth, sets the access token on the session
81-
dbApi.getSession().finishAuthentication();
82-
preferenceController.writeDropboxAccessToken(dbApi.getSession().getOAuth2AccessToken());
76+
preferenceController.writeDropboxAccessToken(Auth.getOAuth2Token());
8377
btnBackupNow.setEnabled(true);
8478
fetchBackups();
8579
} catch (IllegalStateException e) {
@@ -92,7 +86,7 @@ protected void onResume() {
9286
public void backupNow() {
9387
AnswersProxy.get().logButton("Make Backup");
9488
startProgress(getString(R.string.making_backup));
95-
backupController.makeBackup(dbApi, new BackupController.OnBackupListener() {
89+
backupController.makeBackup(dbClient, new BackupController.OnBackupListener() {
9690
@Override
9791
public void onBackupSuccess() {
9892
AnswersProxy.get().logEvent("Backup success");
@@ -137,7 +131,7 @@ public void onClick(DialogInterface dialog, int which) {
137131

138132
private void restoreBackup(final String backupName) {
139133
startProgress(getString(R.string.restoring_backup));
140-
backupController.restoreBackup(dbApi, backupName, new BackupController.OnRestoreBackupListener() {
134+
backupController.restoreBackup(dbClient, backupName, new BackupController.OnRestoreBackupListener() {
141135
@Override
142136
public void onRestoreSuccess() {
143137
AnswersProxy.get().logEvent("Restore Success");
@@ -178,7 +172,7 @@ public void onRestoreFailure(String reason) {
178172

179173
private void fetchBackups() {
180174
startProgress(getString(R.string.fetching_backups));
181-
backupController.fetchBackups(dbApi, new BackupController.OnFetchBackupListListener() {
175+
backupController.fetchBackups(dbClient, new BackupController.OnFetchBackupListListener() {
182176
@Override
183177
public void onBackupsFetched(@NonNull List<String> backupList) {
184178
if (isFinishing()) return;
@@ -193,7 +187,7 @@ public void onBackupsFetched(@NonNull List<String> backupList) {
193187

194188
private void logout() {
195189
preferenceController.writeDropboxAccessToken(null);
196-
dbApi.getSession().startOAuth2Authentication(BackupActivity.this);
190+
Auth.startOAuth2Authentication(BackupActivity.this, APP_KEY);
197191
btnBackupNow.setEnabled(false);
198192
}
199193
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/BackupController.java

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import android.support.annotation.NonNull;
55
import android.support.annotation.Nullable;
66

7-
import com.dropbox.client2.DropboxAPI;
8-
import com.dropbox.client2.android.AndroidAuthSession;
9-
import com.dropbox.client2.exception.DropboxException;
10-
import com.dropbox.client2.exception.DropboxUnlinkedException;
7+
import com.dropbox.core.DbxException;
8+
import com.dropbox.core.v2.DbxClientV2;
9+
import com.dropbox.core.v2.files.FileMetadata;
10+
import com.dropbox.core.v2.files.Metadata;
1111

1212
import java.io.File;
1313
import java.io.FileInputStream;
@@ -33,19 +33,19 @@ public BackupController(FormatController formatController, String filesDir) {
3333
this.filesDir = filesDir;
3434
}
3535

36-
public void makeBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi,
36+
public void makeBackup(@NonNull DbxClientV2 dbClient,
3737
@Nullable OnBackupListener listener) {
3838
FileInputStream fileInputStream = readAppDb();
3939
long fileLength = readAppDbFileLength();
4040
if (fileInputStream == null) return;
4141

42-
DropboxBackupAsyncTask asyncTask = new DropboxBackupAsyncTask(dbApi,
42+
DropboxBackupAsyncTask asyncTask = new DropboxBackupAsyncTask(dbClient,
4343
formatController.formatDateAndTime(System.currentTimeMillis()),
4444
fileInputStream, fileLength, listener);
4545
asyncTask.execute();
4646
}
4747

48-
public void restoreBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi, @NonNull String backupName,
48+
public void restoreBackup(@NonNull DbxClientV2 dbClient, @NonNull String backupName,
4949
@Nullable final OnRestoreBackupListener listener) {
5050
final File file = new File(getRestoreFileName());
5151
FileOutputStream outputStream = null;
@@ -59,7 +59,7 @@ public void restoreBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi, @NonNul
5959
if (listener != null) listener.onRestoreFailure(null);
6060
} else {
6161
final FileOutputStream finalOutputStream = outputStream;
62-
DropboxRestoreBackupAsyncTask asyncTask = new DropboxRestoreBackupAsyncTask(dbApi,
62+
DropboxRestoreBackupAsyncTask asyncTask = new DropboxRestoreBackupAsyncTask(dbClient,
6363
backupName, outputStream, new OnRestoreBackupListener() {
6464
@Override
6565
public void onRestoreSuccess() {
@@ -88,9 +88,9 @@ public void onRestoreFailure(String reason) {
8888
}
8989
}
9090

91-
public void fetchBackups(@NonNull DropboxAPI<AndroidAuthSession> dbApi,
91+
public void fetchBackups(@NonNull DbxClientV2 dbClient,
9292
@Nullable OnFetchBackupListListener listener) {
93-
DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask(dbApi, listener);
93+
DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask(dbClient, listener);
9494
asyncTask.execute();
9595
}
9696

@@ -125,19 +125,19 @@ private String getRestoreFileName() {
125125
return getAppDbFileName() + ".restore";
126126
}
127127

128-
private class DropboxBackupAsyncTask extends AsyncTask<Void, String, String> {
129-
private DropboxAPI<AndroidAuthSession> dbApi;
128+
private static class DropboxBackupAsyncTask extends AsyncTask<Void, String, String> {
129+
private DbxClientV2 dbClient;
130130
private String fileName;
131131
private FileInputStream fileInputStream;
132132
private long fileLength;
133133

134134
@Nullable
135135
private OnBackupListener listener;
136136

137-
public DropboxBackupAsyncTask(DropboxAPI<AndroidAuthSession> dbApi, String fileName,
137+
public DropboxBackupAsyncTask(@NonNull DbxClientV2 dbClient, String fileName,
138138
FileInputStream fileInputStream, long fileLength,
139139
@Nullable OnBackupListener listener) {
140-
this.dbApi = dbApi;
140+
this.dbClient = dbClient;
141141
this.fileName = fileName;
142142
this.fileInputStream = fileInputStream;
143143
this.fileLength = fileLength;
@@ -146,17 +146,17 @@ public DropboxBackupAsyncTask(DropboxAPI<AndroidAuthSession> dbApi, String fileN
146146

147147
@Override
148148
protected String doInBackground(Void... params) {
149-
DropboxAPI.Entry response = null;
149+
FileMetadata info = null;
150+
150151
try {
151-
response = dbApi.putFile(fileName, fileInputStream, fileLength, null, null);
152-
} catch (DropboxUnlinkedException e) {
152+
info = dbClient.files().upload("/" + fileName).uploadAndFinish(fileInputStream);
153+
} catch (DbxException e) {
153154
e.printStackTrace();
154-
return OnBackupListener.ERROR_AUTHENTICATION;
155-
} catch (DropboxException e) {
155+
} catch (IOException e) {
156156
e.printStackTrace();
157157
}
158158

159-
if (response == null) return null;
159+
if (info == null) return null;
160160
else return OnBackupListener.SUCCESS;
161161
}
162162

@@ -170,32 +170,31 @@ protected void onPostExecute(String result) {
170170
}
171171
}
172172

173-
private class DropboxFetchBackupListAsyncTask extends AsyncTask<Void, List<String>, List<String>> {
174-
private DropboxAPI<AndroidAuthSession> dbApi;
173+
private static class DropboxFetchBackupListAsyncTask extends AsyncTask<Void, List<String>, List<String>> {
174+
private DbxClientV2 dbClient;
175175

176176
@Nullable
177177
private OnFetchBackupListListener listener;
178178

179-
public DropboxFetchBackupListAsyncTask(DropboxAPI<AndroidAuthSession> dbApi,
179+
public DropboxFetchBackupListAsyncTask(DbxClientV2 dbClient,
180180
@Nullable OnFetchBackupListListener listener) {
181-
this.dbApi = dbApi;
181+
this.dbClient = dbClient;
182182
this.listener = listener;
183183
}
184184

185185
@Override
186186
protected List<String> doInBackground(Void... params) {
187-
List<DropboxAPI.Entry> entryList = new ArrayList<>();
187+
List<Metadata> entryList = new ArrayList<>();
188188
List<String> backupList = new ArrayList<>();
189189

190190
try {
191-
DropboxAPI.Entry entry = dbApi.metadata("/", -1, null, true, null);
192-
entryList = entry.contents;
193-
} catch (DropboxException e) {
191+
entryList = dbClient.files().listFolder("").getEntries();
192+
} catch (DbxException e) {
194193
e.printStackTrace();
195194
}
196195

197-
for (DropboxAPI.Entry entry : entryList) {
198-
backupList.add(entry.fileName());
196+
for (Metadata entry : entryList) {
197+
backupList.add(entry.getName());
199198
}
200199

201200
return backupList;
@@ -211,32 +210,29 @@ protected void onPostExecute(List<String> backupList) {
211210
}
212211
}
213212

214-
private class DropboxRestoreBackupAsyncTask extends AsyncTask<Void, String, String> {
215-
private DropboxAPI<AndroidAuthSession> dbApi;
213+
private static class DropboxRestoreBackupAsyncTask extends AsyncTask<Void, String, String> {
214+
private DbxClientV2 dbClient;
216215
private String backupName;
217216
private FileOutputStream outputStream;
218217

219218
@Nullable
220219
private OnRestoreBackupListener listener;
221220

222-
public DropboxRestoreBackupAsyncTask(DropboxAPI<AndroidAuthSession> dbApi, String backupName,
221+
public DropboxRestoreBackupAsyncTask(DbxClientV2 dbClient, String backupName,
223222
FileOutputStream outputStream,
224223
@Nullable OnRestoreBackupListener listener) {
225-
this.dbApi = dbApi;
224+
this.dbClient = dbClient;
226225
this.backupName = backupName;
227226
this.outputStream = outputStream;
228227
this.listener = listener;
229228
}
230229

231230
@Override
232231
protected String doInBackground(Void... params) {
233-
DropboxAPI.DropboxFileInfo info = null;
232+
FileMetadata info = null;
234233
try {
235-
info = dbApi.getFile(backupName, null, outputStream, null);
236-
} catch (DropboxUnlinkedException e) {
237-
e.printStackTrace();
238-
return OnRestoreBackupListener.ERROR_AUTHENTICATION;
239-
} catch (DropboxException e) {
234+
info = dbClient.files().download("/" + backupName).download(outputStream);
235+
} catch (DbxException | IOException e) {
240236
e.printStackTrace();
241237
}
242238

0 commit comments

Comments
 (0)