MessageFileViewFragment.java revision 09eb977c062787a18a0c1ec96d929bb2279d14ad
1/*
2 * Copyright (C) 2010 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
17package com.android.email.activity;
18
19import com.android.email.R;
20import com.android.email.Utility;
21import com.android.email.provider.EmailContent.Message;
22
23import android.app.Activity;
24import android.net.Uri;
25import android.os.Bundle;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29
30import java.security.InvalidParameterException;
31
32/**
33 * A {@link MessageViewFragmentBase} subclass for file based messages. (aka EML files)
34 *
35 * See {@link MessageViewBase} for the class relation diagram.
36 */
37public class MessageFileViewFragment extends MessageViewFragmentBase {
38    /**
39     * URI of message to open.  Protect with {@link #mLock}.
40     */
41    private Uri mFileEmailUri;
42
43    /** Lock object to protect {@link #mFileEmailUri} */
44    private final Object mLock = new Object();
45
46    /**
47     * # of instances of this class.  When it gets 0, and the last one is not destroying for
48     * a config change, we delete all the EML files.
49     */
50    private static int sFragmentCount;
51
52    @Override
53    public void onCreate(Bundle savedInstanceState) {
54        super.onCreate(savedInstanceState);
55        sFragmentCount++;
56    }
57
58    /**
59     * Loads the layout.
60     *
61     * This class uses the same layout as {@link MessageViewFragment}, but hides the star.
62     */
63    @Override
64    public View onCreateView(
65            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
66        View view = super.onCreateView(inflater, container, savedInstanceState);
67
68        view.findViewById(R.id.favorite).setVisibility(View.GONE);
69        return view;
70    }
71
72    @Override
73    public void onDestroy() {
74        super.onDestroy();
75
76        // If this is the last fragment of its kind, delete any/all attachment messages
77        sFragmentCount--;
78        if ((sFragmentCount == 0) && !getActivity().isChangingConfigurations()) {
79            getController().deleteAttachmentMessages();
80        }
81    }
82
83    /** Called by activities with a URI to an EML file. */
84    public void openMessage(Uri fileEmailUri) {
85        if (fileEmailUri == null) {
86            throw new InvalidParameterException();
87        }
88        synchronized (mLock) {
89            mFileEmailUri = fileEmailUri;
90        }
91        openMessageIfStarted();
92    }
93
94    @Override
95    public void clearContent() {
96        synchronized (mLock) {
97            super.clearContent();
98            mFileEmailUri = null;
99        }
100    }
101
102    @Override
103    protected boolean isMessageSpecified() {
104        synchronized (mLock) {
105            return mFileEmailUri != null;
106        }
107    }
108
109    /**
110     * NOTE See the comment on the super method.  It's called on a worker thread.
111     */
112    @Override
113    protected Message openMessageSync() {
114        synchronized (mLock) {
115            final Activity activity = getActivity();
116            Uri messageUri = mFileEmailUri;
117            if (messageUri == null) {
118                return null; // Called after clearContent().
119            }
120            // Put up a toast; this can take a little while...
121            Utility.showToast(activity, R.string.message_view_parse_message_toast);
122            Message msg = getController().loadMessageFromUri(messageUri);
123            if (msg == null) {
124                // Indicate that the attachment couldn't be loaded
125                Utility.showToast(activity, R.string.message_view_display_attachment_toast);
126                return null;
127            }
128            return msg;
129        }
130    }
131
132    /**
133     * {@inheritDoc}
134     *
135     * Does exactly same as the super class method, but does an extra sanity check.
136     */
137    @Override
138    protected void reloadUiFromMessage(Message message, boolean okToFetch) {
139        // EML file should never be partially loaded.
140        if (message.mFlagLoaded != Message.FLAG_LOADED_COMPLETE) {
141            throw new IllegalStateException();
142        }
143        super.reloadUiFromMessage(message, okToFetch);
144    }
145}
146