Skip to content

Commit 635f79e

Browse files
committed
ADD exitCodes to onCompleteListener
1 parent 5f3ef8a commit 635f79e

File tree

5 files changed

+184
-48
lines changed

5 files changed

+184
-48
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Usage
7575
-----------
7676

7777
* [Properties](#Properties)
78+
* [Exit Codes](#Exit-Codes)
7879
* [Example Kotlin](#Example-Kotlin)
7980
* [Example Java](#Example-Java)
8081

@@ -215,17 +216,39 @@ The following options are optional and the default options
215216

216217
* Restart your Application. Can be implemented in the onCompleteListener, when "success == true"
217218

218-
**Attention**\
219-
it does not always work reliably!\
220-
But you can use other methods.\
221-
Important is that all activities / fragments that are still open must be closed and reopened\
222-
Because the Database instance is a new one, and the old activities / fragments are trying to work with the old instance
223-
219+
**Attention**\
220+
it does not always work reliably!\
221+
But you can use other methods.\
222+
Important is that all activities / fragments that are still open must be closed and reopened\
223+
Because the Database instance is a new one, and the old activities / fragments are trying to
224+
work with the old instance
224225

225226
```kotlin
226227
.restartApp(Intent(this@MainActivity, MainActivity::class.java))
227228
```
228229

230+
### Exit Codes
231+
232+
Here are all exit codes for the onCompleteListener.
233+
They can be calles using ```OnCompleteListener.$NAME$```
234+
235+
| Exit Code | Name | Description |
236+
| --------- | :--------------------------------------------------- | ------------ |
237+
| 0 | ```EXIT_CODE_SUCCESS``` | No error, action successful |
238+
| 1 | ```EXIT_CODE_ERROR``` | Other Error |
239+
| 2 | ```EXIT_CODE_ERROR_BACKUP_FILE_CHOOSER``` | Error while choosing backup to restore. Maybe no file selected |
240+
| 3 | ```EXIT_CODE_ERROR_BACKUP_FILE_CREATOR``` | Error while choosing backup file to create. Maybe no file selected |
241+
| 4 | ```EXIT_CODE_ERROR_BACKUP_LOCATION_FILE_MISSING``` | [BACKUP_FILE_LOCATION_CUSTOM_FILE] is set but [RoomBackup.backupLocationCustomFile] is not set |
242+
| 5 | ```EXIT_CODE_ERROR_BACKUP_LOCATION_MISSING``` | [RoomBackup.backupLocation] is not set |
243+
| 6 | ```EXIT_CODE_ERROR_BY_USER_CANCELED``` | Restore dialog for internal/external storage was canceled by user |
244+
| 7 | ```EXIT_CODE_ERROR_DECRYPTION_ERROR``` | Cannot decrypt provided backup file |
245+
| 8 | ```EXIT_CODE_ERROR_ENCRYPTION_ERROR``` | Cannot encrypt database backup |
246+
| 9 | ```EXIT_CODE_ERROR_RESTORE_BACKUP_IS_ENCRYPTED``` | You tried to restore a encrypted backup but [RoomBackup.backupIsEncrypted] is set to false |
247+
| 10 | ```EXIT_CODE_ERROR_RESTORE_NO_BACKUPS_AVAILABLE``` | No backups to restore are available in internal/external sotrage |
248+
| 11 | ```EXIT_CODE_ERROR_ROOM_DATABASE_MISSING``` | No room database to backup is provided |
249+
| 12 | ```EXIT_CODE_ERROR_STORAGE_PERMISSONS_NOT_GRANTED``` | Storage permissions not granted for custom dialog |
250+
| 13 | ```EXIT_CODE_ERROR_WRONG_DECRYPTION_PASSWORD``` | Cannot decrypt provided backup file because the password is incorrect |
251+
229252
### Example Kotlin
230253

231254
* ##### Backup

app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/MainActivity.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import androidx.lifecycle.ViewModelProvider
1919
import androidx.recyclerview.widget.ItemTouchHelper
2020
import androidx.recyclerview.widget.LinearLayoutManager
2121
import androidx.recyclerview.widget.RecyclerView
22+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
23+
import com.google.android.material.floatingactionbutton.FloatingActionButton
24+
import com.google.android.material.snackbar.Snackbar
2225
import de.raphaelebner.roomdatabasebackup.core.RoomBackup
2326
import de.raphaelebner.roomdatabasebackup.sample.database.main.FruitDatabase
2427
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.Fruit
2528
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitListAdapter
2629
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitViewModel
27-
import com.google.android.material.dialog.MaterialAlertDialogBuilder
28-
import com.google.android.material.floatingactionbutton.FloatingActionButton
29-
import com.google.android.material.snackbar.Snackbar
3030
import java.io.File
3131

3232

@@ -189,9 +189,13 @@ class MainActivity : AppCompatActivity(), FruitListAdapter.OnItemClickListener {
189189
//maxFileCount: else 1000 because i cannot surround it with if condition
190190
.maxFileCount(if (useMaxFileCount) 5 else 1000)
191191
.apply {
192-
onCompleteListener { success, message ->
193-
Log.d(TAG, "success: $success, message: $message")
194-
Toast.makeText(this@MainActivity, "success: $success, message: $message", Toast.LENGTH_LONG).show()
192+
onCompleteListener { success, message, exitCode ->
193+
Log.d(TAG, "success: $success, message: $message, exitCode: $exitCode")
194+
Toast.makeText(
195+
this@MainActivity,
196+
"success: $success, message: $message, exitCode: $exitCode",
197+
Toast.LENGTH_LONG
198+
).show()
195199
if (success) restartApp(Intent(this@MainActivity, MainActivity::class.java))
196200
}
197201
}
@@ -208,9 +212,13 @@ class MainActivity : AppCompatActivity(), FruitListAdapter.OnItemClickListener {
208212
.backupIsEncrypted(encryptBackup)
209213
.customEncryptPassword(SECRET_PASSWORD)
210214
.apply {
211-
onCompleteListener { success, message ->
212-
Log.d(TAG, "success: $success, message: $message")
213-
Toast.makeText(this@MainActivity, "success: $success, message: $message", Toast.LENGTH_LONG).show()
215+
onCompleteListener { success, message, exitCode ->
216+
Log.d(TAG, "success: $success, message: $message, exitCode: $exitCode")
217+
Toast.makeText(
218+
this@MainActivity,
219+
"success: $success, message: $message, exitCode: $exitCode",
220+
Toast.LENGTH_LONG
221+
).show()
214222
if (success) restartApp(Intent(this@MainActivity, MainActivity::class.java))
215223
}
216224

app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/MainActivityJava.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import androidx.recyclerview.widget.LinearLayoutManager;
2020
import androidx.recyclerview.widget.RecyclerView;
2121

22-
import de.raphaelebner.roomdatabasebackup.core.RoomBackup;
23-
import de.raphaelebner.roomdatabasebackup.sample.database.main.FruitDatabase;
24-
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.Fruit;
25-
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitListAdapter;
26-
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitViewModel;
2722
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
2823
import com.google.android.material.floatingactionbutton.FloatingActionButton;
2924

3025
import org.jetbrains.annotations.NotNull;
3126

3227
import java.io.File;
3328

29+
import de.raphaelebner.roomdatabasebackup.core.RoomBackup;
30+
import de.raphaelebner.roomdatabasebackup.sample.database.main.FruitDatabase;
31+
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.Fruit;
32+
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitListAdapter;
33+
import de.raphaelebner.roomdatabasebackup.sample.database.table.fruit.FruitViewModel;
34+
3435
/**
3536
* MIT License
3637
* <p>
@@ -213,9 +214,10 @@ protected void onCreate(Bundle savedInstanceState) {
213214
roomBackup.backupIsEncrypted(encryptBackup);
214215
roomBackup.customEncryptPassword(MainActivity.SECRET_PASSWORD);
215216
if (useMaxFileCount) roomBackup.maxFileCount(5);
216-
roomBackup.onCompleteListener((success, message) -> {
217+
roomBackup.onCompleteListener((success, message, exitCode) -> {
217218
Log.d(TAG, "oncomplete: " + success + ", message: " + message);
218-
if (success) roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
219+
if (success)
220+
roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
219221
});
220222
roomBackup.backup();
221223

@@ -228,9 +230,10 @@ protected void onCreate(Bundle savedInstanceState) {
228230
roomBackup.enableLogDebug(enableLog);
229231
roomBackup.backupIsEncrypted(encryptBackup);
230232
roomBackup.customEncryptPassword(MainActivity.SECRET_PASSWORD);
231-
roomBackup.onCompleteListener((success, message) -> {
233+
roomBackup.onCompleteListener((success, message, exitCode) -> {
232234
Log.d(TAG, "oncomplete: " + success + ", message: " + message);
233-
if (success) roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
235+
if (success)
236+
roomBackup.restartApp(new Intent(getApplicationContext(), MainActivityJava.class));
234237
});
235238
roomBackup.restore();
236239

core/src/main/java/de/raphaelebner/roomdatabasebackup/core/OnCompleteListener.kt

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.raphaelebner.roomdatabasebackup.core
22

3+
import de.raphaelebner.roomdatabasebackup.core.RoomBackup.Companion.BACKUP_FILE_LOCATION_CUSTOM_FILE
4+
35
/**
46
* MIT License
57
*
@@ -24,5 +26,51 @@ package de.raphaelebner.roomdatabasebackup.core
2426
* SOFTWARE.
2527
*/
2628
interface OnCompleteListener {
27-
fun onComplete(success: Boolean, message: String)
28-
}
29+
fun onComplete(success: Boolean, message: String, exitCode: Int)
30+
31+
companion object {
32+
33+
/** Other Error */
34+
const val EXIT_CODE_ERROR = 1
35+
36+
/** Error while choosing backup to restore. Maybe no file selected */
37+
const val EXIT_CODE_ERROR_BACKUP_FILE_CHOOSER = 2
38+
39+
/** Error while choosing backup file to create. Maybe no file selected */
40+
const val EXIT_CODE_ERROR_BACKUP_FILE_CREATOR = 3
41+
42+
/** [BACKUP_FILE_LOCATION_CUSTOM_FILE] is set but [RoomBackup.backupLocationCustomFile] is not set */
43+
const val EXIT_CODE_ERROR_BACKUP_LOCATION_FILE_MISSING = 4
44+
45+
/** [RoomBackup.backupLocation] is not set */
46+
const val EXIT_CODE_ERROR_BACKUP_LOCATION_MISSING = 5
47+
48+
/** Restore dialog for internal/external storage was canceled by user */
49+
const val EXIT_CODE_ERROR_BY_USER_CANCELED = 6
50+
51+
/** Cannot decrypt provided backup file */
52+
const val EXIT_CODE_ERROR_DECRYPTION_ERROR = 7
53+
54+
/** Cannot encrypt database backup */
55+
const val EXIT_CODE_ERROR_ENCRYPTION_ERROR = 8
56+
57+
/** You tried to restore a encrypted backup but [RoomBackup.backupIsEncrypted] is set to false */
58+
const val EXIT_CODE_ERROR_RESTORE_BACKUP_IS_ENCRYPTED = 9
59+
60+
/** No backups to restore are available in internal/external sotrage */
61+
const val EXIT_CODE_ERROR_RESTORE_NO_BACKUPS_AVAILABLE = 10
62+
63+
/** No room database to backup is provided */
64+
const val EXIT_CODE_ERROR_ROOM_DATABASE_MISSING = 11
65+
66+
/** Storage permissions not granted for custom dialog */
67+
const val EXIT_CODE_ERROR_STORAGE_PERMISSONS_NOT_GRANTED = 12
68+
69+
/** Cannot decrypt provided backup file because the password is incorrect */
70+
const val EXIT_CODE_ERROR_WRONG_DECRYPTION_PASSWORD = 13
71+
72+
/** No error, action successful */
73+
const val EXIT_CODE_SUCCESS = 0
74+
}
75+
76+
}

0 commit comments

Comments
 (0)