Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.db; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.File; |
michael@0 | 8 | import java.util.HashMap; |
michael@0 | 9 | |
michael@0 | 10 | import org.mozilla.gecko.GeckoProfile; |
michael@0 | 11 | |
michael@0 | 12 | import android.content.Context; |
michael@0 | 13 | import android.database.sqlite.SQLiteOpenHelper; |
michael@0 | 14 | import android.text.TextUtils; |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Manages a set of per-profile database storage helpers. |
michael@0 | 18 | */ |
michael@0 | 19 | public class PerProfileDatabases<T extends SQLiteOpenHelper> { |
michael@0 | 20 | |
michael@0 | 21 | private final HashMap<String, T> mStorages = new HashMap<String, T>(); |
michael@0 | 22 | |
michael@0 | 23 | private final Context mContext; |
michael@0 | 24 | private final String mDatabaseName; |
michael@0 | 25 | private final DatabaseHelperFactory<T> mHelperFactory; |
michael@0 | 26 | |
michael@0 | 27 | public interface DatabaseHelperFactory<T> { |
michael@0 | 28 | public T makeDatabaseHelper(Context context, String databasePath); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | public PerProfileDatabases(final Context context, final String databaseName, final DatabaseHelperFactory<T> helperFactory) { |
michael@0 | 32 | mContext = context; |
michael@0 | 33 | mDatabaseName = databaseName; |
michael@0 | 34 | mHelperFactory = helperFactory; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | public String getDatabasePathForProfile(String profile) { |
michael@0 | 38 | final File profileDir = GeckoProfile.get(mContext, profile).getDir(); |
michael@0 | 39 | if (profileDir == null) { |
michael@0 | 40 | return null; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | return new File(profileDir, mDatabaseName).getAbsolutePath(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | public T getDatabaseHelperForProfile(String profile) { |
michael@0 | 47 | return getDatabaseHelperForProfile(profile, false); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | public T getDatabaseHelperForProfile(String profile, boolean isTest) { |
michael@0 | 51 | // Always fall back to default profile if none has been provided. |
michael@0 | 52 | if (TextUtils.isEmpty(profile)) { |
michael@0 | 53 | profile = GeckoProfile.get(mContext).getName(); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | synchronized (this) { |
michael@0 | 57 | if (mStorages.containsKey(profile)) { |
michael@0 | 58 | return mStorages.get(profile); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | final String databasePath = isTest ? mDatabaseName : getDatabasePathForProfile(profile); |
michael@0 | 62 | if (databasePath == null) { |
michael@0 | 63 | throw new IllegalStateException("Database path is null for profile: " + profile); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | final T helper = mHelperFactory.makeDatabaseHelper(mContext, databasePath); |
michael@0 | 67 | DBUtils.ensureDatabaseIsNotLocked(helper, databasePath); |
michael@0 | 68 | |
michael@0 | 69 | mStorages.put(profile, helper); |
michael@0 | 70 | return helper; |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | } |