Android Open Source - forklift Abstract Asset Sqlite Open Helper






From Project

Back to project page forklift.

License

The source code is released under:

Apache License

If you think the Android project forklift listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ch.gluecode.forklift;
//  ww  w . j  ava  2 s  .c o m
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

/**
 * A helper class that allows access to SQLite databases shipped as asset.
 *
 * <p>To access a SQLite database that is shipped as asset, implement this class. Overriding the lifecycle callbacks is
 * optional.</p>
 */
public abstract class AbstractAssetSqliteOpenHelper extends AbstractBundledSqliteOpenHelper {

    private static final String TAG = AbstractAssetSqliteOpenHelper.class.getSimpleName();

    private final String assetName;

    /**
     * Creates a new helper instance that allows to access a bundled SQLite database stored in the application's assets
     * directory. The database is not actually
     * opened until {@link ch.gluecode.forklift.AbstractBundledSqliteOpenHelper#getDatabase()} is called.
     *
     * @param context Context to use to access the database.
     * @param assetName Name of the bundled database file in the application's asset directory.
     * @param databaseName Name of the database in the application directory to access.
     * @param version Version number of the database (>= 1).
     */
    protected AbstractAssetSqliteOpenHelper(Context context, String assetName, String databaseName, int version) {
        super(context, databaseName, version);
        this.assetName = assetName;
    }

    /**
     * Creates a new helper instance that allows to access a bundled SQLite database stored in the application's assets
     * directory. The database is not actually
     * opened until {@link ch.gluecode.forklift.AbstractBundledSqliteOpenHelper#getDatabase()} is called.
     *
     * @param context Context to use to access the database.
     * @param assetName Name of the bundled database file in the application's asset directory.
     * @param databaseName Name of the database in the application directory to access.
     * @param version Version number of the database (>= 1).
     * @param factory Factory to use for creating cursor objects.
     */
    public AbstractAssetSqliteOpenHelper(Context context, String assetName, String databaseName,
                                         int version, SQLiteDatabase.CursorFactory factory) {
        super(context, databaseName, version, factory);

        this.assetName = assetName;
    }

    /**
     * Gets the name of the asset that is used to populate the database.
     *
     * @return Name of the asset.
     */
    public String getAssetName() {
        return assetName;
    }

    @Override
    protected final InputStream getBundledDatabase() {
        try {
            Log.i(TAG, "Trying to open asset file " + assetName + ".");
            return getContext().getAssets().open(assetName);
        } catch (IOException e) {
            throw new BundledSqliteOpenHelperException("Failed to open asset file " + assetName + ".", e);
        }
    }
}




Java Source Code List

ch.gluecode.forklift.AbstractAssetSqliteOpenHelper.java
ch.gluecode.forklift.AbstractBundledSqliteOpenHelper.java
ch.gluecode.forklift.AbstractResourceSqliteOpenHelper.java
ch.gluecode.forklift.BundledSqliteOpenHelperException.java
com.example.forklift.CantonListFragment.java
com.example.forklift.CantonsAssetSqliteOpenHelper.java
com.example.forklift.MountainCursorAdapter.java
com.example.forklift.MountainListFragment.java
com.example.forklift.MountainsResourceSqliteOpenHelper.java
com.example.forklift.SampleActivity.java