From 2ead631ace1fe111e53ff3235a175f0f27169572 Mon Sep 17 00:00:00 2001 From: amoreno Date: Wed, 29 Nov 2017 11:57:40 +0100 Subject: [PATCH 1/3] Solve Android security exposed app error --- .../gradle/wrapper/gradle-wrapper.properties | 4 +- android/src/main/AndroidManifest.xml | 10 +++++ .../java/com/RNFetchBlob/RNFetchBlob.java | 37 ++++++++++++++++--- android/src/main/res/xml/provider_paths.xml | 4 ++ 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 android/src/main/res/xml/provider_paths.xml diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 7e9ac9256..a6a64ce25 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed May 18 12:33:41 CST 2016 +#Wed Nov 29 11:45:41 CET 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index f99bdc6b0..a2ac361f1 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -30,6 +30,16 @@ android:name=".RNFetchBlobService" > + + + + diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java index cb3c4f269..643ec9db4 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java @@ -3,7 +3,10 @@ import android.app.Activity; import android.app.DownloadManager; import android.content.Intent; +import android.content.pm.PackageManager; import android.net.Uri; +import android.os.Build; +import android.support.v4.content.FileProvider; import com.RNFetchBlob.Utils.DataConverter; import com.facebook.react.bridge.ActivityEventListener; @@ -20,6 +23,7 @@ import com.facebook.react.modules.network.ForwardingCookieHandler; import com.facebook.react.modules.network.OkHttpClientProvider; +import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.concurrent.LinkedBlockingQueue; @@ -101,16 +105,36 @@ public void run() { @ReactMethod public void actionViewIntent(String path, String mime, final Promise promise) { try { - Intent intent= new Intent(Intent.ACTION_VIEW) - .setDataAndType(Uri.parse("file://" + path), mime); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - this.getReactApplicationContext().startActivity(intent); + Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(), "com.RNFetchBlob.provider", + new File(path)); + + if (Build.VERSION.SDK_INT >= 24) { + // Create the intent with data and type + Intent intent = new Intent(Intent.ACTION_VIEW) + .setDataAndType(uriForFile, mime); + + // Set flag to give temporary permission to external app to use FileProvider + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + + // Validate that the device can open the file + PackageManager pm = getCurrentActivity().getPackageManager(); + if (intent.resolveActivity(pm) != null) { + this.getReactApplicationContext().startActivity(intent); + } + + } else { + Intent intent = new Intent(Intent.ACTION_VIEW) + .setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + this.getReactApplicationContext().startActivity(intent); + } + ActionViewVisible = true; final LifecycleEventListener listener = new LifecycleEventListener() { @Override public void onHostResume() { - if(ActionViewVisible) + if (ActionViewVisible) promise.resolve(null); RCTContext.removeLifecycleEventListener(this); } @@ -126,7 +150,8 @@ public void onHostDestroy() { } }; RCTContext.addLifecycleEventListener(listener); - } catch(Exception ex) { + } catch (Exception ex) { + ex.printStackTrace(); promise.reject(ex.getLocalizedMessage()); } } diff --git a/android/src/main/res/xml/provider_paths.xml b/android/src/main/res/xml/provider_paths.xml new file mode 100644 index 000000000..ffa74ab56 --- /dev/null +++ b/android/src/main/res/xml/provider_paths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file From 0b3152a2b22a350e2c1fd6163d9a46554d02e5f3 Mon Sep 17 00:00:00 2001 From: amoreno Date: Wed, 29 Nov 2017 12:18:02 +0100 Subject: [PATCH 2/3] Refactor code --- android/src/main/java/com/RNFetchBlob/RNFetchBlob.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java index 643ec9db4..8a8fdf345 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java @@ -151,7 +151,6 @@ public void onHostDestroy() { }; RCTContext.addLifecycleEventListener(listener); } catch (Exception ex) { - ex.printStackTrace(); promise.reject(ex.getLocalizedMessage()); } } From 64b1c616f5d82a53c4bda0743178996dc4472bd2 Mon Sep 17 00:00:00 2001 From: amoreno Date: Tue, 9 Jan 2018 14:11:47 +0100 Subject: [PATCH 3/3] Fix INSTALL_FAILED_CONFLICTING_PROVIDER error --- android/src/main/AndroidManifest.xml | 4 ++-- android/src/main/java/com/RNFetchBlob/RNFetchBlob.java | 4 ++-- android/src/main/res/xml/provider_paths.xml | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index a2ac361f1..5b996b1e5 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -33,9 +33,9 @@ + android:grantUriPermissions="true" /> diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java index 8a8fdf345..cd77e037e 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java @@ -105,8 +105,8 @@ public void run() { @ReactMethod public void actionViewIntent(String path, String mime, final Promise promise) { try { - Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(), "com.RNFetchBlob.provider", - new File(path)); + Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(), + this.getReactApplicationContext().getPackageName() + ".provider", new File(path)); if (Build.VERSION.SDK_INT >= 24) { // Create the intent with data and type diff --git a/android/src/main/res/xml/provider_paths.xml b/android/src/main/res/xml/provider_paths.xml index ffa74ab56..7443816be 100644 --- a/android/src/main/res/xml/provider_paths.xml +++ b/android/src/main/res/xml/provider_paths.xml @@ -1,4 +1,5 @@ + \ No newline at end of file