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

Commit 953ca7e

Browse files
author
Evgenii Kanivets
committed
#150. Implement Dropbox backup removal.
1 parent e9c945e commit 953ca7e

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import butterknife.OnClick;
2626
import butterknife.OnItemClick;
2727
import org.jetbrains.annotations.NotNull;
28+
import org.jetbrains.annotations.Nullable;
2829
import timber.log.Timber;
2930

3031
public class BackupActivity extends BaseBackActivity
@@ -86,7 +87,7 @@ public class BackupActivity extends BaseBackActivity
8687
builder.setMessage(getString(R.string.delete_backup_message, backupName));
8788
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
8889
@Override public void onClick(DialogInterface dialog, int which) {
89-
deleteBackup(backupName);
90+
removeBackup(backupName);
9091
}
9192
});
9293
builder.setNegativeButton(android.R.string.cancel, null);
@@ -155,6 +156,24 @@ public class BackupActivity extends BaseBackActivity
155156
if (BackupController.OnBackupListener.ERROR_AUTHENTICATION.equals(reason)) logout();
156157
}
157158

159+
@Override public void onRemoveSuccess() {
160+
AnswersProxy.get().logEvent("Remove Success");
161+
Timber.d("Remove success.");
162+
if (isFinishing()) return;
163+
164+
stopProgress();
165+
fetchBackups();
166+
}
167+
168+
@Override public void onRemoveFailure(@Nullable String reason) {
169+
AnswersProxy.get().logEvent("Remove Failure");
170+
Timber.d("Remove failure.");
171+
if (isFinishing()) return;
172+
173+
stopProgress();
174+
showToast(reason);
175+
}
176+
158177
@OnClick(R.id.btn_backup_now) public void backupNow() {
159178
AnswersProxy.get().logButton("Make Backup");
160179
startProgress(getString(R.string.making_backup));
@@ -187,8 +206,9 @@ private void fetchBackups() {
187206
backupController.fetchBackups(dbClient);
188207
}
189208

190-
private void deleteBackup(String backupName) {
191-
209+
private void removeBackup(String backupName) {
210+
startProgress("");
211+
backupController.removeBackup(dbClient, backupName);
192212
}
193213

194214
private void logout() {

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.blogspot.e_kanivets.moneytracker.controller.backup
33
import com.blogspot.e_kanivets.moneytracker.controller.FormatController
44
import com.blogspot.e_kanivets.moneytracker.controller.backup.tasks.DropboxBackupAsyncTask
55
import com.blogspot.e_kanivets.moneytracker.controller.backup.tasks.DropboxFetchBackupListAsyncTask
6+
import com.blogspot.e_kanivets.moneytracker.controller.backup.tasks.DropboxRemoveBackupAsyncTask
67
import com.blogspot.e_kanivets.moneytracker.controller.backup.tasks.DropboxRestoreBackupAsyncTask
78
import com.dropbox.core.v2.DbxClientV2
89

@@ -20,18 +21,19 @@ class BackupController(private val formatController: FormatController, filesDir:
2021

2122
fun makeBackup(dbClient: DbxClientV2) {
2223
val fileName = formatController.formatDateAndTime(System.currentTimeMillis())
23-
val asyncTask = DropboxBackupAsyncTask(dbClient, fileName, appDbFileName, onBackupListener)
24-
asyncTask.execute()
24+
DropboxBackupAsyncTask(dbClient, fileName, appDbFileName, onBackupListener).execute()
2525
}
2626

2727
fun restoreBackup(dbClient: DbxClientV2, backupName: String) {
28-
val asyncTask = DropboxRestoreBackupAsyncTask(dbClient, appDbFileName, backupName, onBackupListener)
29-
asyncTask.execute()
28+
DropboxRestoreBackupAsyncTask(dbClient, appDbFileName, backupName, onBackupListener).execute()
3029
}
3130

3231
fun fetchBackups(dbClient: DbxClientV2) {
33-
val asyncTask = DropboxFetchBackupListAsyncTask(dbClient, onBackupListener)
34-
asyncTask.execute()
32+
DropboxFetchBackupListAsyncTask(dbClient, onBackupListener).execute()
33+
}
34+
35+
fun removeBackup(dbClient: DbxClientV2, backupName: String) {
36+
DropboxRemoveBackupAsyncTask(dbClient, backupName, onBackupListener).execute()
3537
}
3638

3739
interface OnBackupListener {
@@ -46,6 +48,10 @@ class BackupController(private val formatController: FormatController, filesDir:
4648

4749
fun onRestoreFailure(reason: String?)
4850

51+
fun onRemoveSuccess()
52+
53+
fun onRemoveFailure(reason: String?)
54+
4955
companion object {
5056
const val SUCCESS = "success"
5157
const val ERROR_AUTHENTICATION = "error_authentication"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.blogspot.e_kanivets.moneytracker.controller.backup.tasks;
2+
3+
import android.os.AsyncTask;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
import com.blogspot.e_kanivets.moneytracker.controller.backup.BackupController;
7+
import com.dropbox.core.DbxException;
8+
import com.dropbox.core.v2.DbxClientV2;
9+
import com.dropbox.core.v2.files.DeleteResult;
10+
import com.dropbox.core.v2.files.FileMetadata;
11+
import com.dropbox.core.v2.files.Metadata;
12+
import java.io.File;
13+
import java.io.FileNotFoundException;
14+
import java.io.FileOutputStream;
15+
import java.io.IOException;
16+
17+
public class DropboxRemoveBackupAsyncTask extends AsyncTask<Void, String, String> {
18+
19+
private DbxClientV2 dbClient;
20+
private String backupName;
21+
22+
@Nullable private BackupController.OnBackupListener listener;
23+
24+
public DropboxRemoveBackupAsyncTask(DbxClientV2 dbClient, String backupName,
25+
@Nullable BackupController.OnBackupListener listener) {
26+
this.dbClient = dbClient;
27+
this.backupName = backupName;
28+
this.listener = listener;
29+
}
30+
31+
@Override protected String doInBackground(Void... params) {
32+
try {
33+
Metadata metadata = dbClient.files().deleteV2("/" + backupName).getMetadata();
34+
return metadata == null ? null : BackupController.OnBackupListener.SUCCESS;
35+
} catch (DbxException e) {
36+
e.printStackTrace();
37+
return e.getMessage();
38+
}
39+
}
40+
41+
@Override protected void onPostExecute(String result) {
42+
super.onPostExecute(result);
43+
if (listener == null) return;
44+
45+
if (BackupController.OnBackupListener.SUCCESS.equals(result)) {
46+
listener.onRemoveSuccess();
47+
} else {
48+
listener.onRemoveFailure(result);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)