X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fbackup%2FMobileLedgerBackupAgent.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fbackup%2FMobileLedgerBackupAgent.java;h=ea09e51417d68735d281c30fa6213303f5425183;hp=0000000000000000000000000000000000000000;hb=41d543229f3231469247e10ec6c197920c0e8bc4;hpb=833544eb24cb630dc1ce221e4aa3dedb3f6341e3 diff --git a/app/src/main/java/net/ktnx/mobileledger/backup/MobileLedgerBackupAgent.java b/app/src/main/java/net/ktnx/mobileledger/backup/MobileLedgerBackupAgent.java new file mode 100644 index 00000000..ea09e514 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/backup/MobileLedgerBackupAgent.java @@ -0,0 +1,74 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.backup; + +import android.app.backup.BackupAgentHelper; +import android.app.backup.BackupDataInput; +import android.app.backup.BackupDataOutput; +import android.os.ParcelFileDescriptor; + +import net.ktnx.mobileledger.db.DB; +import net.ktnx.mobileledger.utils.Logger; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +class MobileLedgerBackupAgent extends BackupAgentHelper { + private static final int READ_BUF_LEN = 10; + public static String SETTINGS_KEY = "settings"; + @Override + public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, + ParcelFileDescriptor newState) throws IOException { + super.onBackup(oldState, data, newState); + backupSettings(data); + newState.close(); + } + private void backupSettings(BackupDataOutput data) throws IOException { + Logger.debug ("backup", "Starting cloud backup"); + ByteArrayOutputStream output = new ByteArrayOutputStream(4096); + RawConfigWriter saver = new RawConfigWriter(output); + saver.writeConfig(); + byte[] bytes = output.toByteArray(); + data.writeEntityHeader(SETTINGS_KEY, bytes.length); + data.writeEntityData(bytes, bytes.length); + Logger.debug("backup", "Done writing backup data"); + } + @Override + public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) + throws IOException { + Logger.debug("restore", "Starting cloud restore"); + if (data.readNextHeader()) { + String key = data.getKey(); + if (key.equals(SETTINGS_KEY)) { + restoreSettings(data); + } + } + } + private void restoreSettings(BackupDataInput data) throws IOException { + byte[] bytes = new byte[data.getDataSize()]; + data.readEntityData(bytes, 0, bytes.length); + RawConfigReader reader = new RawConfigReader(new ByteArrayInputStream(bytes)); + reader.readConfig(); + Logger.debug("restore", "Successfully read restore data. Wiping database"); + DB.get().deleteAllSync(); + Logger.debug("restore", "Database wiped"); + reader.restoreAll(); + Logger.debug("restore", "All data restored from the cloud"); + } +}