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