Java tutorial
/** * @(#)ShareDialogFragment, 2/24/14 * * Copyright 2013 fenbi.com. All rights reserved. * FENBI.COM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.lrhehe.android.common.share; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; import android.app.Dialog; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.RelativeLayout; import android.widget.TextView; import com.lidroid.xutils.view.annotation.ViewInject; import com.lidroid.xutils.ViewUtils; import com.lrhehe.android.R; import com.lrhehe.android.common.constant.UIConst; import com.lrhehe.android.common.helper.DimenHelper; /** * @author yangfan */ public class ShareDialogFragment extends DialogFragment { public static final String DIALOG_WIDTH = "dialog_width"; public static final String DIALOG_HEIGHT = "dialog_height"; public static final String DIALOG_MARGIN_TOP = "dialog_margin_top"; // ??????QQ?Qzone??????? private static final List<String> sharePkgList = new ArrayList<String>(); static { sharePkgList.add("com.tencent.mm"); // sharePkgList.add("com.sina.weibo"); // ? sharePkgList.add("com.tencent.mobileqq"); // QQ sharePkgList.add("com.qzone"); // qzone sharePkgList.add("com.tencent.WBlog"); // ? sharePkgList.add("com.renren.mobile.android"); // sharePkgList.add("com.alibaba.android.babylon"); // ? sharePkgList.add("com.android.mms"); // sharePkgList.add("com.android.email"); // email } private static final Set<String> shareActivityBlackSet = new HashSet<String>(); static { shareActivityBlackSet.add("com.tencent.mobileqq.activity.qfileJumpActivity"); // QQ?? } private static final String APP_WECHAT = "com.tencent.mm"; private static final String APP_WEIBO = "com.sina.weibo"; private static final int MAX_ITEM = 4; private static final int ITEM_PADDING_H = UIConst.MARGIN_SMALL; private static final int ITEM_PADDING_V = UIConst.MARGIN_NORMAL; private static final int ITEM_SIZE = (int) DimenHelper.dip2px(48); private ShareDialogDelegate delegate; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = new Dialog(getActivity()); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // may have proble to inject in a dialog fragment ViewUtils.inject(getActivity()); afterViewsInflate(dialog); return dialog; } protected void afterViewsInflate(Dialog dialog) { //updateShareViews(buildShareResInfos()); } public void setDelegate(ShareDialogDelegate delegate) { this.delegate = delegate; } private List<ResolveInfo> buildShareResInfos() { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/*"); List<ResolveInfo> allInfos = getActivity().getPackageManager().queryIntentActivities(intent, 0); List<ResolveInfo> filterInfos = new ArrayList<ResolveInfo>(); for (ResolveInfo info : allInfos) { for (String sharePkg : sharePkgList) { if (sharePkg.equals(info.activityInfo.packageName) && !shareActivityBlackSet.contains(info.activityInfo.name)) { filterInfos.add(info); break; } } } Collections.sort(filterInfos, new Comparator<ResolveInfo>() { @Override public int compare(ResolveInfo lhs, ResolveInfo rhs) { ActivityInfo lai = lhs.activityInfo; ActivityInfo rai = rhs.activityInfo; int lindex = sharePkgList.indexOf(lai.packageName); int rindex = sharePkgList.indexOf(rai.packageName); if (lindex == rindex && lai.packageName.equals(APP_WECHAT)) { return lai.name.contains("timeline") ? 1 : -1; } return lindex - rindex; } }); return filterInfos; } /* private void updateShareViews(List<ResolveInfo> infos) { PackageManager pm = getActivity().getPackageManager(); activityContainer.removeAllViews(); LinearLayout row = null; int totalCount = (int) (Math.ceil((float) infos.size() / MAX_ITEM) * MAX_ITEM); for (int i = 0; i < totalCount; ++i) { View itemView; if (i < infos.size()) { ResolveInfo info = infos.get(i); itemView = buildShareView((String) info.loadLabel(pm), info.loadIcon(pm)); itemView.setTag(info); itemView.setOnClickListener(onItemClickListener); } else { itemView = new View(getActivity()); } if (i % MAX_ITEM == 0) { if (row != null) { activityContainer.addView(row, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); } row = new LinearLayout(getActivity()); } LayoutParams itemParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT); itemParams.weight = 1; row.addView(itemView, itemParams); } if (row != null) { activityContainer.addView(row, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); } } private View buildShareView(String text, Drawable icon) { LinearLayout layout = new LinearLayout(getActivity()); layout.setOrientation(LinearLayout.VERTICAL); ImageView imageView = new ImageView(getActivity()); imageView.setImageDrawable(icon); layout.addView(imageView, new LayoutParams(ITEM_SIZE, ITEM_SIZE)); TextView textView = new TextView(getActivity()); UIUtils.setTextSizeById(textView, R.dimen.text_normal); textView.setText(text); textView.setGravity(Gravity.CENTER); LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); textParams.setMargins(0, UIConst.MARGIN_NORMAL, 0, 0); layout.addView(textView, textParams); layout.setPadding(ITEM_PADDING_H, ITEM_PADDING_V, ITEM_PADDING_H, ITEM_PADDING_V); layout.setGravity(Gravity.CENTER_HORIZONTAL); return layout; }*/ private void close() { dismiss(); onCancel(getDialog()); } private OnClickListener onItemClickListener = new OnClickListener() { @Override public void onClick(View v) { ActivityInfo info = ((ResolveInfo) v.getTag()).activityInfo; if (info.packageName.equals(APP_WECHAT)) { if (info.name.toLowerCase().contains("timeline")) { delegate.onWeChatTimelineClick(); } else { delegate.onWeChatSessionClick(); } } else if (info.packageName.equals(APP_WEIBO)) { delegate.onWeiboShareClick(info.packageName, info.name); } else { delegate.onOtherShareClick(info.packageName, info.name); } close(); } }; public static interface ShareDialogDelegate { public void onWeChatSessionClick(); public void onWeChatTimelineClick(); public void onWeiboShareClick(String targetPkg, String targetActivity); public void onOtherShareClick(String targetPkg, String targetActivity); } public static Bundle newBundle() { Bundle args = new Bundle(); return args; } }