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 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 18 | import android.content.ContentUris; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 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 | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 23 | import com.android.internal.logging.MetricsLogger; |
| 24 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 25 | import android.net.Uri; |
| 26 | import android.provider.ContactsContract; |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 27 | import android.util.Log; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 28 | |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 29 | import java.io.ByteArrayInputStream; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
mariagpuyol | 609f68a | 2016-02-22 17:45:47 -0800 | [diff] [blame] | 32 | * Provides methods to read name, phone number, photo, etc. from contacts. |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 33 | */ |
| 34 | public class EmergencyContactManager { |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 35 | private static final String TAG = "EmergencyContactManager"; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 36 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 37 | /** |
| 38 | * Returns a {@link Contact} that contains all the relevant information of the contact indexed |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 39 | * by {@code @phoneUri}. |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 40 | */ |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 41 | public static Contact getContact(Context context, Uri phoneUri) { |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 42 | String phoneNumber = null; |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 43 | String phoneType = null; |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 44 | String name = null; |
| 45 | Bitmap photo = null; |
| 46 | final Uri contactLookupUri = |
| 47 | ContactsContract.Contacts.getLookupUri(context.getContentResolver(), |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 48 | phoneUri); |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 49 | Cursor cursor = context.getContentResolver().query( |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 50 | phoneUri, |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 51 | new String[]{ContactsContract.Contacts.DISPLAY_NAME, |
| 52 | ContactsContract.CommonDataKinds.Phone.NUMBER, |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 53 | ContactsContract.CommonDataKinds.Phone.TYPE, |
| 54 | ContactsContract.CommonDataKinds.Phone.LABEL, |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 55 | ContactsContract.CommonDataKinds.Photo.PHOTO_ID}, |
| 56 | null, null, null); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 57 | try { |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 58 | if (cursor.moveToNext()) { |
| 59 | name = cursor.getString(0); |
| 60 | phoneNumber = cursor.getString(1); |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 61 | phoneType = ContactsContract.CommonDataKinds.Phone.getTypeLabel( |
| 62 | context.getResources(), |
| 63 | cursor.getInt(2), |
| 64 | cursor.getString(3)).toString(); |
| 65 | Long photoId = cursor.getLong(4); |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 66 | if (photoId != null && photoId > 0) { |
| 67 | Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, |
| 68 | photoId); |
| 69 | Cursor cursor2 = context.getContentResolver().query( |
| 70 | photoUri, |
| 71 | new String[]{ContactsContract.Contacts.Photo.PHOTO}, |
| 72 | null, null, null); |
| 73 | try { |
| 74 | if (cursor2.moveToNext()) { |
| 75 | byte[] data = cursor2.getBlob(0); |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 76 | if (data != null) { |
| 77 | photo = BitmapFactory.decodeStream(new ByteArrayInputStream(data)); |
| 78 | } |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 79 | } |
| 80 | } finally { |
| 81 | if (cursor2 != null) { |
| 82 | cursor2.close(); |
| 83 | } |
| 84 | } |
| 85 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 86 | } |
| 87 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 88 | if (cursor != null) { |
| 89 | cursor.close(); |
| 90 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 91 | } |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 92 | return new Contact(contactLookupUri, phoneUri, name, phoneNumber, phoneType, photo); |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 93 | } |
| 94 | |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 95 | /** Returns whether the phone uri is not null and corresponds to an existing phone number. */ |
| 96 | public static boolean isValidEmergencyContact(Context context, Uri phoneUri) { |
| 97 | return phoneUri != null && phoneExists(context, phoneUri); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 98 | } |
| 99 | |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 100 | private static boolean phoneExists(Context context, Uri phoneUri) { |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 101 | Cursor cursor = null; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 102 | try { |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 103 | cursor = context.getContentResolver().query(phoneUri, null, null, null, null); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 104 | if (cursor != null && cursor.moveToFirst()) { |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 105 | MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 1); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 106 | return true; |
| 107 | } |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 108 | } catch (SecurityException e) { |
| 109 | Log.w(TAG, "Unable to read contact information", e); |
| 110 | MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 2); |
| 111 | return false; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 112 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 113 | if (cursor != null) { |
| 114 | cursor.close(); |
| 115 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 116 | } |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 117 | MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 0); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 121 | /** Wrapper for a contact with a phone number. */ |
| 122 | public static class Contact { |
| 123 | /** The lookup uri is necessary to display the contact. */ |
| 124 | private final Uri mContactLookupUri; |
| 125 | /** |
| 126 | * The contact uri is associated to a particular phone number and can be used to reload that |
| 127 | * number and keep the number displayed in the preferences fresh. |
| 128 | */ |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 129 | private final Uri mPhoneUri; |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 130 | /** The display name of the contact. */ |
| 131 | private final String mName; |
| 132 | /** The emergency contact's phone number selected by the user. */ |
| 133 | private final String mPhoneNumber; |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 134 | /** The emergency contact's phone number type (mobile, work, home, etc). */ |
| 135 | private final String mPhoneType; |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 136 | /** The contact's photo. */ |
| 137 | private final Bitmap mPhoto; |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 138 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 139 | /** Constructs a new contact. */ |
| 140 | public Contact(Uri contactLookupUri, |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 141 | Uri phoneUri, |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 142 | String name, |
| 143 | String phoneNumber, |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 144 | String phoneType, |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 145 | Bitmap photo) { |
| 146 | mContactLookupUri = contactLookupUri; |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 147 | mPhoneUri = phoneUri; |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 148 | mName = name; |
| 149 | mPhoneNumber = phoneNumber; |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 150 | mPhoneType = phoneType; |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 151 | mPhoto = photo; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 152 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 153 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 154 | /** Returns the contact's CONTENT_LOOKUP_URI. Use this to display the contact. */ |
| 155 | public Uri getContactLookupUri() { |
| 156 | return mContactLookupUri; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 157 | } |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 160 | * The phone uri as defined in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 161 | * this to reload the contact. This links to a particular phone number of the emergency |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 162 | * contact. |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 163 | */ |
mariagpuyol | 08e2e51 | 2017-03-08 10:00:15 -0800 | [diff] [blame] | 164 | public Uri getPhoneUri() { |
| 165 | return mPhoneUri; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 166 | } |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 167 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 168 | /** Returns the display name of the contact. */ |
| 169 | public String getName() { |
| 170 | return mName; |
| 171 | } |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 172 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 173 | /** Returns the phone number selected by the user. */ |
| 174 | public String getPhoneNumber() { |
| 175 | return mPhoneNumber; |
| 176 | } |
| 177 | |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 178 | /** Returns the phone type (e.g. mobile, work, home, etc.) . */ |
| 179 | public String getPhoneType() { |
| 180 | return mPhoneType; |
| 181 | } |
| 182 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 183 | /** Returns the photo assigned to this contact. */ |
| 184 | public Bitmap getPhoto() { |
| 185 | return mPhoto; |
| 186 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 187 | } |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame] | 188 | } |