Created
March 18, 2014 18:41
-
-
Save koocbor/9626587 to your computer and use it in GitHub Desktop.
Custom Dialog to Choose Sharing Intent with Filtering
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
Intent emailIntent = new Intent(); | |
emailIntent.setAction(Intent.ACTION_SEND); | |
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it | |
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(extraText)); | |
emailIntent.putExtra(Intent.EXTRA_SUBJECT, extraSubject); | |
emailIntent.setType("message/rfc822"); | |
PackageManager pm = getPackageManager(); | |
Intent sendIntent = new Intent(Intent.ACTION_SEND); | |
sendIntent.setType("text/plain"); | |
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0); | |
ArrayList<ResolveInfo> dialogResInfo = new ArrayList<ResolveInfo>(); | |
for (int i=0; i<resInfo.size(); i++) { | |
ResolveInfo ri = resInfo.get(i); | |
String packageName = ri.activityInfo.packageName; | |
if (packageName.contains("android.email") || | |
packageName.contains("twitter") || | |
packageName.contains("facebook") || | |
packageName.contains("android.gm")) { | |
dialogResInfo.add(ri); | |
} | |
} | |
Collections.sort(dialogResInfo, new ResolveInfo.DisplayNameComparator(pm)); | |
final Dialog dialog = new Dialog(BaseActivity.this); | |
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes(); | |
layoutParams.gravity = Gravity.CENTER; | |
dialog.getWindow().setAttributes(layoutParams); | |
dialog.setCanceledOnTouchOutside(true); | |
dialog.setContentView(R.layout.dialog_share_chooser); | |
dialog.setCancelable(true); | |
ListView dialogListView = (ListView)dialog.findViewById(R.id.share_list); | |
final ShareListAdapter adapter = new ShareListAdapter(this, pm, dialogResInfo); | |
dialogListView.setAdapter(adapter); | |
dialogListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { | |
ResolveInfo ri = adapter.getItem(position); | |
String packageName = ri.activityInfo.packageName; | |
if (packageName.contains("facebook")) { | |
FacebookDialog fbDialog = new FacebookDialog.ShareDialogBuilder(BaseActivity.this) | |
.setLink(extraText) | |
.build(); | |
fbUiHelper.trackPendingDialogCall(fbDialog.present()); | |
finish(); | |
} else { | |
Intent intent = new Intent(); | |
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); | |
intent.setAction(Intent.ACTION_SEND); | |
intent.setType("text/plain"); | |
if(packageName.contains("twitter")) { | |
intent.putExtra(Intent.EXTRA_TEXT, extraText); | |
} else if(packageName.contains("mms")) { | |
intent.putExtra(Intent.EXTRA_TEXT, extraText); | |
} else if(packageName.contains("android.gm")) { | |
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(extraText)); | |
intent.putExtra(Intent.EXTRA_SUBJECT, extraSubject); | |
intent.setType("message/rfc822"); | |
} | |
startActivity(intent); | |
} | |
} | |
}); | |
dialog.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment