69.源碼解析:友盟分享

1.分享的主持類

   
   
   
   
   
public class SharePresenter { //QQ分享賬號 private final static String QQ_AppID = "***"; private final static String QQ_AppKey = "***"; //微信分享賬號 private final static String WX_AppID = "***"; private final static String WX_AppKey = "***"; // 整個平臺的Controller, 負責管理整個SDK的配置、操做等處理 public static UMSocialService mController = UMServiceFactory.getUMSocialService("test"); private static void init(Activity activity) { initPlatforms(activity); initShareContent(activity); } /** * 配置平臺 */ private static void initPlatforms(Activity activity) { // 添加新浪SSO受權 mController.getConfig().setSsoHandler(new SinaSsoHandler()); // 添加QQ支持, 而且設置QQ分享內容的target url UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity,QQ_AppID,QQ_AppKey); qqSsoHandler.addToSocialSDK(); // 添加QZone平臺 QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity,QQ_AppID,QQ_AppKey); qZoneSsoHandler.addToSocialSDK(); // 添加微信平臺 UMWXHandler wxHandler = new UMWXHandler(activity, WX_AppID, WX_AppKey); wxHandler.addToSocialSDK(); // 支持微信朋友圈 UMWXHandler wxCircleHandler = new UMWXHandler(activity, WX_AppID, WX_AppKey); wxCircleHandler.setToCircle(true); wxCircleHandler.addToSocialSDK(); } /** * 配置分享內容 */ private static void initShareContent(Activity activity) { UMImage urlImage = new UMImage(activity,"http://www.che.com/upload/system/d95aa9d7-7980-4f61-84e1-3c28d320d051.png"); // 設置QQ空間分享內容 QZoneShareContent qzone = new QZoneShareContent(); qzone.setTitle("車城網分享測試"); qzone.setShareContent("買好車,上車城!"); qzone.setTargetUrl("http://www.che.com"); mController.setShareMedia(qzone); // 設置QQ好友分享內容 QQShareContent qq = new QQShareContent(); qq.setTitle("車城網分享測試"); qq.setShareContent("買好車,上車城!"); qq.setTargetUrl("http://www.che.com"); mController.setShareMedia(qq); //設置新浪微博分享內容 SinaShareContent sina = new SinaShareContent(); sina.setShareContent("買好車,上車城!車城是結合線上網絡展現、線下展廳銷售、金融服務的綜合服務平臺。http://www.che.com"); sina.setShareImage(new UMImage(activity, R.drawable.ic_main)); mController.setShareMedia(sina); //設置微信好友分享內容 WeiXinShareContent wx = new WeiXinShareContent(); wx.setShareContent("車城是結合線上網絡展現、線下展廳銷售、金融服務的綜合服務平臺。http://www.che.com"); wx.setTitle("買好車,上車城!"); wx.setTargetUrl("http://www.che.com"); wx.setShareMedia(urlImage); mController.setShareMedia(wx); // 設置朋友圈分享的內容 CircleShareContent circle = new CircleShareContent(); circle.setShareContent("車城是結合線上網絡展現、線下展廳銷售、金融服務的綜合服務平臺。http://www.che.com"); circle.setTitle("買好車,上車城!"); circle.setTargetUrl("http://www.che.com"); circle.setShareMedia(urlImage); mController.setShareMedia(circle); } /** * 彈出對話框,分享到微信、QQ、新浪等 */ public static void share(Activity activity) { init(activity); mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA); mController.openShare(activity, false); } /** * 彈出對話框,分享到微信、QQ、新浪等 */ public static void share(Activity activity,String shareCarName,String sharePrice,String shareImageUrl,String shareUrl) { LogUtil.e("shareCarName"+shareCarName+"\tshareImageUrl="+shareImageUrl+"\tshareUrl="+shareUrl); initPlatforms(activity); initShareContent(activity, shareCarName, sharePrice, shareImageUrl, shareUrl); mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA); mController.openShare(activity, false); } /** * 配置分享內容 */ private static void initShareContent(Activity activity,String shareCarName,String sharePrice,String shareImageUrl,String shareUrl) { shareImageUrl="http://www.che.com/upload/system/d95aa9d7-7980-4f61-84e1-3c28d320d051.png"; UMImage urlImage = new UMImage(activity,shareImageUrl); //設置新浪微博分享內容 SinaShareContent sina = new SinaShareContent(); sina.setShareContent("買好車,上車城!車城是結合線上網絡展現、線下展廳銷售、金融服務的綜合服務平臺。http://www.che.com"); sina.setShareImage(new UMImage(activity, R.drawable.isloading)); mController.setShareMedia(sina); //設置微信好友分享內容 WeiXinShareContent wx = new WeiXinShareContent(); wx.setShareContent("車城交易價: "+sharePrice+",來車城分期貸款:支持0首付,車險辦理,代辦過戶、延保"); wx.setTitle(shareCarName); wx.setTargetUrl(shareUrl); wx.setShareMedia(urlImage); mController.setShareMedia(wx); // 設置朋友圈分享的內容 CircleShareContent circle = new CircleShareContent(); circle.setShareContent("車城交易價: "+sharePrice+",來車城分期貸款:支持0首付,車險辦理,代辦過戶、延保"); circle.setTitle(shareCarName); circle.setTargetUrl(shareUrl); circle.setShareMedia(urlImage); mController.setShareMedia(circle); }}

2.第一步:配置平臺

   
   
   
   
   
// 添加微信平臺 UMWXHandler wxHandler = new UMWXHandler(activity, WX_AppID, WX_AppKey); wxHandler.addToSocialSDK();
首先, 建立 UMWXHandler:
   
   
   
   
   
public UMWXHandler(Context context, String appid, String secret) { this.THUMB_SIZE = 150; this.THUMB_LIMIT = '耀'; this.TITLE_LIMIT = 512; this.DESCRIPTION_LIMIT = 1024; this.REFRESH_TOKEN_EXPIRES = 604800; this.WX_SECRET = ""; this.WX_DEFAULT_ICON = 0; this.WXCIRCLE_DEFAULT_ICON = 0; this.isToCircle = false; this.mRefreshTokenAvailable = true; this.TAG = "UMWXHandler"; this.SHOW_COMPRESS_TOAST = 1; this.SHOW_TITLE_TOAST = 2; this.isShowCompressToast = true; this.mReportDesc = ""; this.mWxMediaMessage = null; this.isFromAuth = false; this.isWXSceneFavorite = false; this.handler = new Handler() { public void handleMessage(Message msg) { if(msg.what == 1 && UMWXHandler.this.isShowCompressToast) { Toast.makeText(UMWXHandler.this.mContext, "圖片大小超過32KB,正在對圖片進行壓縮...", 0).show(); } else if(msg.what == 2) { Toast.makeText(UMWXHandler.this.mContext, "標題長度超過512 Bytes...", 0).show(); } } }; this.mEventHandler = new IWXAPIEventHandler() { public void onResp(BaseResp resp) { if(UMWXHandler.this.isFromAuth) { UMWXHandler.this.dealOAuth(resp); } else { int statusCode = 0; SHARE_MEDIA platform = SocializeConfig.getSelectedPlatfrom(); switch(resp.errCode) { case -4: statusCode = -4; Log.d("UMWXHandler", "### 微信發送被拒絕"); break; case -3: statusCode = -3; break; case -2: statusCode = '鱀'; Log.d("UMWXHandler", "### 微信分享取消"); break; case -1: statusCode = -1; break; case 0: statusCode = 200; Log.d("UMWXHandler", "### 微信分享成功."); break; default: Log.d("UMWXHandler", "### 微信發送 -- 未知錯誤."); } if(!UMWXHandler.this.isFromAuth) { UMWXHandler.this.mSocializeConfig.fireAllListenersOnComplete(SnsPostListener.class, platform, statusCode, UMWXHandler.mEntity); if(statusCode == 200 && UMWXHandler.mEntity != null) { SocializeUtils.sendAnalytic(UMWXHandler.this.mContext, UMWXHandler.mEntity.mDescriptor, UMWXHandler.this.mShareContent, UMWXHandler.this.mShareMedia, UMWXHandler.this.isToCircle?"wxtimeline":"wxsession"); } } } } public void onReq(BaseReq req) { } }; this.mContext = context.getApplicationContext(); this.WX_APPID = appid; if(TextUtils.isEmpty(this.WX_APPID)) { throw new NullPointerException("the weixin appid is null"); } else { this.mWXApi = WXAPIFactory.createWXAPI(this.mContext, this.WX_APPID); this.mWXApi.registerApp(this.WX_APPID); this.WX_SECRET = secret; this.mExtraData.put("wx_appid", this.WX_APPID); if(TextUtils.isEmpty(this.WX_SECRET)) { this.WX_SECRET = ""; } this.mExtraData.put("wx_secret", this.WX_SECRET); if(this.WX_DEFAULT_ICON == 0 || this.WXCIRCLE_DEFAULT_ICON == 0) { this.WX_DEFAULT_ICON = ResContainer.getResourceId(context, ResType.DRAWABLE, "umeng_socialize_wechat"); this.WXCIRCLE_DEFAULT_ICON = ResContainer.getResourceId(context, ResType.DRAWABLE, "umeng_socialize_wxcircle"); } } }
而後,調用addToSocialSDK方法
這是 UMWXHandler 的父類UMSsoHandler的一個方法,
   
   
   
   
   
public void addToSocialSDK() { this.mSocializeConfig.addCustomPlatform(this.build()); this.mSocializeConfig.setSsoHandler(this); }
而後分別調用addCustomPlatform方法和setSsoHandler方法:
   
   
   
   
   
public void addCustomPlatform(CustomPlatform var1) { a((SnsPlatform)var1); }
    
    
    
    
    
public void setSsoHandler(UMSsoHandler var1) { if(var1 == null) { Log.w(b, "ssoHander is null"); } else { int var2 = var1.getRequstCode(); Log.d("", "#### set sso handler, code = " + var2); f.put(var2, var1); CustomPlatform var3 = var1.build(); if(var3 != null) { this.addCustomPlatform(var3); } } }
奶蛋的,友盟將代碼混淆了,不過影響不大,a方法以下:
   
   
   
   
   
private static void a(SnsPlatform var0) { if(var0 != null && !TextUtils.isEmpty(var0.mKeyword)) { String var1 = var0.mKeyword; if(t.containsKey(var1)) { t.remove(var1); } t.put(var1, var0); } }
裏面的t其實就是一個Map:
   
   
   
   
   
private static Map<String, SnsPlatform> t;
addCustomPlatform方法的參數爲 UMSsoHandler的build:
   
   
   
   
   
public final CustomPlatform build() { return this.mCustomPlatform != null?this.mCustomPlatform:this.createNewPlatform(); }
調用了抽象方法createNewPlatform:
   
   
   
   
   
protected abstract CustomPlatform createNewPlatform();
因而咱們去UMSsoHandler的子類UMWXHandler去看具體實現:
   
   
   
   
   
public final CustomPlatform createNewPlatform() { int icon = this.isToCircle?this.WXCIRCLE_DEFAULT_ICON:this.WX_DEFAULT_ICON; String keyWord = this.isToCircle?"weixin_circle":"weixin"; String showWord = this.isToCircle?"朋友圈":"微信"; this.mCustomPlatform = new CustomPlatform(keyWord, showWord, icon); boolean grayIcon = false; int grayIcon1; if(this.isToCircle) { grayIcon1 = ResContainer.getResourceId(this.mContext, ResType.DRAWABLE, "umeng_socialize_wxcircle_gray"); } else { grayIcon1 = ResContainer.getResourceId(this.mContext, ResType.DRAWABLE, "umeng_socialize_wechat_gray"); } this.mCustomPlatform.mGrayIcon = grayIcon1; this.mCustomPlatform.mClickListener = new OnSnsPlatformClickListener() { public void onClick(Context context, SocializeEntity entity, SnsPostListener listener) { UMWXHandler.this.handleOnClick(context, UMWXHandler.this.mCustomPlatform, entity, listener); } }; return this.mCustomPlatform; }
感受 addCustomPlatform方法和setSsoHandler方法最後都是調用build,並添加到map啊,不知道爲何要弄成兩個方法。

3.第二步:設置分享內容

   
   
   
   
   
//設置微信好友分享內容 WeiXinShareContent wx = new WeiXinShareContent(); wx.setShareContent("車城是結合線上網絡展現、線下展廳銷售、金融服務的綜合服務平臺。http://www.che.com"); wx.setTitle("買好車,上車城!"); wx.setTargetUrl("http://www.che.com"); wx.setShareMedia(urlImage); mController.setShareMedia(wx);
WeiXinShareContent是繼承自BaseShareContent的一個實體類,BaseShareContent又繼承自SimpleShareContent,SimpleShareContent實現了UMediaObject接口,UMediaObject接口繼承了Parcelable, Cloneable這兩個接口。

這段代碼用到了一個UMSocialService對象mController,它是個人主持類的靜態變量
   
   
   
   
   
// 整個平臺的Controller, 負責管理整個SDK的配置、操做等處理 public static UMSocialService mController = UMServiceFactory.getUMSocialService("test");
看getUMSocialService方法:
   
   
   
   
   
public static UMSocialService getUMSocialService(String var0) { return getUMSocialService(var0, (RequestType)null); }
轉到兩個參數的去:
   
   
   
   
   
@Deprecated public static UMSocialService getUMSocialService(String var0, RequestType var1) { SocializeEntity var2 = null; if(var1 == null) { var1 = RequestType.SOCIAL; } String var3 = var0 + var1; if(c.g.containsKey(var3)) { var2 = (SocializeEntity)c.g.get(var3); return new c(var2); } else { var3 = var0 + RequestType.SOCIAL; if(var1 == RequestType.ANALYTICS && c.g.containsKey(var3)) { SocializeEntity var4 = (SocializeEntity)c.g.get(var3); var2 = SocializeEntity.cloneNew(var4, var1); } else { var2 = new SocializeEntity(var0, var1); } return new c(var2); } }
轉到c.g.get方法,c類繼承自InitializeController並實現了UMSocialService, g是一個Map,
   
   
   
   
   
public static volatile Map<String, SocializeEntity> g = new HashMap();
最後返回時,調用了c的構造器:
   
   
   
   
   
public c(SocializeEntity var1) { super(var1); }
跳到父類InitializeController:
   
   
   
   
   
public InitializeController(SocializeEntity var1) { super(var1); }
跳到父類BaseController:
   
   
   
   
   
public BaseController(SocializeEntity var1) { this.a = var1; this.c = (CommentService)b.a(this.a, a.b, new Object[0]); this.d = (LikeService)b.a(this.a, a.d, new Object[0]); this.e = (AuthService)b.a(this.a, a.a, new Object[0]); this.b = (ShareService)b.a(this.a, a.c, new Object[0]); this.f = (UserCenterService)b.a(this.a, a.e, new Object[]{this.e}); }
奶蛋的,又尼瑪混淆了,不要沒關係。反正都是初始化變量,沒調用什麼方法,能夠略過。 UMSocialService對象的初始化就到這裏了,下面要調用setShareMedia方法:
   
   
   
   
   
boolean setShareMedia(UMediaObject var1);
這是UMSocialService接口的一個方法,那麼它的實現應該就是在c類中了,去找:
   
   
   
   
   
public boolean setShareMedia(UMediaObject var1) { if(var1 == null) { this.a.setMedia((UMediaObject)null); return true; } else if(var1.isMultiMedia()) { this.a.setMedia(var1); return true; } else { Log.w(h, "unable set share media.type is no support."); return false; } }
調用一個SocializeEntity對象a的setMedia方法:
   
   
   
   
   
public void setMedia(UMediaObject var1) { SHARE_MEDIA var2 = SHARE_MEDIA.GENERIC; if(var1 != null) { var2 = var1.getTargetPlatform(); } if(this.h.containsKey(var2)) { this.h.remove(var2); } this.h.put(var2, var1); }
SocializeEntity類的一個 Map存儲:
   
   
   
   
   
private Map<SHARE_MEDIA, UMediaObject> h = new HashMap();
這裏的SHARE_MEDIA是一個枚舉類。咱們看它的類型WEIXIN:
   
   
   
   
   
WEIXIN("weixin") { public int getReqCode() { return 10086; } public boolean isSupportAuthorization() { return true; } },

4.第三步:彈出分享對話框

   
   
   
   
   
mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA); mController.openShare(activity, false);
看設置須要顯示的分享平臺的方法:
   
   
   
   
   
