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.browse;
19
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.net.Uri;
24
25import com.android.emailcommon.TempDirectory;
26import com.android.emailcommon.internet.MimeMessage;
27import com.android.emailcommon.mail.MessagingException;
28import com.android.mail.ui.MailAsyncTaskLoader;
29import com.android.mail.utils.LogTag;
30import com.android.mail.utils.LogUtils;
31
32import java.io.File;
33import java.io.FileNotFoundException;
34import java.io.IOException;
35import java.io.InputStream;
36
37/**
38 * Loader that builds a ConversationMessage from an EML file Uri.
39 */
40public class EmlMessageLoader extends MailAsyncTaskLoader<ConversationMessage> {
41    private static final String LOG_TAG = LogTag.getLogTag();
42
43    private Uri mEmlFileUri;
44
45    public EmlMessageLoader(Context context, Uri emlFileUri) {
46        super(context);
47        mEmlFileUri = emlFileUri;
48    }
49
50    @Override
51    public ConversationMessage loadInBackground() {
52        final Context context = getContext();
53        TempDirectory.setTempDirectory(context);
54        final ContentResolver resolver = context.getContentResolver();
55        final InputStream stream;
56        try {
57            stream = resolver.openInputStream(mEmlFileUri);
58        } catch (FileNotFoundException e) {
59            LogUtils.e(LOG_TAG, e, "Could not find eml file at uri: %s", mEmlFileUri);
60            return null;
61        }
62
63        final MimeMessage mimeMessage;
64        ConversationMessage convMessage;
65        try {
66            mimeMessage = new MimeMessage(stream);
67            convMessage = new ConversationMessage(context, mimeMessage, mEmlFileUri);
68        } catch (IOException e) {
69            LogUtils.e(LOG_TAG, e, "Could not read eml file");
70            return null;
71        } catch (MessagingException e) {
72            LogUtils.e(LOG_TAG, e, "Error in parsing eml file");
73            return null;
74        } finally {
75            try {
76                stream.close();
77            } catch (IOException e) {
78                convMessage = null;
79            }
80
81            // delete temp files created during parsing
82            final File[] cacheFiles = TempDirectory.getTempDirectory().listFiles();
83            for (final File file : cacheFiles) {
84                if (file.getName().startsWith("body")) {
85                    final boolean deleted = file.delete();
86                    if (!deleted) {
87                        LogUtils.d(LOG_TAG, "Failed to delete temp file" + file.getName());
88                    }
89                }
90            }
91        }
92
93        return convMessage;
94    }
95
96    /**
97     * Helper function to take care of releasing resources associated
98     * with an actively loaded data set.
99     */
100    @Override
101    protected void onDiscardResult(ConversationMessage message) {
102        // if this eml message had attachments, start a service to clean up the cache files
103        if (message.attachmentListUri != null) {
104            final Intent intent = new Intent(Intent.ACTION_DELETE);
105            intent.setClass(getContext(), EmlTempFileDeletionService.class);
106            intent.setData(message.attachmentListUri);
107
108            getContext().startService(intent);
109        }
110    }
111}
112