]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/backup/MobileLedgerBackupAgent.java
provide cloud backup functionality
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / backup / MobileLedgerBackupAgent.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.backup;
19
20 import android.app.backup.BackupAgentHelper;
21 import android.app.backup.BackupDataInput;
22 import android.app.backup.BackupDataOutput;
23 import android.os.ParcelFileDescriptor;
24
25 import net.ktnx.mobileledger.db.DB;
26 import net.ktnx.mobileledger.utils.Logger;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31
32 class MobileLedgerBackupAgent extends BackupAgentHelper {
33     private static final int READ_BUF_LEN = 10;
34     public static String SETTINGS_KEY = "settings";
35     @Override
36     public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
37                          ParcelFileDescriptor newState) throws IOException {
38         super.onBackup(oldState, data, newState);
39         backupSettings(data);
40         newState.close();
41     }
42     private void backupSettings(BackupDataOutput data) throws IOException {
43         Logger.debug ("backup", "Starting cloud backup");
44         ByteArrayOutputStream output = new ByteArrayOutputStream(4096);
45         RawConfigWriter saver = new RawConfigWriter(output);
46         saver.writeConfig();
47         byte[] bytes = output.toByteArray();
48         data.writeEntityHeader(SETTINGS_KEY, bytes.length);
49         data.writeEntityData(bytes, bytes.length);
50         Logger.debug("backup", "Done writing backup data");
51     }
52     @Override
53     public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
54             throws IOException {
55         Logger.debug("restore", "Starting cloud restore");
56         if (data.readNextHeader()) {
57             String key = data.getKey();
58             if (key.equals(SETTINGS_KEY)) {
59                 restoreSettings(data);
60             }
61         }
62     }
63     private void restoreSettings(BackupDataInput data) throws IOException {
64         byte[] bytes = new byte[data.getDataSize()];
65         data.readEntityData(bytes, 0, bytes.length);
66         RawConfigReader reader = new RawConfigReader(new ByteArrayInputStream(bytes));
67         reader.readConfig();
68         Logger.debug("restore", "Successfully read restore data. Wiping database");
69         DB.get().deleteAllSync();
70         Logger.debug("restore", "Database wiped");
71         reader.restoreAll();
72         Logger.debug("restore", "All data restored from the cloud");
73     }
74 }