summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/conversationlist/ShareIntentActivity.java
blob: 6d51de2cb9d8b974f75e0ff2a65e745139e61f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.ui.conversationlist;

import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.android.messaging.Factory;
import com.android.messaging.datamodel.data.ConversationListItemData;
import com.android.messaging.datamodel.data.MessageData;
import com.android.messaging.datamodel.data.PendingAttachmentData;
import com.android.messaging.ui.BaseBugleActivity;
import com.android.messaging.ui.UIIntents;
import com.android.messaging.util.Assert;
import com.android.messaging.util.ContentType;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.MediaMetadataRetrieverWrapper;
import com.android.messaging.util.FileUtil;
import com.android.messaging.util.UiUtils;
import com.android.messaging.util.UriUtil;

import java.io.IOException;
import java.util.ArrayList;

import com.android.messaging.R;

public class ShareIntentActivity extends BaseBugleActivity implements
        ShareIntentFragment.HostInterface {

    private MessageData mDraftMessage;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Intent intent = getIntent();
        if (Intent.ACTION_SEND.equals(intent.getAction()) &&
                (!TextUtils.isEmpty(intent.getStringExtra("address")) ||
                !TextUtils.isEmpty(intent.getStringExtra(Intent.EXTRA_EMAIL)))) {
            // This is really more like a SENDTO intent because a destination is supplied.
            // It's coming through the SEND intent because that's the intent that is used
            // when invoking the chooser with Intent.createChooser().
            final Intent convIntent = UIIntents.get().getLaunchConversationActivityIntent(this);
            // Copy the important items from the original intent to the new intent.
            convIntent.putExtras(intent);
            convIntent.setAction(Intent.ACTION_SENDTO);
            convIntent.setDataAndType(intent.getData(), intent.getType());
            // We have to fire off the intent and finish before trying to show the fragment,
            // otherwise we get some flashing.
            startActivity(convIntent);
            finish();
            return;
        }
        new ShareIntentFragment().show(getFragmentManager(), "ShareIntentFragment");
    }

    @Override
    public void onAttachFragment(final Fragment fragment) {
        final Intent intent = getIntent();
        final String action = intent.getAction();
        if (Intent.ACTION_SEND.equals(action)) {
            final Uri contentUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            if (UriUtil.isFileUri(contentUri)) {
                LogUtil.i(
                    LogUtil.BUGLE_TAG,
                    "Ignoring attachment from file URI which are no longer supported.");
                return;
            }
            final String contentType = extractContentType(contentUri, intent.getType());
            if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.DEBUG)) {
                LogUtil.d(LogUtil.BUGLE_TAG, String.format(
                        "onAttachFragment: contentUri=%s, intent.getType()=%s, inferredType=%s",
                        contentUri, intent.getType(), contentType));
            }
            if (ContentType.TEXT_PLAIN.equals(contentType)) {
                final String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
                if (sharedText != null) {
                    mDraftMessage = MessageData.createSharedMessage(sharedText);
                } else {
                    mDraftMessage = null;
                }
            } else if (ContentType.isImageType(contentType) ||
                    ContentType.isVCardType(contentType) ||
                    ContentType.isAudioType(contentType) ||
                    ContentType.isVideoType(contentType)) {
                if (contentUri != null) {
                    mDraftMessage = MessageData.createSharedMessage(null);
                    addSharedImagePartToDraft(contentType, contentUri);
                } else {
                    mDraftMessage = null;
                }
            } else {
                // Unsupported content type.
                finish();
                UiUtils.showToastAtBottom(R.string.attachment_load_failed_dialog_message);
                LogUtil.e(LogUtil.BUGLE_TAG,
                        "Unsupported shared content type for " + contentUri + ": " + contentType
                        + " (" + intent.getType() + ")");
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
            final String contentType = intent.getType();
            if (ContentType.isImageType(contentType)) {
                // Handle sharing multiple images.
                final ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(
                        Intent.EXTRA_STREAM);
                if (imageUris != null && imageUris.size() > 0) {
                    mDraftMessage = MessageData.createSharedMessage(null);
                    for (final Uri imageUri : imageUris) {
                        if (UriUtil.isFileUri(imageUri)) {
                            LogUtil.i(
                                LogUtil.BUGLE_TAG,
                                "Ignoring attachment from file URI which are no longer supported.");
                            continue;
                        }
                        final String actualContentType = extractContentType(imageUri, contentType);
                        addSharedImagePartToDraft(actualContentType, imageUri);
                    }
                } else {
                    mDraftMessage = null;
                }
            } else {
                // Unsupported content type.
                finish();
                UiUtils.showToastAtBottom(R.string.attachment_load_failed_dialog_message);
                LogUtil.e(LogUtil.BUGLE_TAG, "Unsupported shared content type: " + contentType);
            }
        } else {
            // Unsupported action.
            Assert.fail("Unsupported action type for sharing: " + action);
        }
    }

    private static String extractContentType(final Uri uri, final String contentType) {
        if (uri == null) {
            return contentType;
        }
        // First try looking at file extension.  This is less reliable in some ways but it's
        // recommended by
        // https://developer.android.com/training/secure-file-sharing/retrieve-info.html
        // Some implementations of MediaMetadataRetriever get things horribly
        // wrong for common formats such as jpeg (reports as video/ffmpeg)
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        final String typeFromExtension = resolver.getType(uri);
        if (typeFromExtension != null) {
            return typeFromExtension;
        }
        final MediaMetadataRetrieverWrapper retriever = new MediaMetadataRetrieverWrapper();
        try {
            retriever.setDataSource(uri);
            final String extractedType = retriever.extractMetadata(
                    MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
            if (extractedType != null) {
                return extractedType;
            }
        } catch (final IOException e) {
            LogUtil.i(LogUtil.BUGLE_TAG, "Could not determine type of " + uri, e);
        } finally {
            retriever.release();
        }
        return contentType;
    }

    private void addSharedImagePartToDraft(final String contentType, final Uri imageUri) {
        if (FileUtil.isInPrivateDir(imageUri)) {
            Assert.fail("Cannot send private file " + imageUri.toString());
        } else {
            mDraftMessage.addPart(PendingAttachmentData.createPendingAttachmentData(contentType,
                    imageUri));
        }
    }

    @Override
    public void onConversationClick(final ConversationListItemData conversationListItemData) {
        UIIntents.get().launchConversationActivity(
                this, conversationListItemData.getConversationId(), mDraftMessage);
        finish();
    }

    @Override
    public void onCreateConversationClick() {
        UIIntents.get().launchCreateNewConversationActivity(this, mDraftMessage);
        finish();
    }
}