public void setPlatforms(SHARE_MEDIA... var1) { this.v.clear(); if(var1 != null && var1.length > 0) { SHARE_MEDIA[] var2 = var1; int var3 = var1.length; for(int var4 = 0; var4 < var3; ++var4) { SHARE_MEDIA var5 = var2[var4]; if(SocializeUtils.isValidPlatform(var5)) { this.v.add(var5); } } } }
SocializeConfig 類中的一個List存儲:
   
   
   
   
   
private List<SHARE_MEDIA> v;
再來看openShare方法,這是ShareService的一個方法:
   
   
   
   
   
void openShare(Activity var1, boolean var2);
實如今哪裏呢?UMSocialService中找不到。誰是 UMSocialService的實現類?就是以前的那個c類。
   
   
   
   
   
public void openShare(Activity var1, boolean var2) { this.b.openShare(var1, var2); }
再去看b類
   
   
   
   
   
public void openShare(Activity var1, boolean var2) { this.g = var2; this.openShare(var1, (SnsPostListener)null); }
轉到:
   
   
   
   
   
public void openShare(Activity var1, SnsPostListener var2) { this.a(); if(this.e(var1)) { this.a(var1); this.a.addStatisticsData(var1, SHARE_MEDIA.GENERIC, 1); this.f = null; a var3 = new a(var1); var3.setLayoutParams(new LayoutParams(-1, -1)); this.f = new com.umeng.socialize.view.b(var1, var3, UMServiceFactory.getUMSocialService(this.a.mDescriptor)); this.f.setFocusable(true); this.f.setBackgroundDrawable(new BitmapDrawable()); this.f.a(this.i); var3.a(new OnClickListener() { public void onClick(View var1) { b.this.f.dismiss(); } }); if(var2 != null) { this.c.registerListener(var2); } if(this.g) { this.d(var1); } else { this.f.showAtLocation(var1.getWindow().getDecorView(), 80, 0, 0); } } }
轉到PopupWindow類的子類b:
   
   
   
   
   
