blob: 342ff2dcc2ff680088eb735039ce4d4e076553a0 [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;
mariagpuyolaee23782016-02-16 13:29:49 -080023import android.net.Uri;
24import android.provider.ContactsContract;
mariagpuyolaee23782016-02-16 13:29:49 -080025
mariagpuyol95dc0402016-02-17 11:12:46 -080026import java.io.ByteArrayInputStream;
mariagpuyolaee23782016-02-16 13:29:49 -080027
28/**
mariagpuyol609f68a2016-02-22 17:45:47 -080029 * Provides methods to read name, phone number, photo, etc. from contacts.
mariagpuyolaee23782016-02-16 13:29:49 -080030 */
31public class EmergencyContactManager {
mariagpuyolaee23782016-02-16 13:29:49 -080032
mariagpuyol5cf7f732016-03-08 09:48:15 -080033 /**
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);
mariagpuyolaee23782016-02-16 13:29:49 -080050 try {
mariagpuyol5cf7f732016-03-08 09:48:15 -080051 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 }
mariagpuyolaee23782016-02-16 13:29:49 -080073 }
74 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -080075 if (cursor != null) {
76 cursor.close();
77 }
mariagpuyolaee23782016-02-16 13:29:49 -080078 }
mariagpuyol5cf7f732016-03-08 09:48:15 -080079 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);
mariagpuyolaee23782016-02-16 13:29:49 -080085 }
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 {
mariagpuyol0504a442016-02-17 16:22:57 -080094 if (cursor != null) {
95 cursor.close();
96 }
mariagpuyolaee23782016-02-16 13:29:49 -080097 }
98 return false;
99 }
100
mariagpuyol5cf7f732016-03-08 09:48:15 -0800101 /** 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;
mariagpuyol0504a442016-02-17 16:22:57 -0800116
mariagpuyol5cf7f732016-03-08 09:48:15 -0800117 /** 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;
mariagpuyolaee23782016-02-16 13:29:49 -0800128 }
mariagpuyolaee23782016-02-16 13:29:49 -0800129
mariagpuyol5cf7f732016-03-08 09:48:15 -0800130 /** Returns the contact's CONTENT_LOOKUP_URI. Use this to display the contact. */
131 public Uri getContactLookupUri() {
132 return mContactLookupUri;
mariagpuyol95dc0402016-02-17 11:12:46 -0800133 }
mariagpuyol5cf7f732016-03-08 09:48:15 -0800134
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;
mariagpuyol95dc0402016-02-17 11:12:46 -0800142 }
mariagpuyol95dc0402016-02-17 11:12:46 -0800143
mariagpuyol5cf7f732016-03-08 09:48:15 -0800144 /** Returns the display name of the contact. */
145 public String getName() {
146 return mName;
147 }
mariagpuyol95dc0402016-02-17 11:12:46 -0800148
mariagpuyol5cf7f732016-03-08 09:48:15 -0800149 /** 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 }
mariagpuyolaee23782016-02-16 13:29:49 -0800158 }
mariagpuyolbc6555b2016-02-26 15:38:19 -0800159}