ContactsUpgradeReceiver.java revision 5d0a768b56ed4bd0dfef81b8389247ba74766659
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
29/**
30 * This will be launched during system boot, after the core system has
31 * been brought up but before any non-persistent processes have been
32 * started.  It is launched in a special state, with no content provider
33 * or custom application class associated with the process running.
34 *
35 * It's job is to prime the contacts database. Either create it
36 * if it doesn't exist, or open it and force any necessary upgrades.
37 * All of this heavy lifting happens before the boot animation ends.
38 */
39public class ContactsUpgradeReceiver extends BroadcastReceiver {
40    static final String TAG = "ContactsUpgradeReceiver";
41    static final String PREF_DB_VERSION = "db_version";
42
43    @Override
44    public void onReceive(Context context, Intent intent) {
45        // We are now running with the system up, but no apps started,
46        // so can do whatever cleanup after an upgrade that we want.
47
48        try {
49            long startTime = System.currentTimeMillis();
50
51            // Lookup the last known database version
52            SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
53            int prefVersion = prefs.getInt(PREF_DB_VERSION, 0);
54
55            // If the version is old go ahead and attempt to create or upgrade the database.
56            if (prefVersion != ContactsDatabaseHelper.DATABASE_VERSION) {
57                // Store the current version so this receiver isn't run again until the database
58                // version number changes. This is intentionally done even before the upgrade path
59                // is attempted to be conservative. If the upgrade fails for some reason and we
60                // crash and burn we don't want to get into a loop doing so.
61                prefs.edit().putInt(
62                    PREF_DB_VERSION, ContactsDatabaseHelper.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                ContactsDatabaseHelper helper = ContactsDatabaseHelper.getInstance(context);
68                if (context.getDatabasePath(helper.getDatabaseName()).exists()) {
69                    Log.i(TAG, "Creating or opening contacts 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                ProfileDatabaseHelper profileHelper = ProfileDatabaseHelper.getInstance(context);
80                profileHelper.getWritableDatabase();
81                helper.close();
82
83                // Log the total time taken for the receiver to perform the operation
84                EventLogTags.writeContactsUpgradeReceiver(System.currentTimeMillis() - startTime);
85            }
86        } catch (Throwable t) {
87            // Something has gone terribly wrong. Disable this receiver for good so we can't
88            // possibly end up in a reboot loop.
89            Log.wtf(TAG, "Error during upgrade attempt. Disabling receiver.", t);
90            context.getPackageManager().setComponentEnabledSetting(
91                    new ComponentName(context, getClass()),
92                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
93                    PackageManager.DONT_KILL_APP);
94        }
95    }
96}
97