Commit 483292ab authored by zhouteng's avatar zhouteng

add grant uri permission on Android

parent 2f2ced98
package com.zt.shareextend; package com.zt.shareextend;
import android.Manifest; import android.Manifest;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
...@@ -72,7 +73,6 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -72,7 +73,6 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject); shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
ArrayList<Uri> uriList = new ArrayList<>();
if ("text".equals(type)) { if ("text".equals(type)) {
shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, list.get(0)); shareIntent.putExtra(Intent.EXTRA_TEXT, list.get(0));
...@@ -85,9 +85,10 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -85,9 +85,10 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
} }
} }
ArrayList<Uri> uriList = new ArrayList<>();
for (String path : list) { for (String path : list) {
File f = new File(path); File f = new File(path);
Uri uri = ShareUtils.getUriForFile(mRegistrar.activity(), f); Uri uri = ShareUtils.getUriForFile(getContext(), f);
uriList.add(uri); uriList.add(uri);
} }
...@@ -105,18 +106,18 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi ...@@ -105,18 +106,18 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, Plugi
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
} }
ShareUtils.grantUriPermission(getContext(), uriList, shareIntent);
} }
startChooserActivity(uriList.get(0), shareIntent, sharePanelTitle); startChooserActivity(shareIntent, sharePanelTitle);
} }
private void startChooserActivity(Uri uri, Intent shareIntent,String sharePanelTitle) { private Context getContext() {
Intent chooserIntent = Intent.createChooser(shareIntent, sharePanelTitle /* dialog subject optional */); return mRegistrar.activity() != null ? mRegistrar.activity() : mRegistrar.context();
}
private void startChooserActivity(Intent shareIntent, String sharePanelTitle) {
Intent chooserIntent = Intent.createChooser(shareIntent, sharePanelTitle);
if (mRegistrar.activity() != null) { if (mRegistrar.activity() != null) {
List<ResolveInfo> resInfoList = mRegistrar.activity().getPackageManager().queryIntentActivities(chooserIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
mRegistrar.activity().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
mRegistrar.activity().startActivity(chooserIntent); mRegistrar.activity().startActivity(chooserIntent);
} else { } else {
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
......
package com.zt.shareextend; package com.zt.shareextend;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
...@@ -26,6 +29,14 @@ class ShareUtils { ...@@ -26,6 +29,14 @@ class ShareUtils {
return uri; return uri;
} }
static void grantUriPermission(Context context, List<Uri> uriList, Intent intent) {
for (Uri uri : uriList) {
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resolveInfos) {
context.grantUriPermission(resolveInfo.activityInfo.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
}
static boolean shouldRequestPermission(List<String> pathList) { static boolean shouldRequestPermission(List<String> pathList) {
for (String path : pathList) { for (String path : pathList) {
......
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