blob: b136886b7649140ad8cb10e5add0a289b0dd1489 [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 */
mariagpuyol58748352016-03-09 15:36:28 -080016package com.android.emergency.preferences;
Akshay Kannancdb6c142016-01-20 18:25:44 -080017
Akshay Kannan44ef39d2016-01-26 09:08:41 -080018import android.app.AlertDialog;
Akshay Kannancdb6c142016-01-20 18:25:44 -080019import android.content.Context;
mariagpuyold19ace52016-02-12 10:37:26 -080020import android.content.DialogInterface;
Akshay Kannancdb6c142016-01-20 18:25:44 -080021import android.content.Intent;
mariagpuyol95dc0402016-02-17 11:12:46 -080022import android.graphics.drawable.Drawable;
Akshay Kannancdb6c142016-01-20 18:25:44 -080023import android.net.Uri;
24import android.preference.Preference;
mariagpuyolaee23782016-02-16 13:29:49 -080025import android.support.annotation.NonNull;
mariagpuyol16472ca2016-02-16 17:49:26 -080026import android.support.annotation.Nullable;
mariagpuyold19ace52016-02-12 10:37:26 -080027import android.view.View;
mariagpuyold19ace52016-02-12 10:37:26 -080028
mariagpuyol58748352016-03-09 15:36:28 -080029import com.android.emergency.EmergencyContactManager;
30import com.android.emergency.PhotoUtils;
31import com.android.emergency.R;
Akshay Kannan44ef39d2016-01-26 09:08:41 -080032import com.android.internal.logging.MetricsLogger;
33import com.android.internal.logging.MetricsProto.MetricsEvent;
34
Akshay Kannancdb6c142016-01-20 18:25:44 -080035
36/**
mariagpuyol5cf7f732016-03-08 09:48:15 -080037 * A {@link Preference} to display or call a contact using the specified URI string.
Akshay Kannancdb6c142016-01-20 18:25:44 -080038 */
39public class ContactPreference extends Preference {
40
mariagpuyol5cf7f732016-03-08 09:48:15 -080041 private final EmergencyContactManager.Contact mContact;
mariagpuyol609f68a2016-02-22 17:45:47 -080042 @Nullable private RemoveContactPreferenceListener mRemoveContactPreferenceListener;
mariagpuyold19ace52016-02-12 10:37:26 -080043
44 /**
mariagpuyol609f68a2016-02-22 17:45:47 -080045 * Listener for removing a contact.
mariagpuyold19ace52016-02-12 10:37:26 -080046 */
mariagpuyol609f68a2016-02-22 17:45:47 -080047 public interface RemoveContactPreferenceListener {
mariagpuyold19ace52016-02-12 10:37:26 -080048 /**
mariagpuyol609f68a2016-02-22 17:45:47 -080049 * Callback to remove a contact preference.
mariagpuyold19ace52016-02-12 10:37:26 -080050 */
mariagpuyol609f68a2016-02-22 17:45:47 -080051 void onRemoveContactPreference(ContactPreference preference);
mariagpuyold19ace52016-02-12 10:37:26 -080052 }
Akshay Kannancdb6c142016-01-20 18:25:44 -080053
54 /**
55 * Instantiates a ContactPreference that displays an emergency contact, taking in a Context and
mariagpuyol5cf7f732016-03-08 09:48:15 -080056 * the Uri.
Akshay Kannancdb6c142016-01-20 18:25:44 -080057 */
mariagpuyol5cf7f732016-03-08 09:48:15 -080058 public ContactPreference(Context context, @NonNull Uri contactUri) {
Akshay Kannancdb6c142016-01-20 18:25:44 -080059 super(context);
mariagpuyol609f68a2016-02-22 17:45:47 -080060 setOrder(DEFAULT_ORDER);
mariagpuyol5cf7f732016-03-08 09:48:15 -080061 // This preference is reloaded each time onResume, so it is guaranteed to have a fresh
62 // representation of the contact each time we click on this preference to display or to call
63 // the contact.
64 mContact = EmergencyContactManager.getContact(context, contactUri);
65 setTitle(mContact.getName());
mariagpuyolf9ab89a2016-03-10 09:19:07 -080066 String summary = mContact.getPhoneType() == null ?
67 mContact.getPhoneNumber() :
68 String.format(
69 context.getResources().getString(R.string.phone_type_and_phone_number),
70 mContact.getPhoneType(),
71 mContact.getPhoneNumber());
72 setSummary(summary);
mariagpuyold19ace52016-02-12 10:37:26 -080073 setWidgetLayoutResource(R.layout.preference_user_delete_widget);
mariagpuyol609f68a2016-02-22 17:45:47 -080074 setPersistent(false);
mariagpuyol95dc0402016-02-17 11:12:46 -080075
76 //TODO: Consider doing the following in a non-UI thread.
mariagpuyol5cf7f732016-03-08 09:48:15 -080077 Drawable icon = PhotoUtils.encircleUserPhoto(mContact.getPhoto(), getContext());
mariagpuyol95dc0402016-02-17 11:12:46 -080078 setIcon(icon);
79 }
80
mariagpuyol609f68a2016-02-22 17:45:47 -080081 /** Listener to be informed when a contact preference should be deleted. */
82 public void setRemoveContactPreferenceListener(
83 RemoveContactPreferenceListener removeContactListener) {
84 mRemoveContactPreferenceListener = removeContactListener;
85 }
86
mariagpuyold19ace52016-02-12 10:37:26 -080087 @Override
88 protected void onBindView(View view) {
89 super.onBindView(view);
90 View deleteContactIcon = view.findViewById(R.id.delete_contact);
mariagpuyol609f68a2016-02-22 17:45:47 -080091 if (mRemoveContactPreferenceListener == null) {
mariagpuyol16472ca2016-02-16 17:49:26 -080092 deleteContactIcon.setVisibility(View.GONE);
93 } else {
mariagpuyol609f68a2016-02-22 17:45:47 -080094 deleteContactIcon.setOnClickListener(new View.OnClickListener() {
95 @Override
96 public void onClick(View view) {
97 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
98 builder.setMessage(String.format(getContext()
mariagpuyol5cf7f732016-03-08 09:48:15 -080099 .getString(R.string.remove_contact),
100 mContact.getName()));
mariagpuyol609f68a2016-02-22 17:45:47 -0800101 builder.setPositiveButton(getContext().getString(R.string.remove),
102 new DialogInterface.OnClickListener() {
103 @Override
104 public void onClick(DialogInterface dialogInterface,
105 int which) {
106 if (mRemoveContactPreferenceListener != null) {
107 mRemoveContactPreferenceListener
108 .onRemoveContactPreference(ContactPreference.this);
mariagpuyol16472ca2016-02-16 17:49:26 -0800109 }
mariagpuyol609f68a2016-02-22 17:45:47 -0800110 }
111 }).setNegativeButton(getContext().getString(R.string.cancel), null);
112 builder.create().show();
113 }
114 });
115
mariagpuyold19ace52016-02-12 10:37:26 -0800116 }
Akshay Kannancdb6c142016-01-20 18:25:44 -0800117 }
118
mariagpuyol609f68a2016-02-22 17:45:47 -0800119 public Uri getContactUri() {
mariagpuyol5cf7f732016-03-08 09:48:15 -0800120 return mContact.getContactUri();
mariagpuyol609f68a2016-02-22 17:45:47 -0800121 }
122
Akshay Kannancdb6c142016-01-20 18:25:44 -0800123 /**
Akshay Kannan44ef39d2016-01-26 09:08:41 -0800124 * Calls the contact.
125 */
mariagpuyol5cf7f732016-03-08 09:48:15 -0800126 public void callContact() {
127 Intent callIntent =
128 new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mContact.getPhoneNumber()));
129 MetricsLogger.action(getContext(), MetricsEvent.ACTION_CALL_EMERGENCY_CONTACT);
130 getContext().startActivity(callIntent);
mariagpuyol0504a442016-02-17 16:22:57 -0800131 }
mariagpuyolbc6555b2016-02-26 15:38:19 -0800132}