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