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