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.async;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.os.ParcelFileDescriptor;
23 import android.util.Log;
25 import net.ktnx.mobileledger.utils.Misc;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
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());
41 abstract protected String getStreamMode();
43 abstract protected void initStream();
45 abstract protected void processStream() throws IOException;
52 Log.e("cfg-json", "Error processing settings as JSON", e);
53 if (onErrorListener != null)
54 Misc.onMainThread(() -> onErrorListener.error(e));
61 Log.e("cfg-json", "Error closing file descriptor", e);
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";
108 abstract static public class OnErrorListener {
109 public abstract void error(Exception e);