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 | 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 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 33 | /** |
| 34 | * Returns a {@link Contact} that contains all the relevant information of the contact indexed |
| 35 | * by {@code @contactUri}. |
| 36 | */ |
| 37 | public static Contact getContact(Context context, Uri contactUri) { |
| 38 | String phoneNumber = null; |
| 39 | String name = null; |
| 40 | Bitmap photo = null; |
| 41 | final Uri contactLookupUri = |
| 42 | ContactsContract.Contacts.getLookupUri(context.getContentResolver(), |
| 43 | contactUri); |
| 44 | Cursor cursor = context.getContentResolver().query( |
| 45 | contactUri, |
| 46 | new String[]{ContactsContract.Contacts.DISPLAY_NAME, |
| 47 | ContactsContract.CommonDataKinds.Phone.NUMBER, |
| 48 | ContactsContract.CommonDataKinds.Photo.PHOTO_ID}, |
| 49 | null, null, null); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 50 | try { |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 51 | if (cursor.moveToNext()) { |
| 52 | name = cursor.getString(0); |
| 53 | phoneNumber = cursor.getString(1); |
| 54 | Long photoId = cursor.getLong(2); |
| 55 | if (photoId != null && photoId > 0) { |
| 56 | Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, |
| 57 | photoId); |
| 58 | Cursor cursor2 = context.getContentResolver().query( |
| 59 | photoUri, |
| 60 | new String[]{ContactsContract.Contacts.Photo.PHOTO}, |
| 61 | null, null, null); |
| 62 | try { |
| 63 | if (cursor2.moveToNext()) { |
| 64 | byte[] data = cursor2.getBlob(0); |
| 65 | photo = BitmapFactory.decodeStream(new ByteArrayInputStream(data)); |
| 66 | } |
| 67 | } finally { |
| 68 | if (cursor2 != null) { |
| 69 | cursor2.close(); |
| 70 | } |
| 71 | } |
| 72 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 73 | } |
| 74 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 75 | if (cursor != null) { |
| 76 | cursor.close(); |
| 77 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 78 | } |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 79 | return new Contact(contactLookupUri, contactUri, name, phoneNumber, photo); |
| 80 | } |
| 81 | |
| 82 | /** Returns whether the contact uri is not null and corresponds to an existing contact. */ |
| 83 | public static boolean isValidEmergencyContact(Context context, Uri contactUri) { |
| 84 | return contactUri != null && contactExists(context, contactUri); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private static boolean contactExists(Context context, Uri contactUri) { |
| 88 | Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null); |
| 89 | try { |
| 90 | if (cursor != null && cursor.moveToFirst()) { |
| 91 | return true; |
| 92 | } |
| 93 | } finally { |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 94 | if (cursor != null) { |
| 95 | cursor.close(); |
| 96 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 101 | /** Wrapper for a contact with a phone number. */ |
| 102 | public static class Contact { |
| 103 | /** The lookup uri is necessary to display the contact. */ |
| 104 | private final Uri mContactLookupUri; |
| 105 | /** |
| 106 | * The contact uri is associated to a particular phone number and can be used to reload that |
| 107 | * number and keep the number displayed in the preferences fresh. |
| 108 | */ |
| 109 | private final Uri mContactUri; |
| 110 | /** The display name of the contact. */ |
| 111 | private final String mName; |
| 112 | /** The emergency contact's phone number selected by the user. */ |
| 113 | private final String mPhoneNumber; |
| 114 | /** The contact's photo. */ |
| 115 | private final Bitmap mPhoto; |
mariagpuyol | 0504a44 | 2016-02-17 16:22:57 -0800 | [diff] [blame] | 116 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 117 | /** Constructs a new contact. */ |
| 118 | public Contact(Uri contactLookupUri, |
| 119 | Uri contactUri, |
| 120 | String name, |
| 121 | String phoneNumber, |
| 122 | Bitmap photo) { |
| 123 | mContactLookupUri = contactLookupUri; |
| 124 | mContactUri = contactUri; |
| 125 | mName = name; |
| 126 | mPhoneNumber = phoneNumber; |
| 127 | mPhoto = photo; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 128 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 129 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 130 | /** Returns the contact's CONTENT_LOOKUP_URI. Use this to display the contact. */ |
| 131 | public Uri getContactLookupUri() { |
| 132 | return mContactLookupUri; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 133 | } |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 134 | |
| 135 | /** |
| 136 | * The contact uri as defined in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use |
| 137 | * this to reload the contact. This links to a particular phone number of the emergency |
| 138 | * contact |
| 139 | */ |
| 140 | public Uri getContactUri() { |
| 141 | return mContactUri; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 142 | } |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 143 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 144 | /** Returns the display name of the contact. */ |
| 145 | public String getName() { |
| 146 | return mName; |
| 147 | } |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame] | 148 | |
mariagpuyol | 5cf7f73 | 2016-03-08 09:48:15 -0800 | [diff] [blame^] | 149 | /** Returns the phone number selected by the user. */ |
| 150 | public String getPhoneNumber() { |
| 151 | return mPhoneNumber; |
| 152 | } |
| 153 | |
| 154 | /** Returns the photo assigned to this contact. */ |
| 155 | public Bitmap getPhoto() { |
| 156 | return mPhoto; |
| 157 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 158 | } |
mariagpuyol | bc6555b | 2016-02-26 15:38:19 -0800 | [diff] [blame] | 159 | } |