blob: 764e4a71be83a9c946a2146f20b62749930fc41a [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
18import android.content.ContentResolver;
19import 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
33 /** Returns the display name of the contact. */
34 public static String getName(Context context, Uri contactUri) {
35 Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null);
36 try {
37 if (cursor != null && cursor.moveToFirst()) {
38 return cursor.getString(cursor.getColumnIndex(
39 ContactsContract.Contacts.DISPLAY_NAME));
40 }
41 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -080042 if (cursor != null) {
43 cursor.close();
44 }
mariagpuyolaee23782016-02-16 13:29:49 -080045 }
46 return null;
47 }
48
49 private static boolean contactExists(Context context, Uri contactUri) {
50 Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null);
51 try {
52 if (cursor != null && cursor.moveToFirst()) {
53 return true;
54 }
55 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -080056 if (cursor != null) {
57 cursor.close();
58 }
mariagpuyolaee23782016-02-16 13:29:49 -080059 }
60 return false;
61 }
62
mariagpuyol0504a442016-02-17 16:22:57 -080063
mariagpuyolaee23782016-02-16 13:29:49 -080064 /** Returns the phone number of the contact. */
mariagpuyol0504a442016-02-17 16:22:57 -080065 public static String[] getPhoneNumbers(Context context, Uri contactUri) {
66 // TODO: Investigate passing around CONTENT_LOOKUP_URI instead of content CONTENT_URI.
67 // The method to get the CONTENT_LOOKUP_URI when the user selects a contact is:
68 // ContactsContract.Contacts.getLookupUri(ContentResolver resolver, Uri contactUri)
69 // Then use ContactsContract.Contacts.lookupContact(ContentResolver resolver, Uri lookupUri)
70 // to get the CONTENT_URI
mariagpuyolaee23782016-02-16 13:29:49 -080071 ContentResolver contentResolver = context.getContentResolver();
72 Cursor contactCursor = contentResolver.query(contactUri, null, null, null, null);
73 try {
74 if (contactCursor != null && contactCursor.moveToFirst()) {
mariagpuyolaee23782016-02-16 13:29:49 -080075 if (contactCursor.getInt(contactCursor.getColumnIndex(
76 ContactsContract.Contacts.HAS_PHONE_NUMBER)) != 0) {
mariagpuyol0504a442016-02-17 16:22:57 -080077 String id = contactCursor.getString(
78 contactCursor.getColumnIndex(ContactsContract.Contacts._ID));
mariagpuyolaee23782016-02-16 13:29:49 -080079 Cursor phoneCursor = contentResolver.query(
mariagpuyol0504a442016-02-17 16:22:57 -080080 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
81 null,
82 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
83 null,
84 null);
mariagpuyolaee23782016-02-16 13:29:49 -080085 try {
86 if (phoneCursor != null && phoneCursor.moveToFirst()) {
mariagpuyol0504a442016-02-17 16:22:57 -080087 String[] phoneNumbers =
88 new String[phoneCursor.getCount()];
89 for (int i = 0; i < phoneCursor.getCount(); i++) {
90 String phoneNumber =
91 phoneCursor.getString(phoneCursor.getColumnIndex(
92 ContactsContract.CommonDataKinds.Phone.NUMBER));
93
94 phoneNumbers[i] = phoneNumber;
95 phoneCursor.moveToNext();
96 }
97 return phoneNumbers;
mariagpuyolaee23782016-02-16 13:29:49 -080098 }
99 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -0800100 if (phoneCursor != null) {
101 phoneCursor.close();
102 }
mariagpuyolaee23782016-02-16 13:29:49 -0800103 }
104 }
105 }
106 } finally {
mariagpuyol0504a442016-02-17 16:22:57 -0800107 if (contactCursor != null) {
108 contactCursor.close();
109 }
mariagpuyolaee23782016-02-16 13:29:49 -0800110 }
111 return null;
112 }
113
mariagpuyol95dc0402016-02-17 11:12:46 -0800114 /** Returns the Bitmap corresponding to the contact's photo. */
115 public static Bitmap getContactPhoto(Context context, Uri contactUri) {
116 Uri photoUri = Uri.withAppendedPath(contactUri,
117 ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
118 Cursor cursor = context.getContentResolver().query(photoUri,
119 new String[]{ContactsContract.Contacts.Photo.PHOTO}, null, null, null);
120 if (cursor == null) {
121 return null;
122 }
123 try {
124 if (cursor.moveToFirst()) {
125 byte[] data = cursor.getBlob(0);
126 if (data != null) {
127 return BitmapFactory.decodeStream(new ByteArrayInputStream(data));
128 }
129 }
130 } finally {
131 cursor.close();
132 }
133 return null;
134 }
135
136
mariagpuyolaee23782016-02-16 13:29:49 -0800137 /** Returns whether the contact uri is not null and corresponds to an existing contact. */
mariagpuyol609f68a2016-02-22 17:45:47 -0800138 public static boolean isValidEmergencyContact(Context context, Uri contactUri) {
139 return contactUri != null && contactExists(context, contactUri);
mariagpuyolaee23782016-02-16 13:29:49 -0800140 }
141}