IndexDatabaseHelper.java revision dd41dfc483e830bdb33df858dec99e2c4c806bfc
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 = 106;
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_SWITCH_ON = "data_switch_on";
50        public static final String DATA_SWITCH_ON_NORMALIZED = "data_switch_on_normalized";
51        public static final String DATA_SWITCH_OFF = "data_switch_off";
52        public static final String DATA_SWITCH_OFF_NORMALIZED = "data_switch_off_normalized";
53        public static final String DATA_KEYWORDS = "data_keywords";
54        public static final String CLASS_NAME = "class_name";
55        public static final String SCREEN_TITLE = "screen_title";
56        public static final String INTENT_ACTION = "intent_action";
57        public static final String INTENT_TARGET_PACKAGE = "intent_target_package";
58        public static final String INTENT_TARGET_CLASS = "intent_target_class";
59        public static final String ICON = "icon";
60        public static final String ENABLED = "enabled";
61    }
62
63    public interface MetaColumns {
64        public static final String BUILD = "build";
65    }
66
67    private static final String CREATE_INDEX_TABLE =
68            "CREATE VIRTUAL TABLE " + Tables.TABLE_PREFS_INDEX + " USING fts4" +
69                    "(" +
70                    IndexColumns.LOCALE +
71                    ", " +
72                    IndexColumns.DATA_RANK +
73                    ", " +
74                    IndexColumns.DATA_TITLE +
75                    ", " +
76                    IndexColumns.DATA_TITLE_NORMALIZED +
77                    ", " +
78                    IndexColumns.DATA_SUMMARY_ON +
79                    ", " +
80                    IndexColumns.DATA_SUMMARY_ON_NORMALIZED +
81                    ", " +
82                    IndexColumns.DATA_SUMMARY_OFF +
83                    ", " +
84                    IndexColumns.DATA_SUMMARY_OFF_NORMALIZED +
85                    ", " +
86                    IndexColumns.DATA_ENTRIES +
87                    ", " +
88                    IndexColumns.DATA_SWITCH_ON +
89                    ", " +
90                    IndexColumns.DATA_SWITCH_ON_NORMALIZED +
91                    ", " +
92                    IndexColumns.DATA_SWITCH_OFF +
93                    ", " +
94                    IndexColumns.DATA_SWITCH_OFF_NORMALIZED +
95                    ", " +
96                    IndexColumns.DATA_KEYWORDS +
97                    ", " +
98                    IndexColumns.SCREEN_TITLE +
99                    ", " +
100                    IndexColumns.CLASS_NAME +
101                    ", " +
102                    IndexColumns.ICON +
103                    ", " +
104                    IndexColumns.INTENT_ACTION +
105                    ", " +
106                    IndexColumns.INTENT_TARGET_PACKAGE +
107                    ", " +
108                    IndexColumns.INTENT_TARGET_CLASS +
109                    ", " +
110                    IndexColumns.ENABLED +
111                    ");";
112
113    private static final String CREATE_META_TABLE =
114            "CREATE TABLE " + Tables.TABLE_META_INDEX +
115                    "(" +
116                    MetaColumns.BUILD + " VARCHAR(32) NOT NULL" +
117                    ")";
118
119    private static final String INSERT_BUILD_VERSION =
120            "INSERT INTO " + Tables.TABLE_META_INDEX +
121                    " VALUES ('" + Build.VERSION.INCREMENTAL + "');";
122
123    private static final String SELECT_BUILD_VERSION =
124            "SELECT " + MetaColumns.BUILD + " FROM " + Tables.TABLE_META_INDEX + " LIMIT 1;";
125
126    private static IndexDatabaseHelper sSingleton;
127
128    public static synchronized IndexDatabaseHelper getInstance(Context context) {
129        if (sSingleton == null) {
130            sSingleton = new IndexDatabaseHelper(context);
131        }
132        return sSingleton;
133    }
134
135    public IndexDatabaseHelper(Context context) {
136        super(context, DATABASE_NAME, null, DATABASE_VERSION);
137    }
138
139    @Override
140    public void onCreate(SQLiteDatabase db) {
141        bootstrapDB(db);
142    }
143
144    private void bootstrapDB(SQLiteDatabase db) {
145        db.execSQL(CREATE_INDEX_TABLE);
146        db.execSQL(CREATE_META_TABLE);
147        db.execSQL(INSERT_BUILD_VERSION);
148        Log.i(TAG, "Bootstrapped database");
149    }
150
151    @Override
152    public void onOpen(SQLiteDatabase db) {
153        super.onOpen(db);
154
155        Log.i(TAG, "Using schema version: " + db.getVersion());
156
157        if (!Build.VERSION.INCREMENTAL.equals(getBuildVersion(db))) {
158            Log.w(TAG, "Index needs to be rebuilt as build-version is not the same");
159            // We need to drop the tables and recreate them
160            reconstruct(db);
161        } else {
162            Log.i(TAG, "Index is fine");
163        }
164    }
165
166    @Override
167    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
168        if (oldVersion < DATABASE_VERSION) {
169            Log.w(TAG, "Detected schema version '" +  oldVersion + "'. " +
170                    "Index needs to be rebuilt for schema version '" + newVersion + "'.");
171            // We need to drop the tables and recreate them
172            reconstruct(db);
173        }
174    }
175
176    @Override
177    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
178        Log.w(TAG, "Detected schema version '" +  oldVersion + "'. " +
179                "Index needs to be rebuilt for schema version '" + newVersion + "'.");
180        // We need to drop the tables and recreate them
181        reconstruct(db);
182    }
183
184    private void reconstruct(SQLiteDatabase db) {
185        dropTables(db);
186        bootstrapDB(db);
187    }
188
189    private String getBuildVersion(SQLiteDatabase db) {
190        String version = null;
191        Cursor cursor = null;
192        try {
193            cursor = db.rawQuery(SELECT_BUILD_VERSION, null);
194            if (cursor.moveToFirst()) {
195                version = cursor.getString(0);
196            }
197        }
198        catch (Exception e) {
199            Log.e(TAG, "Cannot get build version from Index metadata");
200        }
201        finally {
202            if (cursor != null) {
203                cursor.close();
204            }
205        }
206        return version;
207    }
208
209    private void dropTables(SQLiteDatabase db) {
210        db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_META_INDEX);
211        db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_PREFS_INDEX);
212    }
213}
214