Home | History | Annotate | Download | only in contacts
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.providers.contacts;
     18 
     19 import android.content.ContentValues;
     20 import android.content.Context;
     21 import android.database.sqlite.SQLiteDatabase;
     22 import android.provider.ContactsContract.Profile;
     23 
     24 import com.android.providers.contacts.util.NeededForTesting;
     25 
     26 /**
     27  * A separate version of the contacts database helper for storing the user's profile data.
     28  */
     29 public class ProfileDatabaseHelper extends ContactsDatabaseHelper {
     30     private static final String TAG = "ProfileDatabaseHelper";
     31 
     32     private static final String DATABASE_NAME = "profile.db";
     33 
     34     // SQLite-standard table and columns for tracking autoincrement sequences.
     35     private static final String SEQUENCE_TABLE = "sqlite_sequence";
     36     private static final String SEQUENCE_NAME = "name";
     37     private static final String SEQUENCE_SEQ = "seq";
     38 
     39     private static ProfileDatabaseHelper sSingleton = null;
     40 
     41     /**
     42      * Returns a new instance for unit tests.
     43      */
     44     @NeededForTesting
     45     public static ProfileDatabaseHelper getNewInstanceForTest(Context context) {
     46         return new ProfileDatabaseHelper(context, null, false);
     47     }
     48 
     49     private ProfileDatabaseHelper(
     50             Context context, String databaseName, boolean optimizationEnabled) {
     51         super(context, databaseName, optimizationEnabled);
     52     }
     53 
     54     public static synchronized ProfileDatabaseHelper getInstance(Context context) {
     55         if (sSingleton == null) {
     56             sSingleton = new ProfileDatabaseHelper(context, DATABASE_NAME, true);
     57         }
     58         return sSingleton;
     59     }
     60 
     61     @Override
     62     protected int dbForProfile() {
     63         return 1;
     64     }
     65 
     66     @Override
     67     protected void initializeAutoIncrementSequences(SQLiteDatabase db) {
     68         for (String table : Tables.SEQUENCE_TABLES) {
     69             ContentValues values = new ContentValues();
     70             values.put(SEQUENCE_NAME, table);
     71             values.put(SEQUENCE_SEQ, Profile.MIN_ID);
     72             db.insert(SEQUENCE_TABLE, null, values);
     73         }
     74     }
     75 }
     76