IndexDatabaseHelper.java revision a41707200b6628376e8fb575e782d8228c0d32a2
1/*
2 * Copyright (C) 2014 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
17package com.android.settings.search;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.database.sqlite.SQLiteDatabase;
22import android.database.sqlite.SQLiteOpenHelper;
23import android.os.Build;
24import android.util.Log;
25
26public class IndexDatabaseHelper extends SQLiteOpenHelper {
27
28    private static final String TAG = "IndexDatabaseHelper";
29
30    private static final String DATABASE_NAME = "search_index.db";
31    private static final int DATABASE_VERSION = 105;
32
33    public interface Tables {
34        public static final String TABLE_PREFS_INDEX = "prefs_index";
35        public static final String TABLE_META_INDEX = "meta_index";
36    }
37
38    public interface IndexColumns {
39        public static final String DOCID = "docid";
40        public static final String LOCALE = "locale";
41        public static final String DATA_RANK = "data_rank";
42        public static final String DATA_TITLE = "data_title";
43        public static final String DATA_TITLE_NORMALIZED = "data_title_normalized";
44        public static final String DATA_SUMMARY_ON = "data_summary_on";
45        public static final String DATA_SUMMARY_ON_NORMALIZED = "data_summary_on_normalized";
46        public static final String DATA_SUMMARY_OFF = "data_summary_off";
47        public static final String DATA_SUMMARY_OFF_NORMALIZED = "data_summary_off_normalized";
48        public static final String DATA_ENTRIES = "data_entries";
49        public static final String DATA_KEYWORDS = "data_keywords";
50        public static final String CLASS_NAME = "class_name";
51        public static final String SCREEN_TITLE = "screen_title";
52        public static final String INTENT_ACTION = "intent_action";
53        public static final String INTENT_TARGET_PACKAGE = "intent_target_package";
54        public static final String INTENT_TARGET_CLASS = "intent_target_class";
55        public static final String ICON = "icon";
56        public static final String ENABLED = "enabled";
57    }
58
59    public interface MetaColumns {
60        public static final String BUILD = "build";
61    }
62
63    private static final String CREATE_INDEX_TABLE =
64            "CREATE VIRTUAL TABLE " + Tables.TABLE_PREFS_INDEX + " USING fts4" +
65                    "(" +
66                    IndexColumns.LOCALE +
67                    ", " +
68                    IndexColumns.DATA_RANK +
69                    ", " +
70                    IndexColumns.DATA_TITLE +
71                    ", " +
72                    IndexColumns.DATA_TITLE_NORMALIZED +
73                    ", " +
74                    IndexColumns.DATA_SUMMARY_ON +
75                    ", " +
76                    IndexColumns.DATA_SUMMARY_ON_NORMALIZED +
77                    ", " +
78                    IndexColumns.DATA_SUMMARY_OFF +
79                    ", " +
80                    IndexColumns.DATA_SUMMARY_OFF_NORMALIZED +
81                    ", " +
82                    IndexColumns.DATA_ENTRIES +
83                    ", " +
84                    IndexColumns.DATA_KEYWORDS +
85                    ", " +
86                    IndexColumns.SCREEN_TITLE +
87                    ", " +
88                    IndexColumns.CLASS_NAME +
89                    ", " +
90                    IndexColumns.ICON +
91                    ", " +
92                    IndexColumns.INTENT_ACTION +
93                    ", " +
94                    IndexColumns.INTENT_TARGET_PACKAGE +
95                    ", " +
96                    IndexColumns.INTENT_TARGET_CLASS +
97                    ", " +
98                    IndexColumns.ENABLED +
99                    ");";
100
101    private static final String CREATE_META_TABLE =
102            "CREATE TABLE " + Tables.TABLE_META_INDEX +
103                    "(" +
104                    MetaColumns.BUILD + " VARCHAR(32) NOT NULL" +
105                    ")";
106
107    private static final String INSERT_BUILD_VERSION =
108            "INSERT INTO " + Tables.TABLE_META_INDEX +
109                    " VALUES ('" + Build.VERSION.INCREMENTAL + "');";
110
111    private static final String SELECT_BUILD_VERSION =
112            "SELECT " + MetaColumns.BUILD + " FROM " + Tables.TABLE_META_INDEX + " LIMIT 1;";
113
114    private static IndexDatabaseHelper sSingleton;
115
116    public static synchronized IndexDatabaseHelper getInstance(Context context) {
117        if (sSingleton == null) {
118            sSingleton = new IndexDatabaseHelper(context);
119        }
120        return sSingleton;
121    }
122
123    public IndexDatabaseHelper(Context context) {
124        super(context, DATABASE_NAME, null, DATABASE_VERSION);
125    }
126
127    @Override
128    public void onCreate(SQLiteDatabase db) {
129        bootstrapDB(db);
130    }
131
132    private void bootstrapDB(SQLiteDatabase db) {
133        db.execSQL(CREATE_INDEX_TABLE);
134        db.execSQL(CREATE_META_TABLE);
135        db.execSQL(INSERT_BUILD_VERSION);
136        Log.i(TAG, "Bootstrapped database");
137    }
138
139    @Override
140    public void onOpen(SQLiteDatabase db) {
141        super.onOpen(db);
142
143        Log.i(TAG, "Using schema version: " + db.getVersion());
144
145        if (!Build.VERSION.INCREMENTAL.equals(getBuildVersion(db))) {
146            Log.w(TAG, "Index needs to be rebuilt as build-version is not the same");
147            // We need to drop the tables and recreate them
148            reconstruct(db);
149        } else {
150            Log.i(TAG, "Index is fine");
151        }
152    }
153
154    @Override
155    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
156        if (oldVersion == 100 || oldVersion == 101 || oldVersion == 102 || oldVersion == 103) {
157            Log.w(TAG, "Detected schema version '" +  oldVersion + "'. " +
158                    "Index needs to be rebuilt for schema version '" + newVersion + "'.");
159            // We need to drop the tables and recreate them
160            reconstruct(db);
161        }
162    }
163
164    @Override
165    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
166        Log.w(TAG, "Detected schema version '" +  oldVersion + "'. " +
167                "Index needs to be rebuilt for schema version '" + newVersion + "'.");
168        // We need to drop the tables and recreate them
169        reconstruct(db);
170    }
171
172    private void reconstruct(SQLiteDatabase db) {
173        dropTables(db);
174        bootstrapDB(db);
175    }
176
177    private String getBuildVersion(SQLiteDatabase db) {
178        String version = null;
179        Cursor cursor = null;
180        try {
181            cursor = db.rawQuery(SELECT_BUILD_VERSION, null);
182            if (cursor.moveToFirst()) {
183                version = cursor.getString(0);
184            }
185        }
186        catch (Exception e) {
187            Log.e(TAG, "Cannot get build version from Index metadata");
188        }
189        finally {
190            if (cursor != null) {
191                cursor.close();
192            }
193        }
194        return version;
195    }
196
197    private void dropTables(SQLiteDatabase db) {
198        db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_META_INDEX);
199        db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_PREFS_INDEX);
200    }
201}
202