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