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