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