Skip to content

fixed bug, only share file once #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build/
ios/.generated/
packages
pubspec.lock
.vscode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ AdvancedShare.generic(
```
``` dart
AdvancedShare.generic(
url: "file:///storage/emulated/0/Download/test.txt"
url: "content://{applicationId}.adv_provider/shared/myfile.jpg"
);
```
``` dart
Expand Down
2 changes: 1 addition & 1 deletion android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
connection.project.dir=../example/android
connection.project.dir=
eclipse.preferences.version=1
3 changes: 2 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.mertcan.advancedshare">
<application>
<provider
<!-- Define your provider -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.adv_provider"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);
AdvancedSharePlugin instance = new AdvancedSharePlugin(registrar);
channel.setMethodCallHandler(instance);

}

@Override
Expand Down
16 changes: 9 additions & 7 deletions android/src/main/java/in/mertcan/advancedshare/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.io.IOException;

public class FileHelper {
private static final String SHARED_PROVIDER_AUTHORITY = ".adv_provider";
private static final String SHARED_FOLDER= "shared";
private final Registrar registrar;
private final String authorities;
private String url;
Expand All @@ -24,14 +26,14 @@ public class FileHelper {

public FileHelper(Registrar registrar, String url) {
this.registrar = registrar;
this.authorities = registrar.context().getPackageName() + ".adv_provider";
this.authorities = registrar.context().getPackageName() + SHARED_PROVIDER_AUTHORITY;
this.url = url;
this.uri = Uri.parse(this.url);
}

public FileHelper(Registrar registrar, String url, String type) {
this.registrar = registrar;
this.authorities = registrar.context().getPackageName() + ".adv_provider";
this.authorities = registrar.context().getPackageName() + SHARED_PROVIDER_AUTHORITY;
this.url = url;
this.uri = Uri.parse(url);
this.type = type;
Expand Down Expand Up @@ -106,19 +108,19 @@ private String getRealPath(Uri uri) {
public Uri getUri() {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(getType());

if (isBase64File()) {
final String tempPath = registrar.context().getCacheDir().getPath();
final String prefix = "" + System.currentTimeMillis() / 1000;
String encodedFile = uri.getSchemeSpecificPart()
.substring(uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
try {
File tempFile = new File(tempPath, prefix + "." + extension);
final FileOutputStream stream = new FileOutputStream(tempFile);
final File sharedFolder = new File(registrar.context().getFilesDir(), SHARED_FOLDER);
sharedFolder.mkdirs();
final File sharedFile = File.createTempFile(prefix, "." + extension, sharedFolder);
final FileOutputStream stream = new FileOutputStream(sharedFile);
stream.write(Base64.decode(encodedFile, Base64.DEFAULT));
stream.flush();
stream.close();
return FileProvider.getUriForFile(registrar.context(), authorities, tempFile);
return FileProvider.getUriForFile(registrar.context(), this.authorities, sharedFile);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,67 @@
package in.mertcan.advancedshare.shareintents;

import io.flutter.plugin.common.PluginRegistry.Registrar;

import android.content.Intent;

import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v4.app.ShareCompat;
import android.net.Uri;
import java.util.Map;

import java.util.List;
import in.mertcan.advancedshare.FileHelper;

public abstract class Base {
protected final Registrar registrar;
protected Map params;
protected String title = "Share";
protected FileHelper fileHelper;
protected Intent intent;
protected ShareCompat.IntentBuilder intentBuilder;

public Base(Registrar registrar) {
this.registrar = registrar;
this.intent = new Intent();
this.intent.setAction(Intent.ACTION_SEND);
this.intent.setType("text/plain");
this.intentBuilder = ShareCompat.IntentBuilder.from(this.registrar.activity());
}

public int share(Map params) {
this.params = params;
fileHelper = getFileHelper(params);

if (checkKey("title")) {
title = (String) params.get("title");
intentBuilder.setChooserTitle(title);
}

if (checkKey("msg")) {
intent.putExtra(Intent.EXTRA_TEXT, (String) params.get("msg"));
intentBuilder.setText((String) params.get("msg"));
}

if (checkKey("subject")) {
intent.putExtra(Intent.EXTRA_SUBJECT, (String) params.get("subject"));
intentBuilder.setSubject((String) params.get("subject"));
}

if (checkKey("url")) {
if (fileHelper.isFile()) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_STREAM, fileHelper.getUri());
intent.setType(fileHelper.getType());
final String SHARED_PROVIDER_AUTHORITY = registrar.context().getPackageName() + ".adv_provider";
final Uri uri = (Uri) fileHelper.getUri();
intentBuilder.setType(fileHelper.getType());
intentBuilder.setStream(uri);

List<ResolveInfo> resInfoList = this.registrar.context().getPackageManager().queryIntentActivities(intentBuilder.getIntent(), PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.registrar.context().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
}
return 0;
}

protected void openChooser() {
final Intent chooser = intentBuilder.createChooserIntent();
if (registrar.activity() == null) {
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
registrar.context().startActivity(chooser);
} else {
registrar.activity().startActivity(chooser);
}
}
protected void openSingleApplication(String packageName) {
final Intent intent = intentBuilder.getIntent();
intent.setPackage(packageName);
Intent chooser = Intent.createChooser(intent, title);
if (registrar.activity() == null) {
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Expand All @@ -57,7 +70,6 @@ protected void openChooser() {
registrar.activity().startActivity(chooser);
}
}

protected FileHelper getFileHelper(Map params) {
String url = "";
if (checkKey("url")) {
Expand All @@ -69,13 +81,10 @@ protected FileHelper getFileHelper(Map params) {
return new FileHelper(registrar, (String) url);
}
}

public boolean checkKey(String key) {
if (params != null && !params.isEmpty()) {
return params.get(key) != null;
}

return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public int share(Map params) {
super.share(params);
if (getPackage() != null) {
if (isPackageInstalled(getPackage(), registrar.context())) {
this.intent.setPackage(getPackage());
openChooser();
openSingleApplication(getPackage());
return 1;
} else {
return 2;
Expand Down
3 changes: 1 addition & 2 deletions android/src/main/res/xml/adv_share_provider_paths.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache_files" path="." />
<external-path name="external_files" path="."/>
<files-path name="shared" path="shared/" />
</paths>
2 changes: 1 addition & 1 deletion example/android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Wed May 02 23:10:33 EET 2018
connection.project.dir=
eclipse.preferences.version=1
4 changes: 2 additions & 2 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
Expand Down
Empty file modified example/android/gradlew
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions example/ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions example/ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
69 changes: 69 additions & 0 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}

def parse_KV_file(file, separator='=')
file_abs_path = File.expand_path(file)
if !File.exists? file_abs_path
return [];
end
pods_ary = []
skip_line_start_symbols = ["#", "/"]
File.foreach(file_abs_path) { |line|
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
plugin = line.split(pattern=separator)
if plugin.length == 2
podname = plugin[0].strip()
path = plugin[1].strip()
podpath = File.expand_path("#{path}", file_abs_path)
pods_ary.push({:name => podname, :path => podpath});
else
puts "Invalid plugin specification: #{line}"
end
}
return pods_ary
end

target 'Runner' do
# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
# referring to absolute paths on developers' machines.
system('rm -rf .symlinks')
system('mkdir -p .symlinks/plugins')

# Flutter Pods
generated_xcode_build_settings = parse_KV_file('./Flutter/Generated.xcconfig')
if generated_xcode_build_settings.empty?
puts "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first."
end
generated_xcode_build_settings.map { |p|
if p[:name] == 'FLUTTER_FRAMEWORK_DIR'
symlink = File.join('.symlinks', 'flutter')
File.symlink(File.dirname(p[:path]), symlink)
pod 'Flutter', :path => File.join(symlink, File.basename(p[:path]))
end
}

# Plugin Pods
plugin_pods = parse_KV_file('../.flutter-plugins')
plugin_pods.map { |p|
symlink = File.join('.symlinks', 'plugins', p[:name])
File.symlink(p[:path], symlink)
pod p[:name], :path => File.join(symlink, 'ios')
}
end

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
33 changes: 1 addition & 32 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,4 @@ flutter:

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
flutter: ">=0.1.4 <2.0.0"

# To add assets to your plugin package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.io/assets-and-images/#from-packages
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.

# To add custom fonts to your plugin package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.io/custom-fonts/#from-packages
flutter: ">=0.1.4 <2.0.0"