Back to project page Music-Share-Android.
The source code is released under:
GNU General Public License
If you think the Android project Music-Share-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.paperairplane.music.share.utils; /*from w w w . j a v a 2 s .c o m*/ import java.util.Iterator; import java.util.List; import android.app.AlertDialog; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Resources.NotFoundException; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.paperairplane.music.share.Consts; import com.paperairplane.music.share.R; import com.paperairplane.music.share.utils.MyLogger; /** * ?????ActivityChooser * * @author Harry Chen (<a href="mailto:chenshengqi1@gmail.com">Harry Chen</a>) * @author Xavier Yao (<a href="mailto:xavieryao@me.com">Xavier Yao</a>) * @see <a * href="http://www.github.com/PaperAirPlane-Dev-Team/Music-Share-Android">Our * GitHub</a> */ public class IntentResolver { private Context mCtx; private PackageManager mPm; private Handler mHandler; private AlertDialog intentDialog; /** * ???????Intent??ResolveInfo??????????????? * * @param ctx * ???Context * @param i * ??????Intent * @param handler * ????UI?????Handler */ public void handleIntent(Context ctx, Intent i, Handler handler) { mCtx = ctx; mHandler = handler; boolean isShare = i.hasExtra("artworkUrl"); mPm = ctx.getPackageManager(); List<ResolveInfo> info = mPm.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY); MyLogger.d(Consts.DEBUG_TAG, "handleIntent"); // ?????? Iterator<ResolveInfo> it = info.listIterator(); String myPackageName = mCtx.getPackageName(); while (it.hasNext()) { ResolveInfo ri = it.next(); if (ri.activityInfo.packageName.equals(myPackageName)) { info.remove(ri); break; /* * ??????????,??List????,Iterator???????? * ????????,??????????,"?"????????? ???????????????????? * ???????"M"?????? ???????????? ??????break??????? */ } } if (isShare) { // ??SEND??????????????? ResolveInfo share2weibo = new ResolveInfo(); share2weibo.icon = R.drawable.weibo_logo; share2weibo.labelRes = R.string.share2weibo; share2weibo.activityInfo = new ActivityInfo(); share2weibo.activityInfo.flags = Consts.ShareMeans.INTERNAL; info.add(0, share2weibo); } // ??Intent?? showDialog(info, i); } /** * ??"???"?????ListView?Adapter */ private class IntentListAdapter extends BaseAdapter { List<ResolveInfo> info; public IntentListAdapter(List<ResolveInfo> info) { this.info = info; } @Override public int getCount() { return info.size(); } @Override public Object getItem(int position) { return info.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View vwItem = LayoutInflater.from(mCtx).inflate( R.layout.resolve_list_item, null); ImageView ivItemIcon = (ImageView) vwItem.findViewById(R.id.icon); TextView tvItemLabel = (TextView) vwItem .findViewById(android.R.id.text1); TextView tvItemExtended = (TextView) vwItem .findViewById(android.R.id.text2); // ???????? Drawable icon; String label; ResolveInfo ri = info.get(position); if (ri.activityInfo.flags != Consts.ShareMeans.INTERNAL) { // ??????PackageManager??????? icon = ri.activityInfo.loadIcon(mPm); label = ri.activityInfo.loadLabel(mPm).toString().trim(); } else { // ???????????????? // ?????????,????????App?????? // XXX ????? try { icon = mCtx.getResources().getDrawable(ri.icon); label = mCtx.getString(ri.labelRes); } catch (NotFoundException e) { icon = mCtx.getResources().getDrawable( R.drawable.ic_launcher); label = mCtx.getString(R.string.app_name); } } ivItemIcon.setImageDrawable(icon); tvItemLabel.setText(label); tvItemExtended.setVisibility(View.GONE); if (Build.VERSION.SDK_INT < 11) { tvItemLabel.setTextColor(mCtx.getResources().getColor( android.R.color.primary_text_light)); } else { tvItemLabel.setTextColor(mCtx.getResources().getColor( android.R.color.primary_text_dark)); } return vwItem; } } /** * ??ResolveInfo?List * * @param info * ??App???? * @param i * ????Intent */ private void showDialog(final List<ResolveInfo> info, final Intent i) { if (info.size() == 0) { new AlertDialog.Builder(mCtx) .setMessage(mCtx.getString(R.string.no_app_found)) .setPositiveButton(mCtx.getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); return; } else if (info.size() == 1) { Intent intent = generateIntent(i, info.get(0)); mCtx.startActivity(intent); return; } OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ResolveInfo ri = info.get(position); boolean isInternal = (ri.activityInfo.flags == Consts.ShareMeans.INTERNAL); if (!isInternal) { // ????????? Intent intent = generateIntent(i, ri); if (ri.activityInfo.packageName.contains("mms") || ri.activityInfo.packageName.contains("sms")) { intent.putExtra("sms_body", intent.getStringExtra(Intent.EXTRA_TEXT)); } mCtx.startActivity(intent); } else { // ?????????? Bundle bundle; bundle = i.getExtras(); Message m = mHandler.obtainMessage( Consts.Status.SEND_WEIBO, bundle); mHandler.sendMessage(m); } intentDialog.cancel(); // ?????!????????? } }; ListView v = new ListView(mCtx); v.setCacheColorHint(0); if (Build.VERSION.SDK_INT < 11) { // ????????????????????? v.setBackgroundColor(mCtx.getResources().getColor( android.R.color.primary_text_dark)); } v.setAdapter(new IntentListAdapter(info)); v.setOnItemClickListener(listener); String title = (i.getAction().equals(Intent.ACTION_VIEW)) ? mCtx .getString(R.string.how_to_play) : mCtx .getString(R.string.how_to_share); intentDialog = new AlertDialog.Builder(mCtx).setView(v).setTitle(title) .create(); //??????????????Dialog?,????????? //??????????????????,???? intentDialog.show(); } /** * ????ResolveInfo?????Intent * * @param i * ???Intent * @param ri * ??App???? * @return */ private Intent generateIntent(final Intent i, ResolveInfo ri) { Intent intent = new Intent(i); intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); intent.setComponent(new ComponentName( ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name)); return intent; } }