Skip to content

Commit

Permalink
Fix opening email with file attachment causes app crash on Android 8 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
katzer committed Sep 13, 2017
1 parent 701ccc0 commit a569fcf
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

## ChangeLog

#### Version 0.8.9 (not yet released)
- Fix opening email with file attachment causes app crash on Android 8 (#270)

#### Version 0.8.8 (07.09.2017)
- Fix crash on iOS if attachment could not be found
- Fix wrong plugin ID in package.json
Expand Down
22 changes: 22 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,35 @@
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
</config-file>

<config-file target="AndroidManifest.xml" parent="application">
<provider
android:name="de.appplant.cordova.emailcomposer.EmailComposerProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/emailcomposer_provider_paths"/>
</provider>
</config-file>

<framework src="com.android.support:support-v4:24.1.1+" />

<source-file
src="src/android/xml/emailcomposer_provider_paths.xml"
target-dir="res/xml" />

<source-file
src="src/android/EmailComposer.java"
target-dir="src/de/appplant/cordova/emailcomposer" />

<source-file
src="src/android/EmailComposerImpl.java"
target-dir="src/de/appplant/cordova/emailcomposer" />

<source-file
src="src/android/EmailComposerProvider.java"
target-dir="src/de/appplant/cordova/emailcomposer" />
</platform>

<!-- windows -->
Expand Down
2 changes: 1 addition & 1 deletion src/android/EmailComposer.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,4 @@ public void onRequestPermissionResult (int code, String[] permissions,
}
}

}
}
31 changes: 23 additions & 8 deletions src/android/EmailComposerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private Uri getUriForPath (String path, Context ctx) {
} else if (path.startsWith("app://")) {
return getUriForAppInternalPath(path, ctx);
} else if (path.startsWith("file:///")) {
return getUriForAbsolutePath(path);
return getUriForAbsolutePath(path, ctx);
} else if (path.startsWith("file://")) {
return getUriForAssetPath(path, ctx);
} else if (path.startsWith("base64:")) {
Expand All @@ -254,17 +254,18 @@ private Uri getUriForPath (String path, Context ctx) {
* The URI for a file.
*
* @param path The given absolute path.
* @param ctx The application context.
* @return The URI pointing to the given path.
*/
private Uri getUriForAbsolutePath (String path) {
private Uri getUriForAbsolutePath (String path, Context ctx) {
String absPath = path.replaceFirst("file://", "");
File file = new File(absPath);

if (!file.exists()) {
Log.e(LOG_TAG, "File not found: " + file.getAbsolutePath());
}

return Uri.fromFile(file);
return getUriForFile(ctx, file);
}

/**
Expand Down Expand Up @@ -310,7 +311,7 @@ private Uri getUriForAssetPath (String path, Context ctx) {
}
}

return Uri.fromFile(file);
return getUriForFile(ctx, file);
}

/**
Expand Down Expand Up @@ -350,7 +351,7 @@ private Uri getUriForAppInternalPath (String path, Context ctx) {
e.printStackTrace();
}

return Uri.fromFile(file);
return getUriForFile(ctx, file);
}

/**
Expand Down Expand Up @@ -401,7 +402,7 @@ private Uri getUriForResourcePath (String path, Context ctx) {
}
}

return Uri.fromFile(file);
return getUriForFile(ctx, file);
}

/**
Expand Down Expand Up @@ -451,7 +452,7 @@ private Uri getUriForBase64Content (String content, Context ctx) {
}
}

return Uri.fromFile(file);
return getUriForFile(ctx, file);
}

/**
Expand Down Expand Up @@ -503,6 +504,20 @@ private int getResId (String resPath, Context ctx) {
return resId;
}

/**
* Get content URI for the specified file.
*
* @param ctx The application context.
* @param file The file to get the URI.
*
* @return content://...
*/
private Uri getUriForFile(Context ctx, File file) {
String authority = ctx.getPackageName() + ".provider";

return EmailComposerProvider.getUriForFile(ctx, authority, file);
}

/**
* If email apps are available.
*
Expand Down Expand Up @@ -585,4 +600,4 @@ private static boolean safeClose (final FileOutputStream outStream) {
return false;
}

}
}
26 changes: 26 additions & 0 deletions src/android/EmailComposerProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 de.appplant.cordova.emailcomposer;

import android.support.v4.content.FileProvider;

public class EmailComposerProvider extends FileProvider {
// Nothing to do here
}
24 changes: 24 additions & 0 deletions src/android/xml/emailcomposer_provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
-->

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

0 comments on commit a569fcf

Please sign in to comment.