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.ContentUris; 19 import android.content.Context; 20 import android.database.Cursor; 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.net.Uri; 24 import android.provider.ContactsContract; 25 26 import java.io.ByteArrayInputStream; 27 28 /** 29 * Provides methods to read name, phone number, photo, etc. from contacts. 30 */ 31 public class EmergencyContactManager { 32 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 phoneType = null; 40 String name = null; 41 Bitmap photo = null; 42 final Uri contactLookupUri = 43 ContactsContract.Contacts.getLookupUri(context.getContentResolver(), 44 contactUri); 45 Cursor cursor = context.getContentResolver().query( 46 contactUri, 47 new String[]{ContactsContract.Contacts.DISPLAY_NAME, 48 ContactsContract.CommonDataKinds.Phone.NUMBER, 49 ContactsContract.CommonDataKinds.Phone.TYPE, 50 ContactsContract.CommonDataKinds.Phone.LABEL, 51 ContactsContract.CommonDataKinds.Photo.PHOTO_ID}, 52 null, null, null); 53 try { 54 if (cursor.moveToNext()) { 55 name = cursor.getString(0); 56 phoneNumber = cursor.getString(1); 57 phoneType = ContactsContract.CommonDataKinds.Phone.getTypeLabel( 58 context.getResources(), 59 cursor.getInt(2), 60 cursor.getString(3)).toString(); 61 Long photoId = cursor.getLong(4); 62 if (photoId != null && photoId > 0) { 63 Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, 64 photoId); 65 Cursor cursor2 = context.getContentResolver().query( 66 photoUri, 67 new String[]{ContactsContract.Contacts.Photo.PHOTO}, 68 null, null, null); 69 try { 70 if (cursor2.moveToNext()) { 71 byte[] data = cursor2.getBlob(0); 72 photo = BitmapFactory.decodeStream(new ByteArrayInputStream(data)); 73 } 74 } finally { 75 if (cursor2 != null) { 76 cursor2.close(); 77 } 78 } 79 } 80 } 81 } finally { 82 if (cursor != null) { 83 cursor.close(); 84 } 85 } 86 return new Contact(contactLookupUri, contactUri, name, phoneNumber, phoneType, photo); 87 } 88 89 /** Returns whether the contact uri is not null and corresponds to an existing contact. */ 90 public static boolean isValidEmergencyContact(Context context, Uri contactUri) { 91 return contactUri != null && contactExists(context, contactUri); 92 } 93 94 private static boolean contactExists(Context context, Uri contactUri) { 95 Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null); 96 try { 97 if (cursor != null && cursor.moveToFirst()) { 98 return true; 99 } 100 } finally { 101 if (cursor != null) { 102 cursor.close(); 103 } 104 } 105 return false; 106 } 107 108 /** Wrapper for a contact with a phone number. */ 109 public static class Contact { 110 /** The lookup uri is necessary to display the contact. */ 111 private final Uri mContactLookupUri; 112 /** 113 * The contact uri is associated to a particular phone number and can be used to reload that 114 * number and keep the number displayed in the preferences fresh. 115 */ 116 private final Uri mContactUri; 117 /** The display name of the contact. */ 118 private final String mName; 119 /** The emergency contact's phone number selected by the user. */ 120 private final String mPhoneNumber; 121 /** The emergency contact's phone number type (mobile, work, home, etc). */ 122 private final String mPhoneType; 123 /** The contact's photo. */ 124 private final Bitmap mPhoto; 125 126 /** Constructs a new contact. */ 127 public Contact(Uri contactLookupUri, 128 Uri contactUri, 129 String name, 130 String phoneNumber, 131 String phoneType, 132 Bitmap photo) { 133 mContactLookupUri = contactLookupUri; 134 mContactUri = contactUri; 135 mName = name; 136 mPhoneNumber = phoneNumber; 137 mPhoneType = phoneType; 138 mPhoto = photo; 139 } 140 141 /** Returns the contact's CONTENT_LOOKUP_URI. Use this to display the contact. */ 142 public Uri getContactLookupUri() { 143 return mContactLookupUri; 144 } 145 146 /** 147 * The contact uri as defined in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use 148 * this to reload the contact. This links to a particular phone number of the emergency 149 * contact 150 */ 151 public Uri getContactUri() { 152 return mContactUri; 153 } 154 155 /** Returns the display name of the contact. */ 156 public String getName() { 157 return mName; 158 } 159 160 /** Returns the phone number selected by the user. */ 161 public String getPhoneNumber() { 162 return mPhoneNumber; 163 } 164 165 /** Returns the phone type (e.g. mobile, work, home, etc.) . */ 166 public String getPhoneType() { 167 return mPhoneType; 168 } 169 170 /** Returns the photo assigned to this contact. */ 171 public Bitmap getPhoto() { 172 return mPhoto; 173 } 174 } 175 }