/** * Copyright (c) 2015, The CyanogenMod Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.cyanogenmod.cmsettings; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Environment; import android.os.UserHandle; import android.util.Log; import java.io.File; /** * The CMDatabaseHelper allows creation of a database to store CM specific settings for a user * in System, Secure, and Global tables. */ public class CMDatabaseHelper extends SQLiteOpenHelper{ private static final String TAG = "CMDatabaseHelper"; private static final boolean LOCAL_LOGV = false; private static final String DATABASE_NAME = "cmsettings.db"; private static final int DATABASE_VERSION = 1; static class CMTableNames { static final String TABLE_SYSTEM = "system"; static final String TABLE_SECURE = "secure"; static final String TABLE_GLOBAL = "global"; } private static final String CREATE_TABLE_SQL_FORMAT = "CREATE TABLE %s (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT UNIQUE ON CONFLICT REPLACE," + "value TEXT" + ");)"; private static final String CREATE_INDEX_SQL_FORMAT = "CREATE INDEX %sIndex%d ON %s (name);"; private int mUserHandle; /** * Gets the appropriate database path for a specific user * @param userId The database path for this user * @return The database path string */ static String dbNameForUser(final int userId) { // The owner gets the unadorned db name; if (userId == UserHandle.USER_OWNER) { return DATABASE_NAME; } else { // Place the database in the user-specific data tree so that it's // cleaned up automatically when the user is deleted. File databaseFile = new File( Environment.getUserSystemDirectory(userId), DATABASE_NAME); return databaseFile.getPath(); } } /** * Creates an instance of {@link CMDatabaseHelper} * @param context * @param userId */ public CMDatabaseHelper(Context context, int userId) { super(context, dbNameForUser(userId), null, DATABASE_VERSION); mUserHandle = userId; } /** * Creates System, Secure, and Global tables in the specified {@link SQLiteDatabase} * @param db The database. */ @Override public void onCreate(SQLiteDatabase db) { db.beginTransaction(); try { createDbTable(db, CMTableNames.TABLE_SYSTEM); createDbTable(db, CMTableNames.TABLE_SECURE); if (mUserHandle == UserHandle.USER_OWNER) { createDbTable(db, CMTableNames.TABLE_GLOBAL); } db.setTransactionSuccessful(); if (LOCAL_LOGV) Log.v(TAG, "Successfully created tables for cm settings db"); } finally { db.endTransaction(); } } /** * Creates a table and index for the specified database and table name * @param db * @param tableName */ private void createDbTable(SQLiteDatabase db, String tableName) { if (LOCAL_LOGV) Log.v(TAG, "Creating table and index for: " + tableName); String createTableSql = String.format(CREATE_TABLE_SQL_FORMAT, tableName); db.execSQL(createTableSql); String createIndexSql = String.format(CREATE_INDEX_SQL_FORMAT, tableName, 1, tableName); db.execSQL(createIndexSql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }