blob: b7cce210f136f7ca10786f508da4de88442a4ab2 [file] [log] [blame]
mariagpuyolaee23782016-02-16 13:29:49 -08001/*
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 */
16package com.android.emergency;
17
mariagpuyol5cf7f732016-03-08 09:48:15 -080018import android.content.ContentUris;
mariagpuyolaee23782016-02-16 13:29:49 -080019import android.content.Context;
mariagpuyolaee23782016-02-16 13:29:49 -080020import android.database.Cursor;
mariagpuyol95dc0402016-02-17 11:12:46 -080021import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
mariagpuyol348b0472017-02-27 14:31:18 -080023import com.android.internal.logging.MetricsLogger;
24import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
mariagpuyolaee23782016-02-16 13:29:49 -080025import android.net.Uri;
26import android.provider.ContactsContract;
mariagpuyol348b0472017-02-27 14:31:18 -080027import android.util.Log;
mariagpuyolaee23782016-02-16 13:29:49 -080028
mariagpuyol95dc0402016-02-17 11:12:46 -080029import java.io.ByteArrayInputStream;
mariagpuyolaee23782016-02-16 13:29:49 -080030
31/**
mariagpuyol609f68a2016-02-22 17:45:47 -080032 * Provides methods to read name, phone number, photo, etc. from contacts.
mariagpuyolaee23782016-02-16 13:29:49 -080033 */
34public class EmergencyContactManager {
mariagpuyol348b0472017-02-27 14:31:18 -080035 private static final String TAG = "EmergencyContactManager";
mariagpuyolaee23782016-02-16 13:29:49 -080036
mariagpuyol5cf7f732016-03-08 09:48:15 -080037 /**
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;
mariagpuyolf9ab89a2016-03-10 09:19:07 -080043 String phoneType = null;
mariagpuyol5cf7f732016-03-08 09:48:15 -080044 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,
mariagpuyolf9ab89a2016-03-10 09:19:07 -080053 ContactsContract.CommonDataKinds.Phone.TYPE,
54 ContactsContract.CommonDataKinds.Phone.LABEL,
mariagpuyol5cf7f732016-03-08 09:48:15 -080055 ContactsContract.CommonDataKinds.Photo.PHOTO_ID},
56 null, null, null);
mariagpuyolaee23782016-02-16 13:29:49 -080057 try {
mariagpuyol5cf7f732016-03-08 09:48:15 -080058 if (cursor.moveToNext()) {
59 name = cursor.getString(0);
60 phoneNumber = cursor.getString(1);
mariagpuyolf9ab89a2016-03-10 09:19:07 -080061 phoneType = ContactsContract.CommonDataKinds.Phone.getTypeLabel(
62 context.getResources(),
63 cursor.getInt(2),
64 cursor.getString(3)).toString();
65 Long photoId = cursor.getLong(4);
mariagpuyol5cf7f732016-03-08 09:48:15 -080066 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 }
mariagpuyolaee23782016-02-16 13:29:49 -080084 }
85 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -080086 if (cursor != null) {
87 cursor.close();
88 }
mariagpuyolaee23782016-02-16 13:29:49 -080089 }
mariagpuyolf9ab89a2016-03-10 09:19:07 -080090 return new Contact(contactLookupUri, contactUri, name, phoneNumber, phoneType, photo);
mariagpuyol5cf7f732016-03-08 09:48:15 -080091 }
92
mariagpuyol348b0472017-02-27 14:31:18 -080093 /** 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);
mariagpuyolaee23782016-02-16 13:29:49 -080096 }
97
mariagpuyol348b0472017-02-27 14:31:18 -080098 private static boolean phoneExists(Context context, Uri phoneUri) {
99 Cursor cursor = null;
mariagpuyolaee23782016-02-16 13:29:49 -0800100 try {
mariagpuyol348b0472017-02-27 14:31:18 -0800101 cursor = context.getContentResolver().query(phoneUri, null, null, null, null);
mariagpuyolaee23782016-02-16 13:29:49 -0800102 if (cursor != null && cursor.moveToFirst()) {
mariagpuyol348b0472017-02-27 14:31:18 -0800103 MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 1);
mariagpuyolaee23782016-02-16 13:29:49 -0800104 return true;
105 }
mariagpuyol348b0472017-02-27 14:31:18 -0800106 } 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;
mariagpuyolaee23782016-02-16 13:29:49 -0800110 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -0800111 if (cursor != null) {
112 cursor.close();
113 }
mariagpuyolaee23782016-02-16 13:29:49 -0800114 }
mariagpuyol348b0472017-02-27 14:31:18 -0800115 MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 0);
mariagpuyolaee23782016-02-16 13:29:49 -0800116 return false;
117 }
118
mariagpuyol5cf7f732016-03-08 09:48:15 -0800119 /** 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;
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800132 /** The emergency contact's phone number type (mobile, work, home, etc). */
133 private final String mPhoneType;
mariagpuyol5cf7f732016-03-08 09:48:15 -0800134 /** The contact's photo. */
135 private final Bitmap mPhoto;
mariagpuyol0504a442016-02-17 16:22:57 -0800136
mariagpuyol5cf7f732016-03-08 09:48:15 -0800137 /** Constructs a new contact. */
138 public Contact(Uri contactLookupUri,
139 Uri contactUri,
140 String name,
141 String phoneNumber,
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800142 String phoneType,
mariagpuyol5cf7f732016-03-08 09:48:15 -0800143 Bitmap photo) {
144 mContactLookupUri = contactLookupUri;
145 mContactUri = contactUri;
146 mName = name;
147 mPhoneNumber = phoneNumber;
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800148 mPhoneType = phoneType;
mariagpuyol5cf7f732016-03-08 09:48:15 -0800149 mPhoto = photo;
mariagpuyolaee23782016-02-16 13:29:49 -0800150 }
mariagpuyolaee23782016-02-16 13:29:49 -0800151
mariagpuyol5cf7f732016-03-08 09:48:15 -0800152 /** Returns the contact's CONTENT_LOOKUP_URI. Use this to display the contact. */
153 public Uri getContactLookupUri() {
154 return mContactLookupUri;
mariagpuyol95dc0402016-02-17 11:12:46 -0800155 }
mariagpuyol5cf7f732016-03-08 09:48:15 -0800156
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;
mariagpuyol95dc0402016-02-17 11:12:46 -0800164 }
mariagpuyol95dc0402016-02-17 11:12:46 -0800165
mariagpuyol5cf7f732016-03-08 09:48:15 -0800166 /** Returns the display name of the contact. */
167 public String getName() {
168 return mName;
169 }
mariagpuyol95dc0402016-02-17 11:12:46 -0800170
mariagpuyol5cf7f732016-03-08 09:48:15 -0800171 /** Returns the phone number selected by the user. */
172 public String getPhoneNumber() {
173 return mPhoneNumber;
174 }
175
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800176 /** Returns the phone type (e.g. mobile, work, home, etc.) . */
177 public String getPhoneType() {
178 return mPhoneType;
179 }
180
mariagpuyol5cf7f732016-03-08 09:48:15 -0800181 /** Returns the photo assigned to this contact. */
182 public Bitmap getPhoto() {
183 return mPhoto;
184 }
mariagpuyolaee23782016-02-16 13:29:49 -0800185 }
mariagpuyol348b0472017-02-27 14:31:18 -0800186}