1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.ui.contact;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.AlertDialog;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.DialogInterface;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.res.Resources;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.LayoutInflater;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.Button;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.TextView;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.ContactIconView;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.UIIntents;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.AccessibilityUtil;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class AddContactsConfirmationDialog implements DialogInterface.OnClickListener {
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final Context mContext;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final Uri mAvatarUri;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final String mNormalizedDestination;
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public AddContactsConfirmationDialog(final Context context, final Uri avatarUri,
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String normalizedDestination) {
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mContext = context;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mAvatarUri = avatarUri;
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mNormalizedDestination = normalizedDestination;
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void show() {
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final int confirmAddContactStringId = R.string.add_contact_confirmation;
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final int cancelStringId = android.R.string.cancel;
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final AlertDialog alertDialog = new AlertDialog.Builder(mContext)
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        .setTitle(R.string.add_contact_confirmation_dialog_title)
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        .setView(createBodyView())
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        .setPositiveButton(confirmAddContactStringId, this)
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        .setNegativeButton(cancelStringId, null)
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        .create();
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        alertDialog.show();
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Resources resources = mContext.getResources();
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Button cancelButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (cancelButton != null) {
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            cancelButton.setTextColor(resources.getColor(R.color.contact_picker_button_text_color));
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Button addButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (addButton != null) {
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            addButton.setTextColor(resources.getColor(R.color.contact_picker_button_text_color));
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onClick(final DialogInterface dialog, final int which) {
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        UIIntents.get().launchAddContactActivity(mContext, mNormalizedDestination);
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private View createBodyView() {
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View view = LayoutInflater.from(mContext).inflate(
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                R.layout.add_contacts_confirmation_dialog_body, null);
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ContactIconView iconView = (ContactIconView) view.findViewById(R.id.contact_icon);
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        iconView.setImageResourceUri(mAvatarUri);
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final TextView textView = (TextView) view.findViewById(R.id.participant_name);
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        textView.setText(mNormalizedDestination);
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Accessibility reason : in case phone numbers are mixed in the display name,
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // we need to vocalize it for talkback.
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String vocalizedDisplayName = AccessibilityUtil.getVocalizedPhoneNumber(
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mContext.getResources(), mNormalizedDestination);
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        textView.setContentDescription(vocalizedDisplayName);
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return view;
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
86