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
#### Version 0.8.3 (not yet released)
- 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 #710 crash due to >500 notifications (Android)
- Fixed #682 crash while resuming app from notification (Android 6)
......
......@@ -42,6 +42,7 @@ import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
/**
* Util class to map unified asset URIs to native URIs. URIs like file:///
......@@ -152,21 +153,15 @@ class AssetUtil {
* URI pointing to the given 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");
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 {
AssetManager assets = context.getAssets();
FileOutputStream outStream = new FileOutputStream(file);
......@@ -197,29 +192,19 @@ class AssetUtil {
* URI pointing to the given 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://", "");
int resId = getResIdForDrawable(resPath);
File file = getTmpFile();
if (resId == 0) {
Log.e("Asset", "File not found: " + resPath);
return Uri.EMPTY;
}
String resName = extractResourceName(resPath);
String extName = extractResourceExtension(resPath);
String storage = dir.toString() + STORAGE_FOLDER;
File file = new File(storage, resName + extName);
//noinspection ResultOfMethodCallIgnored
new File(storage).mkdir();
if (file == null) {
Log.e("Asset", "Missing external cache dir");
return Uri.EMPTY;
}
try {
Resources res = context.getResources();
......@@ -249,21 +234,13 @@ class AssetUtil {
* Uri of the downloaded file
*/
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");
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 {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
......@@ -343,7 +320,7 @@ class AssetUtil {
* Resource path as string
*/
int getResIdForDrawable(String clsName, String resPath) {
String drawable = extractResourceName(resPath);
String drawable = getBaseName(resPath);
int resId = 0;
try {
......@@ -396,7 +373,7 @@ class AssetUtil {
* @param resPath
* Resource path as string
*/
private String extractResourceName (String resPath) {
private String getBaseName (String resPath) {
String drawable = resPath;
if (drawable.contains("/")) {
......@@ -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
* Resource path as string
* @return
* File with a random UUID name
*/
private String extractResourceExtension (String resPath) {
String extName = "png";
private File getTmpFile () {
// 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