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

Commit 64cae2d

Browse files
author
Evgenii Kanivets
committed
#150. Refactore BackupListener.
1 parent b4af995 commit 64cae2d

File tree

3 files changed

+85
-108
lines changed

3 files changed

+85
-108
lines changed

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

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
import com.blogspot.e_kanivets.moneytracker.controller.backup.tasks.DropboxRestoreBackupAsyncTask;
1010
import com.dropbox.core.v2.DbxClientV2;
1111

12-
import java.io.File;
13-
import java.io.FileInputStream;
14-
import java.io.FileNotFoundException;
15-
import java.io.FileOutputStream;
16-
import java.io.IOException;
1712
import java.util.List;
1813

1914
/**
@@ -32,98 +27,29 @@ public BackupController(FormatController formatController, String filesDir) {
3227
this.filesDir = filesDir;
3328
}
3429

35-
public void makeBackup(@NonNull DbxClientV2 dbClient,
36-
@Nullable OnBackupListener listener) {
37-
FileInputStream fileInputStream = readAppDb();
38-
long fileLength = readAppDbFileLength();
39-
if (fileInputStream == null) return;
40-
41-
DropboxBackupAsyncTask asyncTask = new DropboxBackupAsyncTask(dbClient,
42-
formatController.formatDateAndTime(System.currentTimeMillis()),
43-
fileInputStream, fileLength, listener);
30+
public void makeBackup(@NonNull DbxClientV2 dbClient, @Nullable OnBackupListener listener) {
31+
DropboxBackupAsyncTask asyncTask =
32+
new DropboxBackupAsyncTask(dbClient, formatController.formatDateAndTime(System.currentTimeMillis()),
33+
getAppDbFileName(), listener);
4434
asyncTask.execute();
4535
}
4636

4737
public void restoreBackup(@NonNull DbxClientV2 dbClient, @NonNull String backupName,
48-
@Nullable final OnRestoreBackupListener listener) {
49-
final File file = new File(getRestoreFileName());
50-
FileOutputStream outputStream = null;
51-
try {
52-
outputStream = new FileOutputStream(file);
53-
} catch (FileNotFoundException e) {
54-
e.printStackTrace();
55-
}
56-
57-
if (outputStream == null) {
58-
if (listener != null) listener.onRestoreFailure(null);
59-
} else {
60-
final FileOutputStream finalOutputStream = outputStream;
61-
DropboxRestoreBackupAsyncTask asyncTask = new DropboxRestoreBackupAsyncTask(dbClient,
62-
backupName, outputStream, new OnRestoreBackupListener() {
63-
@Override
64-
public void onRestoreSuccess() {
65-
try {
66-
finalOutputStream.close();
67-
} catch (IOException e) {
68-
if (listener != null) listener.onRestoreFailure(null);
69-
e.printStackTrace();
70-
}
71-
72-
if (file.exists() && file.length() != 0) {
73-
boolean renamed = file.renameTo(new File(getAppDbFileName()));
74-
if (listener != null) {
75-
if (renamed) listener.onRestoreSuccess();
76-
else listener.onRestoreFailure(null);
77-
}
78-
}
79-
}
80-
81-
@Override
82-
public void onRestoreFailure(String reason) {
83-
if (listener != null) listener.onRestoreFailure(reason);
84-
}
85-
});
86-
asyncTask.execute();
87-
}
38+
@Nullable final OnRestoreBackupListener listener) {
39+
DropboxRestoreBackupAsyncTask asyncTask =
40+
new DropboxRestoreBackupAsyncTask(dbClient, getAppDbFileName(), backupName, listener);
41+
asyncTask.execute();
8842
}
8943

90-
public void fetchBackups(@NonNull DbxClientV2 dbClient,
91-
@Nullable OnFetchBackupListListener listener) {
44+
public void fetchBackups(@NonNull DbxClientV2 dbClient, @Nullable OnFetchBackupListListener listener) {
9245
DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask(dbClient, listener);
9346
asyncTask.execute();
9447
}
9548

96-
@Nullable
97-
private FileInputStream readAppDb() {
98-
File dbFile = new File(getAppDbFileName());
99-
FileInputStream fis = null;
100-
101-
try {
102-
fis = new FileInputStream(dbFile);
103-
} catch (FileNotFoundException e) {
104-
e.printStackTrace();
105-
}
106-
107-
return fis;
108-
}
109-
110-
private long readAppDbFileLength() {
111-
File dbFile = new File(getAppDbFileName());
112-
113-
if (dbFile.exists()) return dbFile.length();
114-
else return 0;
115-
}
116-
117-
@NonNull
118-
private String getAppDbFileName() {
49+
@NonNull private String getAppDbFileName() {
11950
return filesDir + "/databases/database";
12051
}
12152

122-
@NonNull
123-
private String getRestoreFileName() {
124-
return getAppDbFileName() + ".restore";
125-
}
126-
12753
public interface OnBackupListener {
12854
String SUCCESS = "success";
12955
String ERROR_AUTHENTICATION = "error_authentication";

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/backup/tasks/DropboxBackupAsyncTask.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,31 @@
77
import com.dropbox.core.DbxException;
88
import com.dropbox.core.v2.DbxClientV2;
99
import com.dropbox.core.v2.files.FileMetadata;
10+
import java.io.File;
1011
import java.io.FileInputStream;
12+
import java.io.FileNotFoundException;
1113
import java.io.IOException;
1214

1315
public class DropboxBackupAsyncTask extends AsyncTask<Void, String, String> {
16+
1417
private DbxClientV2 dbClient;
18+
private String appDbFileName;
1519
private String fileName;
16-
private FileInputStream fileInputStream;
17-
private long fileLength;
1820

19-
@Nullable
20-
private BackupController.OnBackupListener listener;
21+
@Nullable private BackupController.OnBackupListener listener;
2122

22-
public DropboxBackupAsyncTask(@NonNull DbxClientV2 dbClient, String fileName,
23-
FileInputStream fileInputStream, long fileLength,
23+
public DropboxBackupAsyncTask(@NonNull DbxClientV2 dbClient, String fileName, String appDbFileName,
2424
@Nullable BackupController.OnBackupListener listener) {
2525
this.dbClient = dbClient;
2626
this.fileName = fileName;
27-
this.fileInputStream = fileInputStream;
28-
this.fileLength = fileLength;
27+
this.appDbFileName = appDbFileName;
2928
this.listener = listener;
3029
}
3130

32-
@Override
33-
protected String doInBackground(Void... params) {
31+
@Override protected String doInBackground(Void... params) {
32+
FileInputStream fileInputStream = readAppDb();
33+
if (fileInputStream == null) return null;
34+
3435
FileMetadata info = null;
3536

3637
try {
@@ -41,16 +42,34 @@ protected String doInBackground(Void... params) {
4142
e.printStackTrace();
4243
}
4344

44-
if (info == null) return null;
45-
else return BackupController.OnBackupListener.SUCCESS;
45+
if (info == null) {
46+
return null;
47+
} else {
48+
return BackupController.OnBackupListener.SUCCESS;
49+
}
4650
}
4751

48-
@Override
49-
protected void onPostExecute(String result) {
52+
@Override protected void onPostExecute(String result) {
5053
super.onPostExecute(result);
5154
if (listener == null) return;
5255

53-
if (BackupController.OnBackupListener.SUCCESS.equals(result)) listener.onBackupSuccess();
54-
else listener.onBackupFailure(result);
56+
if (BackupController.OnBackupListener.SUCCESS.equals(result)) {
57+
listener.onBackupSuccess();
58+
} else {
59+
listener.onBackupFailure(result);
60+
}
61+
}
62+
63+
@Nullable private FileInputStream readAppDb() {
64+
File dbFile = new File(appDbFileName);
65+
FileInputStream fis = null;
66+
67+
try {
68+
fis = new FileInputStream(dbFile);
69+
} catch (FileNotFoundException e) {
70+
e.printStackTrace();
71+
}
72+
73+
return fis;
5574
}
5675
}
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,69 @@
11
package com.blogspot.e_kanivets.moneytracker.controller.backup.tasks;
22

33
import android.os.AsyncTask;
4+
import android.support.annotation.NonNull;
45
import android.support.annotation.Nullable;
56
import com.blogspot.e_kanivets.moneytracker.controller.backup.BackupController;
67
import com.dropbox.core.DbxException;
78
import com.dropbox.core.v2.DbxClientV2;
89
import com.dropbox.core.v2.files.FileMetadata;
10+
import java.io.File;
11+
import java.io.FileNotFoundException;
912
import java.io.FileOutputStream;
1013
import java.io.IOException;
1114

1215
public class DropboxRestoreBackupAsyncTask extends AsyncTask<Void, String, String> {
16+
1317
private DbxClientV2 dbClient;
18+
private String appDbFileName;
1419
private String backupName;
15-
private FileOutputStream outputStream;
1620

1721
@Nullable private BackupController.OnRestoreBackupListener listener;
1822

19-
public DropboxRestoreBackupAsyncTask(DbxClientV2 dbClient, String backupName, FileOutputStream outputStream,
23+
public DropboxRestoreBackupAsyncTask(DbxClientV2 dbClient, String appDbFileName, String backupName,
2024
@Nullable BackupController.OnRestoreBackupListener listener) {
2125
this.dbClient = dbClient;
26+
this.appDbFileName = appDbFileName;
2227
this.backupName = backupName;
23-
this.outputStream = outputStream;
2428
this.listener = listener;
2529
}
2630

2731
@Override protected String doInBackground(Void... params) {
28-
FileMetadata info = null;
32+
final File file = new File(getRestoreFileName());
33+
FileOutputStream outputStream = null;
2934
try {
30-
info = dbClient.files().download("/" + backupName).download(outputStream);
31-
} catch (DbxException | IOException e) {
35+
outputStream = new FileOutputStream(file);
36+
} catch (FileNotFoundException e) {
3237
e.printStackTrace();
3338
}
3439

35-
if (info == null) {
40+
if (outputStream == null) {
3641
return null;
3742
} else {
38-
return BackupController.OnBackupListener.SUCCESS;
43+
FileMetadata info = null;
44+
try {
45+
info = dbClient.files().download("/" + backupName).download(outputStream);
46+
} catch (DbxException | IOException e) {
47+
e.printStackTrace();
48+
}
49+
50+
if (info == null) {
51+
return null;
52+
} else {
53+
try {
54+
outputStream.close();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
return null;
58+
}
59+
60+
if (file.exists() && file.length() != 0) {
61+
boolean renamed = file.renameTo(new File(appDbFileName));
62+
return renamed ? BackupController.OnBackupListener.SUCCESS : null;
63+
} else {
64+
return null;
65+
}
66+
}
3967
}
4068
}
4169

@@ -49,4 +77,8 @@ public DropboxRestoreBackupAsyncTask(DbxClientV2 dbClient, String backupName, Fi
4977
listener.onRestoreFailure(result);
5078
}
5179
}
80+
81+
@NonNull private String getRestoreFileName() {
82+
return appDbFileName + ".restore";
83+
}
5284
}

0 commit comments

Comments
 (0)