Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#141[1h]. Migrate Dropbox API from v1 to v2.
  • Loading branch information
Evgenii Kanivets committed Dec 4, 2017
commit 74bffc1e166374f4d6e3e7237345db3323bbf4f7
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4' // Charts
compile 'com.jakewharton.timber:timber:4.1.2' // Advanced logging tool
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.dropbox.core:dropbox-core-sdk:3.0.5' // Dropbox Core API

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.0.43-beta'
Expand Down
Binary file removed app/libs/dropbox-android-sdk-1.6.3.jar
Binary file not shown.
Binary file removed app/libs/json_simple-1.1.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import com.blogspot.e_kanivets.moneytracker.controller.BackupController;
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
import com.blogspot.e_kanivets.moneytracker.util.AnswersProxy;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.android.Auth;
import com.dropbox.core.v2.DbxClientV2;

import java.util.List;

Expand All @@ -28,14 +28,13 @@

public class BackupActivity extends BaseBackActivity {
private static final String APP_KEY = "5lqugcckdy9y6lj";
private static final String APP_SECRET = "psbu50k9713u68j";

@Inject
PreferenceController preferenceController;
@Inject
BackupController backupController;

private DropboxAPI<AndroidAuthSession> dbApi;
private DbxClientV2 dbClient;

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

AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
String accessToken = preferenceController.readDropboxAccessToken();

AndroidAuthSession session = new AndroidAuthSession(appKeys);
dbApi = new DropboxAPI<>(session);
if (accessToken == null) dbApi.getSession().startOAuth2Authentication(BackupActivity.this);
if (accessToken == null) Auth.startOAuth2Authentication(BackupActivity.this, APP_KEY);
else {
dbApi.getSession().setOAuth2AccessToken(accessToken);
DbxRequestConfig config = new DbxRequestConfig("open_money_tracker");
dbClient = new DbxClientV2(config, accessToken);
fetchBackups();
}

Expand All @@ -75,11 +71,9 @@ protected void initViews() {
protected void onResume() {
super.onResume();

if (dbApi.getSession().authenticationSuccessful()) {
if (Auth.getOAuth2Token() != null) {
try {
// Required to complete auth, sets the access token on the session
dbApi.getSession().finishAuthentication();
preferenceController.writeDropboxAccessToken(dbApi.getSession().getOAuth2AccessToken());
preferenceController.writeDropboxAccessToken(Auth.getOAuth2Token());
btnBackupNow.setEnabled(true);
fetchBackups();
} catch (IllegalStateException e) {
Expand All @@ -92,7 +86,7 @@ protected void onResume() {
public void backupNow() {
AnswersProxy.get().logButton("Make Backup");
startProgress(getString(R.string.making_backup));
backupController.makeBackup(dbApi, new BackupController.OnBackupListener() {
backupController.makeBackup(dbClient, new BackupController.OnBackupListener() {
@Override
public void onBackupSuccess() {
AnswersProxy.get().logEvent("Backup success");
Expand Down Expand Up @@ -137,7 +131,7 @@ public void onClick(DialogInterface dialog, int which) {

private void restoreBackup(final String backupName) {
startProgress(getString(R.string.restoring_backup));
backupController.restoreBackup(dbApi, backupName, new BackupController.OnRestoreBackupListener() {
backupController.restoreBackup(dbClient, backupName, new BackupController.OnRestoreBackupListener() {
@Override
public void onRestoreSuccess() {
AnswersProxy.get().logEvent("Restore Success");
Expand Down Expand Up @@ -178,7 +172,7 @@ public void onRestoreFailure(String reason) {

private void fetchBackups() {
startProgress(getString(R.string.fetching_backups));
backupController.fetchBackups(dbApi, new BackupController.OnFetchBackupListListener() {
backupController.fetchBackups(dbClient, new BackupController.OnFetchBackupListListener() {
@Override
public void onBackupsFetched(@NonNull List<String> backupList) {
if (isFinishing()) return;
Expand All @@ -193,7 +187,7 @@ public void onBackupsFetched(@NonNull List<String> backupList) {

private void logout() {
preferenceController.writeDropboxAccessToken(null);
dbApi.getSession().startOAuth2Authentication(BackupActivity.this);
Auth.startOAuth2Authentication(BackupActivity.this, APP_KEY);
btnBackupNow.setEnabled(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.exception.DropboxUnlinkedException;
import com.dropbox.core.DbxException;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;
import com.dropbox.core.v2.files.Metadata;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -33,19 +33,19 @@ public BackupController(FormatController formatController, String filesDir) {
this.filesDir = filesDir;
}

public void makeBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi,
public void makeBackup(@NonNull DbxClientV2 dbClient,
@Nullable OnBackupListener listener) {
FileInputStream fileInputStream = readAppDb();
long fileLength = readAppDbFileLength();
if (fileInputStream == null) return;

DropboxBackupAsyncTask asyncTask = new DropboxBackupAsyncTask(dbApi,
DropboxBackupAsyncTask asyncTask = new DropboxBackupAsyncTask(dbClient,
formatController.formatDateAndTime(System.currentTimeMillis()),
fileInputStream, fileLength, listener);
asyncTask.execute();
}

public void restoreBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi, @NonNull String backupName,
public void restoreBackup(@NonNull DbxClientV2 dbClient, @NonNull String backupName,
@Nullable final OnRestoreBackupListener listener) {
final File file = new File(getRestoreFileName());
FileOutputStream outputStream = null;
Expand All @@ -59,7 +59,7 @@ public void restoreBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi, @NonNul
if (listener != null) listener.onRestoreFailure(null);
} else {
final FileOutputStream finalOutputStream = outputStream;
DropboxRestoreBackupAsyncTask asyncTask = new DropboxRestoreBackupAsyncTask(dbApi,
DropboxRestoreBackupAsyncTask asyncTask = new DropboxRestoreBackupAsyncTask(dbClient,
backupName, outputStream, new OnRestoreBackupListener() {
@Override
public void onRestoreSuccess() {
Expand Down Expand Up @@ -88,9 +88,9 @@ public void onRestoreFailure(String reason) {
}
}

public void fetchBackups(@NonNull DropboxAPI<AndroidAuthSession> dbApi,
public void fetchBackups(@NonNull DbxClientV2 dbClient,
@Nullable OnFetchBackupListListener listener) {
DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask(dbApi, listener);
DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask(dbClient, listener);
asyncTask.execute();
}

Expand Down Expand Up @@ -125,19 +125,19 @@ private String getRestoreFileName() {
return getAppDbFileName() + ".restore";
}

private class DropboxBackupAsyncTask extends AsyncTask<Void, String, String> {
private DropboxAPI<AndroidAuthSession> dbApi;
private static class DropboxBackupAsyncTask extends AsyncTask<Void, String, String> {
private DbxClientV2 dbClient;
private String fileName;
private FileInputStream fileInputStream;
private long fileLength;

@Nullable
private OnBackupListener listener;

public DropboxBackupAsyncTask(DropboxAPI<AndroidAuthSession> dbApi, String fileName,
public DropboxBackupAsyncTask(@NonNull DbxClientV2 dbClient, String fileName,
FileInputStream fileInputStream, long fileLength,
@Nullable OnBackupListener listener) {
this.dbApi = dbApi;
this.dbClient = dbClient;
this.fileName = fileName;
this.fileInputStream = fileInputStream;
this.fileLength = fileLength;
Expand All @@ -146,17 +146,17 @@ public DropboxBackupAsyncTask(DropboxAPI<AndroidAuthSession> dbApi, String fileN

@Override
protected String doInBackground(Void... params) {
DropboxAPI.Entry response = null;
FileMetadata info = null;

try {
response = dbApi.putFile(fileName, fileInputStream, fileLength, null, null);
} catch (DropboxUnlinkedException e) {
info = dbClient.files().upload("/" + fileName).uploadAndFinish(fileInputStream);
} catch (DbxException e) {
e.printStackTrace();
return OnBackupListener.ERROR_AUTHENTICATION;
} catch (DropboxException e) {
} catch (IOException e) {
e.printStackTrace();
}

if (response == null) return null;
if (info == null) return null;
else return OnBackupListener.SUCCESS;
}

Expand All @@ -170,32 +170,31 @@ protected void onPostExecute(String result) {
}
}

private class DropboxFetchBackupListAsyncTask extends AsyncTask<Void, List<String>, List<String>> {
private DropboxAPI<AndroidAuthSession> dbApi;
private static class DropboxFetchBackupListAsyncTask extends AsyncTask<Void, List<String>, List<String>> {
private DbxClientV2 dbClient;

@Nullable
private OnFetchBackupListListener listener;

public DropboxFetchBackupListAsyncTask(DropboxAPI<AndroidAuthSession> dbApi,
public DropboxFetchBackupListAsyncTask(DbxClientV2 dbClient,
@Nullable OnFetchBackupListListener listener) {
this.dbApi = dbApi;
this.dbClient = dbClient;
this.listener = listener;
}

@Override
protected List<String> doInBackground(Void... params) {
List<DropboxAPI.Entry> entryList = new ArrayList<>();
List<Metadata> entryList = new ArrayList<>();
List<String> backupList = new ArrayList<>();

try {
DropboxAPI.Entry entry = dbApi.metadata("/", -1, null, true, null);
entryList = entry.contents;
} catch (DropboxException e) {
entryList = dbClient.files().listFolder("").getEntries();
} catch (DbxException e) {
e.printStackTrace();
}

for (DropboxAPI.Entry entry : entryList) {
backupList.add(entry.fileName());
for (Metadata entry : entryList) {
backupList.add(entry.getName());
}

return backupList;
Expand All @@ -211,32 +210,29 @@ protected void onPostExecute(List<String> backupList) {
}
}

private class DropboxRestoreBackupAsyncTask extends AsyncTask<Void, String, String> {
private DropboxAPI<AndroidAuthSession> dbApi;
private static class DropboxRestoreBackupAsyncTask extends AsyncTask<Void, String, String> {
private DbxClientV2 dbClient;
private String backupName;
private FileOutputStream outputStream;

@Nullable
private OnRestoreBackupListener listener;

public DropboxRestoreBackupAsyncTask(DropboxAPI<AndroidAuthSession> dbApi, String backupName,
public DropboxRestoreBackupAsyncTask(DbxClientV2 dbClient, String backupName,
FileOutputStream outputStream,
@Nullable OnRestoreBackupListener listener) {
this.dbApi = dbApi;
this.dbClient = dbClient;
this.backupName = backupName;
this.outputStream = outputStream;
this.listener = listener;
}

@Override
protected String doInBackground(Void... params) {
DropboxAPI.DropboxFileInfo info = null;
FileMetadata info = null;
try {
info = dbApi.getFile(backupName, null, outputStream, null);
} catch (DropboxUnlinkedException e) {
e.printStackTrace();
return OnRestoreBackupListener.ERROR_AUTHENTICATION;
} catch (DropboxException e) {
info = dbClient.files().download("/" + backupName).download(outputStream);
} catch (DbxException | IOException e) {
e.printStackTrace();
}

Expand Down