1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/db/PerProfileDatabaseProvider.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,50 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.db; 1.9 + 1.10 +import org.mozilla.gecko.db.PerProfileDatabases.DatabaseHelperFactory; 1.11 + 1.12 +import android.content.Context; 1.13 +import android.database.sqlite.SQLiteOpenHelper; 1.14 + 1.15 +/** 1.16 + * Abstract class containing methods needed to make a SQLite-based content 1.17 + * provider with a database helper of type T, where one database helper is 1.18 + * held per profile. 1.19 + */ 1.20 +public abstract class PerProfileDatabaseProvider<T extends SQLiteOpenHelper> extends AbstractPerProfileDatabaseProvider { 1.21 + private PerProfileDatabases<T> databases; 1.22 + 1.23 + @Override 1.24 + protected PerProfileDatabases<T> getDatabases() { 1.25 + return databases; 1.26 + } 1.27 + 1.28 + protected abstract String getDatabaseName(); 1.29 + 1.30 + /** 1.31 + * Creates and returns an instance of the appropriate DB helper. 1.32 + * 1.33 + * @param context to use to create the database helper 1.34 + * @param databasePath path to the DB file 1.35 + * @return instance of the database helper 1.36 + */ 1.37 + protected abstract T createDatabaseHelper(Context context, String databasePath); 1.38 + 1.39 + @Override 1.40 + public boolean onCreate() { 1.41 + synchronized (this) { 1.42 + databases = new PerProfileDatabases<T>( 1.43 + getContext(), getDatabaseName(), new DatabaseHelperFactory<T>() { 1.44 + @Override 1.45 + public T makeDatabaseHelper(Context context, String databasePath) { 1.46 + return createDatabaseHelper(context, databasePath); 1.47 + } 1.48 + }); 1.49 + } 1.50 + 1.51 + return true; 1.52 + } 1.53 +}