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.
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.
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/>.
18 package net.ktnx.mobileledger.backup;
20 import android.app.backup.BackupAgentHelper;
21 import android.app.backup.BackupDataInput;
22 import android.app.backup.BackupDataOutput;
23 import android.os.ParcelFileDescriptor;
25 import net.ktnx.mobileledger.db.DB;
26 import net.ktnx.mobileledger.utils.Logger;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
32 class MobileLedgerBackupAgent extends BackupAgentHelper {
33 private static final int READ_BUF_LEN = 10;
34 public static String SETTINGS_KEY = "settings";
36 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
37 ParcelFileDescriptor newState) throws IOException {
38 super.onBackup(oldState, data, newState);
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);
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");
53 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
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);
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));
68 Logger.debug("restore", "Successfully read restore data. Wiping database");
69 DB.get().deleteAllSync();
70 Logger.debug("restore", "Database wiped");
72 Logger.debug("restore", "All data restored from the cloud");