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 |
| 39 | * by {@code @contactUri}. |
| 40 | */ |
| 41 | public static Contact getContact(Context context, Uri contactUri) { |
| 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(), |
| 48 | contactUri); |
| 49 | Cursor cursor = context.getContentResolver().query( |
| 50 | contactUri, |
| 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); |
| 76 | photo = BitmapFactory.decodeStream(new ByteArrayInputStream(data)); |
| 77 | } |
| 78 | } finally { |
| 79 | if (cursor2 != null) { |
| 80 | cursor2.close(); |
| 81 | } |
| 82 | } |
| 83 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 84 | } |
| 85 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 86 | if (cursor != null) { |
| 87 | cursor.close(); |
| 88 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 89 | } |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 90 | return new Contact(contactLookupUri, contactUri, name, phoneNumber, phoneType, photo); |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 91 | } |
| 92 | |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame^] | 93 | /** Returns whether the phone uri is not null and corresponds to an existing phone number. */ |
| 94 | public static boolean isValidEmergencyContact(Context context, Uri phoneUri) { |
| 95 | return phoneUri != null && phoneExists(context, phoneUri); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 96 | } |
| 97 | |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame^] | 98 | private static boolean phoneExists(Context context, Uri phoneUri) { |
| 99 | Cursor cursor = null; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 100 | try { |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame^] | 101 | cursor = context.getContentResolver().query(phoneUri, null, null, null, null); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 102 | if (cursor != null && cursor.moveToFirst()) { |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame^] | 103 | MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 1); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 104 | return true; |
| 105 | } |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame^] | 106 | } catch (SecurityException e) { |
| 107 | Log.w(TAG, "Unable to read contact information", e); |
| 108 | MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 2); |
| 109 | return false; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 110 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 111 | if (cursor != null) { |
| 112 | cursor.close(); |
| 113 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 114 | } |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame^] | 115 | MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 0); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 119 | /** Wrapper for a contact with a phone number. */ |
| 120 | public static class Contact { |
| 121 | /** The lookup uri is necessary to display the contact. */ |
| 122 | private final Uri mContactLookupUri; |
| 123 | /** |
| 124 | * The contact uri is associated to a particular phone number and can be used to reload that |
| 125 | * number and keep the number displayed in the preferences fresh. |
| 126 | */ |
| 127 | private final Uri mContactUri; |
| 128 | /** The display name of the contact. */ |
| 129 | private final String mName; |
| 130 | /** The emergency contact's phone number selected by the user. */ |
| 131 | private final String mPhoneNumber; |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 132 | /** The emergency contact's phone number type (mobile, work, home, etc). */ |
| 133 | private final String mPhoneType; |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 134 | /** The contact's photo. */ |
| 135 | private final Bitmap mPhoto; |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 136 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 137 | /** Constructs a new contact. */ |
| 138 | public Contact(Uri contactLookupUri, |
| 139 | Uri contactUri, |
| 140 | String name, |
| 141 | String phoneNumber, |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 142 | String phoneType, |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 143 | Bitmap photo) { |
| 144 | mContactLookupUri = contactLookupUri; |
| 145 | mContactUri = contactUri; |
| 146 | mName = name; |
| 147 | mPhoneNumber = phoneNumber; |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 148 | mPhoneType = phoneType; |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 149 | mPhoto = photo; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 150 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 151 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 152 | /** Returns the contact's CONTENT_LOOKUP_URI. Use this to display the contact. */ |
| 153 | public Uri getContactLookupUri() { |
| 154 | return mContactLookupUri; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 155 | } |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * The contact uri as defined in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use |
| 159 | * this to reload the contact. This links to a particular phone number of the emergency |
| 160 | * contact |
| 161 | */ |
| 162 | public Uri getContactUri() { |
| 163 | return mContactUri; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 164 | } |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 165 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 166 | /** Returns the display name of the contact. */ |
| 167 | public String getName() { |
| 168 | return mName; |
| 169 | } |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 170 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 171 | /** Returns the phone number selected by the user. */ |
| 172 | public String getPhoneNumber() { |
| 173 | return mPhoneNumber; |
| 174 | } |
| 175 | |
mariagpuyol | f9ab89a | 2016-03-10 09:19:07 -0800 | [diff] [blame] | 176 | /** Returns the phone type (e.g. mobile, work, home, etc.) . */ |
| 177 | public String getPhoneType() { |
| 178 | return mPhoneType; |
| 179 | } |
| 180 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame] | 181 | /** Returns the photo assigned to this contact. */ |
| 182 | public Bitmap getPhoto() { |
| 183 | return mPhoto; |
| 184 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 185 | } |
mariagpuyol | 348b047 | 2017-02-27 14:31:18 -0800 | [diff] [blame^] | 186 | } |