125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton/*
225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * Copyright (C) 2010 The Android Open Source Project
325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton *
425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * Licensed under the Apache License, Version 2.0 (the "License");
525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * you may not use this file except in compliance with the License.
625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * You may obtain a copy of the License at
725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton *
825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton *      http://www.apache.org/licenses/LICENSE-2.0
925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton *
1025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * Unless required by applicable law or agreed to in writing, software
1125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * distributed under the License is distributed on an "AS IS" BASIS,
1225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * See the License for the specific language governing permissions and
1425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * limitations under the License
1525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton */
1625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
1725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonpackage com.android.providers.calendar;
1825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
1925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.content.BroadcastReceiver;
2025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.content.ComponentName;
2125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.content.Context;
2225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.content.Intent;
2325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.content.SharedPreferences;
2425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.content.pm.PackageManager;
2525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.util.EventLog;
2625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonimport android.util.Log;
2725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
2825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton/**
2925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * This will be launched during system boot, after the core system has
3025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * been brought up but before any non-persistent processes have been
3125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * started.  It is launched in a special state, with no content provider
3225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * or custom application class associated with the process running.
3325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton *
3425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * It's job is to prime the calendar database. Either create it
3525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * if it doesn't exist, or open it and force any necessary upgrades.
3625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton * All of this heavy lifting happens before the boot animation ends.
3725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton */
3825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamiltonpublic class CalendarUpgradeReceiver extends BroadcastReceiver {
3925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton    static final String TAG = "CalendarUpgradeReceiver";
4025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton    static final String PREF_DB_VERSION = "db_version";
4125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
4225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton    @Override
4325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton    public void onReceive(Context context, Intent intent) {
4425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton        // We are now running with the system up, but no apps started,
4525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton        // so can do whatever cleanup after an upgrade that we want.
4625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
4725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton        try {
4825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            long startTime = System.currentTimeMillis();
4925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
5025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            // Lookup the last known database version
5125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
5225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            int prefVersion = prefs.getInt(PREF_DB_VERSION, 0);
5325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
5425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            // If the version is old go ahead and attempt to create or upgrade the database.
5525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            if (prefVersion != CalendarDatabaseHelper.DATABASE_VERSION) {
5625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // Store the current version so this receiver isn't run again until the database
5725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // version number changes. This is intentionally done even before the upgrade path
5825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // is attempted to be conservative. If the upgrade fails for some reason and we
5925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // crash and burn we don't want to get into a loop doing so.
6025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                prefs.edit().putInt(PREF_DB_VERSION, CalendarDatabaseHelper.DATABASE_VERSION).commit();
6125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
6225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // Ask for a reference to the database to force the helper to either
6325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // create the database or open it up, performing any necessary upgrades
6425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // in the process.
6525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                Log.i(TAG, "Creating or opening calendar database");
6625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                CalendarDatabaseHelper helper = CalendarDatabaseHelper.getInstance(context);
6725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                helper.getWritableDatabase();
6825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                helper.close();
6925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton
7025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                // Log the total time taken for the receiver to perform the operation
7125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                EventLogTags.writeCalendarUpgradeReceiver(System.currentTimeMillis() - startTime);
7225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            }
7325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton        } catch (Throwable t) {
7425b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            // Something has gone terribly wrong. Disable this receiver for good so we can't
7525b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            // possibly end up in a reboot loop.
7625b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            Log.wtf(TAG, "Error during upgrade attempt. Disabling receiver.", t);
7725b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton            context.getPackageManager().setComponentEnabledSetting(
7825b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                    new ComponentName(context, getClass()),
7925b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
8025b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton                    PackageManager.DONT_KILL_APP);
8125b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton        }
8225b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton    }
8325b0f0ee070a5fff18cfcaffa86cb1607941c8a5Jeff Hamilton}
84