Skip to content
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

V2.0.1 #116

Open
wants to merge 10 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
28 changes: 28 additions & 0 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ android {
}

dependencies {
implementation 'com.facebook.android:facebook-share:5.15.3'
implementation 'com.twitter.sdk.android:twitter:3.1.1' //twitter share
implementation 'com.facebook.android:facebook-share:17.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.content.pm.ResolveInfo;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
Expand All @@ -17,14 +19,16 @@
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.MessageDialog;
import com.facebook.share.widget.ShareDialog;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
Expand All @@ -44,10 +48,17 @@ public class FlutterShareMePlugin implements MethodCallHandler, FlutterPlugin, A
final private static String _methodWhatsApp = "whatsapp_share";
final private static String _methodWhatsAppPersonal = "whatsapp_personal";
final private static String _methodWhatsAppBusiness = "whatsapp_business_share";

final private static String _methodFaceBook = "facebook_share";
final private static String _methodMessenger = "messenger_share";

final private static String _methodTwitter = "twitter_share";

final private static String _methodSystemShare = "system_share";

final private static String _methodInstagramShare = "instagram_share";
final private static String _methodInstagramShareText = "instagram_share_text";

final private static String _methodTelegramShare = "telegram_share";


Expand Down Expand Up @@ -90,17 +101,20 @@ private void onAttachedToEngine(BinaryMessenger messenger) {
*/
@Override
public void onMethodCall(MethodCall call, @NonNull Result result) {
String url, msg;
String url, msg, fileType;
switch (call.method) {
case _methodFaceBook:
url = call.argument("url");
msg = call.argument("msg");
shareToFacebook(url, msg, result);
break;
case _methodMessenger:
msg = call.argument("msg");
shareToMessenger( msg, result);
break;
case _methodTwitter:
url = call.argument("url");
msg = call.argument("msg");
shareToTwitter(url, msg, result);
shareToTwitter(msg, result);
break;
case _methodWhatsApp:
msg = call.argument("msg");
Expand All @@ -123,7 +137,12 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
break;
case _methodInstagramShare:
msg = call.argument("url");
shareInstagramStory(msg, result);
fileType = call.argument("fileType");
shareInstagramStory(msg, fileType, result);
break;
case _methodInstagramShareText:
msg = call.argument("msg");
shareInstagramText(msg, result);
break;
case _methodTelegramShare:
msg = call.argument("msg");
Expand Down Expand Up @@ -156,23 +175,48 @@ private void shareSystem(Result result, String msg) {
/**
* share to twitter
*
* @param url String
* @param msg String
* @param result Result
*/
private void shareToTwitter(String msg, Result result) {
if (twitterInstalled()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();

private void shareToTwitter(String url, String msg, Result result) {
try {
TweetComposer.Builder builder = new TweetComposer.Builder(activity)
.text(msg);
if (url != null && url.length() > 0) {
builder.url(new URL(url));
Intent textIntent = new Intent(Intent.ACTION_SEND);
textIntent.setType("text/plain");
textIntent.putExtra(Intent.EXTRA_TEXT, msg);

List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(textIntent, 0);

for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;

Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, msg);
targetedShareIntent.setPackage(packageName);

if (packageName.equals("com.twitter.android")) { // Add only instagram
targetedShareIntents.add(targetedShareIntent);
}
}

builder.show();
result.success("success");
} catch (MalformedURLException e) {
e.printStackTrace();
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select how to share");

chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

try {
activity.startActivity(chooserIntent);
result.success("Success");
} catch (ActivityNotFoundException e) {
e.printStackTrace();
result.success("Failure");
}

} else {
result.error("Twitter not found", "Twitter is not installed on device.", "");
}
}

Expand Down Expand Up @@ -215,6 +259,53 @@ public void onError(FacebookException error) {

}

/**
* share to Messenger
*
* @param msg String
* @param result Result
*/
private void shareToMessenger(String msg, Result result) {
if (messengerInstalled()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();

Intent textIntent = new Intent(Intent.ACTION_SEND);
textIntent.setType("text/plain");
textIntent.putExtra(Intent.EXTRA_TEXT, msg);

List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(textIntent, 0);

for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;

Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, msg);
targetedShareIntent.setPackage(packageName);

if (packageName.equals("com.facebook.orca")) { // Add only instagram
targetedShareIntents.add(targetedShareIntent);
}
}

Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select how to share");

chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

try {
activity.startActivity(chooserIntent);
result.success("Success");
} catch (ActivityNotFoundException e) {
e.printStackTrace();
result.success("Failure");
}

} else {
result.error("Messenger not found", "Messenger is not installed on device.", "");
}
}
/**
* share to whatsapp
*
Expand Down Expand Up @@ -294,16 +385,20 @@ private void shareWhatsAppPersonal(String msg, String phoneNumber, Result result
/**
* share to instagram
*
* @param url local image path
* @param result flutterResult
* @param url local file path
* @param fileType type of file to share (image or video)
* @param result flutterResult
*/
private void shareInstagramStory(String url, Result result) {
private void shareInstagramStory(String url, String fileType, Result result) {
if (instagramInstalled()) {
File file = new File(url);
Uri fileUri = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", file);

Intent instagramIntent = new Intent(Intent.ACTION_SEND);
instagramIntent.setType("image/*");
if(fileType.equals("image"))
instagramIntent.setType("image/*");
else if(fileType.equals("video"))
instagramIntent.setType("video/*");
instagramIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
instagramIntent.setPackage("com.instagram.android");
try {
Expand All @@ -318,6 +413,53 @@ private void shareInstagramStory(String url, Result result) {
}
}

private void shareInstagramText(String msg, Result result) {
if (instagramInstalled()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();

Intent textIntent = new Intent(Intent.ACTION_SEND);
textIntent.setType("text/plain");
textIntent.putExtra(Intent.EXTRA_TEXT, msg);

List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(textIntent, 0);

for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;

Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, msg);
targetedShareIntent.setPackage(packageName);

if (packageName.equals("com.instagram.android")) { // Add only instagram
targetedShareIntents.add(targetedShareIntent);
}
}

//Intent i = new Intent(this);
//targetedShareIntents.add(i);

Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select how to share");

chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

try {
activity.startActivity(chooserIntent);
result.success("Success");
} catch (ActivityNotFoundException e) {
e.printStackTrace();
result.success("Failure");
}



} else {
result.error("Instagram not found", "Instagram is not installed on device.", "");
}
}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
activity = binding.getActivity();
Expand Down Expand Up @@ -352,6 +494,38 @@ private boolean instagramInstalled() {
} catch (PackageManager.NameNotFoundException e) {
return false;
}
// return false;
}

private boolean twitterInstalled() {
try {
if (activity != null) {
activity.getPackageManager()
.getApplicationInfo("com.twitter.android", 0);
return true;
} else {
Log.d("App", "Twitter app is not installed on your device");
return false;
}
} catch (PackageManager.NameNotFoundException e) {
return false;
}
// return false;
}

private boolean messengerInstalled() {
try {
if (activity != null) {
activity.getPackageManager()
.getApplicationInfo("com.facebook.orca", 0);
return true;
} else {
Log.d("App", "Messenger app is not installed on your device");
return false;
}
} catch (PackageManager.NameNotFoundException e) {
return false;
}
// return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package zhuoyuan.li.example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>8.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
platform :ios, '9.0'
# platform :ios, '9.0'

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