1/*
2 * Copyright (C) 2013 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 * Copyright (C) 2013 The Android Open Source Project
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 *      http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License
31 */
32
33import android.content.BroadcastReceiver;
34import android.content.Context;
35import android.content.Intent;
36import android.content.SharedPreferences;
37import android.os.SystemClock;
38import android.preference.PreferenceManager;
39import android.provider.ContactsContract;
40import android.text.TextUtils;
41import android.util.Log;
42
43/**
44 * Receiver that handles boot completion broadcast.
45 */
46public class BootCompletedReceiver extends BroadcastReceiver {
47
48    private static final String TAG = BootCompletedReceiver.class.getSimpleName();
49    public static final String DATABASE_TIME_CREATED = "database_time_created";
50
51    public void onReceive(Context context, Intent intent) {
52        // This notification is sent here instead of ContactsDatabaseHelper.onCreate() because it
53        // needs to be sent after boot completes. onCreate() starts before boot complete happens
54        // and the notification would be dropped by the framework.
55
56        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
57        final String prefTime = prefs.getString(DATABASE_TIME_CREATED, "");
58
59        String dbTime = ContactsDatabaseHelper.getInstance(context).getProperty(
60                ContactsDatabaseHelper.DbProperties.DATABASE_TIME_CREATED, "");
61        int count = 0;
62
63        // db time should already exist because it's always created by the ContactsUpgradeReceiver
64        // pre boot. But in the event that it may be a bit slow, wait a bit.
65        while (TextUtils.isEmpty(dbTime) && count != 3) {
66            SystemClock.sleep(1000);
67            dbTime = ContactsDatabaseHelper.getInstance(context).getProperty(
68                    ContactsDatabaseHelper.DbProperties.DATABASE_TIME_CREATED, "");
69            count++;
70        }
71
72        if (TextUtils.isEmpty(prefTime) || !prefTime.equals(dbTime)) {
73            Log.i(TAG, "Boot complete, newly created contact database detected. Pref Time: " +
74                    prefTime + " DB Time: " + dbTime);
75
76            final Intent broadcast = new Intent(ContactsContract.Intents.CONTACTS_DATABASE_CREATED);
77            broadcast.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
78            context.sendBroadcast(broadcast, android.Manifest.permission.READ_CONTACTS);
79
80            prefs.edit().putString(DATABASE_TIME_CREATED, dbTime).commit();
81        }
82    }
83
84}
85