Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.emergency; |
| 17 | |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 18 | import android.Manifest; |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 19 | import android.app.AlertDialog; |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 21 | import android.content.DialogInterface; |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 22 | import android.content.Intent; |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame^] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.drawable.Drawable; |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 27 | import android.net.Uri; |
| 28 | import android.preference.Preference; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 29 | import android.support.annotation.NonNull; |
mariagpuyol | 16472ca | 2016-02-16 17:49:26 -0800 | [diff] [blame] | 30 | import android.support.annotation.Nullable; |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 31 | import android.support.v4.app.ActivityCompat; |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 32 | import android.view.View; |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 33 | |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 34 | import com.android.internal.logging.MetricsLogger; |
| 35 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame^] | 36 | import com.android.settingslib.drawable.CircleFramedDrawable; |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 37 | |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * A {@link Preference} to display a contact using the specified URI string. |
| 41 | */ |
| 42 | public class ContactPreference extends Preference { |
| 43 | |
| 44 | private final Uri mUri; |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 45 | private final DeleteContactListener mDeleteContactListener; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 46 | private final String mName; |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Listener for deleting a contact. |
| 50 | */ |
| 51 | public interface DeleteContactListener { |
| 52 | /** |
| 53 | * Callback to delete a contact. |
| 54 | */ |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 55 | void onContactDelete(Uri contactUri); |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 56 | } |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Instantiates a ContactPreference that displays an emergency contact, taking in a Context and |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 60 | * the Uri, name and phone number of the contact and a listener to be informed when clicking on |
| 61 | * the delete icon. |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 62 | */ |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 63 | public ContactPreference(Context context, |
| 64 | @NonNull Uri contactUri, |
| 65 | @NonNull String contactName, |
mariagpuyol | 16472ca | 2016-02-16 17:49:26 -0800 | [diff] [blame] | 66 | @Nullable DeleteContactListener deleteContactListener) { |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 67 | super(context); |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 68 | mUri = contactUri; |
| 69 | mName = contactName; |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 70 | mDeleteContactListener = deleteContactListener; |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 71 | setTitle(mName); |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 72 | setWidgetLayoutResource(R.layout.preference_user_delete_widget); |
mariagpuyol | 95dc040 | 2016-02-17 11:12:46 -0800 | [diff] [blame^] | 73 | |
| 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; |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | protected void onBindView(View view) { |
| 103 | super.onBindView(view); |
| 104 | View deleteContactIcon = view.findViewById(R.id.delete_contact); |
mariagpuyol | 16472ca | 2016-02-16 17:49:26 -0800 | [diff] [blame] | 105 | 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 | } |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 127 | } |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 131 | * Calls the contact. |
| 132 | */ |
| 133 | public void callContact() { |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 134 | String phoneNumber = EmergencyContactManager.getNumber(getContext(), mUri); |
| 135 | if (phoneNumber != null) { |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 136 | if (ActivityCompat.checkSelfPermission(getContext(), |
| 137 | Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 138 | Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 139 | MetricsLogger.action(getContext(), MetricsEvent.ACTION_CALL_EMERGENCY_CONTACT); |
| 140 | getContext().startActivity(callIntent); |
| 141 | } |
mariagpuyol | aee2378 | 2016-02-16 13:29:49 -0800 | [diff] [blame] | 142 | } else { |
| 143 | // TODO: Show dialog saying that there is no number. |
Akshay Kannan | 44ef39d | 2016-01-26 09:08:41 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 148 | * Displays a contact card for the contact. |
| 149 | */ |
| 150 | public void displayContact() { |
| 151 | Intent contactIntent = new Intent(Intent.ACTION_VIEW); |
mariagpuyol | d19ace5 | 2016-02-12 10:37:26 -0800 | [diff] [blame] | 152 | contactIntent.setData(mUri); |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 153 | getContext().startActivity(contactIntent); |
| 154 | } |
Akshay Kannan | cdb6c14 | 2016-01-20 18:25:44 -0800 | [diff] [blame] | 155 | } |