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.contacts; 18 19import android.content.BroadcastReceiver; 20import android.content.ComponentName; 21import android.content.Context; 22import android.content.Intent; 23import android.content.SharedPreferences; 24import android.content.pm.PackageManager; 25import android.os.Build; 26import android.os.RemoteException; 27import android.util.Log; 28 29import libcore.icu.ICU; 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 * It's job is to prime the contacts 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 */ 41public class ContactsUpgradeReceiver extends BroadcastReceiver { 42 static final String TAG = "ContactsUpgradeReceiver"; 43 static final String PREF_DB_VERSION = "db_version"; 44 static final String PREF_ICU_VERSION = "icu_version"; 45 static final String PREF_OS_VERSION = "os_version"; 46 47 @Override 48 public void onReceive(Context context, Intent intent) { 49 // We are now running with the system up, but no apps started, 50 // so can do whatever cleanup after an upgrade that we want. 51 52 try { 53 long startTime = System.currentTimeMillis(); 54 55 // Lookup the last known database version 56 final SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); 57 final int prefDbVersion = prefs.getInt(PREF_DB_VERSION, 0); 58 59 final String curIcuVersion = ICU.getIcuVersion(); 60 final String curOsVersion = getOsVersionString(); 61 62 final String prefIcuVersion = prefs.getString(PREF_ICU_VERSION, ""); 63 final String prefOsVersion = prefs.getString(PREF_OS_VERSION, ""); 64 65 // If the version is old go ahead and attempt to create or upgrade the database. 66 if (prefDbVersion != ContactsDatabaseHelper.DATABASE_VERSION || 67 !prefIcuVersion.equals(curIcuVersion) || 68 !prefOsVersion.equals(curOsVersion)) { 69 // Store the current version so this receiver isn't run again until the database 70 // version number changes. This is intentionally done even before the upgrade path 71 // is attempted to be conservative. If the upgrade fails for some reason and we 72 // crash and burn we don't want to get into a loop doing so. 73 SharedPreferences.Editor editor = prefs.edit(); 74 editor.putInt(PREF_DB_VERSION, ContactsDatabaseHelper.DATABASE_VERSION); 75 editor.putString(PREF_ICU_VERSION, curIcuVersion); 76 editor.putString(PREF_OS_VERSION, curOsVersion); 77 editor.commit(); 78 79 // Ask for a reference to the database to force the helper to either 80 // create the database or open it up, performing any necessary upgrades 81 // in the process. 82 ContactsDatabaseHelper helper = ContactsDatabaseHelper.getInstance(context); 83 ProfileDatabaseHelper profileHelper = ProfileDatabaseHelper.getInstance(context); 84 CallLogDatabaseHelper calllogHelper = CallLogDatabaseHelper.getInstance(context); 85 86 Log.i(TAG, "Creating or opening contacts database"); 87 88 helper.getWritableDatabase(); 89 helper.forceDirectoryRescan(); 90 91 profileHelper.getWritableDatabase(); 92 calllogHelper.getWritableDatabase(); 93 94 ContactsProvider2.updateLocaleOffline(context, helper, profileHelper); 95 96 // Log the total time taken for the receiver to perform the operation 97 EventLogTags.writeContactsUpgradeReceiver(System.currentTimeMillis() - startTime); 98 } 99 } catch (Throwable t) { 100 // Something has gone terribly wrong. Disable this receiver for good so we can't 101 // possibly end up in a reboot loop. 102 Log.wtf(TAG, "Error during upgrade attempt. Disabling receiver.", t); 103 context.getPackageManager().setComponentEnabledSetting( 104 new ComponentName(context, getClass()), 105 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 106 PackageManager.DONT_KILL_APP); 107 } 108 } 109 110 private static String getOsVersionString() { 111 return Build.ID; 112 } 113} 114