MessageFileViewFragment.java revision 61eec98d398358f61cf601e805e8bfd01e4b3418
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 private Uri mFileEmailUri; 39 /** 40 * # of instances of this class. When it gets 0, and the last one is not destroying for 41 * a config change, we delete all the EML files. 42 */ 43 private static int sFragmentCount; 44 45 @Override 46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 sFragmentCount++; 49 } 50 51 /** 52 * Loads the layout. 53 * 54 * This class uses the same layout as {@link MessageViewFragment}, but hides the star. 55 */ 56 @Override 57 public View onCreateView( 58 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 59 View view = super.onCreateView(inflater, container, savedInstanceState); 60 61 view.findViewById(R.id.favorite).setVisibility(View.GONE); 62 return view; 63 } 64 65 @Override 66 public void onDestroy() { 67 super.onDestroy(); 68 69 // If this is the last fragment of its kind, delete any/all attachment messages 70 sFragmentCount--; 71 if ((sFragmentCount == 0) && !getActivity().isChangingConfigurations()) { 72 getController().deleteAttachmentMessages(); 73 } 74 } 75 76 /** Called by activities with a URI to an EML file. */ 77 public void openMessage(Uri fileEmailUri) { 78 if (fileEmailUri == null) { 79 throw new InvalidParameterException(); 80 } 81 mFileEmailUri = fileEmailUri; 82 openMessageIfStarted(); 83 } 84 85 @Override 86 public void clearContent() { 87 super.clearContent(); 88 mFileEmailUri = null; 89 } 90 91 @Override 92 protected boolean isMessageSpecified() { 93 return mFileEmailUri != null; 94 } 95 96 @Override 97 protected Message openMessageSync() { 98 final Activity activity = getActivity(); 99 // Put up a toast; this can take a little while... 100 Utility.showToast(activity, R.string.message_view_parse_message_toast); 101 Message msg = getController().loadMessageFromUri(mFileEmailUri); 102 if (msg == null) { 103 // Indicate that the attachment couldn't be loaded 104 Utility.showToast(activity, R.string.message_view_display_attachment_toast); 105 return null; 106 } 107 return msg; 108 } 109 110 /** 111 * {@inheritDoc} 112 * 113 * Does exactly same as the super class method, but does an extra sanity check. 114 */ 115 @Override 116 protected void reloadUiFromMessage(Message message, boolean okToFetch) { 117 // EML file should never be partially loaded. 118 if (message.mFlagLoaded != Message.FLAG_LOADED_COMPLETE) { 119 throw new IllegalStateException(); 120 } 121 super.reloadUiFromMessage(message, okToFetch); 122 } 123} 124