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.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.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
46    @Override
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            SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
56            int prefDbVersion = prefs.getInt(PREF_DB_VERSION, 0);
57            final String curIcuVersion = ICU.getIcuVersion();
58            final String prefIcuVersion = prefs.getString(PREF_ICU_VERSION, "");
59
60            // If the version is old go ahead and attempt to create or upgrade the database.
61            if (prefDbVersion != ContactsDatabaseHelper.DATABASE_VERSION ||
62                    !prefIcuVersion.equals(curIcuVersion)) {
63                // Store the current version so this receiver isn't run again until the database
64                // version number changes. This is intentionally done even before the upgrade path
65                // is attempted to be conservative. If the upgrade fails for some reason and we
66                // crash and burn we don't want to get into a loop doing so.
67                SharedPreferences.Editor editor = prefs.edit();
68                editor.putInt(PREF_DB_VERSION, ContactsDatabaseHelper.DATABASE_VERSION);
69                editor.putString(PREF_ICU_VERSION, curIcuVersion);
70                editor.commit();
71
72                // Ask for a reference to the database to force the helper to either
73                // create the database or open it up, performing any necessary upgrades
74                // in the process.
75                ContactsDatabaseHelper helper = ContactsDatabaseHelper.getInstance(context);
76                ProfileDatabaseHelper profileHelper = ProfileDatabaseHelper.getInstance(context);
77                Log.i(TAG, "Creating or opening contacts database");
78                try {
79                    ActivityManagerNative.getDefault().showBootMessage(
80                            context.getText(R.string.upgrade_msg), true);
81                } catch (RemoteException e) {
82                }
83
84                helper.getWritableDatabase();
85                profileHelper.getWritableDatabase();
86                ContactsProvider2.updateLocaleOffline(context, helper, profileHelper);
87
88                // Log the total time taken for the receiver to perform the operation
89                EventLogTags.writeContactsUpgradeReceiver(System.currentTimeMillis() - startTime);
90            }
91        } catch (Throwable t) {
92            // Something has gone terribly wrong. Disable this receiver for good so we can't
93            // possibly end up in a reboot loop.
94            Log.wtf(TAG, "Error during upgrade attempt. Disabling receiver.", t);
95            context.getPackageManager().setComponentEnabledSetting(
96                    new ComponentName(context, getClass()),
97                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
98                    PackageManager.DONT_KILL_APP);
99        }
100    }
101}
102