X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fbackup%2FConfigReader.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fbackup%2FConfigReader.java;h=7959da518efc0c48f057f652392f625366c75ce7;hp=0000000000000000000000000000000000000000;hb=41d543229f3231469247e10ec6c197920c0e8bc4;hpb=833544eb24cb630dc1ce221e4aa3dedb3f6341e3 diff --git a/app/src/main/java/net/ktnx/mobileledger/backup/ConfigReader.java b/app/src/main/java/net/ktnx/mobileledger/backup/ConfigReader.java new file mode 100644 index 00000000..7959da51 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/backup/ConfigReader.java @@ -0,0 +1,76 @@ +/* + * 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.content.Context; +import android.net.Uri; + +import net.ktnx.mobileledger.dao.ProfileDAO; +import net.ktnx.mobileledger.db.DB; +import net.ktnx.mobileledger.db.Profile; +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.utils.Misc; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class ConfigReader extends ConfigIO { + private final OnDoneListener onDoneListener; + private RawConfigReader r; + public ConfigReader(Context context, Uri uri, OnErrorListener onErrorListener, + OnDoneListener onDoneListener) throws FileNotFoundException { + super(context, uri, onErrorListener); + + this.onDoneListener = onDoneListener; + } + @Override + protected String getStreamMode() { + return "r"; + } + @Override + protected void initStream() { + RawConfigReader r = new RawConfigReader(new FileInputStream(pfd.getFileDescriptor())); + } + @Override + protected void processStream() throws IOException { + r.readConfig(); + r.restoreAll(); + String currentProfile = r.getCurrentProfile(); + + if (Data.getProfile() == null) { + Profile p = null; + final ProfileDAO dao = DB.get() + .getProfileDAO(); + if (currentProfile != null) + p = dao.getByUuidSync(currentProfile); + + if (p == null) + dao.getAnySync(); + + if (p != null) + Data.postCurrentProfile(p); + } + + if (onDoneListener != null) + Misc.onMainThread(onDoneListener::done); + } + abstract static public class OnDoneListener { + public abstract void done(); + } +}