blob: 0ce3501b37ab6d2f47c3c733e91a9e6332f0cade [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
mariagpuyol08e2e512017-03-08 10:00:15 -080039 * by {@code @phoneUri}.
mariagpuyol5cf7f732016-03-08 09:48:15 -080040 */
mariagpuyol08e2e512017-03-08 10:00:15 -080041 public static Contact getContact(Context context, Uri phoneUri) {
mariagpuyol5cf7f732016-03-08 09:48:15 -080042 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(),
mariagpuyol08e2e512017-03-08 10:00:15 -080048 phoneUri);
mariagpuyol5cf7f732016-03-08 09:48:15 -080049 Cursor cursor = context.getContentResolver().query(
mariagpuyol08e2e512017-03-08 10:00:15 -080050 phoneUri,
mariagpuyol5cf7f732016-03-08 09:48:15 -080051 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);
mariagpuyol08e2e512017-03-08 10:00:15 -080076 if (data != null) {
77 photo = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
78 }
mariagpuyol5cf7f732016-03-08 09:48:15 -080079 }
80 } finally {
81 if (cursor2 != null) {
82 cursor2.close();
83 }
84 }
85 }
mariagpuyolaee23782016-02-16 13:29:49 -080086 }
87 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -080088 if (cursor != null) {
89 cursor.close();
90 }
mariagpuyolaee23782016-02-16 13:29:49 -080091 }
mariagpuyol08e2e512017-03-08 10:00:15 -080092 return new Contact(contactLookupUri, phoneUri, name, phoneNumber, phoneType, photo);
mariagpuyol5cf7f732016-03-08 09:48:15 -080093 }
94
mariagpuyol348b0472017-02-27 14:31:18 -080095 /** 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);
mariagpuyolaee23782016-02-16 13:29:49 -080098 }
99
mariagpuyol348b0472017-02-27 14:31:18 -0800100 private static boolean phoneExists(Context context, Uri phoneUri) {
mariagpuyol08e2e512017-03-08 10:00:15 -0800101 Cursor cursor = null;
mariagpuyolaee23782016-02-16 13:29:49 -0800102 try {
mariagpuyol348b0472017-02-27 14:31:18 -0800103 cursor = context.getContentResolver().query(phoneUri, null, null, null, null);
mariagpuyolaee23782016-02-16 13:29:49 -0800104 if (cursor != null && cursor.moveToFirst()) {
mariagpuyol348b0472017-02-27 14:31:18 -0800105 MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 1);
mariagpuyolaee23782016-02-16 13:29:49 -0800106 return true;
107 }
mariagpuyol348b0472017-02-27 14:31:18 -0800108 } 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;
mariagpuyolaee23782016-02-16 13:29:49 -0800112 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -0800113 if (cursor != null) {
114 cursor.close();
115 }
mariagpuyolaee23782016-02-16 13:29:49 -0800116 }
mariagpuyol348b0472017-02-27 14:31:18 -0800117 MetricsLogger.action(context, MetricsEvent.ACTION_PHONE_EXISTS, 0);
mariagpuyolaee23782016-02-16 13:29:49 -0800118 return false;
119 }
120
mariagpuyol5cf7f732016-03-08 09:48:15 -0800121 /** 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 */
mariagpuyol08e2e512017-03-08 10:00:15 -0800129 private final Uri mPhoneUri;
mariagpuyol5cf7f732016-03-08 09:48:15 -0800130 /** 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;
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800134 /** The emergency contact's phone number type (mobile, work, home, etc). */
135 private final String mPhoneType;
mariagpuyol5cf7f732016-03-08 09:48:15 -0800136 /** The contact's photo. */
137 private final Bitmap mPhoto;
mariagpuyol0504a442016-02-17 16:22:57 -0800138
mariagpuyol5cf7f732016-03-08 09:48:15 -0800139 /** Constructs a new contact. */
140 public Contact(Uri contactLookupUri,
mariagpuyol08e2e512017-03-08 10:00:15 -0800141 Uri phoneUri,
mariagpuyol5cf7f732016-03-08 09:48:15 -0800142 String name,
143 String phoneNumber,
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800144 String phoneType,
mariagpuyol5cf7f732016-03-08 09:48:15 -0800145 Bitmap photo) {
146 mContactLookupUri = contactLookupUri;
mariagpuyol08e2e512017-03-08 10:00:15 -0800147 mPhoneUri = phoneUri;
mariagpuyol5cf7f732016-03-08 09:48:15 -0800148 mName = name;
149 mPhoneNumber = phoneNumber;
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800150 mPhoneType = phoneType;
mariagpuyol5cf7f732016-03-08 09:48:15 -0800151 mPhoto = photo;
mariagpuyolaee23782016-02-16 13:29:49 -0800152 }
mariagpuyolaee23782016-02-16 13:29:49 -0800153
mariagpuyol5cf7f732016-03-08 09:48:15 -0800154 /** Returns the contact's CONTENT_LOOKUP_URI. Use this to display the contact. */
155 public Uri getContactLookupUri() {
156 return mContactLookupUri;
mariagpuyol95dc0402016-02-17 11:12:46 -0800157 }
mariagpuyol5cf7f732016-03-08 09:48:15 -0800158
159 /**
mariagpuyol08e2e512017-03-08 10:00:15 -0800160 * The phone uri as defined in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use
mariagpuyol5cf7f732016-03-08 09:48:15 -0800161 * this to reload the contact. This links to a particular phone number of the emergency
mariagpuyol08e2e512017-03-08 10:00:15 -0800162 * contact.
mariagpuyol5cf7f732016-03-08 09:48:15 -0800163 */
mariagpuyol08e2e512017-03-08 10:00:15 -0800164 public Uri getPhoneUri() {
165 return mPhoneUri;
mariagpuyol95dc0402016-02-17 11:12:46 -0800166 }
mariagpuyol95dc0402016-02-17 11:12:46 -0800167
mariagpuyol5cf7f732016-03-08 09:48:15 -0800168 /** Returns the display name of the contact. */
169 public String getName() {
170 return mName;
171 }
mariagpuyol95dc0402016-02-17 11:12:46 -0800172
mariagpuyol5cf7f732016-03-08 09:48:15 -0800173 /** Returns the phone number selected by the user. */
174 public String getPhoneNumber() {
175 return mPhoneNumber;
176 }
177
mariagpuyolf9ab89a2016-03-10 09:19:07 -0800178 /** Returns the phone type (e.g. mobile, work, home, etc.) . */
179 public String getPhoneType() {
180 return mPhoneType;
181 }
182
mariagpuyol5cf7f732016-03-08 09:48:15 -0800183 /** Returns the photo assigned to this contact. */
184 public Bitmap getPhoto() {
185 return mPhoto;
186 }
mariagpuyolaee23782016-02-16 13:29:49 -0800187 }
mariagpuyol348b0472017-02-27 14:31:18 -0800188}