Skip to content

Instantly share code, notes, and snippets.

@MensObscura
Forked from Nutomic/CustomChooserIntent.java
Created March 31, 2016 12:21
Show Gist options
  • Save MensObscura/a1696fcedd512ff516ec1dd02e536697 to your computer and use it in GitHub Desktop.
Save MensObscura/a1696fcedd512ff516ec1dd02e536697 to your computer and use it in GitHub Desktop.
Create Intent Chooser with Application Whitelist. Based on https://gist.github.com/mediavrog/5625602
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Parcelable;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
/**
* Created by throsa on 30/03/2016.
*/
public class CustomChooserIntent {
private static final String TAG = CustomChooserIntent.class.getSimpleName();
private static boolean mailIntents = false;
private static boolean smsIntents = false;
/**
* Creates a chooser that only shows installed apps that are allowed by the whitelist.
*
* @param pm PackageManager instance.
* @param target The intent to share.
* @param title The title of the chooser dialog.
* @param whitelist A list of package names that are allowed to show.
* @return Updated intent, to be passed to {@link android.content.Context#startActivity}.
*/
public static Intent create(PackageManager pm, Intent target, String title,
List<String> whitelist) {
Intent dummy = new Intent(target.getAction());
dummy.setType(target.getType());
List<ResolveInfo> resInfo = pm.queryIntentActivities(dummy, 0);
List<HashMap<String, String>> metaInfo = new ArrayList<>();
for (ResolveInfo ri : resInfo) {
if (ri.activityInfo == null || !whitelist.contains(ri.activityInfo.packageName)) {
continue;
}
HashMap<String, String> info = new HashMap<>();
//Avoid twitter Direct Message
if (!(ri.activityInfo.packageName.equals("com.twitter.android") && ri.activityInfo.name.contains("DM"))) {
info.put("packageName", ri.activityInfo.packageName);
info.put("className", ri.activityInfo.name);
info.put("simpleName", String.valueOf(ri.activityInfo.loadLabel(pm)));
metaInfo.add(info);
}
}
if (metaInfo.isEmpty()) {
// Force empty chooser by setting a nonexistent target class.
Intent emptyIntent = (Intent) target.clone();
emptyIntent.setPackage("com.your.package");
emptyIntent.setClassName("com.your.package", "NonExistingActivity");
return Intent.createChooser(emptyIntent, title);
}
// Sort items by display name.
Collections.sort(metaInfo, new Comparator<HashMap<String, String>>() {
@Override
public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
return map.get("simpleName").compareTo(map2.get("simpleName"));
}
});
// create the custom intent list
List<Intent> targetedIntents = new ArrayList<>();
for (HashMap<String, String> mi : metaInfo) {
Intent targetedShareIntent = (Intent) target.clone();
targetedShareIntent.setPackage(mi.get("packageName"));
targetedShareIntent.setClassName(mi.get("packageName"), mi.get("className"));
targetedIntents.add(targetedShareIntent);
}
//set sms Intents
if (smsIntents) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("sms:"));
putInList(pm, smsIntent, target, targetedIntents);
}
//set mail Intents
if (mailIntents) {
Intent mailIntent = new Intent(Intent.ACTION_VIEW);
mailIntent.setData(Uri.parse("mailto:?subject= &body="));
putInList(pm, mailIntent, target, targetedIntents);
}
if (targetedIntents != null && targetedIntents.size() > 0) {
Intent chooserIntent = Intent.createChooser(targetedIntents.get(0), title);
targetedIntents.remove(0);
Parcelable[] targetedIntentsParcelable =
targetedIntents.toArray(new Parcelable[targetedIntents.size()]);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntentsParcelable);
return chooserIntent;
}
return null;
}
private static void putInList(PackageManager pm, Intent intent, Intent target, List<Intent> list) {
List<ResolveInfo> activities = pm.queryIntentActivities(intent, intent.getFlags());
for (ResolveInfo ri : activities) {
Intent targetedShareIntent = (Intent) target.clone();
targetedShareIntent.setAction(Intent.ACTION_VIEW);
targetedShareIntent.setPackage(ri.activityInfo.packageName);
targetedShareIntent.setClassName(ri.activityInfo.packageName, ri.activityInfo.name);
list.add(targetedShareIntent);
}
}
public static void enableMailIntents(boolean state) {
mailIntents = state;
}
public static void enableSmsIntents(boolean state) {
smsIntents = state;
}
}
@MensObscura
Copy link
Author

An example of usage for (Facebook, Twitter, Pinterest,sms, mail)

  Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.custom_chooser_title));
    List<String> tWhiteList = new ArrayList<>();
    tWhiteList.add("com.facebook.katana");
    tWhiteList.add("com.twitter.android");
    tWhiteList.add("com.pinterest");
    CustomChooserIntent.enableMailIntents(true);
    CustomChooserIntent.enableSmsIntents(true);
    startActivity(CustomChooserIntent.create(mContext.getPackageManager(), sharingIntent, getResources().getString(
            R.string.account_share_app), tWhiteList));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment