-
-
Save xmenxwk/6153819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test | |
{ | |
public void Test1() | |
{ | |
// Usage: | |
// blacklist | |
String[] blacklist = new String[]{"com.any.package", "net.other.package"}; | |
// your share intent | |
Intent intent = new Intent(Intent.ACTION_SEND); | |
intent.setType("text/plain"); | |
intent.putExtra(Intent.EXTRA_TEXT, "some text"); | |
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a subject"); | |
// ... anything else you want to add | |
// invoke custom chooser | |
startActivity(generateCustomChooserIntent(intent, blacklist)); | |
} | |
} | |
public final class Utils | |
{ | |
public static final class IntentInfo { | |
public String SimpleName; | |
public String PackageName; | |
public String ClassName; | |
} | |
public static Intent GenerateCustomChooserIntent(Intent prototype, String[] forbiddenChoices) { | |
final String Title = "Share"; | |
List<Intent> targetedShareIntents = new ArrayList<Intent>(); | |
List<IntentInfo> intentMetaInfo = new ArrayList<IntentInfo>(); | |
Intent chooserIntent; | |
Intent dummy = new Intent(prototype.getAction()); | |
dummy.setType(prototype.getType()); | |
PackageManager pm = Utils.GetContext().getPackageManager(); | |
List<ResolveInfo> resInfo = pm.queryIntentActivities(dummy, 0);// PackageManager.MATCH_DEFAULT_ONLY);//I think default is not good idea | |
if (!resInfo.isEmpty()) { | |
HashSet<String> blackListedPackagesHashset = new HashSet<String>(); | |
if (forbiddenChoices != null) { | |
for (String blackListPackageName : forbiddenChoices) | |
blackListedPackagesHashset.add(blackListPackageName.toLowerCase(Locale.US)); | |
} | |
for (ResolveInfo resolveInfo : resInfo) { | |
if (resolveInfo.activityInfo == null | |
|| blackListedPackagesHashset.contains(resolveInfo.activityInfo.packageName | |
.toLowerCase(Locale.US))) | |
continue; | |
IntentInfo info = new IntentInfo(); | |
info.PackageName = resolveInfo.activityInfo.packageName; | |
info.ClassName = resolveInfo.activityInfo.name; | |
info.SimpleName = String.valueOf(resolveInfo.activityInfo.loadLabel(pm)); | |
intentMetaInfo.add(info); | |
} | |
if (!intentMetaInfo.isEmpty()) { | |
// sorting for nice readability | |
Collections.sort(intentMetaInfo, new Comparator<IntentInfo>() { | |
@Override | |
public int compare(IntentInfo info1, IntentInfo info2) { | |
return info1.SimpleName.compareTo(info2.SimpleName); | |
} | |
}); | |
// create the custom intent list | |
for (IntentInfo metaInfo : intentMetaInfo) { | |
Intent targetedShareIntent = (Intent) prototype.clone(); | |
targetedShareIntent.setPackage(metaInfo.PackageName); | |
targetedShareIntent.setClassName(metaInfo.PackageName, metaInfo.ClassName); | |
targetedShareIntents.add(targetedShareIntent); | |
} | |
chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), | |
Title); | |
if (targetedShareIntents.size() > 0) | |
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, | |
targetedShareIntents.toArray(new Parcelable[] {})); | |
return chooserIntent; | |
} | |
} | |
return Intent.createChooser(prototype, Title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment