SecureConversationViewController.java revision e6bf30d6e98eb6140b7a5f301efe631ff35c9f7b
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.ui;
19
20import android.app.Fragment;
21import android.app.FragmentManager;
22import android.content.res.Resources;
23import android.graphics.Rect;
24import android.os.Bundle;
25import android.support.v4.text.BidiFormatter;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.webkit.WebSettings;
30
31import com.android.mail.FormattedDateBuilder;
32import com.android.mail.R;
33import com.android.mail.browse.BorderView;
34import com.android.mail.browse.ConversationMessage;
35import com.android.mail.browse.ConversationViewAdapter;
36import com.android.mail.browse.ConversationViewAdapter.MessageHeaderItem;
37import com.android.mail.browse.ConversationViewHeader;
38import com.android.mail.browse.InlineAttachmentViewIntentBuilderCreator;
39import com.android.mail.browse.InlineAttachmentViewIntentBuilderCreatorHolder;
40import com.android.mail.browse.MessageFooterView;
41import com.android.mail.browse.MessageHeaderView;
42import com.android.mail.browse.MessageScrollView;
43import com.android.mail.browse.MessageWebView;
44import com.android.mail.browse.ScrollNotifier.ScrollListener;
45import com.android.mail.browse.WebViewContextMenu;
46import com.android.mail.print.PrintUtils;
47import com.android.mail.providers.Conversation;
48import com.android.mail.providers.Message;
49import com.android.mail.utils.ConversationViewUtils;
50
51/**
52 * Controller to do most of the heavy lifting for
53 * {@link SecureConversationViewFragment} and
54 * {@link com.android.mail.browse.EmlMessageViewFragment}. Currently that work
55 * is pretty much the rendering logic.
56 */
57public class SecureConversationViewController implements
58        MessageHeaderView.MessageHeaderViewCallbacks, ScrollListener {
59    private static final String BEGIN_HTML =
60                                           "<body style=\"margin: 0 %spx;\"><div style=\"margin: 16px 0; font-size: 80%%\">";
61    private static final String END_HTML = "</div></body>";
62
63    private final SecureConversationViewControllerCallbacks mCallbacks;
64    private final BidiFormatter mBidiFormatter;
65
66    private MessageWebView mWebView;
67    private ConversationViewHeader mConversationHeaderView;
68    private MessageHeaderView mMessageHeaderView;
69    private MessageHeaderView mSnapHeaderView;
70    private MessageFooterView mMessageFooterView;
71    private ConversationMessage mMessage;
72    private MessageScrollView mScrollView;
73
74    private ConversationViewProgressController mProgressController;
75    private FormattedDateBuilder mDateBuilder;
76
77    private int mSideMarginInWebPx;
78
79    public SecureConversationViewController(SecureConversationViewControllerCallbacks callbacks) {
80        mCallbacks = callbacks;
81        mBidiFormatter = BidiFormatter.getInstance();
82    }
83
84    public View onCreateView(LayoutInflater inflater, ViewGroup container,
85            Bundle savedInstanceState) {
86        View rootView = inflater.inflate(R.layout.secure_conversation_view, container, false);
87        mScrollView = (MessageScrollView) rootView.findViewById(R.id.scroll_view);
88        mConversationHeaderView = (ConversationViewHeader) rootView.findViewById(R.id.conv_header);
89        mMessageHeaderView = (MessageHeaderView) rootView.findViewById(R.id.message_header);
90        mSnapHeaderView = (MessageHeaderView) rootView.findViewById(R.id.snap_header);
91        mMessageFooterView = (MessageFooterView) rootView.findViewById(R.id.message_footer);
92
93        mScrollView.addScrollListener(this);
94
95        // Add color backgrounds to the header and footer.
96        // Otherwise the backgrounds are grey. They can't
97        // be set in xml because that would add more overdraw
98        // in ConversationViewFragment.
99        final int color = rootView.getResources().getColor(
100                R.color.message_header_background_color);
101        mMessageHeaderView.setBackgroundColor(color);
102        mSnapHeaderView.findViewById(R.id.upper_header).setBackgroundColor(color);
103        mMessageFooterView.setBackgroundColor(color);
104
105        ((BorderView) rootView.findViewById(R.id.top_border)).disableCardBottomBorder();
106        ((BorderView) rootView.findViewById(R.id.bottom_border)).disableCardTopBorder();
107
108        mProgressController = new ConversationViewProgressController(
109                mCallbacks.getFragment(), mCallbacks.getHandler());
110        mProgressController.instantiateProgressIndicators(rootView);
111        mWebView = (MessageWebView) rootView.findViewById(R.id.webview);
112        mWebView.setOverScrollMode(View.OVER_SCROLL_NEVER);
113        mWebView.setWebViewClient(mCallbacks.getWebViewClient());
114        final InlineAttachmentViewIntentBuilderCreator creator =
115                InlineAttachmentViewIntentBuilderCreatorHolder.
116                        getInlineAttachmentViewIntentCreator();
117        mWebView.setOnCreateContextMenuListener(new WebViewContextMenu(
118                mCallbacks.getFragment().getActivity(),
119                creator.createInlineAttachmentViewIntentBuilder(null, null, -1)));
120        mWebView.setFocusable(false);
121        final WebSettings settings = mWebView.getSettings();
122
123        settings.setJavaScriptEnabled(false);
124        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
125
126        ConversationViewUtils.setTextZoom(mCallbacks.getFragment().getResources(), settings);
127
128        settings.setSupportZoom(true);
129        settings.setBuiltInZoomControls(true);
130        settings.setDisplayZoomControls(false);
131
132        mScrollView.setInnerScrollableView(mWebView);
133
134        return rootView;
135    }
136
137    public void onActivityCreated(Bundle savedInstanceState) {
138        mCallbacks.setupConversationHeaderView(mConversationHeaderView);
139
140        final Fragment fragment = mCallbacks.getFragment();
141
142        mDateBuilder = new FormattedDateBuilder(fragment.getActivity());
143        mMessageHeaderView.initialize(
144                mCallbacks.getConversationAccountController(), mCallbacks.getAddressCache());
145        mMessageHeaderView.setContactInfoSource(mCallbacks.getContactInfoSource());
146        mMessageHeaderView.setCallbacks(this);
147        mMessageHeaderView.setExpandable(false);
148        mMessageHeaderView.setViewOnlyMode(mCallbacks.isViewOnlyMode());
149
150        mSnapHeaderView.setSnappy();
151        mSnapHeaderView.initialize(
152                mCallbacks.getConversationAccountController(), mCallbacks.getAddressCache());
153        mSnapHeaderView.setContactInfoSource(mCallbacks.getContactInfoSource());
154        mSnapHeaderView.setCallbacks(this);
155        mSnapHeaderView.setExpandable(false);
156        mSnapHeaderView.setViewOnlyMode(mCallbacks.isViewOnlyMode());
157
158        mCallbacks.setupMessageHeaderVeiledMatcher(mMessageHeaderView);
159        mCallbacks.setupMessageHeaderVeiledMatcher(mSnapHeaderView);
160
161        mMessageFooterView.initialize(fragment.getLoaderManager(), fragment.getFragmentManager());
162
163        mCallbacks.startMessageLoader();
164
165        mProgressController.showLoadingStatus(mCallbacks.isViewVisibleToUser());
166
167        final Resources r = mCallbacks.getFragment().getResources();
168        mSideMarginInWebPx = (int) (r.getDimensionPixelOffset(
169                R.dimen.conversation_message_content_margin_side) / r.getDisplayMetrics().density);
170    }
171
172    @Override
173    public void onNotifierScroll(final int y) {
174        // We need to decide whether or not to display the snap header.
175        // Get the location of the moveable message header inside the scroll view.
176        Rect rect = new Rect();
177        mScrollView.offsetDescendantRectToMyCoords(mMessageHeaderView, rect);
178
179        // If we have scrolled further than the distance from the top of the scrollView to the top
180        // of the message header, then the message header is at least partially ofscreen. As soon
181        // as the message header goes partially offscreen we need to display the snap header.
182        if (y > rect.top) {
183            mSnapHeaderView.setVisibility(View.VISIBLE);
184        } else {
185            mSnapHeaderView.setVisibility(View.GONE);
186        }
187    }
188
189    /**
190     * Populate the adapter with overlay views (message headers, super-collapsed
191     * blocks, a conversation header), and return an HTML document with spacer
192     * divs inserted for all overlays.
193     */
194    public void renderMessage(ConversationMessage message) {
195        mMessage = message;
196
197        final boolean alwaysShowImages = mCallbacks.shouldAlwaysShowImages();
198        mWebView.getSettings().setBlockNetworkImage(
199                !alwaysShowImages && !mMessage.alwaysShowImages);
200
201        // Add formatting to message body
202        // At this point, only adds margins.
203        StringBuilder dataBuilder = new StringBuilder(
204                String.format(BEGIN_HTML, mSideMarginInWebPx));
205        dataBuilder.append(mMessage.getBodyAsHtml());
206        dataBuilder.append(END_HTML);
207
208        mWebView.loadDataWithBaseURL(mCallbacks.getBaseUri(), dataBuilder.toString(),
209                "text/html", "utf-8", null);
210        final MessageHeaderItem item = ConversationViewAdapter.newMessageHeaderItem(
211                null, mDateBuilder, mMessage, true, mMessage.alwaysShowImages);
212        // Clear out the old info from the header before (re)binding
213        mMessageHeaderView.unbind();
214        mMessageHeaderView.bind(item, false);
215
216        mSnapHeaderView.unbind();
217        mSnapHeaderView.bind(item, false);
218
219        if (mMessage.hasAttachments) {
220            mMessageFooterView.setVisibility(View.VISIBLE);
221            mMessageFooterView.bind(item, mCallbacks.getAccountUri(), false);
222        }
223    }
224
225    public ConversationMessage getMessage() {
226        return mMessage;
227    }
228
229    public ConversationViewHeader getConversationHeaderView() {
230        return mConversationHeaderView;
231    }
232
233    public void dismissLoadingStatus() {
234        mProgressController.dismissLoadingStatus();
235    }
236
237    public void setSubject(String subject) {
238        mConversationHeaderView.setSubject(subject);
239    }
240
241    public void printMessage() {
242        final Conversation conversation = mMessage.getConversation();
243        PrintUtils.printMessage(mCallbacks.getFragment().getActivity(), mMessage,
244                conversation != null ? conversation.subject : mMessage.subject,
245                mCallbacks.getAddressCache(), mCallbacks.getBaseUri(), false /* useJavascript */);
246
247    }
248
249    public BidiFormatter getBidiFormatter() {
250        return mBidiFormatter;
251    }
252
253    // Start MessageHeaderViewCallbacks implementations
254
255    @Override
256    public void setMessageSpacerHeight(MessageHeaderItem item, int newSpacerHeight) {
257        // Do nothing.
258    }
259
260    @Override
261    public void setMessageExpanded(MessageHeaderItem item, int newSpacerHeight,
262            int topBorderHeight, int bottomBorderHeight) {
263        // Do nothing.
264    }
265
266    @Override
267    public void setMessageDetailsExpanded(MessageHeaderItem i, boolean expanded, int heightBefore) {
268        // Do nothing.
269    }
270
271    @Override
272    public void showExternalResources(final Message msg) {
273        mWebView.getSettings().setBlockNetworkImage(false);
274    }
275
276    @Override
277    public void showExternalResources(final String rawSenderAddress) {
278        mWebView.getSettings().setBlockNetworkImage(false);
279    }
280
281    @Override
282    public boolean supportsMessageTransforms() {
283        return false;
284    }
285
286    @Override
287    public String getMessageTransforms(final Message msg) {
288        return null;
289    }
290
291    @Override
292    public FragmentManager getFragmentManager() {
293        return mCallbacks.getFragment().getFragmentManager();
294    }
295
296    // End MessageHeaderViewCallbacks implementations
297}
298