Skip to content

Instantly share code, notes, and snippets.

@StKotok
Created June 3, 2016 17:55
Show Gist options
  • Save StKotok/9c9550501310d156eaffc858ebe92e0f to your computer and use it in GitHub Desktop.
Save StKotok/9c9550501310d156eaffc858ebe92e0f to your computer and use it in GitHub Desktop.
package net.neatapps.avi.app.util;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import net.neatapps.avi.app.R;
import java.util.List;
public class Sharing {
public static void shareText(String textForShare, Activity activity) {
Intent intent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
} else {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
}
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, textForShare);
startShareIntent(intent, activity);
}
public static void shareJpegWithSubjectAndText(Activity activity, Uri imageUri, String subject, String text) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, imageUri);
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, text);
String messageToUser = activity.getString(R.string.choose_social);
activity.startActivity(Intent.createChooser(i, messageToUser));
}
public static void shareImage(Activity activity, Uri imageUri) {
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_STREAM, imageUri);
i.setType("image/jpeg");
try {
String messageToUser = activity.getString(R.string.choose_social);
activity.startActivity(Intent.createChooser(i, messageToUser));
} catch (android.content.ActivityNotFoundException ex) {
Utils.complain("Problem with sharing: ActivityNotFoundException", activity);
}
}
public static void shareToTwitter(Uri imageUri, View v) { // Need be checked
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.mipmap.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
}
public static void shareToWhatsapp(Uri imageUri, View v) { // Need be checked
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.mipmap.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("com.whatsapp")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
}
private static void startShareIntent(Intent launchIntent, Activity activity) {
if (launchIntent != null) {
final String action = launchIntent.getAction();
if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) {
if (Build.VERSION.SDK_INT >= 21) {
// If we're on Lollipop, we can open the intent as a document
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
} else {
// Else, we will use the old CLEAR_WHEN_TASK_RESET flag
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
}
}
activity.startActivity(launchIntent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment