]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/backup/ConfigIO.java
ec7a94999ae90e3a8120d28008e9dfce7f3d2605
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / backup / ConfigIO.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 import android.os.ParcelFileDescriptor;
23 import android.util.Log;
24
25 import net.ktnx.mobileledger.utils.Misc;
26
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29
30 abstract class ConfigIO extends Thread {
31     protected final OnErrorListener onErrorListener;
32     protected ParcelFileDescriptor pfd;
33     ConfigIO(Context context, Uri uri, OnErrorListener onErrorListener)
34             throws FileNotFoundException {
35         this.onErrorListener = onErrorListener;
36         pfd = context.getContentResolver()
37                      .openFileDescriptor(uri, getStreamMode());
38
39         initStream();
40     }
41     abstract protected String getStreamMode();
42
43     abstract protected void initStream();
44
45     abstract protected void processStream() throws IOException;
46     @Override
47     public void run() {
48         try {
49             processStream();
50         }
51         catch (Exception e) {
52             Log.e("cfg-json", "Error processing settings as JSON", e);
53             if (onErrorListener != null)
54                 Misc.onMainThread(() -> onErrorListener.error(e));
55         }
56         finally {
57             try {
58                 pfd.close();
59             }
60             catch (Exception e) {
61                 Log.e("cfg-json", "Error closing file descriptor", e);
62             }
63         }
64     }
65     protected static class Keys {
66         static final String ACCOUNTS = "accounts";
67         static final String AMOUNT = "amount";
68         static final String AMOUNT_GROUP = "amountGroup";
69         static final String API_VER = "apiVersion";
70         static final String AUTH_PASS = "authPass";
71         static final String AUTH_USER = "authUser";
72         static final String CAN_POST = "permitPosting";
73         static final String COLOUR = "colour";
74         static final String COMMENT = "comment";
75         static final String COMMENT_GROUP = "commentMatchGroup";
76         static final String COMMODITIES = "commodities";
77         static final String CURRENCY = "commodity";
78         static final String CURRENCY_GROUP = "commodityGroup";
79         static final String CURRENT_PROFILE = "currentProfile";
80         static final String DATE_DAY = "dateDay";
81         static final String DATE_DAY_GROUP = "dateDayMatchGroup";
82         static final String DATE_MONTH = "dateMonth";
83         static final String DATE_MONTH_GROUP = "dateMonthMatchGroup";
84         static final String DATE_YEAR = "dateYear";
85         static final String DATE_YEAR_GROUP = "dateYearMatchGroup";
86         static final String DEFAULT_COMMODITY = "defaultCommodity";
87         static final String FUTURE_DATES = "futureDates";
88         static final String HAS_GAP = "hasGap";
89         static final String IS_FALLBACK = "isFallback";
90         static final String NAME = "name";
91         static final String NAME_GROUP = "nameMatchGroup";
92         static final String NEGATE_AMOUNT = "negateAmount";
93         static final String POSITION = "position";
94         static final String PREF_ACCOUNT = "preferredAccountsFilter";
95         static final String PROFILES = "profiles";
96         static final String REGEX = "regex";
97         static final String SHOW_COMMENTS = "showCommentsByDefault";
98         static final String SHOW_COMMODITY = "showCommodityByDefault";
99         static final String TEMPLATES = "templates";
100         static final String TEST_TEXT = "testText";
101         static final String TRANSACTION = "description";
102         static final String TRANSACTION_GROUP = "descriptionMatchGroup";
103         static final String URL = "url";
104         static final String USE_AUTH = "useAuth";
105         static final String UUID = "uuid";
106     }
107
108     abstract static public class OnErrorListener {
109         public abstract void error(Exception e);
110     }
111 }