Skip to content
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
13 changes: 8 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ version '1.0-SNAPSHOT'
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:7.3.0'
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 29
namespace 'com.example.fluttershare'
compileSdkVersion 33

defaultConfig {
minSdkVersion 16
targetSdkVersion 33
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

lintOptions {
disable 'InvalidPackage'
}
}

dependencies {
api 'commons-io:commons-io:2.6'
implementation 'androidx.core:core:1.3.2'
implementation 'androidx.core:core:1.9.0'
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.fluttershare;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
Expand All @@ -13,48 +14,63 @@
import java.util.ArrayList;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;


/** FlutterSharePlugin */
public class FlutterSharePlugin implements FlutterPlugin, MethodCallHandler {
public class FlutterSharePlugin implements FlutterPlugin, ActivityAware, MethodCallHandler {
private Context context;
private Activity activity;
private MethodChannel methodChannel;

public FlutterSharePlugin() {}
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
this.context = binding.getApplicationContext();
methodChannel = new MethodChannel(binding.getBinaryMessenger(), "flutter_share");
methodChannel.setMethodCallHandler(this);
}

/** Plugin registration. */
@SuppressWarnings("deprecation")
public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
final FlutterSharePlugin instance = new FlutterSharePlugin();
instance.onAttachedToEngine(registrar.context(), registrar.messenger());
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
if (methodChannel != null) {
methodChannel.setMethodCallHandler(null);
methodChannel = null;
}
context = null;
activity = null;
}

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
activity = binding.getActivity();
}

private void onAttachedToEngine(Context applicationContext, BinaryMessenger messenger) {
this.context = applicationContext;
methodChannel = new MethodChannel(messenger, "flutter_share");
methodChannel.setMethodCallHandler(this);
@Override
public void onDetachedFromActivityForConfigChanges() {
activity = null;
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
context = null;
methodChannel.setMethodCallHandler(null);
methodChannel = null;
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
activity = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
activity = null;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (activity == null) {
result.error("NO_ACTIVITY", "Share plugin requires a foreground activity.", null);
return;
}

if (call.method.equals("shareFile")) {
shareFile(call, result);
} else if (call.method.equals("share")) {
Expand All @@ -65,15 +81,13 @@ public void onMethodCall(MethodCall call, Result result) {
}

private void share(MethodCall call, Result result) {
try
{
try {
String title = call.argument("title");
String text = call.argument("text");
String linkUrl = call.argument("linkUrl");
String chooserTitle = call.argument("chooserTitle");

if (title == null || title.isEmpty())
{
if (title == null || title.isEmpty()) {
Log.println(Log.ERROR, "", "FlutterShare Error: Title null or empty");
result.error("FlutterShare: Title cannot be null or empty", null, null);
return;
Expand Down Expand Up @@ -105,28 +119,24 @@ private void share(MethodCall call, Result result) {
Intent chooserIntent = Intent.createChooser(intent, chooserTitle);
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooserIntent);
activity.startActivity(chooserIntent);

result.success(true);
}
catch (Exception ex)
{
} catch (Exception ex) {
Log.println(Log.ERROR, "", "FlutterShare: Error");
result.error(ex.getMessage(), null, null);
}
}

private void shareFile(MethodCall call, Result result) {
try
{
try {
String title = call.argument("title");
String text = call.argument("text");
String filePath = call.argument("filePath");
String fileType = call.argument("fileType");
String chooserTitle = call.argument("chooserTitle");

if (filePath == null || filePath.isEmpty())
{
if (filePath == null || filePath.isEmpty()) {
Log.println(Log.ERROR, "", "FlutterShare: ShareLocalFile Error: filePath null or empty");
result.error("FlutterShare: FilePath cannot be null or empty", null, null);
return;
Expand All @@ -149,12 +159,10 @@ private void shareFile(MethodCall call, Result result) {
Intent chooserIntent = Intent.createChooser(intent, chooserTitle);
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooserIntent);
activity.startActivity(chooserIntent);

result.success(true);
}
catch (Exception ex)
{
} catch (Exception ex) {
result.error(ex.getMessage(), null, null);
Log.println(Log.ERROR, "", "FlutterShare: Error");
}
Expand Down
2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"file_picker","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/file_picker-3.0.1/","dependencies":[]},{"name":"flutter_share","path":"/Users/britto/dev/projects/flutter_share/","dependencies":[]},{"name":"path_provider","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.1/","dependencies":[]}],"android":[{"name":"file_picker","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/file_picker-3.0.1/","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"flutter_plugin_android_lifecycle","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_plugin_android_lifecycle-2.0.1/","dependencies":[]},{"name":"flutter_share","path":"/Users/britto/dev/projects/flutter_share/","dependencies":[]},{"name":"path_provider","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.1/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-2.0.0/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-2.0.0/","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-2.0.0/","dependencies":[]}],"web":[{"name":"file_picker","path":"/Users/britto/flutter/.pub-cache/hosted/pub.dartlang.org/file_picker-3.0.1/","dependencies":[]}]},"dependencyGraph":[{"name":"file_picker","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"flutter_share","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2021-04-08 12:20:00.830702","version":"2.0.4"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"file_picker","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/file_picker-3.0.1/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"flutter_share","path":"/Users/abdullahtas/Desktop/flutter_share/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"path_provider","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/path_provider-2.0.1/","native_build":true,"dependencies":[],"dev_dependency":false}],"android":[{"name":"file_picker","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/file_picker-3.0.1/","native_build":true,"dependencies":["flutter_plugin_android_lifecycle"],"dev_dependency":false},{"name":"flutter_plugin_android_lifecycle","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/flutter_plugin_android_lifecycle-2.0.1/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"flutter_share","path":"/Users/abdullahtas/Desktop/flutter_share/","native_build":true,"dependencies":[],"dev_dependency":false},{"name":"path_provider","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/path_provider-2.0.1/","native_build":true,"dependencies":[],"dev_dependency":false}],"macos":[{"name":"path_provider_macos","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/path_provider_macos-2.0.0/","native_build":true,"dependencies":[],"dev_dependency":false}],"linux":[{"name":"path_provider_linux","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/path_provider_linux-2.0.0/","native_build":false,"dependencies":[],"dev_dependency":false}],"windows":[{"name":"path_provider_windows","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/path_provider_windows-2.0.0/","native_build":false,"dependencies":[],"dev_dependency":false}],"web":[{"name":"file_picker","path":"/Users/abdullahtas/.pub-cache/hosted/pub.dev/file_picker-3.0.1/","dependencies":[],"dev_dependency":false}]},"dependencyGraph":[{"name":"file_picker","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"flutter_share","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2025-04-17 23:58:30.732018","version":"3.29.3","swift_package_manager_enabled":{"ios":false,"macos":false}}
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
Loading