UndemoteOutgoingCallReceiver.java revision e2f52619318af486a4fef2bca86c8237c46603f6
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.dialer.interactions;
18
19import android.content.BroadcastReceiver;
20import android.content.ContentValues;
21import android.content.Context;
22import android.content.Intent;
23import android.database.Cursor;
24import android.net.Uri;
25import android.provider.ContactsContract.PhoneLookup;
26import android.provider.ContactsContract.PinnedPositions;
27import android.text.TextUtils;
28
29/**
30 * This broadcast receiver is used to listen to outgoing calls and undemote formerly demoted
31 * contacts if a phone call is made to a phone number belonging to that contact.
32 */
33public class UndemoteOutgoingCallReceiver extends BroadcastReceiver {
34
35    private static final long NO_CONTACT_FOUND = -1;
36
37    @Override
38    public void onReceive(Context context, Intent intent) {
39        if (intent != null && Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {
40            final String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
41            if (TextUtils.isEmpty(number)) {
42                return;
43            }
44            final long id = getContactIdFromPhoneNumber(context, number);
45            if (id != NO_CONTACT_FOUND) {
46                undemoteContactWithId(context, id);
47            }
48        }
49    }
50
51    private void undemoteContactWithId(Context context, long id) {
52        final ContentValues cv = new ContentValues(1);
53        cv.put(String.valueOf(id), PinnedPositions.UNDEMOTE);
54        // If the contact is not demoted, this will not do anything. Otherwise, it will
55        // restore it to an unpinned position. If it was a frequently called contact, it will
56        // show up once again show up on the favorites screen.
57        context.getContentResolver().update(PinnedPositions.UPDATE_URI, cv, null, null);
58    }
59
60    private long getContactIdFromPhoneNumber(Context context, String number) {
61        final Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
62                Uri.encode(number));
63        final Cursor cursor = context.getContentResolver().query(contactUri, new String[] {
64                PhoneLookup._ID}, null, null, null);
65        try {
66            if (cursor.moveToFirst()) {
67                final long id = cursor.getLong(0);
68                return id;
69            } else {
70                return NO_CONTACT_FOUND;
71            }
72        } finally {
73            cursor.close();
74        }
75    }
76}
77