Bläddra i källkod

Solve Android security exposed app error

Augusto 6 år sedan
förälder
incheckning
a88b59f0e9

+ 2
- 2
android/gradle/wrapper/gradle-wrapper.properties Visa fil

@@ -1,6 +1,6 @@
1
-#Sat Aug 12 07:48:35 CEST 2017
1
+#Fri Jun 01 10:33:07 BRT 2018
2 2
 distributionBase=GRADLE_USER_HOME
3 3
 distributionPath=wrapper/dists
4 4
 zipStoreBase=GRADLE_USER_HOME
5 5
 zipStorePath=wrapper/dists
6
-distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
6
+distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

+ 31
- 3
android/src/main/AndroidManifest.xml Visa fil

@@ -1,9 +1,37 @@
1 1
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2 2
     package="com.RNFetchBlob">
3 3
 
4
-    <application
5
-        android:label="@string/app_name">
4
+    <!-- Required to access Google Play Licensing -->
5
+    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
6 6
 
7
+    <!-- Required to download files from Google Play -->
8
+    <uses-permission android:name="android.permission.INTERNET" />
9
+
10
+    <!-- Required to keep CPU alive while downloading files
11
+        (NOT to keep screen awake) -->
12
+    <uses-permission android:name="android.permission.WAKE_LOCK" />
13
+
14
+    <!-- Required to poll the state of the network connection
15
+        and respond to changes -->
16
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
17
+
18
+    <!-- Required to check whether Wi-Fi is enabled -->
19
+    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
20
+
21
+    <!-- Required to read and write the expansion files on shared storage -->
22
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
23
+
24
+    <application android:label="@string/app_name">
25
+
26
+        <provider
27
+            android:name="android.support.v4.content.FileProvider"
28
+            android:authorities="${applicationId}.provider"
29
+            android:exported="false"
30
+            android:grantUriPermissions="true">
31
+            <meta-data
32
+                android:name="android.support.FILE_PROVIDER_PATHS"
33
+                android:resource="@xml/provider_paths" />
34
+        </provider>
7 35
     </application>
8 36
 
9
-</manifest>
37
+</manifest>

+ 29
- 4
android/src/main/java/com/RNFetchBlob/RNFetchBlob.java Visa fil

@@ -3,7 +3,10 @@ package com.RNFetchBlob;
3 3
 import android.app.Activity;
4 4
 import android.app.DownloadManager;
5 5
 import android.content.Intent;
6
+import android.content.pm.PackageManager;
6 7
 import android.net.Uri;
8
+import android.os.Build;
9
+import android.support.v4.content.FileProvider;
7 10
 import android.util.SparseArray;
8 11
 
9 12
 import com.facebook.react.bridge.ActivityEventListener;
@@ -21,9 +24,12 @@ import com.facebook.react.bridge.WritableMap;
21 24
 import com.facebook.react.modules.network.ForwardingCookieHandler;
22 25
 import com.facebook.react.modules.network.CookieJarContainer;
23 26
 import com.facebook.react.modules.network.OkHttpClientProvider;
27
+import com.squareup.okhttp.OkHttpClient;
28
+
24 29
 import okhttp3.OkHttpClient;
25 30
 import okhttp3.JavaNetCookieJar;
26 31
 
32
+import java.io.File;
27 33
 import java.util.HashMap;
28 34
 import java.util.Map;
29 35
 import java.util.concurrent.LinkedBlockingQueue;
@@ -105,10 +111,29 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
105 111
     @ReactMethod
106 112
     public void actionViewIntent(String path, String mime, final Promise promise) {
107 113
         try {
108
-            Intent intent= new Intent(Intent.ACTION_VIEW)
109
-                    .setDataAndType(Uri.parse("file://" + path), mime);
110
-            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111
-            this.getReactApplicationContext().startActivity(intent);
114
+            Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(),
115
+                    this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
116
+
117
+            if (Build.VERSION.SDK_INT >= 24) {
118
+                // Create the intent with data and type
119
+                Intent intent = new Intent(Intent.ACTION_VIEW)
120
+                        .setDataAndType(uriForFile, mime);
121
+
122
+                // Set flag to give temporary permission to external app to use FileProvider
123
+                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
124
+
125
+                // Validate that the device can open the file
126
+                PackageManager pm = getCurrentActivity().getPackageManager();
127
+                if (intent.resolveActivity(pm) != null) {
128
+                    this.getReactApplicationContext().startActivity(intent);
129
+                }
130
+
131
+            } else {
132
+                Intent intent = new Intent(Intent.ACTION_VIEW)
133
+                        .setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
134
+
135
+                this.getReactApplicationContext().startActivity(intent);
136
+            }
112 137
             ActionViewVisible = true;
113 138
 
114 139
             final LifecycleEventListener listener = new LifecycleEventListener() {

+ 9
- 0
android/src/main/res/xml/provider_paths.xml Visa fil

@@ -0,0 +1,9 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<paths xmlns:android="http://schemas.android.com/apk/res/android">
3
+    <external-path
4
+        name="external_files"
5
+        path="." />
6
+    <files-path
7
+        name="files-path"
8
+        path="." /> <!-- Used to access into application data files -->
9
+</paths>