-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathGnuCashApplication.java
More file actions
382 lines (322 loc) · 14.4 KB
/
GnuCashApplication.java
File metadata and controls
382 lines (322 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright (c) 2013 - 2014 Ngewi Fet <ngewif@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gnucash.android.app;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.multidex.MultiDexApplication;
import android.support.v7.preference.PreferenceManager;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.core.CrashlyticsCore;
import com.uservoice.uservoicesdk.Config;
import com.uservoice.uservoicesdk.UserVoice;
import org.gnucash.android.BuildConfig;
import org.gnucash.android.R;
import org.gnucash.android.db.BookDbHelper;
import org.gnucash.android.db.DatabaseHelper;
import org.gnucash.android.db.adapter.AccountsDbAdapter;
import org.gnucash.android.db.adapter.BooksDbAdapter;
import org.gnucash.android.db.adapter.BudgetAmountsDbAdapter;
import org.gnucash.android.db.adapter.BudgetsDbAdapter;
import org.gnucash.android.db.adapter.CommoditiesDbAdapter;
import org.gnucash.android.db.adapter.PricesDbAdapter;
import org.gnucash.android.db.adapter.RecurrenceDbAdapter;
import org.gnucash.android.db.adapter.ScheduledActionDbAdapter;
import org.gnucash.android.db.adapter.SplitsDbAdapter;
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.model.Commodity;
import org.gnucash.android.model.Money;
import org.gnucash.android.receivers.PeriodicJobReceiver;
import org.gnucash.android.service.ScheduledActionService;
import org.gnucash.android.ui.settings.PreferenceActivity;
import java.util.Currency;
import java.util.Locale;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import io.fabric.sdk.android.Fabric;
/**
* An {@link Application} subclass for retrieving static context
* @author Ngewi Fet <ngewif@gmail.com>
*
*/
public class GnuCashApplication extends MultiDexApplication {
/**
* Authority (domain) for the file provider. Also used in the app manifest
*/
public static final String FILE_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider";
/**
* Lifetime of passcode session
*/
public static final long SESSION_TIMEOUT = 5 * 1000;
/**
* Init time of passcode session
*/
public static long PASSCODE_SESSION_INIT_TIME = 0L;
private static Context context;
private static AccountsDbAdapter mAccountsDbAdapter;
private static TransactionsDbAdapter mTransactionsDbAdapter;
private static SplitsDbAdapter mSplitsDbAdapter;
private static ScheduledActionDbAdapter mScheduledActionDbAdapter;
private static CommoditiesDbAdapter mCommoditiesDbAdapter;
private static PricesDbAdapter mPricesDbAdapter;
private static BudgetsDbAdapter mBudgetsDbAdapter;
private static BudgetAmountsDbAdapter mBudgetAmountsDbAdapter;
private static RecurrenceDbAdapter mRecurrenceDbAdapter;
private static BooksDbAdapter mBooksDbAdapter;
private static volatile DatabaseHelper mDbHelper;
// lock for accessing the current database
public static final ReadWriteLock dbLock = new ReentrantReadWriteLock();
/**
* Returns darker version of specified <code>color</code>.
* Use for theming the status bar color when setting the color of the actionBar
*/
public static int darken(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f; // value component
return Color.HSVToColor(hsv);
}
@Override
public void onCreate(){
super.onCreate();
GnuCashApplication.context = getApplicationContext();
Fabric.with(this, new Crashlytics.Builder().core(
new CrashlyticsCore.Builder().disabled(!isCrashlyticsEnabled()).build())
.build());
setUpUserVoice();
BookDbHelper bookDbHelper = new BookDbHelper(getApplicationContext());
mBooksDbAdapter = new BooksDbAdapter(bookDbHelper.getWritableDatabase());
initializeDatabaseAdapters();
setDefaultCurrencyCode(getDefaultCurrencyCode());
StethoUtils.install(this);
}
/**
* Initialize database adapter singletons for use in the application
* This method should be called every time a new book is opened
*/
public static void initializeDatabaseAdapters() {
final Lock exclusiveLock = dbLock.writeLock();
exclusiveLock.lock();
if (mDbHelper != null){ //close if open
mDbHelper.getReadableDatabase().close();
}
try {
mDbHelper = new DatabaseHelper(getAppContext(),
mBooksDbAdapter.getActiveBookUID());
} catch (BooksDbAdapter.NoActiveBookFoundException e) {
mBooksDbAdapter.fixBooksDatabase();
mDbHelper = new DatabaseHelper(getAppContext(),
mBooksDbAdapter.getActiveBookUID());
}
SQLiteDatabase mainDb;
try {
mainDb = mDbHelper.getWritableDatabase();
} catch (SQLException e) {
Crashlytics.logException(e);
Log.e("GnuCashApplication", "Error getting database: " + e.getMessage());
mainDb = mDbHelper.getReadableDatabase();
}
mSplitsDbAdapter = new SplitsDbAdapter(mainDb);
mTransactionsDbAdapter = new TransactionsDbAdapter(mainDb, mSplitsDbAdapter);
mAccountsDbAdapter = new AccountsDbAdapter(mainDb, mTransactionsDbAdapter);
mRecurrenceDbAdapter = new RecurrenceDbAdapter(mainDb);
mScheduledActionDbAdapter = new ScheduledActionDbAdapter(mainDb, mRecurrenceDbAdapter);
mPricesDbAdapter = new PricesDbAdapter(mainDb);
mCommoditiesDbAdapter = new CommoditiesDbAdapter(mainDb);
mBudgetAmountsDbAdapter = new BudgetAmountsDbAdapter(mainDb);
mBudgetsDbAdapter = new BudgetsDbAdapter(mainDb, mBudgetAmountsDbAdapter, mRecurrenceDbAdapter);
exclusiveLock.unlock();
}
public static AccountsDbAdapter getAccountsDbAdapter() {
return mAccountsDbAdapter;
}
public static TransactionsDbAdapter getTransactionDbAdapter() {
return mTransactionsDbAdapter;
}
public static SplitsDbAdapter getSplitsDbAdapter() {
return mSplitsDbAdapter;
}
public static ScheduledActionDbAdapter getScheduledEventDbAdapter(){
return mScheduledActionDbAdapter;
}
public static CommoditiesDbAdapter getCommoditiesDbAdapter(){
return mCommoditiesDbAdapter;
}
public static PricesDbAdapter getPricesDbAdapter(){
return mPricesDbAdapter;
}
public static BudgetsDbAdapter getBudgetDbAdapter() {
return mBudgetsDbAdapter;
}
public static RecurrenceDbAdapter getRecurrenceDbAdapter() {
return mRecurrenceDbAdapter;
}
public static BudgetAmountsDbAdapter getBudgetAmountsDbAdapter(){
return mBudgetAmountsDbAdapter;
}
public static BooksDbAdapter getBooksDbAdapter(){
return mBooksDbAdapter;
}
/**
* Returns the currently active database in the application
* @return Currently active {@link SQLiteDatabase}
*/
public static SQLiteDatabase getActiveDb(){
return mDbHelper.getWritableDatabase();
}
/**
* Returns the application context
* @return Application {@link Context} object
*/
public static Context getAppContext() {
return GnuCashApplication.context;
}
/**
* Checks if crashlytics is enabled
* @return {@code true} if crashlytics is enabled, {@code false} otherwise
*/
public static boolean isCrashlyticsEnabled(){
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(context.getString(R.string.key_enable_crashlytics), false);
}
/**
* Returns <code>true</code> if double entry is enabled in the app settings, <code>false</code> otherwise.
* If the value is not set, the default value can be specified in the parameters.
* @return <code>true</code> if double entry is enabled, <code>false</code> otherwise
*/
public static boolean isDoubleEntryEnabled(){
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
return sharedPrefs.getBoolean(context.getString(R.string.key_use_double_entry), true);
}
/**
* Returns <code>true</code> if setting is enabled to save opening balances after deleting transactions,
* <code>false</code> otherwise.
* @param defaultValue Default value to return if double entry is not explicitly set
* @return <code>true</code> if opening balances should be saved, <code>false</code> otherwise
*/
public static boolean shouldSaveOpeningBalances(boolean defaultValue){
SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences();
return sharedPrefs.getBoolean(context.getString(R.string.key_save_opening_balances), defaultValue);
}
/**
* Returns the default currency code for the application. <br/>
* What value is actually returned is determined in this order of priority:<ul>
* <li>User currency preference (manually set be user in the app)</li>
* <li>Default currency for the device locale</li>
* <li>United States Dollars</li>
* </ul>
*
* @return Default currency code string for the application
*/
public static String getDefaultCurrencyCode(){
Locale locale = getDefaultLocale();
String currencyCode = "USD"; //start with USD as the default
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
try { //there are some strange locales out there
currencyCode = Currency.getInstance(locale).getCurrencyCode();
} catch (Throwable e) {
Crashlytics.logException(e);
Log.e(context.getString(R.string.app_name), "" + e.getMessage());
} finally {
currencyCode = prefs.getString(context.getString(R.string.key_default_currency), currencyCode);
}
return currencyCode;
}
/**
* Sets the default currency for the application in all relevant places:
* <ul>
* <li>Shared preferences</li>
* <li>{@link Money#DEFAULT_CURRENCY_CODE}</li>
* <li>{@link Commodity#DEFAULT_COMMODITY}</li>
* </ul>
* @param currencyCode ISO 4217 currency code
* @see #getDefaultCurrencyCode()
*/
public static void setDefaultCurrencyCode(@NonNull String currencyCode){
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString(getAppContext().getString(R.string.key_default_currency), currencyCode)
.apply();
Money.DEFAULT_CURRENCY_CODE = currencyCode;
Commodity.DEFAULT_COMMODITY = mCommoditiesDbAdapter.getCommodity(currencyCode);
}
/**
* Returns the default locale which is used for currencies, while handling special cases for
* locales which are not supported for currency such as en_GB
* @return The default locale for this device
*/
public static Locale getDefaultLocale() {
Locale locale = Locale.getDefault();
//sometimes the locale en_UK is returned which causes a crash with Currency
if (locale.getCountry().equals("UK")) {
locale = new Locale(locale.getLanguage(), "GB");
}
//for unsupported locale es_LG
if (locale.getCountry().equals("LG")){
locale = new Locale(locale.getLanguage(), "ES");
}
if (locale.getCountry().equals("en")){
locale = Locale.US;
}
return locale;
}
/**
* Starts the service for scheduled events and schedules an alarm to call the service twice daily.
* <p>If the alarm already exists, this method does nothing. If not, the alarm will be created
* Hence, there is no harm in calling the method repeatedly</p>
* @param context Application context
*/
public static void startScheduledActionExecutionService(Context context){
Intent alarmIntent = new Intent(context, PeriodicJobReceiver.class);
alarmIntent.setAction(PeriodicJobReceiver.ACTION_SCHEDULED_ACTIONS);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, alarmIntent,
PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null) //if service is already scheduled, just return
return;
else
pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_HOUR, pendingIntent);
ScheduledActionService.enqueueWork(context);
}
/**
* Sets up UserVoice.
*
* <p>Allows users to contact with us and access help topics.</p>
*/
private void setUpUserVoice() {
// Set this up once when your application launches
Config config = new Config("gnucash.uservoice.com");
config.setTopicId(107400);
config.setForumId(320493);
config.putUserTrait("app_version_name", BuildConfig.VERSION_NAME);
config.putUserTrait("app_version_code", BuildConfig.VERSION_CODE);
config.putUserTrait("android_version", Build.VERSION.RELEASE);
// config.identifyUser("USER_ID", "User Name", "email@example.com");
UserVoice.init(config, this);
}
}