Commit a6165e59 authored by zhouteng's avatar zhouteng

将动态权限的检查封装到插件库中

parent 4eb8ee25
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zt.shareextend">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application>
<provider
......
......@@ -2,13 +2,19 @@ package com.zt.shareextend;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
......@@ -18,19 +24,24 @@ import java.util.Map;
/**
* Plugin method host for presenting a share sheet via Intent
*/
public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
public class ShareExtendPlugin implements MethodChannel.MethodCallHandler, PluginRegistry.RequestPermissionsResultListener {
/// the authorities for FileProvider
private static final String authorities = "com.zt.shareextend.fileprovider";
private static final int CODE_ASK_PERMISSION = 100;
private static final String CHANNEL = "share_extend";
private final Registrar mRegistrar;
private String text;
private String type;
public static void registerWith(Registrar registrar) {
MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);
ShareExtendPlugin instance = new ShareExtendPlugin(registrar);
final ShareExtendPlugin instance = new ShareExtendPlugin(registrar);
registrar.addRequestPermissionsResultListener(instance);
channel.setMethodCallHandler(instance);
}
private final Registrar mRegistrar;
private ShareExtendPlugin(Registrar registrar) {
this.mRegistrar = registrar;
......@@ -54,6 +65,8 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
if (text == null || text.isEmpty()) {
throw new IllegalArgumentException("Non-empty text expected");
}
this.text = text;
this.type = type;
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
......@@ -62,6 +75,14 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.setType("text/plain");
} else {
if (isPathInExternalStorage(text)) {
if (!checkPermisson()) {
requestPermission();
return;
}
}
File f = new File(text);
Uri uri = getUriForFile(mRegistrar.context(), f);
if ("file".equals(type)) {
......@@ -82,8 +103,32 @@ public class ShareExtendPlugin implements MethodChannel.MethodCallHandler {
}
}
private boolean isPathInExternalStorage(String path) {
File storagePath = Environment.getExternalStorageDirectory();
return path.startsWith(storagePath.getAbsolutePath());
}
private boolean checkPermisson() {
if (ContextCompat.checkSelfPermission(mRegistrar.context(), Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
}
private void requestPermission() {
ActivityCompat.requestPermissions(mRegistrar.activity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CODE_ASK_PERMISSION);
}
@Override
public boolean onRequestPermissionsResult(int requestCode, String[] perms, int[] grantResults) {
if (requestCode == CODE_ASK_PERMISSION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
share(text, type);
}
return false;
}
public static Uri getUriForFile(Context context, File file) {
private static Uri getUriForFile(Context context, File file) {
if (Build.VERSION.SDK_INT >= 24) {
return FileProvider.getUriForFile(context, authorities, file);
} else {
......
......@@ -3,7 +3,6 @@ import 'dart:io';
import 'package:share_extend/share_extend.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission/permission.dart';
import 'package:path_provider/path_provider.dart';
void main() => runApp(new MyApp());
......@@ -43,21 +42,11 @@ class _MyAppState extends State<MyApp> {
},
child: new Text("share image"),
),
new RaisedButton(
onPressed: () async {
File f =
await ImagePicker.pickImage(source: ImageSource.gallery);
ShareExtend.share(f.path, "file");
},
child: new Text("share file"),
),
new RaisedButton(
onPressed: () {
if (Platform.isAndroid) {
_shareFileWithPerm();
}
_shareApplicationDocumentsFile();
},
child: new Text("share android external storage file"),
child: new Text("share file"),
),
],
)),
......@@ -65,44 +54,14 @@ class _MyAppState extends State<MyApp> {
);
}
/// share the external storage file ,first check the permission
_shareFileWithPerm() async {
if (await _checkPermission()) {
_shareFile();
} else {
var result = await _requestPermission();
/// to ask for permission
if (result == PermissionStatus.allow) {
_shareFile();
}
}
}
/// create a test file for the share example
_shareFile() async {
String filePath = await _createTestFileToShare();
ShareExtend.share(filePath, "file");
}
///create a test file to share
_createTestFileToShare() async {
Directory storageDir = await getExternalStorageDirectory();
File testFile = new File("${storageDir.path}/flutter/test.txt");
///share the documents file
_shareApplicationDocumentsFile() async {
Directory dir = await getApplicationDocumentsDirectory();
File testFile = new File("${dir.path}/flutter/test.txt");
if (!await testFile.exists()) {
await testFile.create(recursive: true);
testFile.writeAsStringSync("test for share documents file");
}
testFile.writeAsStringSync("test for share");
return testFile.path;
}
_checkPermission() async {
List<Permissions> perms =
await Permission.getPermissionStatus([PermissionName.Storage]);
return perms[0].permissionStatus == PermissionStatus.allow;
}
_requestPermission() async {
return await Permission.requestSinglePermission(PermissionName.Storage);
ShareExtend.share(testFile.path, "file");
}
}
......@@ -21,8 +21,6 @@ dependencies:
cupertino_icons: ^0.1.2
image_picker: ^0.4.10
path_provider: ^0.4.1
permission: ^0.1.0
dev_dependencies:
flutter_test:
......
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