blob: 34983b6a945f798c7f9e12fe3526cd8904d5d25a [file] [log] [blame]
Akshay Kannancdb6c142016-01-20 18:25:44 -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
Akshay Kannan44ef39d2016-01-26 09:08:41 -080018import android.Manifest;
Akshay Kannan44ef39d2016-01-26 09:08:41 -080019import android.app.AlertDialog;
Akshay Kannancdb6c142016-01-20 18:25:44 -080020import android.content.Context;
mariagpuyold19ace52016-02-12 10:37:26 -080021import android.content.DialogInterface;
Akshay Kannancdb6c142016-01-20 18:25:44 -080022import android.content.Intent;
Akshay Kannan44ef39d2016-01-26 09:08:41 -080023import android.content.pm.PackageManager;
mariagpuyol95dc0402016-02-17 11:12:46 -080024import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.graphics.drawable.Drawable;
Akshay Kannancdb6c142016-01-20 18:25:44 -080027import android.net.Uri;
28import android.preference.Preference;
mariagpuyolaee23782016-02-16 13:29:49 -080029import android.support.annotation.NonNull;
mariagpuyol16472ca2016-02-16 17:49:26 -080030import android.support.annotation.Nullable;
Akshay Kannan44ef39d2016-01-26 09:08:41 -080031import android.support.v4.app.ActivityCompat;
mariagpuyold19ace52016-02-12 10:37:26 -080032import android.view.View;
mariagpuyold19ace52016-02-12 10:37:26 -080033
Akshay Kannan44ef39d2016-01-26 09:08:41 -080034import com.android.internal.logging.MetricsLogger;
35import com.android.internal.logging.MetricsProto.MetricsEvent;
mariagpuyol95dc0402016-02-17 11:12:46 -080036import com.android.settingslib.drawable.CircleFramedDrawable;
Akshay Kannan44ef39d2016-01-26 09:08:41 -080037
Akshay Kannancdb6c142016-01-20 18:25:44 -080038
39/**
40 * A {@link Preference} to display a contact using the specified URI string.
41 */
42public class ContactPreference extends Preference {
43
44 private final Uri mUri;
mariagpuyold19ace52016-02-12 10:37:26 -080045 private final DeleteContactListener mDeleteContactListener;
mariagpuyolaee23782016-02-16 13:29:49 -080046 private final String mName;
mariagpuyold19ace52016-02-12 10:37:26 -080047
48 /**
49 * Listener for deleting a contact.
50 */
51 public interface DeleteContactListener {
52 /**
53 * Callback to delete a contact.
54 */
mariagpuyolaee23782016-02-16 13:29:49 -080055 void onContactDelete(Uri contactUri);
mariagpuyold19ace52016-02-12 10:37:26 -080056 }
Akshay Kannancdb6c142016-01-20 18:25:44 -080057
58 /**
59 * Instantiates a ContactPreference that displays an emergency contact, taking in a Context and
mariagpuyolaee23782016-02-16 13:29:49 -080060 * the Uri, name and phone number of the contact and a listener to be informed when clicking on
61 * the delete icon.
Akshay Kannancdb6c142016-01-20 18:25:44 -080062 */
mariagpuyolaee23782016-02-16 13:29:49 -080063 public ContactPreference(Context context,
64 @NonNull Uri contactUri,
65 @NonNull String contactName,
mariagpuyol16472ca2016-02-16 17:49:26 -080066 @Nullable DeleteContactListener deleteContactListener) {
Akshay Kannancdb6c142016-01-20 18:25:44 -080067 super(context);
mariagpuyolaee23782016-02-16 13:29:49 -080068 mUri = contactUri;
69 mName = contactName;
mariagpuyold19ace52016-02-12 10:37:26 -080070 mDeleteContactListener = deleteContactListener;
mariagpuyolaee23782016-02-16 13:29:49 -080071 setTitle(mName);
mariagpuyold19ace52016-02-12 10:37:26 -080072 setWidgetLayoutResource(R.layout.preference_user_delete_widget);
mariagpuyol95dc0402016-02-17 11:12:46 -080073
74 //TODO: Consider doing the following in a non-UI thread.
75 Bitmap photo = EmergencyContactManager.getContactPhoto(context, mUri);
76 if (photo == null) {
77 photo = convertToBitmap(context.getResources().getDrawable(
78 R.drawable.ic_account_circle));
79 }
80 Drawable icon = new CircleFramedDrawable(photo,
81 (int) context.getResources().getDimension(R.dimen.circle_avatar_size));
82 setIcon(icon);
83 }
84
85 /**
86 * Converts a given drawable icon to a bitmap.
87 */
88 private static Bitmap convertToBitmap(Drawable icon) {
89 if (icon == null) {
90 return null;
91 }
92 int width = icon.getIntrinsicWidth();
93 int height = icon.getIntrinsicHeight();
94 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
95 Canvas canvas = new Canvas(bitmap);
96 icon.setBounds(0, 0, width, height);
97 icon.draw(canvas);
98 return bitmap;
mariagpuyold19ace52016-02-12 10:37:26 -080099 }
100
101 @Override
102 protected void onBindView(View view) {
103 super.onBindView(view);
104 View deleteContactIcon = view.findViewById(R.id.delete_contact);
mariagpuyol16472ca2016-02-16 17:49:26 -0800105 if (mDeleteContactListener == null) {
106 deleteContactIcon.setVisibility(View.GONE);
107 } else {
108 if (deleteContactIcon != null) {
109 deleteContactIcon.setOnClickListener(new View.OnClickListener() {
110 @Override
111 public void onClick(View view) {
112 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
113 builder.setMessage(String.format(getContext()
114 .getString(R.string.remove_contact), mName));
115 builder.setPositiveButton(getContext().getString(R.string.remove),
116 new DialogInterface.OnClickListener() {
117 @Override
118 public void onClick(DialogInterface dialogInterface,
119 int which) {
120 mDeleteContactListener.onContactDelete(mUri);
121 }
122 }).setNegativeButton(getContext().getString(R.string.cancel), null);
123 builder.create().show();
124 }
125 });
126 }
mariagpuyold19ace52016-02-12 10:37:26 -0800127 }
Akshay Kannancdb6c142016-01-20 18:25:44 -0800128 }
129
130 /**
Akshay Kannan44ef39d2016-01-26 09:08:41 -0800131 * Calls the contact.
132 */
133 public void callContact() {
mariagpuyolaee23782016-02-16 13:29:49 -0800134 String phoneNumber = EmergencyContactManager.getNumber(getContext(), mUri);
135 if (phoneNumber != null) {
Akshay Kannan44ef39d2016-01-26 09:08:41 -0800136 if (ActivityCompat.checkSelfPermission(getContext(),
137 Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
mariagpuyolaee23782016-02-16 13:29:49 -0800138 Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
Akshay Kannan44ef39d2016-01-26 09:08:41 -0800139 MetricsLogger.action(getContext(), MetricsEvent.ACTION_CALL_EMERGENCY_CONTACT);
140 getContext().startActivity(callIntent);
141 }
mariagpuyolaee23782016-02-16 13:29:49 -0800142 } else {
143 // TODO: Show dialog saying that there is no number.
Akshay Kannan44ef39d2016-01-26 09:08:41 -0800144 }
145 }
146
147 /**
Akshay Kannancdb6c142016-01-20 18:25:44 -0800148 * Displays a contact card for the contact.
149 */
150 public void displayContact() {
151 Intent contactIntent = new Intent(Intent.ACTION_VIEW);
mariagpuyold19ace52016-02-12 10:37:26 -0800152 contactIntent.setData(mUri);
Akshay Kannancdb6c142016-01-20 18:25:44 -0800153 getContext().startActivity(contactIntent);
154 }
Akshay Kannancdb6c142016-01-20 18:25:44 -0800155}