mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.emergency; |
| 17 | |
| 18 | import android.content.ContentResolver; |
| 19 | import android.content.Context; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 20 | import android.database.Cursor; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.BitmapFactory; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 23 | import android.net.Uri; |
| 24 | import android.provider.ContactsContract; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 25 | |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 26 | import java.io.ByteArrayInputStream; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
mariagpuyol | 609f68a | 2016-02-22 17:45:47 -0800 | [diff] [blame^] | 29 | * Provides methods to read name, phone number, photo, etc. from contacts. |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 30 | */ |
| 31 | public class EmergencyContactManager { |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 32 | |
| 33 | /** Returns the display name of the contact. */ |
| 34 | public static String getName(Context context, Uri contactUri) { |
| 35 | Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null); |
| 36 | try { |
| 37 | if (cursor != null && cursor.moveToFirst()) { |
| 38 | return cursor.getString(cursor.getColumnIndex( |
| 39 | ContactsContract.Contacts.DISPLAY_NAME)); |
| 40 | } |
| 41 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 42 | if (cursor != null) { |
| 43 | cursor.close(); |
| 44 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 45 | } |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | private static boolean contactExists(Context context, Uri contactUri) { |
| 50 | Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null); |
| 51 | try { |
| 52 | if (cursor != null && cursor.moveToFirst()) { |
| 53 | return true; |
| 54 | } |
| 55 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 56 | if (cursor != null) { |
| 57 | cursor.close(); |
| 58 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 63 | |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 64 | /** Returns the phone number of the contact. */ |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 65 | public static String[] getPhoneNumbers(Context context, Uri contactUri) { |
| 66 | // TODO: Investigate passing around CONTENT_LOOKUP_URI instead of content CONTENT_URI. |
| 67 | // The method to get the CONTENT_LOOKUP_URI when the user selects a contact is: |
| 68 | // ContactsContract.Contacts.getLookupUri(ContentResolver resolver, Uri contactUri) |
| 69 | // Then use ContactsContract.Contacts.lookupContact(ContentResolver resolver, Uri lookupUri) |
| 70 | // to get the CONTENT_URI |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 71 | ContentResolver contentResolver = context.getContentResolver(); |
| 72 | Cursor contactCursor = contentResolver.query(contactUri, null, null, null, null); |
| 73 | try { |
| 74 | if (contactCursor != null && contactCursor.moveToFirst()) { |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 75 | if (contactCursor.getInt(contactCursor.getColumnIndex( |
| 76 | ContactsContract.Contacts.HAS_PHONE_NUMBER)) != 0) { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 77 | String id = contactCursor.getString( |
| 78 | contactCursor.getColumnIndex(ContactsContract.Contacts._ID)); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 79 | Cursor phoneCursor = contentResolver.query( |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 80 | ContactsContract.CommonDataKinds.Phone.CONTENT_URI, |
| 81 | null, |
| 82 | ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, |
| 83 | null, |
| 84 | null); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 85 | try { |
| 86 | if (phoneCursor != null && phoneCursor.moveToFirst()) { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 87 | String[] phoneNumbers = |
| 88 | new String[phoneCursor.getCount()]; |
| 89 | for (int i = 0; i < phoneCursor.getCount(); i++) { |
| 90 | String phoneNumber = |
| 91 | phoneCursor.getString(phoneCursor.getColumnIndex( |
| 92 | ContactsContract.CommonDataKinds.Phone.NUMBER)); |
| 93 | |
| 94 | phoneNumbers[i] = phoneNumber; |
| 95 | phoneCursor.moveToNext(); |
| 96 | } |
| 97 | return phoneNumbers; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 98 | } |
| 99 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 100 | if (phoneCursor != null) { |
| 101 | phoneCursor.close(); |
| 102 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 107 | if (contactCursor != null) { |
| 108 | contactCursor.close(); |
| 109 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 110 | } |
| 111 | return null; |
| 112 | } |
| 113 | |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 114 | /** Returns the Bitmap corresponding to the contact's photo. */ |
| 115 | public static Bitmap getContactPhoto(Context context, Uri contactUri) { |
| 116 | Uri photoUri = Uri.withAppendedPath(contactUri, |
| 117 | ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); |
| 118 | Cursor cursor = context.getContentResolver().query(photoUri, |
| 119 | new String[]{ContactsContract.Contacts.Photo.PHOTO}, null, null, null); |
| 120 | if (cursor == null) { |
| 121 | return null; |
| 122 | } |
| 123 | try { |
| 124 | if (cursor.moveToFirst()) { |
| 125 | byte[] data = cursor.getBlob(0); |
| 126 | if (data != null) { |
| 127 | return BitmapFactory.decodeStream(new ByteArrayInputStream(data)); |
| 128 | } |
| 129 | } |
| 130 | } finally { |
| 131 | cursor.close(); |
| 132 | } |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 137 | /** Returns whether the contact uri is not null and corresponds to an existing contact. */ |
mariagpuyol | 609f68a | 2016-02-22 17:45:47 -0800 | [diff] [blame^] | 138 | public static boolean isValidEmergencyContact(Context context, Uri contactUri) { |
| 139 | return contactUri != null && contactExists(context, contactUri); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 140 | } |
| 141 | } |