ShareActivity.java
15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package com.cnlive.share.ui;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.cnlive.core.libs.base.application.AppConfig;
import com.cnlive.core.libs.base.util.AlertUtil;
import com.cnlive.core.libs.base.util.DoublePressed;
import com.cnlive.core.libs.base.util.SharedPreferencesHelper;
import com.cnlive.share.R;
import com.cnlive.share.data.IconInfo;
import com.cnlive.share.data.ShareInfo;
import com.cnlive.share.observable.ShareClickObservable;
import com.cnlive.share.model.ShareStateInfo;
import com.cnlive.share.observable.ShareStateObservable;
import com.cnlive.share.observable.ShareWjjStateObservable;
import com.cnlive.share.observable.WechatShareObservable;
import com.cnlive.share.util.ShareOtherUtil;
import com.cnlive.share.util.ShareUtils;
import com.cnlive.share.util.Util;
import com.sina.weibo.sdk.share.WbShareCallback;
import com.sina.weibo.sdk.share.WbShareHandler;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.cnlive.share.BuildConfig;
import com.tencent.tauth.UiError;
import java.util.ArrayList;
import java.util.List;
import java.util.Observer;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
/**
* 作者: 吴奎庆
* <p>
* 时间: 2018/11/30
* <p>
* 简介:
*/
@Route(path = "/share/ShareActivity")
public class ShareActivity extends AppCompatActivity implements View.OnClickListener, ShareDialogListAdapter.ItemClick, IUiListener, WbShareCallback, Observer {
public static final String SHARE_TITLE = "快来加入中国网家家,优享美好生活!";
public static final String SHARE_CONTENT = "中国网家家 让生活更美好!";
private LinearLayout ll_cancle;
private ShareInfo info;
private RecyclerView shareRv;
private RecyclerView shareOther;
private ShareDialogListAdapter adapter;
private boolean isShowWjjLife;
private boolean isShowWjjFriend;
private boolean isShowOther;
private boolean isShowReport;
//是否打开eventbus 三方分享得通知
private boolean isOpenShareCallBackEentBus;
private boolean showThirdPlatShare = true;
private WbShareHandler shareHandler;
private String shopShareType;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_dialog);
WechatShareObservable.getInstance().addObserver(this);
ShareWjjStateObservable.newInstance().addObserver(this);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
showThirdPlatShare = SharedPreferencesHelper.getInstance(this).getBoolean("SHOW_THIRD_PLAT_SHARE", true);
initView();
initData(getIntent());
}
private void initData(Intent intent) {
if (intent == null) {
AlertUtil.showDeftToast(this, "数据异常");
finish();
}
String title = getIntent().getStringExtra("title");
if (TextUtils.isEmpty(title)) title = SHARE_TITLE;
String subTitle = getIntent().getStringExtra("subTitle");
if (TextUtils.isEmpty(subTitle)) subTitle = SHARE_CONTENT;
String shareUrl = getIntent().getStringExtra("shareUrl");
isOpenShareCallBackEentBus = getIntent().getBooleanExtra("isOpenEventBusCallBack", false);
if (TextUtils.isEmpty(shareUrl)) shareUrl = AppConfig.getShareDownload();
//拼接电商商品分享的相关参数
//// 商品分享类型 1:商品
shopShareType = getIntent().getStringExtra("shopShareType");
String shareUrlImg = getIntent().getStringExtra("shareUrlImg");
if (TextUtils.isEmpty(shareUrlImg)) shareUrlImg = AppConfig.getShareIcon();
String webpageUrl = getIntent().getStringExtra("webpageUrl");
String userName = getIntent().getStringExtra("userName");
String path = getIntent().getStringExtra("path");
int miniprogramType = getIntent().getIntExtra("miniprogramType", -1);
String type = getIntent().getStringExtra("shareType");
String contentId = getIntent().getStringExtra("contentId");
String reportType = getIntent().getStringExtra("reportType");
//网家家分享区分外部内部时 内部用的url
String wjjShareUrl = getIntent().getStringExtra("wjjShareUrl");
isShowWjjLife = getIntent().getBooleanExtra("isShowWjjLife", false);
isShowWjjFriend = getIntent().getBooleanExtra("isShowWjjFriend", false);
isShowOther = getIntent().getBooleanExtra("isShowOther", false);
isShowReport = getIntent().getBooleanExtra("isShowReport", false);
info = new ShareInfo();
info.setUrl(shareUrl);
info.setTitle(title);
info.setContent(subTitle);
info.setImgurl(shareUrlImg);
info.setShareType(type);
info.setContentId(contentId);
if (!TextUtils.isEmpty(wjjShareUrl)) {
info.setProcessWjjShare(true);
info.setWjjShareUrl(wjjShareUrl);
}
if (TextUtils.isEmpty(reportType)) reportType = "3";
info.setReportType(reportType);
//小程序用的参数
info.setMiniprogramType(miniprogramType);
info.setPath(path);
info.setUserName(userName);
info.setWebpageUrl(webpageUrl);
init();
}
private void initView() {
shareRv = findViewById(R.id.share_rv);
shareOther = findViewById(R.id.share_other);
ll_cancle = findViewById(R.id.ll_cancle);
ll_cancle.setOnClickListener(this);
}
private List<IconInfo> initList() {
//注册微博
if (Util.isWeiboInstallAndInit(this)) {
shareHandler = new WbShareHandler(this);
shareHandler.registerApp();
}
Tencent tencent = Tencent.createInstance(BuildConfig.QQ_SHARE, this.getApplicationContext());
List<IconInfo> list = new ArrayList<>();
if (isShowWjjFriend)
list.add(new IconInfo(IconInfo.WJJ_FRIEND, "网家家好友", R.drawable.wjjhaoyou));
if (isShowWjjLife)
list.add(new IconInfo(IconInfo.WJJ_CIRCLE, "网家家生活圈", R.drawable.shenghuoquan));
if ((AppConfig.getShareDownload().equals(info.getUrl()))
|| (info.getUrl().contains("wjjh5.cnlive.com") || info.getUrl().contains("wjjh5test.cnlive.com"))
|| (!info.getUrl().contains("wjjh5.cnlive.com") && !info.getUrl().contains("wjjh5test.cnlive.com") && showThirdPlatShare)) {
list.add(new IconInfo(IconInfo.WECHAT_FRIEND, "微信好友", R.drawable.wechat));
list.add(new IconInfo(IconInfo.WECHAT_CIRCLE, "微信朋友圈", R.drawable.pengyouquan));
list.add(new IconInfo(IconInfo.QQ, "QQ好友", R.drawable.qq));
list.add(new IconInfo(IconInfo.WEBO, "微博", R.drawable.weibo));
}
if (isShowOther) list.add(new IconInfo(IconInfo.BROWSER, "在浏览器打开", R.drawable.browser));
return list;
}
private List<IconInfo> initListOther() {
List<IconInfo> list = new ArrayList<>();
if (info != null && isShowReport)
list.add(new IconInfo(IconInfo.REPORT, "投诉", R.drawable.report_icon));
return list;
}
private void init() {
adapter = new ShareDialogListAdapter(this, initList());
adapter.setItemClick(this);
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
shareRv.setLayoutManager(manager);
shareRv.setAdapter(adapter);
if (info != null && isShowReport) {
shareOther.setVisibility(View.VISIBLE);
findViewById(R.id.line).setVisibility(View.VISIBLE);
ShareDialogListAdapter adapterOther = new ShareDialogListAdapter(this, initListOther());
adapterOther.setItemClick(this);
LinearLayoutManager layoutManagerOther = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
shareOther.setLayoutManager(layoutManagerOther);
shareOther.setAdapter(adapterOther);
}
}
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.ll_cancle) {
finish();
}
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
public void onItemClick(int position, String type) {
if (info.getUrl() == null || !Util.isNetUrl(info.getUrl())) {
info.setUrl(AppConfig.getShareDownload());
}
if (DoublePressed.onDoublePressed(false, 1000L)) return;
switch (type) {
case IconInfo.WJJ_FRIEND:
ShareUtils.shareAppIM(info, this);
ShareClickObservable.getInstance().update(1);
// finish();
break;
case IconInfo.WJJ_CIRCLE:
ShareUtils.shareAppMoment(info, this);
ShareClickObservable.getInstance().update(2);
// finish();
break;
case IconInfo.WECHAT_FRIEND:
if (!TextUtils.isEmpty(info.getPath()) && !TextUtils.isEmpty(info.getUserName()) && !TextUtils.isEmpty(info.getWebpageUrl()) && info.getMiniprogramType() != -1) {
ShareUtils.shareWechatApplet(this, info);
ShareClickObservable.getInstance().update(3);
} else {
ShareUtils.shareWechat(info, this);
}
break;
case IconInfo.WECHAT_CIRCLE:
ShareUtils.shareWCE(info, this);
ShareClickObservable.getInstance().update(3);
break;
case IconInfo.QQ:
ShareUtils.shareQQActivity(info, this, this);
ShareClickObservable.getInstance().update(5);
break;
case IconInfo.WEBO:
ShareUtils.shareWeiBo(this, info);
ShareClickObservable.getInstance().update(6);
break;
case IconInfo.BROWSER:
ShareUtils.startBrowser(this, info.getUrl());
ShareClickObservable.getInstance().update(7);
finish();
break;
case IconInfo.REPORT:
if (info != null && !TextUtils.isEmpty(info.getContentId())) {
ShareOtherUtil.report(this, info.getContentId(), info.getReportType());
ShareClickObservable.getInstance().update(8);
} else {
AlertUtil.showDeftToast(this, "数据异常");
}
finish();
break;
}
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.share_activity_in, R.anim.share_activity_out);
}
@Override
protected void onDestroy() {
super.onDestroy();
WechatShareObservable.getInstance().deleteObserver(this);
ShareWjjStateObservable.newInstance().deleteObserver(this);
}
public void showMessage(String msg) {
AlertUtil.showDeftToast(this, msg);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (shareHandler != null) shareHandler.doResultIntent(data, this);
// 官方文档没这句代码, 但是很很很重要, 不然不会回调!
Tencent.onActivityResultData(requestCode, resultCode, data, this);
if (requestCode == Constants.REQUEST_API) {
if (resultCode == Constants.REQUEST_QQ_SHARE ||
resultCode == Constants.REQUEST_QZONE_SHARE ||
resultCode == Constants.REQUEST_OLD_SHARE) {
Tencent.handleResultData(data, this);
}
}
}
@Override
public void onComplete(Object o) {
if (isOpenShareCallBackEentBus) {
shopCallback("5", true);
} else {
ShareUtils.processQQCallBack(this, info.getShareType(), info.getContentId());
showMessage("分享成功");
}
finish();
}
@Override
public void onError(UiError uiError) {
if (isOpenShareCallBackEentBus) {
shopCallback("5", false);
} else {
showMessage("分享失败");
}
finish();
}
@Override
public void onCancel() {
if (isOpenShareCallBackEentBus) {
shopCallback("5", false);
} else {
showMessage("分享取消");
}
finish();
}
@Override
public void onWbShareSuccess() {
if (isOpenShareCallBackEentBus) {
shopCallback("6", true);
} else {
showMessage("分享成功");
}
finish();
}
@Override
public void onWbShareCancel() {
if (isOpenShareCallBackEentBus) {
shopCallback("6", false);
} else {
showMessage("分享取消");
}
finish();
}
@Override
public void onWbShareFail() {
if (isOpenShareCallBackEentBus) {
shopCallback("6", false);
} else {
showMessage("分享失败");
}
finish();
}
private void shopCallback(String type, boolean isSuccess) {
if (isOpenShareCallBackEentBus) {
Intent intent = new Intent();
intent.putExtra("shopShareType", shopShareType);
intent.putExtra("isShareSuccess", isSuccess);
setResult(10010, intent);
ShareStateInfo info = new ShareStateInfo();
info.setShareSuccess(isSuccess);
info.setShopShareType(shopShareType);
info.setType(type);
ShareStateObservable.newInstance().update(info);
}
}
@Override
public void update(java.util.Observable observable, Object o) {
if (observable instanceof WechatShareObservable && o != null) {
int state = (int) o;
if (1 == state) {
//成功
shopCallback("3", true);
} else if (2 == state) {
//取消
if (isOpenShareCallBackEentBus) {
shopCallback("3", false);
}
} else if (3 == state) {
//失败
shopCallback("3", false);
} else {
//其他
shopCallback("3", false);
}
finish();
} else if (observable instanceof ShareWjjStateObservable && o != null) {
boolean isSuccess= (boolean) o;
if (isSuccess){
shopCallback("1", true);
}else {
shopCallback("1", false);
}
finish();
}
}
}