• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.providers.tv;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.media.tv.TvContract;
23 import android.net.Uri;
24 import java.io.File;
25 
26 class TvProviderForTesting extends TvProvider {
27     private static final String FAKE_SESSION_TOKEN = "TvProviderForTesting";
28 
29     String callingPackage;
30     DatabaseHelper mDatabaseHelper;
31 
32     @Override
onCreate()33     public boolean onCreate() {
34         mDatabaseHelper = new DatabaseHelperForTesting(getContext(), TvProvider.DATABASE_VERSION);
35         setOpenHelper(mDatabaseHelper, false);
36         return super.onCreate();
37     }
38 
39     @Override
scheduleEpgDataCleanup()40     void scheduleEpgDataCleanup() {}
41 
42     @Override
getCallingPackage_()43     String getCallingPackage_() {
44         if (callingPackage != null) {
45             return callingPackage;
46         }
47         return getContext().getPackageName();
48     }
49 
50     @Override
shutdown()51     public void shutdown() {
52         super.shutdown();
53 
54         if (mDatabaseHelper != null) {
55             SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
56             File databaseFile = new File(db.getPath());
57             mDatabaseHelper.close();
58             SQLiteDatabase.deleteDatabase(databaseFile);
59         }
60     }
61 
setTransientRowHelper(TransientRowHelper helper)62     void setTransientRowHelper(TransientRowHelper helper) {
63         mTransientRowHelper = helper;
64     }
65 
getDatabaseHelper()66     DatabaseHelper getDatabaseHelper() {
67         return mDatabaseHelper;
68     }
69 
70     // This method is a bypass for testing to avoid async'ly updating restriction of TvProvider
insertWatchedProgramSync(ContentValues values)71     Uri insertWatchedProgramSync(ContentValues values) {
72         values.put(WATCHED_PROGRAMS_COLUMN_CONSOLIDATED, 1);
73         values.put(TvContract.WatchedPrograms.COLUMN_INTERNAL_SESSION_TOKEN, FAKE_SESSION_TOKEN);
74         if (mDatabaseHelper == null) {
75             mDatabaseHelper = DatabaseHelper.getInstance(getContext());
76         }
77         SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
78         long rowId = db.insert(WATCHED_PROGRAMS_TABLE, null, values);
79         return TvContract.buildWatchedProgramUri(rowId);
80     }
81 
82     private static class DatabaseHelperForTesting extends TvProvider.DatabaseHelper {
83         private static final String DATABASE_NAME = "tvtest.db";
84 
DatabaseHelperForTesting(Context context, int version)85         private DatabaseHelperForTesting(Context context, int version) {
86             super(context, DATABASE_NAME, version);
87         }
88     }
89 }
90