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 static android.Manifest.permission.READ_CONTACTS; 20 import static android.Manifest.permission.WRITE_CONTACTS; 21 22 import android.content.BroadcastReceiver; 23 import android.content.ContentValues; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.database.Cursor; 27 import android.net.Uri; 28 import android.provider.ContactsContract; 29 import android.provider.ContactsContract.PhoneLookup; 30 import android.provider.ContactsContract.PinnedPositions; 31 import android.text.TextUtils; 32 33 import com.android.contacts.common.util.PermissionsUtil; 34 35 /** 36 * This broadcast receiver is used to listen to outgoing calls and undemote formerly demoted 37 * contacts if a phone call is made to a phone number belonging to that contact. 38 * 39 * NOTE This doesn't work for corp contacts. 40 */ 41 public class UndemoteOutgoingCallReceiver extends BroadcastReceiver { 42 43 private static final long NO_CONTACT_FOUND = -1; 44 45 @Override onReceive(final Context context, Intent intent)46 public void onReceive(final Context context, Intent intent) { 47 if (!PermissionsUtil.hasPermission(context, READ_CONTACTS) 48 || !PermissionsUtil.hasPermission(context, WRITE_CONTACTS)) { 49 return; 50 } 51 if (intent != null && Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { 52 final String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 53 if (TextUtils.isEmpty(number)) { 54 return; 55 } 56 new Thread() { 57 @Override 58 public void run() { 59 final long id = getContactIdFromPhoneNumber(context, number); 60 if (id != NO_CONTACT_FOUND) { 61 undemoteContactWithId(context, id); 62 } 63 } 64 }.start(); 65 } 66 } 67 undemoteContactWithId(Context context, long id)68 private void undemoteContactWithId(Context context, long id) { 69 // If the contact is not demoted, this will not do anything. Otherwise, it will 70 // restore it to an unpinned position. If it was a frequently called contact, it will 71 // show up once again show up on the favorites screen. 72 if (PermissionsUtil.hasPermission(context, WRITE_CONTACTS)) { 73 try { 74 PinnedPositions.undemote(context.getContentResolver(), id); 75 } catch (SecurityException e) { 76 // Just in case 77 } 78 } 79 } 80 getContactIdFromPhoneNumber(Context context, String number)81 private long getContactIdFromPhoneNumber(Context context, String number) { 82 if (!PermissionsUtil.hasPermission(context, READ_CONTACTS)) { 83 return NO_CONTACT_FOUND; 84 } 85 final Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 86 Uri.encode(number)); 87 final Cursor cursor; 88 try { 89 cursor = context.getContentResolver().query(contactUri, new String[] { 90 PhoneLookup._ID}, null, null, null); 91 } catch (SecurityException e) { 92 // Just in case 93 return NO_CONTACT_FOUND; 94 } 95 if (cursor == null) { 96 return NO_CONTACT_FOUND; 97 } 98 try { 99 if (cursor.moveToFirst()) { 100 final long id = cursor.getLong(0); 101 return id; 102 } else { 103 return NO_CONTACT_FOUND; 104 } 105 } finally { 106 cursor.close(); 107 } 108 } 109 } 110