public void showAtLocation(View var1, int var2, int var3, int var4) { try { super.showAtLocation(var1, var2, var3, var4); if(this.e != null) { this.e.onShow(); } } catch (Exception var6) { var6.printStackTrace(); } }
這個PopupWindow的視圖是根據 SocializeConfig 類中的那個List來建立的:
建立的視圖的點擊事件:
   
   
   
   
   
var4.setOnClickListener(new OnClickListener() { public void onClick(View var1x) { b.this.dismiss(); SHARE_MEDIA var2 = SHARE_MEDIA.convertToEmun(var3.mKeyword); if(!DeviceConfig.isNetworkAvailable(b.this.a) && var2 != SHARE_MEDIA.SMS) { Toast.makeText(b.this.a, "您的網絡不可用,請檢查網絡鏈接...", 0).show(); } else { a(var3, var2); } } });
跳轉到a:
   
   
   
   
   
private void a(SnsPlatform var1x, SHARE_MEDIA var2) { if(var2 != null) { b.this.c.getEntity().addStatisticsData(b.this.a, var2, 14); } if(var1x != null) { SocializeConfig.setSelectedPlatfrom(var2); SnsPostListener var3 = ListenerUtils.createSnsPostListener(); var1x.performClick(b.this.a, b.this.c.getEntity(), var3); } }
點擊了以後如何執行的呢?
轉到SnsPlatform類的performClick方法
   
   
   
   
   
public void performClick(Context var1, SocializeEntity var2, SnsPostListener var3) { if(this.mClickListener != null) { this.mClickListener.onClick(var1, var2, var3); } }
轉到 OnSnsPlatformClickListener:
   
   
   
   
   
public interface OnSnsPlatformClickListener { void onClick(Context var1, SocializeEntity var2, SocializeListeners.SnsPostListener var3); }
可是,SnsPlatform類的OnSnsPlatformClickListener並無初始化,因而去它的子類,那麼,誰是 SnsPlatform的實現類?

未完待續……









相關文章
相關標籤/搜索