]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/backup/ConfigReader.java
provide cloud backup functionality
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / backup / ConfigReader.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.content.Context;
21 import android.net.Uri;
22
23 import net.ktnx.mobileledger.dao.ProfileDAO;
24 import net.ktnx.mobileledger.db.DB;
25 import net.ktnx.mobileledger.db.Profile;
26 import net.ktnx.mobileledger.model.Data;
27 import net.ktnx.mobileledger.utils.Misc;
28
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32
33 public class ConfigReader extends ConfigIO {
34     private final OnDoneListener onDoneListener;
35     private RawConfigReader r;
36     public ConfigReader(Context context, Uri uri, OnErrorListener onErrorListener,
37                         OnDoneListener onDoneListener) throws FileNotFoundException {
38         super(context, uri, onErrorListener);
39
40         this.onDoneListener = onDoneListener;
41     }
42     @Override
43     protected String getStreamMode() {
44         return "r";
45     }
46     @Override
47     protected void initStream() {
48         RawConfigReader r = new RawConfigReader(new FileInputStream(pfd.getFileDescriptor()));
49     }
50     @Override
51     protected void processStream() throws IOException {
52         r.readConfig();
53         r.restoreAll();
54         String currentProfile = r.getCurrentProfile();
55
56         if (Data.getProfile() == null) {
57             Profile p = null;
58             final ProfileDAO dao = DB.get()
59                                      .getProfileDAO();
60             if (currentProfile != null)
61                 p = dao.getByUuidSync(currentProfile);
62
63             if (p == null)
64                 dao.getAnySync();
65
66             if (p != null)
67                 Data.postCurrentProfile(p);
68         }
69
70         if (onDoneListener != null)
71             Misc.onMainThread(onDoneListener::done);
72     }
73     abstract static public class OnDoneListener {
74         public abstract void done();
75     }
76 }