MessageFileViewFragment.java revision afdec1caf1216342ac5d811eeb321f408ccc42aa
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 android.app.Activity;
20import android.net.Uri;
21import android.os.Bundle;
22import android.util.Log;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26
27import com.android.email.Email;
28import com.android.email.R;
29import com.android.emailcommon.Logging;
30import com.android.emailcommon.provider.EmailContent.Message;
31import com.android.emailcommon.utility.Utility;
32
33/**
34 * A {@link MessageViewFragmentBase} subclass for file based messages. (aka EML files)
35 */
36public class MessageFileViewFragment extends MessageViewFragmentBase {
37    /**
38     * URI of message to open.
39     */
40    private Uri mFileEmailUri;
41
42    /**
43     * # of instances of this class.  When it gets 0, and the last one is not destroying for
44     * a config change, we delete all the EML files.
45     */
46    private static int sFragmentCount;
47
48    @Override
49    public void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51        sFragmentCount++;
52    }
53
54    @Override
55    public View onCreateView(
56            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
57        View result = super.onCreateView(inflater, container, savedInstanceState);
58
59        // Actions are not available in this view.
60        UiUtilities.setVisibilitySafe(result, R.id.favorite, View.GONE);
61        UiUtilities.setVisibilitySafe(result, R.id.reply, View.GONE);
62        UiUtilities.setVisibilitySafe(result, R.id.reply_all, View.GONE);
63        UiUtilities.setVisibilitySafe(result, R.id.forward, View.GONE);
64        UiUtilities.setVisibilitySafe(result, R.id.more, View.GONE);
65
66        return result;
67    }
68
69
70    @Override
71    public void onActivityCreated(Bundle savedInstanceState) {
72        super.onActivityCreated(savedInstanceState);
73        if (mFileEmailUri == null) { // sanity check.  setFileUri() must have been called.
74            throw new IllegalStateException();
75        }
76    }
77
78    @Override
79    public void onDestroy() {
80        super.onDestroy();
81
82        // If this is the last fragment of its kind, delete any/all attachment messages
83        sFragmentCount--;
84        if ((sFragmentCount == 0) && !getActivity().isChangingConfigurations()) {
85            getController().deleteAttachmentMessages();
86        }
87    }
88
89    /**
90     * Called by the host activity to set the URL to the EML file to open.
91     * Must be called before {@link #onActivityCreated(Bundle)}.
92     *
93     * Note: We don't use the fragment transaction for this fragment, so we can't use
94     * {@link #getArguments()} to pass arguments.
95     */
96    public void setFileUri(Uri fileEmailUri) {
97        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
98            Log.d(Logging.LOG_TAG, this + " openMessage");
99        }
100        if (mFileEmailUri != null) {
101            throw new IllegalStateException();
102        }
103        if (fileEmailUri == null) {
104            throw new IllegalArgumentException();
105        }
106        mFileEmailUri = fileEmailUri;
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(Activity activity) {
114        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
115            Log.d(Logging.LOG_TAG, this + " openMessageSync");
116        }
117        // Put up a toast; this can take a little while...
118        Utility.showToast(activity, R.string.message_view_parse_message_toast);
119        Message msg = getController().loadMessageFromUri(mFileEmailUri);
120        if (msg == null) {
121            // Indicate that the EML couldn't be loaded
122            Utility.showToast(activity, R.string.message_view_display_attachment_toast);
123            return null;
124        }
125        return msg;
126    }
127
128    @Override
129    protected Message reloadMessageSync(Activity activity) {
130        // EML files will never change, so just return the same copy.
131        return getMessage();
132    }
133
134    /**
135     * {@inheritDoc}
136     *
137     * Does exactly same as the super class method, but does an extra sanity check.
138     */
139    @Override
140    protected void reloadUiFromMessage(Message message, boolean okToFetch) {
141        // EML file should never be partially loaded.
142        if (message.mFlagLoaded != Message.FLAG_LOADED_COMPLETE) {
143            throw new IllegalStateException();
144        }
145        super.reloadUiFromMessage(message, okToFetch);
146    }
147}
148