Skip to content

Instantly share code, notes, and snippets.

@czyrux
Last active August 29, 2015 14:16
Show Gist options
  • Save czyrux/7b1f21a2254e89785d00 to your computer and use it in GitHub Desktop.
Save czyrux/7b1f21a2254e89785d00 to your computer and use it in GitHub Desktop.
Example on how to share content with specific requirements in Android
/**
* Object holding information to share.
*
* @author Antonio Gutierrez <[email protected]> on 02/03/15.
*/
public class ShareableData {
private String subject;
private String html;
private String text;
private String url;
private String twitterAccount;
public ShareableData() { }
public String getSubject() {
return subject;
}
public void setSubject(final String subject) {
this.subject = subject;
}
public String getHtml() {
return html;
}
public void setHtml(final String html) {
this.html = html;
}
public String getText() {
return text;
}
public void setText(final String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(final String url) {
this.url = url;
}
public String getTwitterAccount() {
return twitterAccount;
}
public void setTwitterAccount(final String twitterAccount) {
this.twitterAccount = twitterAccount;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Parcelable;
import android.text.Html;
import android.text.TextUtils;
/**
* Utility class to share content in Android.
*
* @author Antonio Gutierrez <[email protected]> on 02/03/15.
*/
public class ShareHelper {
private static final String SHARE_TXT_HTML = "text/html";
private static final String SHARE_TXT_PLAIN = "text/plain";
private static final String TWITTER_PACKAGE = "com.twitter.android";
private ShareHelper() { }
private static boolean isAppAvailable(final Context context, final String appPackageName) {
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(appPackageName, 0);
return info != null;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
private static List<ResolveInfo> getAppsSupportingIntentType(final Context context, final String type) {
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(type);
return context.getPackageManager().queryIntentActivities(shareIntent, 0);
}
private static List<Intent> buildListIntents(final List<ResolveInfo> resInfo, final Intent baseIntent,
final Set<String> selectedPackageNames) {
List<Intent> targetedShareIntents = new ArrayList<>();
for (ResolveInfo resolveInfo : resInfo) {
final String packageName = resolveInfo.activityInfo.packageName;
if (selectedPackageNames.contains(packageName)) {
continue;
}
final Intent targetedShareIntent = (Intent) baseIntent.clone();
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
selectedPackageNames.add(packageName);
}
return targetedShareIntents;
}
private static List<Intent> buildIntentsForAppsSupportingType(final Activity context, final String intentType,
final Intent targetedShareIntent, final Set<String> selectedPackageNames) {
// Query app supporting Intent type
List<ResolveInfo> resInfo = getAppsSupportingIntentType(context, intentType);
if (resInfo.size() == 0) {
return Collections.emptyList();
}
return buildListIntents(resInfo, targetedShareIntent, selectedPackageNames);
}
private static List<Intent> buildIntentsForAppsSupportingPlainText(final Activity context, final ShareableData data,
final Set<String> selectedPackageNames) {
if (TextUtils.isEmpty(data.getUrl()) || TextUtils.isEmpty(data.getText())) {
return Collections.emptyList();
}
// Build base intent
final Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType(SHARE_TXT_PLAIN);
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, data.getText() + " " + data.getUrl());
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getSubject());
return buildIntentsForAppsSupportingType(context, SHARE_TXT_PLAIN, targetedShareIntent, selectedPackageNames);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static List<Intent> buildIntentsForAppsSupportingHtml(final Activity context, final ShareableData data,
final Set<String> selectedPackageNames) {
if (TextUtils.isEmpty(data.getHtml())) {
return Collections.emptyList();
}
// Build base intent
final Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType(SHARE_TXT_HTML);
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(data.getHtml()));
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getSubject());
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
targetedShareIntent.putExtra(Intent.EXTRA_HTML_TEXT, data.getHtml());
}
return buildIntentsForAppsSupportingType(context, SHARE_TXT_HTML, targetedShareIntent, selectedPackageNames);
}
public static Intent buildTwitterShareIntent(final ShareableData data) {
final Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType(SHARE_TXT_PLAIN);
targetedShareIntent.putExtra(Intent.EXTRA_TEXT,
data.getText() + "\n" + data.getUrl() + " " + data.getTwitterAccount());
targetedShareIntent.setPackage(TWITTER_PACKAGE);
return targetedShareIntent;
}
public static Intent buildShareIntent(final Activity context, final ShareableData data,
final String sharingHeader) {
final Set<String> selectedPackageNames = new HashSet<>();
final List<Intent> targetedShareIntents = new ArrayList<>();
// Special handling for Twitter App
if (isAppAvailable(context, TWITTER_PACKAGE)) {
targetedShareIntents.add(buildTwitterShareIntent(data));
selectedPackageNames.add(TWITTER_PACKAGE);
}
// App supporting HTML
targetedShareIntents.addAll(buildIntentsForAppsSupportingHtml(context, data, selectedPackageNames));
// App supporting simple text
targetedShareIntents.addAll(buildIntentsForAppsSupportingPlainText(context, data, selectedPackageNames));
Intent chooserIntent = Intent.createChooser(new Intent(Intent.ACTION_SEND), sharingHeader);
if (targetedShareIntents.size() > 0) {
// Grab first one so the chooser can match something and add rest as extra param
// Intent added when calling Intent#createChooser will be added at the end.
chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), sharingHeader);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
}
return chooserIntent;
}
}
public class SomeActivity extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Some code...
}
private void share() {
ShareableData data = new ShareableData();
data.setUrl("http://www.google.de");
data.setText("Example of cool sharing");
data.setTwitterAccount("@some_twitter_account");
data.setHtml("This <b>example</> is awesome. <a href=\"http://www.google.de\">google.de</a>");
data.setSubject("Look what I found!");
startActivity(ShareHelper.buildShareIntent(this, data, "Awesome header"));
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_share) {
share();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment