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