18900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu/*
28900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * Copyright (C) 2017 The Android Open Source Project
38900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu *
48900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * Licensed under the Apache License, Version 2.0 (the "License");
58900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * you may not use this file except in compliance with the License.
68900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * You may obtain a copy of the License at
78900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu *
88900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu *      http://www.apache.org/licenses/LICENSE-2.0
98900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu *
108900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * Unless required by applicable law or agreed to in writing, software
118900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * distributed under the License is distributed on an "AS IS" BASIS,
128900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * See the License for the specific language governing permissions and
148900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * limitations under the License.
158900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu */
168900da40c20aab6fc875acc3dab4d4018a4e551cfionaxupackage com.android.providers.telephony;
178900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
188900da40c20aab6fc875acc3dab4d4018a4e551cfionaxuimport android.database.sqlite.SQLiteDatabase;
198900da40c20aab6fc875acc3dab4d4018a4e551cfionaxuimport android.database.sqlite.SQLiteOpenHelper;
208900da40c20aab6fc875acc3dab4d4018a4e551cfionaxuimport android.util.Log;
218900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
228900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu/**
238900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu * A subclass of CarrierIdProvider used for testing on an in-memory database
248900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu */
258900da40c20aab6fc875acc3dab4d4018a4e551cfionaxupublic class CarrierIdProviderTestable extends CarrierIdProvider {
268900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    private static final String TAG = CarrierIdProviderTestable.class.getSimpleName();
278900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
288900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    private InMemoryCarrierIdProviderDbHelper mDbHelper;
298900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
308900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    @Override
318900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    public boolean onCreate() {
328900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        Log.d(TAG, "onCreate called: mDbHelper = new InMemoryCarrierIdProviderDbHelper()");
338900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        mDbHelper = new InMemoryCarrierIdProviderDbHelper();
348900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        return true;
358900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    }
368900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
378900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    // close mDbHelper database object
388900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    protected void closeDatabase() {
398900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        mDbHelper.close();
408900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    }
418900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
428900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    @Override
438900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    SQLiteDatabase getReadableDatabase() {
448900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        Log.d(TAG, "getReadableDatabase called" + mDbHelper.getReadableDatabase());
458900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        return mDbHelper.getReadableDatabase();
468900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    }
478900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
488900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    @Override
498900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    SQLiteDatabase getWritableDatabase() {
508900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        Log.d(TAG, "getWritableDatabase called" + mDbHelper.getWritableDatabase());
518900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        return mDbHelper.getWritableDatabase();
528900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    }
538900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
548900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    /**
558900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu     * An in memory DB for CarrierIdProviderTestable to use
568900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu     */
578900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    public static class InMemoryCarrierIdProviderDbHelper extends SQLiteOpenHelper {
588900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        public InMemoryCarrierIdProviderDbHelper() {
598900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            super(null,      // no context is needed for in-memory db
608900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu                  null,      // db file name is null for in-memory db
618900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu                  null,      // CursorFactory is null by default
628900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu                  1);        // db version is no-op for tests
638900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            Log.d(TAG, "InMemoryCarrierIdProviderDbHelper creating in-memory database");
648900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        }
658900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
668900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        @Override
678900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        public void onCreate(SQLiteDatabase db) {
688900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            //set up the Carrier id table
698900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            Log.d(TAG, "InMemoryCarrierIdProviderDbHelper onCreate creating the carrier infp table");
708900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            db.execSQL(CarrierIdProvider.getStringForCarrierIdTableCreation(
718900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu                    CarrierIdProvider.CARRIER_ID_TABLE));
728900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            db.execSQL(CarrierIdProvider.getStringForIndexCreation(
738900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu                    CarrierIdProvider.CARRIER_ID_TABLE));
748900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        }
758900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu
768900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        @Override
778900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
788900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            Log.d(TAG, "InMemoryCarrierIdProviderDbHelper onUpgrade doing nothing");
798900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu            return;
808900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu        }
818900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu    }
828900da40c20aab6fc875acc3dab4d4018a4e551cfionaxu}
83