1 /* 2 * Copyright (C) 2018 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.launcher3.util; 18 19 import static android.database.sqlite.SQLiteDatabase.NO_LOCALIZED_COLLATORS; 20 21 import android.content.Context; 22 import android.content.ContextWrapper; 23 import android.database.DatabaseErrorHandler; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.database.sqlite.SQLiteDatabase.CursorFactory; 26 import android.database.sqlite.SQLiteDatabase.OpenParams; 27 import android.database.sqlite.SQLiteOpenHelper; 28 import android.os.Build; 29 30 /** 31 * Extension of {@link SQLiteOpenHelper} which avoids creating default locale table by 32 * A context wrapper which creates databases without support for localized collators. 33 */ 34 public abstract class NoLocaleSQLiteHelper extends SQLiteOpenHelper { 35 36 private static final boolean ATLEAST_P = 37 Build.VERSION.SDK_INT >= Build.VERSION_CODES.P; 38 NoLocaleSQLiteHelper(Context context, String name, int version)39 public NoLocaleSQLiteHelper(Context context, String name, int version) { 40 super(ATLEAST_P ? context : new NoLocalContext(context), name, null, version); 41 if (ATLEAST_P) { 42 setOpenParams(new OpenParams.Builder().addOpenFlags(NO_LOCALIZED_COLLATORS).build()); 43 } 44 } 45 46 private static class NoLocalContext extends ContextWrapper { NoLocalContext(Context base)47 public NoLocalContext(Context base) { 48 super(base); 49 } 50 51 @Override openOrCreateDatabase( String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler)52 public SQLiteDatabase openOrCreateDatabase( 53 String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) { 54 return super.openOrCreateDatabase( 55 name, mode | Context.MODE_NO_LOCALIZED_COLLATORS, factory, errorHandler); 56 } 57 } 58 } 59