Commit b953e96a by Sebastián Katzer

Use android.resource:// scheme for res:// uri

parent da5b82fc
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
package de.appplant.cordova.plugin.notification.util; package de.appplant.cordova.plugin.notification.util;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
...@@ -62,7 +63,7 @@ public class AssetUtil { ...@@ -62,7 +63,7 @@ public class AssetUtil {
/** /**
* Constructor * Constructor
* *
* @param context Application context * @param context Application context.
*/ */
private AssetUtil(Context context) { private AssetUtil(Context context) {
this.context = context; this.context = context;
...@@ -71,7 +72,7 @@ public class AssetUtil { ...@@ -71,7 +72,7 @@ public class AssetUtil {
/** /**
* Static method to retrieve class instance. * Static method to retrieve class instance.
* *
* @param context Application context * @param context Application context.
*/ */
public static AssetUtil getInstance(Context context) { public static AssetUtil getInstance(Context context) {
return new AssetUtil(context); return new AssetUtil(context);
...@@ -80,8 +81,7 @@ public class AssetUtil { ...@@ -80,8 +81,7 @@ public class AssetUtil {
/** /**
* The URI for a path. * The URI for a path.
* *
* @param path * @param path The given path.
* The given path
*/ */
public Uri parse (String path) { public Uri parse (String path) {
if (path == null || path.isEmpty()) { if (path == null || path.isEmpty()) {
...@@ -102,11 +102,9 @@ public class AssetUtil { ...@@ -102,11 +102,9 @@ public class AssetUtil {
/** /**
* URI for a file. * URI for a file.
* *
* @param path * @param path Absolute path like file:///...
* Absolute path like file:///...
* *
* @return * @return URI pointing to the given path.
* URI pointing to the given path
*/ */
private Uri getUriFromPath(String path) { private Uri getUriFromPath(String path) {
String absPath = path.replaceFirst("file://", ""); String absPath = path.replaceFirst("file://", "");
...@@ -123,11 +121,9 @@ public class AssetUtil { ...@@ -123,11 +121,9 @@ public class AssetUtil {
/** /**
* URI for an asset. * URI for an asset.
* *
* @param path * @param path Asset path like file://...
* Asset path like file://...
* *
* @return * @return URI pointing to the given path.
* URI pointing to the given path
*/ */
private Uri getUriFromAsset(String path) { private Uri getUriFromAsset(String path) {
String resPath = path.replaceFirst("file:/", "www"); String resPath = path.replaceFirst("file:/", "www");
...@@ -162,53 +158,34 @@ public class AssetUtil { ...@@ -162,53 +158,34 @@ public class AssetUtil {
/** /**
* The URI for a resource. * The URI for a resource.
* *
* @param path * @param path The given relative path.
* The given relative path
* *
* @return * @return URI pointing to the given path.
* URI pointing to the given path
*/ */
private Uri getUriForResourcePath(String path) { private Uri getUriForResourcePath(String path) {
Resources res = context.getResources();
String resPath = path.replaceFirst("res://", ""); String resPath = path.replaceFirst("res://", "");
int resId = getResId(resPath); int resId = getResId(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;
} }
if (file == null) { return new Uri.Builder()
Log.e("Asset", "Missing external cache dir"); .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
return Uri.EMPTY; .authority(res.getResourcePackageName(resId))
} .appendPath(res.getResourceTypeName(resId))
.appendPath(res.getResourceEntryName(resId))
try { .build();
Resources res = context.getResources();
FileOutputStream outStream = new FileOutputStream(file);
InputStream inputStream = res.openRawResource(resId);
copyFile(inputStream, outStream);
outStream.flush();
outStream.close();
return Uri.fromFile(file);
} catch (Exception e) {
e.printStackTrace();
}
return Uri.EMPTY;
} }
/** /**
* Uri from remote located content. * Uri from remote located content.
* *
* @param path * @param path Remote address.
* Remote address
* *
* @return * @return Uri of the downloaded file.
* Uri of the downloaded file
*/ */
private Uri getUriFromRemote(String path) { private Uri getUriFromRemote(String path) {
File file = getTmpFile(); File file = getTmpFile();
...@@ -258,10 +235,8 @@ public class AssetUtil { ...@@ -258,10 +235,8 @@ public class AssetUtil {
/** /**
* Copy content from input stream into output stream. * Copy content from input stream into output stream.
* *
* @param in * @param in The input stream.
* The input stream * @param out The output stream.
* @param out
* The output stream
*/ */
private void copyFile(InputStream in, OutputStream out) throws IOException { private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
...@@ -324,8 +299,7 @@ public class AssetUtil { ...@@ -324,8 +299,7 @@ public class AssetUtil {
/** /**
* Extract name of drawable resource from path. * Extract name of drawable resource from path.
* *
* @param resPath * @param resPath Resource path as string.
* Resource path as string
*/ */
private String getBaseName (String resPath) { private String getBaseName (String resPath) {
String drawable = resPath; String drawable = resPath;
...@@ -344,8 +318,7 @@ public class AssetUtil { ...@@ -344,8 +318,7 @@ public class AssetUtil {
/** /**
* Returns a file located under the external cache dir of that app. * Returns a file located under the external cache dir of that app.
* *
* @return * @return File with a random UUID name.
* File with a random UUID name
*/ */
private File getTmpFile () { private File getTmpFile () {
// If random UUID is not be enough see // If random UUID is not be enough see
...@@ -356,10 +329,9 @@ public class AssetUtil { ...@@ -356,10 +329,9 @@ public class AssetUtil {
/** /**
* Returns a file located under the external cache dir of that app. * Returns a file located under the external cache dir of that app.
* *
* @param name * @param name The name of the file.
* The name of the file *
* @return * @return File with the provided name.
* File with the provided name
*/ */
private File getTmpFile (String name) { private File getTmpFile (String name) {
File dir = context.getExternalCacheDir(); File dir = context.getExternalCacheDir();
......
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