1/*
2 * Copyright (C) 2013 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.browse;
19
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.content.Context;
23import android.os.Bundle;
24import android.app.DialogFragment;
25import android.view.LayoutInflater;
26import android.view.View;
27
28import com.android.mail.R;
29import com.android.mail.providers.Account;
30import com.android.mail.providers.Address;
31
32import java.util.HashMap;
33import java.util.Map;
34
35/**
36 * {@link DialogFragment} used by secure conversation views to display
37 * the expanded message details as a dialog.
38 */
39public class MessageHeaderDetailsDialogFragment extends DialogFragment {
40
41    private static final String ARG_ACCOUNT = "account";
42    private static final String ARG_ADDRESS_CACHE = "addresses";
43    private static final String ARG_FROM = "from";
44    private static final String ARG_REPLY_TO = "replyto";
45    private static final String ARG_TO = "to";
46    private static final String ARG_CC = "cc";
47    private static final String ARG_BCC = "bcc";
48    private static final String ARG_RECEIVED_TIME = "received-timestamp";
49
50    // Public no-args constructor needed for fragment re-instantiation
51    public MessageHeaderDetailsDialogFragment() {}
52
53    /**
54     * Creates a new {@link MessageHeaderDetailsDialogFragment}.
55     * @param addressCache a mapping of RFC822 addresses as strings to {@link Address}.
56     * @param account {@link Account} used as the from address for any messages created
57     *                by tapping an email address.
58     * @param from from addresses for the message
59     * @param replyTo replyTo addresses for the message
60     * @param to to addresses for the message
61     * @param cc cc addresses for the message
62     * @param bcc bcc addresses for the message
63     * @return a newly created {@link MessageHeaderDetailsDialogFragment}
64     */
65    public static MessageHeaderDetailsDialogFragment newInstance(
66            Map<String, Address> addressCache, Account account, String[] from, String[] replyTo,
67            String[] to, String[] cc, String[] bcc, CharSequence receivedTimestamp) {
68        final MessageHeaderDetailsDialogFragment f = new MessageHeaderDetailsDialogFragment();
69
70        // Supply needed items as arguments
71        final Bundle args = new Bundle(7);
72        args.putParcelable(ARG_ACCOUNT, account);
73
74        final Bundle addresses = new Bundle();
75        addAddressesToBundle(addresses, addressCache, from);
76        addAddressesToBundle(addresses, addressCache, replyTo);
77        addAddressesToBundle(addresses, addressCache, to);
78        addAddressesToBundle(addresses, addressCache, cc);
79        addAddressesToBundle(addresses, addressCache, bcc);
80        args.putBundle(ARG_ADDRESS_CACHE, addresses);
81
82        args.putStringArray(ARG_FROM, from);
83        args.putStringArray(ARG_REPLY_TO, replyTo);
84        args.putStringArray(ARG_TO, to);
85        args.putStringArray(ARG_CC, cc);
86        args.putStringArray(ARG_BCC, bcc);
87        args.putCharSequence(ARG_RECEIVED_TIME, receivedTimestamp);
88        f.setArguments(args);
89
90        return f;
91    }
92
93    private static void addAddressesToBundle(
94            Bundle addresses, Map<String, Address> addressCache, String[] emails) {
95        for (final String email : emails) {
96            addresses.putParcelable(email, MessageHeaderView.getAddress(addressCache, email));
97        }
98    }
99
100    @Override
101    public Dialog onCreateDialog(final Bundle onSavedInstanceState) {
102        final Context context = getActivity();
103        AlertDialog.Builder builder = new AlertDialog.Builder(context);
104        final View expandedDetails = MessageHeaderView.inflateExpandedDetails(
105                LayoutInflater.from(context));
106
107        final Bundle args = getArguments();
108
109        // turn bundle back into Map<String, Address>
110        final Bundle addresses = args.getBundle(ARG_ADDRESS_CACHE);
111        final Map<String, Address> addressCache = new HashMap<String, Address>();
112        for (String email : addresses.keySet()) {
113            addressCache.put(email, (Address) addresses.getParcelable(email));
114        }
115
116        MessageHeaderView.renderExpandedDetails(getResources(), expandedDetails, null,
117                addressCache, (Account) args.getParcelable(ARG_ACCOUNT), null,
118                args.getStringArray(ARG_FROM), args.getStringArray(ARG_REPLY_TO),
119                args.getStringArray(ARG_TO), args.getStringArray(ARG_CC),
120                args.getStringArray(ARG_BCC), args.getCharSequence(ARG_RECEIVED_TIME));
121
122        expandedDetails.findViewById(R.id.details_expander)
123                .setVisibility(View.GONE);
124        builder.setView(expandedDetails)
125                .setCancelable(true)
126                .setTitle(context.getString(R.string.message_details_title));
127        return builder.create();
128    }
129}
130