Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit bec2675

Browse files
[[ Bug 11069 ]] mobileComposeMail attachment missing in Android
Now provides the files located in the application AK through a one-time accessible ContentProvider
1 parent 595d7e8 commit bec2675

File tree

5 files changed

+145
-9
lines changed

5 files changed

+145
-9
lines changed

engine/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<data android:scheme="runrev" />
4040
</intent-filter>
4141
</activity>
42+
<provider
43+
android:name="com.runrev.android.AttachmentProvider"
44+
android:authorities="com.runrev.android.attachmentprovider"
45+
android:exported="true"
46+
android:grantUriPermissions="true" >
47+
</provider>
4248
<activity android:name="com.inneractive.api.ads.InneractiveFullScreenView"></activity>
4349

4450
<service android:name="AppService" />

engine/rsrc/android-manifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ ${USES_PERMISSION}${USES_FEATURE}
2525
</intent-filter>
2626
${CUSTOM_URL_FILTER}
2727
</activity>
28+
<provider
29+
android:name="com.runrev.android.AttachmentProvider"
30+
android:authorities="com.runrev.android.attachmentprovider"
31+
android:exported="true"
32+
android:grantUriPermissions="true" >
33+
</provider>
2834
${SERVICE}
2935
${RECEIVER}
3036
${PUSH_RECEIVER}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
package com.runrev.android;
18+
19+
import java.io.*;
20+
import java.util.*;
21+
import java.lang.*;
22+
import java.nio.channels.FileChannel;
23+
24+
import android.content.ContentProvider;
25+
import android.content.ContentValues;
26+
import android.text.*;
27+
import android.os.ParcelFileDescriptor;
28+
import android.net.Uri;
29+
import android.util.Log;
30+
import android.content.res.AssetFileDescriptor;
31+
32+
import android.database.*;
33+
34+
35+
public class AttachmentProvider extends ContentProvider
36+
{
37+
public static final String URI = "content://com.runrev.android.attachmentprovider";
38+
public static final String AUTHORITY = "com.runrev.android.attachmentprovider";
39+
40+
41+
@Override
42+
public boolean onCreate()
43+
{
44+
return true;
45+
}
46+
47+
@Override
48+
public AssetFileDescriptor openAssetFile(Uri uri, String mode)
49+
throws FileNotFoundException
50+
{
51+
// Check whether we are really asked for a file from the APK
52+
if (!uri.toString().contains(getContext().getPackageCodePath()))
53+
throw new FileNotFoundException("Unable to open non-asset file");
54+
55+
String t_file_path = uri.getLastPathSegment();
56+
57+
try
58+
{
59+
AssetFileDescriptor afd = getContext().getAssets().openFd(t_file_path);
60+
return afd;
61+
}
62+
catch(IOException e)
63+
{
64+
Log.i("revandroid", "Failed to open: " + e.getMessage());
65+
throw new FileNotFoundException("unable to find file " + t_file_path);
66+
}
67+
}
68+
69+
// Fetch the URI stored for the ask email
70+
@Override
71+
public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
72+
{
73+
return null;
74+
}
75+
76+
// The insert method update the saved values for the email
77+
@Override
78+
public Uri insert (Uri uri, ContentValues p_values)
79+
{
80+
// Does nothing
81+
return uri;
82+
}
83+
84+
@Override
85+
public int update (Uri uri, ContentValues values, String selection, String[] selectionArgs)
86+
{
87+
// Does nothing
88+
return 0;
89+
}
90+
91+
@Override
92+
public int delete (Uri uri, String selection, String[] selectionArgs)
93+
{
94+
// Does nothing
95+
return 0;
96+
}
97+
98+
@Override
99+
public String getType (Uri uri)
100+
{
101+
// Does nothing
102+
return null;
103+
}
104+
}

engine/src/java/com/runrev/android/Email.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,35 @@ private String combineMimeTypes(String type_a, String type_b)
8989
return "*/*"; //"multipart/mixed";
9090
}
9191

92-
private boolean addAttachment(Uri uri, String mime_type, String name)
92+
private boolean addAttachment(Uri uri, String mime_type, String name, boolean p_is_in_apk)
9393
{
9494
if (m_attachment_uris == null)
9595
m_attachment_uris = new ArrayList<Uri>();
96-
97-
m_attachment_uris.add(uri);
96+
97+
// SN-2014-01-29: [[ Bug 11069 ]] mobileComposeMail attachment missing in Android
98+
// We need to call a Content Provider to get the right to access them
99+
Uri t_uri;
100+
if (p_is_in_apk)
101+
t_uri = Uri.parse(AttachmentProvider.URI + "/" + uri.getPath());
102+
else
103+
t_uri = uri;
104+
105+
m_attachment_uris.add(t_uri);
106+
98107
m_mime_type = combineMimeTypes(m_mime_type, mime_type);
99108
return true;
100109
}
101110

102-
public boolean addAttachment(String path, String mime_type, String name)
111+
public boolean addAttachment(String path, String mime_type, String name, boolean p_is_in_apk)
103112
{
104-
return addAttachment(Uri.fromFile(new File(path)), mime_type, name);
113+
return addAttachment(Uri.fromFile(new File(path)), mime_type, name, p_is_in_apk);
105114
}
106115

107116
public boolean addAttachment(byte[] data, String mime_type, String name)
108117
{
109118
try
110119
{
111-
File t_tempfile;
120+
File t_tempfile;
112121
t_tempfile = File.createTempFile("eml", name);
113122

114123
FileOutputStream t_out = new FileOutputStream(t_tempfile);
@@ -120,7 +129,9 @@ public boolean addAttachment(byte[] data, String mime_type, String name)
120129

121130
m_temp_files.add(t_tempfile);
122131

123-
return addAttachment(Uri.fromFile(t_tempfile), mime_type, name);
132+
// SN-2014-01-29: [[ Bug 11069 ]] mobileComposeMail attachment missing in Android
133+
// Temporary file is meant to be always readable
134+
return addAttachment(Uri.fromFile(t_tempfile), mime_type, name, false);
124135
}
125136
catch (Exception e)
126137
{
@@ -161,6 +172,11 @@ public Intent createIntent()
161172
t_mail_intent.setType(m_mime_type);
162173
else
163174
t_mail_intent.setType("text/plain");
175+
176+
// SN-2014-01-28: [[ Bug 11069 ]] mobileComposeMail attachment missing in Android
177+
// This permission of reading must be added to the Intent to allow it to read the file
178+
t_mail_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
179+
164180
return t_mail_intent;
165181
}
166182

engine/src/java/com/runrev/android/Engine.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,12 +1833,16 @@ public void prepareEmail(String address, String cc, String bcc, String subject,
18331833

18341834
public void addAttachment(String path, String mime_type, String name)
18351835
{
1836-
m_email.addAttachment(path, mime_type, name);
1836+
// SN-2014-01-29: [[ Bug 11069 ]] mobileComposeMail attachment missing in Android
1837+
// Specifying whether a file is located in the APK
1838+
boolean t_apk_path;
1839+
t_apk_path = path.contains(getActivity().getPackageCodePath());
1840+
m_email.addAttachment(path, mime_type, name, t_apk_path);
18371841
}
18381842

18391843
public void addAttachment(byte[] data, String mime_type, String name)
18401844
{
1841-
m_email.addAttachment(data, mime_type, name);
1845+
m_email.addAttachment(data, mime_type, name);
18421846
}
18431847

18441848
public void sendEmail()

0 commit comments

Comments
 (0)