Commit 4f4147d0 by Sebastián Katzer

Fixes #588 crash when basename & extension can't be extracted

parent 52054f5d
...@@ -5,6 +5,7 @@ Please also read the [Upgrade Guide](https://github.com/katzer/cordova-plugin-lo ...@@ -5,6 +5,7 @@ Please also read the [Upgrade Guide](https://github.com/katzer/cordova-plugin-lo
#### Version 0.8.3 (not yet released) #### Version 0.8.3 (not yet released)
- New "quarter" intervall for iOS & Android - New "quarter" intervall for iOS & Android
- Fixed #588 crash when basename & extension can't be extracted (Android)
- Fixed #732 loop between update and trigger (Android) - Fixed #732 loop between update and trigger (Android)
- Fixed #710 crash due to >500 notifications (Android) - Fixed #710 crash due to >500 notifications (Android)
- Fixed #682 crash while resuming app from notification (Android 6) - Fixed #682 crash while resuming app from notification (Android 6)
......
...@@ -42,6 +42,7 @@ import java.io.OutputStream; ...@@ -42,6 +42,7 @@ import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.UUID;
/** /**
* Util class to map unified asset URIs to native URIs. URIs like file:/// * Util class to map unified asset URIs to native URIs. URIs like file:///
...@@ -152,21 +153,15 @@ class AssetUtil { ...@@ -152,21 +153,15 @@ class AssetUtil {
* URI pointing to the given path * URI pointing to the given path
*/ */
private Uri getUriFromAsset(String path) { private Uri getUriFromAsset(String path) {
File dir = context.getExternalCacheDir(); String resPath = path.replaceFirst("file:/", "www");
String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
File file = getTmpFile(fileName);
if (dir == null) { if (file == null) {
Log.e("Asset", "Missing external cache dir"); Log.e("Asset", "Missing external cache dir");
return Uri.EMPTY; return Uri.EMPTY;
} }
String resPath = path.replaceFirst("file:/", "www");
String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
String storage = dir.toString() + STORAGE_FOLDER;
File file = new File(storage, fileName);
//noinspection ResultOfMethodCallIgnored
new File(storage).mkdir();
try { try {
AssetManager assets = context.getAssets(); AssetManager assets = context.getAssets();
FileOutputStream outStream = new FileOutputStream(file); FileOutputStream outStream = new FileOutputStream(file);
...@@ -197,29 +192,19 @@ class AssetUtil { ...@@ -197,29 +192,19 @@ class AssetUtil {
* URI pointing to the given path * URI pointing to the given path
*/ */
private Uri getUriForResourcePath(String path) { private Uri getUriForResourcePath(String path) {
File dir = context.getExternalCacheDir();
if (dir == null) {
Log.e("Asset", "Missing external cache dir");
return Uri.EMPTY;
}
String resPath = path.replaceFirst("res://", ""); String resPath = path.replaceFirst("res://", "");
int resId = getResIdForDrawable(resPath); int resId = getResIdForDrawable(resPath);
File file = getTmpFile();
if (resId == 0) { if (resId == 0) {
Log.e("Asset", "File not found: " + resPath); Log.e("Asset", "File not found: " + resPath);
return Uri.EMPTY; return Uri.EMPTY;
} }
String resName = extractResourceName(resPath); if (file == null) {
String extName = extractResourceExtension(resPath); Log.e("Asset", "Missing external cache dir");
String storage = dir.toString() + STORAGE_FOLDER; return Uri.EMPTY;
File file = new File(storage, resName + extName); }
//noinspection ResultOfMethodCallIgnored
new File(storage).mkdir();
try { try {
Resources res = context.getResources(); Resources res = context.getResources();
...@@ -249,21 +234,13 @@ class AssetUtil { ...@@ -249,21 +234,13 @@ class AssetUtil {
* Uri of the downloaded file * Uri of the downloaded file
*/ */
private Uri getUriFromRemote(String path) { private Uri getUriFromRemote(String path) {
File dir = context.getExternalCacheDir(); File file = getTmpFile();
if (dir == null) { if (file == null) {
Log.e("Asset", "Missing external cache dir"); Log.e("Asset", "Missing external cache dir");
return Uri.EMPTY; return Uri.EMPTY;
} }
String resName = extractResourceName(path);
String extName = extractResourceExtension(path);
String storage = dir.toString() + STORAGE_FOLDER;
File file = new File(storage, resName + extName);
//noinspection ResultOfMethodCallIgnored
new File(storage).mkdir();
try { try {
URL url = new URL(path); URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
...@@ -343,7 +320,7 @@ class AssetUtil { ...@@ -343,7 +320,7 @@ class AssetUtil {
* Resource path as string * Resource path as string
*/ */
int getResIdForDrawable(String clsName, String resPath) { int getResIdForDrawable(String clsName, String resPath) {
String drawable = extractResourceName(resPath); String drawable = getBaseName(resPath);
int resId = 0; int resId = 0;
try { try {
...@@ -396,7 +373,7 @@ class AssetUtil { ...@@ -396,7 +373,7 @@ class AssetUtil {
* @param resPath * @param resPath
* Resource path as string * Resource path as string
*/ */
private String extractResourceName (String resPath) { private String getBaseName (String resPath) {
String drawable = resPath; String drawable = resPath;
if (drawable.contains("/")) { if (drawable.contains("/")) {
...@@ -411,19 +388,39 @@ class AssetUtil { ...@@ -411,19 +388,39 @@ class AssetUtil {
} }
/** /**
* Extract extension of drawable resource from path. * Returns a file located under the external cache dir of that app.
* *
* @param resPath * @return
* Resource path as string * File with a random UUID name
*/ */
private String extractResourceExtension (String resPath) { private File getTmpFile () {
String extName = "png"; // If random UUID is not be enough see
// https://github.com/LukePulverenti/cordova-plugin-local-notifications/blob/267170db14044cbeff6f4c3c62d9b766b7a1dd62/src/android/notification/AssetUtil.java#L255
return getTmpFile(UUID.randomUUID().toString());
}
if (resPath.contains(".")) { /**
extName = resPath.substring(resPath.lastIndexOf('.')); * Returns a file located under the external cache dir of that app.
*
* @param name
* The name of the file
* @return
* File with the provided name
*/
private File getTmpFile (String name) {
File dir = context.getExternalCacheDir();
if (dir == null) {
Log.e("Asset", "Missing external cache dir");
return null;
} }
return extName; String storage = dir.toString() + STORAGE_FOLDER;
//noinspection ResultOfMethodCallIgnored
new File(storage).mkdir();
return new File(storage, name);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment