Android App – Memorize Scriptures


A co-worker carried around 3×5 cards in order to memorize scripture references. He asked how hard it would be to create an Android app to replace his 3×5 cards. I became interested and volunteered to create the app which lead to my first published Android app and also my Android app creation obsession. 🙂

First challenge, have the entire LDS standard works of scripture available in a sqlite database. Luckily, a fellow Mormon developer has made the scriptures available in several data formats including sqlite found here: http://scriptures.nephi.org/. Android is a lot happier if the id field of each sqlite table is ‘_id’ so I had to do some modifications to the tables.

I placed the pre-populated SQLite database in the assets folder of my app and copied over to the App’s data directory on Android if it was it’s first time running on a device. Here’s my helper class for doing all this:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;
import android.util.Log;

import java.io.*;
import java.sql.SQLException;
import java.util.Locale;

class DatabaseHelper extends SQLiteOpenHelper {
    private static String DB_PATH = "/data/data/com.spriton.memorizescriptures2/databases/";
    public static final String DATABASE_NAME = "scriptures.sqlite";

    private static final int SCHEMA_VERSION = 1;

    public SQLiteDatabase db;
    private final Context myContext;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
        this.myContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void createDatabase() {
        if (!DbExists()) {
            this.getReadableDatabase();
            copyDataBase();
        }
    }

    private boolean DbExists() {
        SQLiteDatabase db = null;
        try {
            String databasePath = DB_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(databasePath, null,  SQLiteDatabase.OPEN_READWRITE);

            db.setLocale(Locale.getDefault());
            db.setLockingEnabled(true);
            db.setVersion(1);

            Cursor c = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table'", null);
            if(c.getCount()  " + outFilePath);
        try {
            iStream = myContext.getAssets().open(DATABASE_NAME);
            oStream = new FileOutputStream(outFilePath);
            byte[] buffer = new byte[2048];
            int length;
            while ((length = iStream.read(buffer)) > 0) {
                oStream.write(buffer, 0, length);
            }
            oStream.flush();
            oStream.close();
            iStream.close();
        } catch (IOException e) {
            Log.e("scripture", "exception", e);
        }
    }

    public void openDatabase() throws SQLException {
        String path = DB_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public SQLiteDatabase getDatabase() {
        return db;
    }

    @Override
    public synchronized void close() {
        if (db != null) {
            db.close();
        }
        super.close();
    }

Second challenge, find a simple and light ORM for Android and SQLite. This is where OrmLite came in handy. I’m used to Hibernate, so the object mappings and queries took some getting used to but they worked just fine.

Here’s an OrmLite query example:

     QueryBuilder query = OrmHelper.getHelper(this).getScriptureDao().queryBuilder();
     Where where = query.where();
     where.isNull("deleted");
     query.orderBy("created", true);
     List scriptures = query.query();

Third challenge, learning how to style Android. It took a lot of searching and trial and error to get the app to look the way I wanted. I was targeting Android API 10 (2.3) so I was not able to use the newer Holo themes that make things look sharp. I did find a Holo Theme Generator that gave me a good start once I got it in my project and got it to build properly.

The result:

Menu Review2 ViewAll2

NewScripture

This app is available on:
Google Play
Amazon Marketplace

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s