1 /* 2 * Copyright (C) 2016 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.BroadcastReceiver; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.content.pm.PackageManager; 25 import android.os.RemoteException; 26 import android.util.EventLog; 27 import android.util.Log; 28 29 import com.android.providers.tv.TvProvider.DatabaseHelper; 30 31 /** 32 * This will be launched during system boot, after the core system has 33 * been brought up but before any non-persistent processes have been 34 * started. It is launched in a special state, with no content provider 35 * or custom application class associated with the process running. 36 * 37 * Its job is to prime the tv provider database. Either create it 38 * if it doesn't exist, or open it and force any necessary upgrades. 39 * All of this heavy lifting happens before the boot animation ends. 40 */ 41 public class TvProviderUpgradeReceiver extends BroadcastReceiver { 42 static final String TAG = "TvProviderUpgradeReceiver"; 43 static final String PREF_DB_VERSION = "db_version"; 44 45 @Override 46 public void onReceive(Context context, Intent intent) { 47 // We are now running with the system up, but no apps started, 48 // so can do whatever cleanup after an upgrade that we want. 49 50 try { 51 long startTime = System.currentTimeMillis(); 52 53 // Lookup the last known database version 54 SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); 55 int prefVersion = prefs.getInt(PREF_DB_VERSION, 0); 56 57 // If the version is old go ahead and attempt to create or upgrade the database. 58 if (prefVersion != TvProvider.DATABASE_VERSION) { 59 // Store the current version so this receiver isn't run again until the database 60 // version number changes. This is intentionally done even before the upgrade path 61 // is attempted to be conservative. If the upgrade fails for some reason and we 62 // crash and burn we don't want to get into a loop doing so. 63 prefs.edit().putInt(PREF_DB_VERSION, TvProvider.DATABASE_VERSION).commit(); 64 65 // Ask for a reference to the database to force the helper to either 66 // create the database or open it up, performing any necessary upgrades 67 // in the process. 68 DatabaseHelper helper = DatabaseHelper.getInstance(context); 69 if (context.getDatabasePath(helper.getDatabaseName()).exists()) { 70 Log.i(TAG, "Creating or opening tv provider database"); 71 helper.getWritableDatabase(); 72 } 73 } 74 } catch (Throwable t) { 75 // Something has gone terribly wrong. Disable this receiver for good so we can't 76 // possibly end up in a reboot loop. 77 Log.wtf(TAG, "Error during upgrade attempt. Disabling receiver.", t); 78 context.getPackageManager().setComponentEnabledSetting( 79 new ComponentName(context, getClass()), 80 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 81 PackageManager.DONT_KILL_APP); 82 } 83 } 84 } 85