<uses-permission android:name="android.permission.INTERNET" />
<application
- android:name=".MobileLedgerApplication"
+ android:name=".App"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@drawable/app_icon"
--- /dev/null
+/*
+ * Copyright © 2019 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 <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger;
+
+import android.app.Application;
+import android.content.SharedPreferences;
+import android.content.res.Configuration;
+import android.content.res.Resources;
+import android.database.sqlite.SQLiteDatabase;
+import android.preference.PreferenceManager;
+
+import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.utils.Globals;
+import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
+
+import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
+
+public class App extends Application {
+ public static App instance;
+ private MobileLedgerDatabase dbHelper;
+ @Override
+ public void onCreate() {
+ instance = this;
+ super.onCreate();
+ updateMonthNames();
+ SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
+ Data.optShowOnlyStarred.set(p.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
+ SharedPreferences.OnSharedPreferenceChangeListener handler =
+ (preference, value) -> Data.optShowOnlyStarred
+ .set(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
+ p.registerOnSharedPreferenceChangeListener(handler);
+ }
+ private void updateMonthNames() {
+ Resources rm = getResources();
+ Globals.monthNames = rm.getStringArray(R.array.month_names);
+ }
+ @Override
+ public void onTerminate() {
+ if (dbHelper != null) dbHelper.close();
+ super.onTerminate();
+ }
+ @Override
+ public void onConfigurationChanged(Configuration newConfig) {
+ super.onConfigurationChanged(newConfig);
+ updateMonthNames();
+ }
+ public static SQLiteDatabase getDatabase() {
+ if (instance == null) throw new RuntimeException("Application not created yet");
+
+ return instance.getDB();
+ }
+ public SQLiteDatabase getDB() {
+ if (dbHelper == null) initDb();
+
+ final SQLiteDatabase db = dbHelper.getWritableDatabase();
+ db.execSQL("pragma case_sensitive_like=ON;");
+
+ return db;
+ }
+ private synchronized void initDb() {
+ if (dbHelper != null) return;
+
+ dbHelper = new MobileLedgerDatabase(this);
+ }
+}
+++ /dev/null
-/*
- * Copyright © 2019 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 <https://www.gnu.org/licenses/>.
- */
-
-package net.ktnx.mobileledger;
-
-import android.app.Application;
-import android.content.SharedPreferences;
-import android.content.res.Configuration;
-import android.content.res.Resources;
-import android.preference.PreferenceManager;
-
-import net.ktnx.mobileledger.model.Data;
-import net.ktnx.mobileledger.utils.Globals;
-import net.ktnx.mobileledger.utils.MLDB;
-
-import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
-
-public class MobileLedgerApplication extends Application {
-
- @Override
- public void onCreate() {
- super.onCreate();
- updateMonthNames();
- MLDB.init(this);
- SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
- Data.optShowOnlyStarred.set(p.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
- SharedPreferences.OnSharedPreferenceChangeListener handler =
- (preference, value) -> Data.optShowOnlyStarred
- .set(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
- p.registerOnSharedPreferenceChangeListener(handler);
- }
- private void updateMonthNames() {
- Resources rm = getResources();
- Globals.monthNames = rm.getStringArray(R.array.month_names);
- }
- @Override
- public void onTerminate() {
- MLDB.done();
- super.onTerminate();
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- updateMonthNames();
- }
-}
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.LedgerAccount;
import net.ktnx.mobileledger.utils.LockHolder;
-import net.ktnx.mobileledger.utils.MLDB;
import java.util.ArrayList;
String profile = Data.profile.getValue().getUuid();
try {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
db.beginTransaction();
try {
try (LockHolder lh = params[0].accountList.lockForWriting()) {
import android.database.sqlite.SQLiteDatabase;
-import net.ktnx.mobileledger.utils.MLDB;
+import net.ktnx.mobileledger.App;
import java.util.concurrent.BlockingQueue;
try {
DbOpItem item = queue.take();
debug("opQrunner", "Got "+item.sql);
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
debug("opQrunner", "Executing "+item.sql);
db.execSQL(item.sql, item.params);
}
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.model.Data;
-import net.ktnx.mobileledger.utils.MLDB;
import java.util.HashMap;
import java.util.Map;
Map<String, Boolean> unique = new HashMap<>();
debug("descriptions", "Starting refresh");
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
Data.backgroundTaskStarted();
try {
import android.os.AsyncTask;
import android.os.OperationCanceledException;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.err.HTTPException;
import net.ktnx.mobileledger.json.AccountListParser;
import net.ktnx.mobileledger.json.ParsedBalance;
import net.ktnx.mobileledger.model.LedgerTransactionAccount;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.activity.MainActivity;
-import net.ktnx.mobileledger.utils.MLDB;
import net.ktnx.mobileledger.utils.NetworkUtil;
import java.io.BufferedReader;
default:
throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
}
- try (SQLiteDatabase db = MLDB.getDatabase()) {
+ // FIXME: why the resource block here? that would close the global DB connection
+ try (SQLiteDatabase db = App.getDatabase()) {
try (InputStream resp = http.getInputStream()) {
if (http.getResponseCode() != 200)
throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
}
publishProgress(progress);
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
ArrayList<LedgerAccount> accountList = new ArrayList<>();
boolean listFilledOK = false;
try (InputStream resp = http.getInputStream()) {
default:
throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
}
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (InputStream resp = http.getInputStream()) {
if (http.getResponseCode() != 200)
throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.LedgerAccount;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
-import net.ktnx.mobileledger.utils.MLDB;
import java.util.ArrayList;
if (onlyStarred) sql += " AND a.hidden = 0";
sql += " ORDER BY a.name";
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) {
while (cursor.moveToNext()) {
final String accName = cursor.getString(0);
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.LedgerTransaction;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.model.TransactionListItem;
import net.ktnx.mobileledger.utils.Globals;
-import net.ktnx.mobileledger.utils.MLDB;
import java.text.ParseException;
import java.util.ArrayList;
}
debug("UTT", sql);
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
String lastDateString = Globals.formatLedgerDate(new Date());
Date lastDate = Globals.parseLedgerDate(lastDateString);
boolean odd = true;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
import net.ktnx.mobileledger.ui.activity.MainActivity;
import net.ktnx.mobileledger.utils.LockHolder;
String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
if (profileUUID == null) return -1;
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor c = db
.rawQuery("SELECT theme from profiles where uuid=?", new String[]{profileUUID}))
{
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.async.DbOpQueue;
import net.ktnx.mobileledger.utils.Globals;
import net.ktnx.mobileledger.utils.Logger;
public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) {
MobileLedgerProfile result = null;
ArrayList<MobileLedgerProfile> list = new ArrayList<>();
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
"auth_password, permit_posting, theme, order_no, " +
"preferred_accounts_filter FROM " +
return result;
}
public static void storeProfilesOrder() {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
db.beginTransaction();
try {
int orderNo = 0;
this.authPassword = authPassword;
}
public void storeInDB() {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
db.beginTransaction();
try {
// debug("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " +
// debug("profile", String.format("Transaction %d stored", tr.getId()));
}
public String getOption(String name, String default_value) {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
new String[]{uuid, name}))
{
setOption(name, String.valueOf(value));
}
public void removeFromDB() {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
debug("db", String.format("removing profile %s from DB", uuid));
db.beginTransaction();
try {
}
@NonNull
public LedgerAccount loadAccount(String name) {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
return loadAccount(db, name);
}
@Nullable
public LedgerAccount tryLoadAccount(String acct_name) {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
return tryLoadAccount(db, acct_name);
}
@NonNull
}
public LedgerTransaction loadTransaction(int transactionId) {
LedgerTransaction tr = new LedgerTransaction(transactionId, this.uuid);
- tr.loadData(MLDB.getDatabase());
+ tr.loadData(App.getDatabase());
return tr;
}
}
public List<LedgerAccount> loadChildAccountsOf(LedgerAccount acc) {
List<LedgerAccount> result = new ArrayList<>();
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor c = db.rawQuery(
"SELECT a.name FROM accounts a WHERE a.profile = ? and a.name like ?||':%'",
new String[]{uuid, acc.getName()}))
ArrayList<LedgerAccount> visibleList = new ArrayList<>();
visibleList.add(acc);
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor c = db.rawQuery(
"SELECT a.name FROM accounts a WHERE a.profile = ? and a.name like ?||':%'",
new String[]{uuid, acc.getName()}))
return result;
}
public void wipeAllData() {
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
db.beginTransaction();
try {
String[] pUuid = new String[]{uuid};
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.BuildConfig;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
debug("descr", sql);
debug("descr", params.toString());
- try (Cursor c = MLDB.getDatabase().rawQuery(sql, params.toArray(new String[]{}))) {
+ try (Cursor c = App.getDatabase().rawQuery(sql, params.toArray(new String[]{}))) {
if (!c.moveToNext()) return;
String profileUUID = c.getString(0);
import android.widget.LinearLayout;
import android.widget.TextView;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.LedgerTransaction;
import net.ktnx.mobileledger.model.TransactionListItem;
import net.ktnx.mobileledger.utils.Colors;
import net.ktnx.mobileledger.utils.Globals;
-import net.ktnx.mobileledger.utils.MLDB;
import java.text.DateFormat;
import java.util.Date;
LedgerTransaction tr = p[0].transaction;
boolean odd = p[0].odd;
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
tr.loadData(db);
publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
package net.ktnx.mobileledger.utils;
import android.annotation.TargetApi;
-import android.app.Application;
import android.content.Context;
-import android.content.res.Resources;
import android.database.Cursor;
import android.database.MatrixCursor;
-import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
-import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.os.Build;
import android.provider.FontsContract;
-import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.FilterQueryProvider;
import android.widget.SimpleCursorAdapter;
+import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.async.DbOpQueue;
import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
import net.ktnx.mobileledger.model.Data;
import org.jetbrains.annotations.NonNls;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.util.Locale;
import static net.ktnx.mobileledger.utils.Logger.debug;
@NonNls
public static final String OPT_PROFILE_UUID = "profile_uuid";
private static final String NO_PROFILE = "-";
- private static MobileLedgerDatabase dbHelper;
- private static Application context;
- private static void checkState() {
- if (context == null)
- throw new IllegalStateException("First call init with a valid context");
- }
- public static SQLiteDatabase getDatabase() {
- checkState();
-
- SQLiteDatabase db;
-
- db = dbHelper.getWritableDatabase();
-
- db.execSQL("pragma case_sensitive_like=ON;");
- return db;
- }
@SuppressWarnings("unused")
static public int getIntOption(String name, int default_value) {
String s = getOption(name, String.valueOf(default_value));
AsyncTask<Void, Void, String> t = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
- SQLiteDatabase db = getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor cursor = db
.rawQuery("select value from options where profile = ? and name=?",
new String[]{NO_PROFILE, name}))
}
static public String getOption(String name, String default_value) {
debug("db", "about to fetch option " + name);
- SQLiteDatabase db = getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
new String[]{NO_PROFILE, name}))
{
params = new String[]{str, str, str, str};
}
debug("autocompletion", sql);
- SQLiteDatabase db = MLDB.getDatabase();
+ SQLiteDatabase db = App.getDatabase();
try (Cursor matches = db.rawQuery(sql, params)) {
int i = 0;
});
}
}
- public static synchronized void init(Application context) {
- MLDB.context = context;
- if (dbHelper != null)
- throw new IllegalStateException("It appears init() was already called");
- dbHelper = new MobileLedgerDatabase(context);
- }
- public static synchronized void done() {
- if (dbHelper != null) {
- debug("db", "Closing DB helper");
- dbHelper.close();
- dbHelper = null;
- }
- }
}
-class MobileLedgerDatabase extends SQLiteOpenHelper {
- private static final String DB_NAME = "MoLe.db";
- private static final int LATEST_REVISION = 22;
- private static final String CREATE_DB_SQL = "create_db";
-
- private final Application mContext;
-
- MobileLedgerDatabase(Application context) {
- super(context, DB_NAME, null, LATEST_REVISION);
- debug("db", "creating helper instance");
- mContext = context;
- super.setWriteAheadLoggingEnabled(true);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- debug("db", "onCreate called");
- applyRevisionFile(db, CREATE_DB_SQL);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- debug("db", "onUpgrade called");
- for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i);
- }
-
- private void applyRevision(SQLiteDatabase db, int rev_no) {
- String rev_file = String.format(Locale.US, "sql_%d", rev_no);
-
- applyRevisionFile(db, rev_file);
- }
- private void applyRevisionFile(SQLiteDatabase db, String rev_file) {
- final Resources rm = mContext.getResources();
- int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
- if (res_id == 0)
- throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file));
- db.beginTransaction();
- try (InputStream res = rm.openRawResource(res_id)) {
- debug("db", "Applying " + rev_file);
- InputStreamReader isr = new InputStreamReader(res);
- BufferedReader reader = new BufferedReader(isr);
-
- String line;
- int line_no = 1;
- while ((line = reader.readLine()) != null) {
- if (line.startsWith("--")) {
- line_no++;
- continue;
- }
- if (line.isEmpty()) {
- line_no++;
- continue;
- }
- try {
- db.execSQL(line);
- }
- catch (Exception e) {
- throw new RuntimeException(
- String.format("Error applying %s, line %d", rev_file, line_no), e);
- }
- line_no++;
- }
-
- db.setTransactionSuccessful();
- }
- catch (IOException e) {
- Log.e("db", String.format("Error opening raw resource for %s", rev_file));
- e.printStackTrace();
- }
- finally {
- db.endTransaction();
- }
- }
-}
--- /dev/null
+/*
+ * Copyright © 2019 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 <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.utils;
+
+import android.app.Application;
+import android.content.res.Resources;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.util.Log;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Locale;
+
+import static net.ktnx.mobileledger.utils.Logger.debug;
+
+public class MobileLedgerDatabase extends SQLiteOpenHelper {
+ private static final String DB_NAME = "MoLe.db";
+ private static final int LATEST_REVISION = 22;
+ private static final String CREATE_DB_SQL = "create_db";
+
+ private final Application mContext;
+
+ public MobileLedgerDatabase(Application context) {
+ super(context, DB_NAME, null, LATEST_REVISION);
+ debug("db", "creating helper instance");
+ mContext = context;
+ super.setWriteAheadLoggingEnabled(true);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ debug("db", "onCreate called");
+ applyRevisionFile(db, CREATE_DB_SQL);
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ debug("db", "onUpgrade called");
+ for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i);
+ }
+
+ private void applyRevision(SQLiteDatabase db, int rev_no) {
+ String rev_file = String.format(Locale.US, "sql_%d", rev_no);
+
+ applyRevisionFile(db, rev_file);
+ }
+ private void applyRevisionFile(SQLiteDatabase db, String rev_file) {
+ final Resources rm = mContext.getResources();
+ int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
+ if (res_id == 0)
+ throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file));
+ db.beginTransaction();
+ try (InputStream res = rm.openRawResource(res_id)) {
+ debug("db", "Applying " + rev_file);
+ InputStreamReader isr = new InputStreamReader(res);
+ BufferedReader reader = new BufferedReader(isr);
+
+ String line;
+ int line_no = 1;
+ while ((line = reader.readLine()) != null) {
+ if (line.startsWith("--")) {
+ line_no++;
+ continue;
+ }
+ if (line.isEmpty()) {
+ line_no++;
+ continue;
+ }
+ try {
+ db.execSQL(line);
+ }
+ catch (Exception e) {
+ throw new RuntimeException(
+ String.format("Error applying %s, line %d", rev_file, line_no), e);
+ }
+ line_no++;
+ }
+
+ db.setTransactionSuccessful();
+ }
+ catch (IOException e) {
+ Log.e("db", String.format("Error opening raw resource for %s", rev_file));
+ e.printStackTrace();
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+}