MessageFileView.java revision 954f037d8f1e64ae88e02fe18ecf6b72b00f3daa
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.emailcommon.Logging;
21import com.android.emailcommon.provider.EmailContent.Account;
22import com.android.emailcommon.utility.EmailAsyncTask;
23import com.android.emailcommon.utility.Utility;
24import com.google.common.annotations.VisibleForTesting;
25
26import android.app.ActionBar;
27import android.app.Activity;
28import android.net.Uri;
29import android.os.Bundle;
30import android.util.Log;
31import android.view.MenuItem;
32
33/**
34 * Activity to show file-based messages.  (i.e. *.eml files, and possibly *.msg files).
35 */
36public class MessageFileView extends Activity implements MessageViewFragmentBase.Callback {
37    private ActionBar mActionBar;
38
39    private MessageFileViewFragment mFragment;
40
41    private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker();
42
43    @Override
44    public void onCreate(Bundle icicle) {
45        super.onCreate(icicle);
46        ActivityHelper.debugSetWindowFlags(this);
47        setContentView(R.layout.message_file_view);
48
49        mActionBar = getActionBar();
50        mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP
51                | ActionBar.DISPLAY_SHOW_TITLE);
52
53        mFragment = (MessageFileViewFragment) getFragmentManager().findFragmentById(
54                R.id.message_file_view_fragment);
55        mFragment.setCallback(this);
56
57        final Uri fileEmailUri = getIntent().getData();
58        if (fileEmailUri == null) {
59            Log.w(Logging.LOG_TAG, "Insufficient intent parameter.  Closing...");
60            finish();
61            return;
62        }
63
64        mFragment.setFileUri(fileEmailUri);
65
66        // Set title.
67        new LoadFilenameTask(fileEmailUri).executeParallel();
68    }
69
70    @Override
71    public void onResume() {
72        super.onResume();
73    }
74
75    @Override
76    public void onDestroy() {
77        super.onDestroy();
78        mTaskTracker.cancellAllInterrupt();
79    }
80
81    @Override
82    public boolean onOptionsItemSelected(MenuItem item) {
83        switch (item.getItemId()) {
84            case android.R.id.home:
85                onBackPressed(); // Treat as "back".
86                return true;
87        }
88
89        return super.onOptionsItemSelected(item);
90    }
91
92    /**
93     * Set the activity title.  ("Viewing FILENAME")
94     */
95    private void setTitle(String filename) {
96        mActionBar.setTitle(getString(R.string.eml_view_title, filename));
97    }
98
99    /**
100     * Load the filename of the EML, and update the activity title.
101     */
102    private class LoadFilenameTask extends EmailAsyncTask<Void, Void, String> {
103        private final Uri mContentUri;
104
105        public LoadFilenameTask(Uri contentUri) {
106            super(mTaskTracker);
107            mContentUri = contentUri;
108        }
109
110        @Override
111        protected String doInBackground(Void... params) {
112            return Utility.getContentFileName(MessageFileView.this, mContentUri);
113        }
114
115        @Override
116        protected void onPostExecute(String filename) {
117            if (filename == null) {
118                return;
119            }
120            setTitle(filename);
121        }
122    }
123
124    @Override
125    public boolean onUrlInMessageClicked(String url) {
126        // EML files don't have the "owner" account, so use the default account as the sender.
127        return ActivityHelper.openUrlInMessage(this, url, Account.PSEUDO_ACCOUNT_ID_NONE);
128    }
129
130    @Override
131    public void onMessageNotExists() { // Probably meessage deleted.
132        finish();
133    }
134
135    @Override
136    public void onMessageViewShown(int mailboxType) {
137        // Not important for EMLs
138    }
139
140    @Override
141    public void onMessageViewGone() {
142        // Not important for EMLs
143    }
144
145    @Override
146    public void onLoadMessageStarted() {
147        // Not important for EMLs
148    }
149
150    @Override
151    public void onLoadMessageFinished() {
152        // Not important for EMLs
153    }
154
155    @Override
156    public void onLoadMessageError(String errorMessage) {
157        // Not important for EMLs
158    }
159
160    @VisibleForTesting
161    MessageFileViewFragment getFragment() {
162        return mFragment;
163    }
164}
165