mobile/android/base/db/AbstractPerProfileDatabaseProvider.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 org.mozilla.gecko.mozglue.RobocopTarget;
michael@0 8
michael@0 9 import android.database.sqlite.SQLiteDatabase;
michael@0 10 import android.database.sqlite.SQLiteOpenHelper;
michael@0 11 import android.net.Uri;
michael@0 12
michael@0 13 /**
michael@0 14 * The base class for ContentProviders that wish to use a different DB
michael@0 15 * for each profile.
michael@0 16 *
michael@0 17 * This class has logic shared between ordinary per-profile CPs and
michael@0 18 * those that wish to share DB connections between CPs.
michael@0 19 */
michael@0 20 public abstract class AbstractPerProfileDatabaseProvider extends AbstractTransactionalProvider {
michael@0 21
michael@0 22 /**
michael@0 23 * Extend this to provide access to your own map of shared databases. This
michael@0 24 * is a method so that your subclass doesn't collide with others!
michael@0 25 */
michael@0 26 protected abstract PerProfileDatabases<? extends SQLiteOpenHelper> getDatabases();
michael@0 27
michael@0 28 /*
michael@0 29 * Fetches a readable database based on the profile indicated in the
michael@0 30 * passed URI. If the URI does not contain a profile param, the default profile
michael@0 31 * is used.
michael@0 32 *
michael@0 33 * @param uri content URI optionally indicating the profile of the user
michael@0 34 * @return instance of a readable SQLiteDatabase
michael@0 35 */
michael@0 36 @Override
michael@0 37 protected SQLiteDatabase getReadableDatabase(Uri uri) {
michael@0 38 String profile = null;
michael@0 39 if (uri != null) {
michael@0 40 profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
michael@0 41 }
michael@0 42
michael@0 43 return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getReadableDatabase();
michael@0 44 }
michael@0 45
michael@0 46 /*
michael@0 47 * Fetches a writable database based on the profile indicated in the
michael@0 48 * passed URI. If the URI does not contain a profile param, the default profile
michael@0 49 * is used
michael@0 50 *
michael@0 51 * @param uri content URI optionally indicating the profile of the user
michael@0 52 * @return instance of a writable SQLiteDatabase
michael@0 53 */
michael@0 54 @Override
michael@0 55 protected SQLiteDatabase getWritableDatabase(Uri uri) {
michael@0 56 String profile = null;
michael@0 57 if (uri != null) {
michael@0 58 profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
michael@0 59 }
michael@0 60
michael@0 61 return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getWritableDatabase();
michael@0 62 }
michael@0 63
michael@0 64 protected SQLiteDatabase getWritableDatabaseForProfile(String profile, boolean isTest) {
michael@0 65 return getDatabases().getDatabaseHelperForProfile(profile, isTest).getWritableDatabase();
michael@0 66 }
michael@0 67
michael@0 68 /**
michael@0 69 * This method should ONLY be used for testing purposes.
michael@0 70 *
michael@0 71 * @param uri content URI optionally indicating the profile of the user
michael@0 72 * @return instance of a writable SQLiteDatabase
michael@0 73 */
michael@0 74 @Override
michael@0 75 @RobocopTarget
michael@0 76 public SQLiteDatabase getWritableDatabaseForTesting(Uri uri) {
michael@0 77 return getWritableDatabase(uri);
michael@0 78 }
michael@0 79 }

mercurial