|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.db; |
|
6 |
|
7 import org.mozilla.gecko.db.PerProfileDatabases.DatabaseHelperFactory; |
|
8 |
|
9 import android.content.Context; |
|
10 import android.database.sqlite.SQLiteOpenHelper; |
|
11 |
|
12 /** |
|
13 * Abstract class containing methods needed to make a SQLite-based content |
|
14 * provider with a database helper of type T, where one database helper is |
|
15 * held per profile. |
|
16 */ |
|
17 public abstract class PerProfileDatabaseProvider<T extends SQLiteOpenHelper> extends AbstractPerProfileDatabaseProvider { |
|
18 private PerProfileDatabases<T> databases; |
|
19 |
|
20 @Override |
|
21 protected PerProfileDatabases<T> getDatabases() { |
|
22 return databases; |
|
23 } |
|
24 |
|
25 protected abstract String getDatabaseName(); |
|
26 |
|
27 /** |
|
28 * Creates and returns an instance of the appropriate DB helper. |
|
29 * |
|
30 * @param context to use to create the database helper |
|
31 * @param databasePath path to the DB file |
|
32 * @return instance of the database helper |
|
33 */ |
|
34 protected abstract T createDatabaseHelper(Context context, String databasePath); |
|
35 |
|
36 @Override |
|
37 public boolean onCreate() { |
|
38 synchronized (this) { |
|
39 databases = new PerProfileDatabases<T>( |
|
40 getContext(), getDatabaseName(), new DatabaseHelperFactory<T>() { |
|
41 @Override |
|
42 public T makeDatabaseHelper(Context context, String databasePath) { |
|
43 return createDatabaseHelper(context, databasePath); |
|
44 } |
|
45 }); |
|
46 } |
|
47 |
|
48 return true; |
|
49 } |
|
50 } |