-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathWeChatModule.java
More file actions
812 lines (738 loc) · 30.2 KB
/
WeChatModule.java
File metadata and controls
812 lines (738 loc) · 30.2 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
package com.heng.wechat;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.common.executors.UiThreadImmediateExecutorService;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.util.UriUtil;
import com.facebook.datasource.BaseDataSubscriber;
import com.facebook.datasource.DataSource;
import com.facebook.datasource.DataSubscriber;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.OrientedDrawable;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.CloseableStaticBitmap;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXMusicObject;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.modelmsg.WXVideoObject;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.modelpay.PayResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* Created by heng on 2015/12/10.
* <p/>
* Edited by heng on 15/12/18
* Added share webPage and weChat Pay
* <p/>
* Edited by heng on 2015/12/22
* Add remote image async download
* <p/>
* Edited by heng on 2015/12/29
* 1.Removed Handler and Thread
* 2.Modify options param
* 3.Added share local
* 4.Added and remote image(分享远程图片到朋友圈和收藏都会失败,具体原因待查,建议把远程图片下载到本地来分享)
* <p/>
* Edited by heng on 2016/02/02
* 1.Added method openWXApp
* 2.Edited callback(err,res)
* 3.Reconstruction code
*/
public class WeChatModule extends ReactContextBaseJavaModule {
public static final String REACT_MODULE_NAME = "WeChatAndroid";
public static IWXAPI wxApi = null;
public static String appId;
public static ReactApplicationContext reactApplicationContext;
/*============ WeChat share options key ==============*/
public static final String OPTIONS_TITLE = "title";
public static final String OPTIONS_DESC = "desc";
public static final String OPTIONS_TAG_NAME = "tagName";
public static final String OPTIONS_THUMB_SIZE = "thumbSize";
public static final String OPTIONS_TRANSACTION = "transaction";
public static final String OPTIONS_SCENE = "scene";
public static final String OPTIONS_TYPE = "type";
public static final String OPTIONS_TEXT = "text";
public static final String OPTIONS_IMAGE_URL = "imageUrl";
public static final String OPTIONS_IMAGE_PATH = "imagePath";
public static final String OPTIONS_THUMB_IMAGE = "thumbImage";
public static final String OPTIONS_WEBPAGE_URL = "webpageUrl";
public static final String OPTIONS_MUSIC_URL = "musicUrl";
public static final String OPTIONS_MUSIC_LOW_BAND_URL = "musicLowBandUrl";
public static final String OPTIONS_VIDEO_URL = "videoUrl";
public static final String OPTIONS_VIDEO_LOW_BAND_URL = "videoLowBandUrl";
/*============ WeChat share options key ==============*/
/*============ WeChat pay options key ==============*/
public static final String OPTIONS_APP_ID = "appId";
public static final String OPTIONS_NONCE_STR = "nonceStr";
public static final String OPTIONS_PACKAGE_VALUE = "packageValue";
public static final String OPTIONS_PARTNER_ID = "partnerId";
public static final String OPTIONS_PREPAY_ID = "prepayId";
public static final String OPTIONS_TIME_STAMP = "timeStamp";
public static final String OPTIONS_SIGN = "sign";
/*============ WeChat pay options key ==============*/
private static final String RCTWXShareTypeNews = "news";
private static final String RCTWXShareTypeImage = "image";
private static final String RCTWXShareTypeText = "text";
private static final String RCTWXShareTypeVideo = "video";
private static final String RCTWXShareTypeAudio = "audio";
private static final String RCTWXShareType = "type";
private static final String RCTWXShareText = "text";
private static final String RCTWXShareTitle = "title";
private static final String RCTWXShareDescription = "description";
private static final String RCTWXShareWebpageUrl = "webpageUrl";
private static final String RCTWXShareImageUrl = "imageUrl";
private static final String RCTWXShareThumbImageSize = "thumbImageSize";
private final static String NOT_REGISTERED = "registerApp required.";
private final static String INVOKE_FAILED = "WeChat API invoke returns false.";
public static final int TYPE_TEXT = 1; //文字
public static final int TYPE_IMAGE = 2; //图片
public static final int TYPE_WEB_PAGE = 3; //网页
public static final int TYPE_MUSIC = 4; //音乐
public static final int TYPE_VIDEO = 5; //视频
String tagName = null;
String title = null;
String desc = null;
String transaction = null;
Bitmap bitmap = null; //分享的缩略图
int thumbSize = 150; //分享的缩略图大小
int scene; //分享的方式(0:聊天界面,1:朋友圈,2:收藏)
public WeChatModule(ReactApplicationContext reactContext) {
super(reactContext);
reactApplicationContext = reactContext;
}
@Override
public String getName() {
return REACT_MODULE_NAME;
}
/**
* 注册AppID到微信(使用微信SDK必须先调用此方法)
*/
@ReactMethod
public void registerApp(String appId, Callback callback) {
if (TextUtils.isEmpty(appId)) {
if (callback != null) {
callback.invoke("appId must be not null !");
}
} else {
WeChatModule.appId = appId;
WeChatModule.wxApi = WXAPIFactory.createWXAPI(getReactApplicationContext(), appId, true);
boolean registered = WeChatModule.wxApi.registerApp(appId);
if (callback != null) {
callback.invoke(null, registered);
}
}
}
private void commonCallback(Callback callback, boolean res) {
if (callback != null) {
if (WeChatModule.wxApi == null) {
callback.invoke("please registerApp before this !");
return;
}
callback.invoke(null, res);
}
}
/**
* 打开微信客户端
*/
@ReactMethod
public void openWXApp(Callback callback) {
commonCallback(callback, WeChatModule.wxApi.openWXApp());
}
/**
* 判断是否安装微信
*/
@ReactMethod
public void isWXAppInstalled(Callback callback) {
commonCallback(callback, WeChatModule.wxApi.isWXAppInstalled());
}
/**
* 判断安装的版本是否为微信支持的API
*/
@ReactMethod
public void isWXAppSupportAPI(Callback callback) {
commonCallback(callback, WeChatModule.wxApi.isWXAppSupportAPI());
}
/**
* 获取微信支持的API版本
*/
@ReactMethod
public void getWXAppSupportAPI(Callback callback) {
if (callback != null) {
if (WeChatModule.wxApi == null) {
callback.invoke("please registerApp before this !");
return;
}
int supportAPI = WeChatModule.wxApi.getWXAppSupportAPI();
callback.invoke(null, supportAPI);
}
}
/**
* 微信授权登录
*/
@ReactMethod
public void sendAuthReq(String scope, String state, Callback callback) {
if (WeChatModule.wxApi == null) {
if (callback != null) {
callback.invoke("please registerApp before this !");
}
} else {
if (TextUtils.isEmpty(scope)) {
scope = "snsapi_userinfo";
}
if (TextUtils.isEmpty(state)) {
state = "SECRET";
}
SendAuth.Req req = new SendAuth.Req();
req.scope = scope;
req.state = state;
boolean sendReqOK = WeChatModule.wxApi.sendReq(req);
if (callback != null) {
callback.invoke(null, sendReqOK);
}
}
}
/**
* 分享到微信
*/
@ReactMethod
public void sendReq(ReadableMap options, Callback callback) {
if (WeChatModule.wxApi == null) {
if (callback != null) {
callback.invoke("please registerApp before this !");
}
} else {
if (options == null) {
if (callback != null) {
callback.invoke("please setting options !");
}
} else {
if (options.hasKey(OPTIONS_TYPE)) {
WXMediaMessage msg = new WXMediaMessage();
int type = options.getInt(OPTIONS_TYPE);
switch (type) {
case TYPE_TEXT:
msg.mediaObject = getTextObj(options);
break;
case TYPE_IMAGE:
msg.mediaObject = getImageObj(options);
break;
case TYPE_WEB_PAGE:
msg.mediaObject = getWebpageObj(options);
break;
case TYPE_MUSIC:
msg.mediaObject = getMusicObj(options);
break;
case TYPE_VIDEO:
msg.mediaObject = getVideoObj(options);
break;
default:
if (callback != null) {
callback.invoke("please check correct media type !");
}
break;
}
if (options.hasKey(OPTIONS_TITLE)) {
title = options.getString(OPTIONS_TITLE);
}
if (options.hasKey(OPTIONS_DESC)) {
desc = options.getString(OPTIONS_DESC);
}
if (options.hasKey(OPTIONS_TAG_NAME)) {
tagName = options.getString(OPTIONS_TAG_NAME);
}
if (options.hasKey(OPTIONS_THUMB_SIZE)) {
thumbSize = options.getInt(OPTIONS_THUMB_SIZE);
}
if (options.hasKey(OPTIONS_TRANSACTION)) {
transaction = options.getString(OPTIONS_TRANSACTION);
}
if (options.hasKey(OPTIONS_SCENE)) {
scene = options.getInt(OPTIONS_SCENE);
}
if (!TextUtils.isEmpty(title)) {
msg.title = title;
}
if (!TextUtils.isEmpty(desc)) {
msg.description = desc;
}
if (!TextUtils.isEmpty(tagName)) {
msg.mediaTagName = tagName;
}
if (bitmap != null) {
Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, thumbSize, thumbSize, true);
bitmap.recycle();
msg.thumbData = bmpToByteArray(thumbBmp, true);
}
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.message = msg;
if (!TextUtils.isEmpty(transaction)) {
req.transaction = transaction;
} else {
req.transaction = String.valueOf(System.currentTimeMillis());
}
if (scene == 0 || scene == 1 || scene == 2) {
req.scene = scene;
} else {
req.scene = 0;
}
boolean sendReqOK = WeChatModule.wxApi.sendReq(req);
if (callback != null) {
callback.invoke(null, sendReqOK);
}
} else {
if (callback != null) {
callback.invoke("please setting share type !");
}
}
}
}
}
/**
* 分享到朋友圈
*/
@ReactMethod
public void shareToTimeline(ReadableMap data, Callback callback) {
_share(SendMessageToWX.Req.WXSceneTimeline, data, callback);
}
/**
* 微信支付
*/
@ReactMethod
public void weChatPay(ReadableMap options, Callback callback) {
if (WeChatModule.wxApi == null) {
if (callback != null) {
callback.invoke("please registerApp before this !");
}
return;
}
String appId = WeChatModule.appId;
String nonceStr = "";
String packageValue = "";
String partnerId = "";
String prepayId = "";
String timeStamp = "";
String sign = "";
if (options != null) {
if (options.hasKey(OPTIONS_APP_ID)) {
appId = options.getString(OPTIONS_APP_ID);
}
if (options.hasKey(OPTIONS_NONCE_STR)) {
nonceStr = options.getString(OPTIONS_NONCE_STR);
}
if (options.hasKey(OPTIONS_PACKAGE_VALUE)) {
packageValue = options.getString(OPTIONS_PACKAGE_VALUE);
}
if (options.hasKey(OPTIONS_PARTNER_ID)) {
partnerId = options.getString(OPTIONS_PARTNER_ID);
}
if (options.hasKey(OPTIONS_PREPAY_ID)) {
prepayId = options.getString(OPTIONS_PREPAY_ID);
}
if (options.hasKey(OPTIONS_TIME_STAMP)) {
timeStamp = options.getString(OPTIONS_TIME_STAMP);
}
if (options.hasKey(OPTIONS_SIGN)) {
sign = options.getString(OPTIONS_SIGN);
}
}
PayReq request = new PayReq();
request.appId = appId;
request.nonceStr = nonceStr;
request.packageValue = packageValue;
request.partnerId = partnerId;
request.prepayId = prepayId;
request.timeStamp = timeStamp;
request.sign = sign;
boolean sendReqOK = WeChatModule.wxApi.sendReq(request);
if (callback != null) {
callback.invoke(null, sendReqOK);
}
}
/**
* Added by heng on 2015/12/29
* <p/>
* Bitmap to byte array
*/
private byte[] bmpToByteArray(Bitmap bmp, boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 获取文本对象
* */
private WXTextObject getTextObj(ReadableMap options) {
WXTextObject textObject = new WXTextObject();
if (options.hasKey(OPTIONS_TEXT)) {
textObject.text = options.getString(OPTIONS_TEXT);
}
return textObject;
}
/**
* 获取图片对象
* */
private WXImageObject getImageObj(ReadableMap options) {
WXImageObject imageObject = new WXImageObject();
if (options.hasKey(OPTIONS_IMAGE_URL)) {
String remoteUrl = options.getString(OPTIONS_IMAGE_URL);
imageObject.imageUrl = remoteUrl;
try {
bitmap = BitmapFactory.decodeStream(new URL(remoteUrl).openStream());
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
}
}
if (options.hasKey(OPTIONS_IMAGE_PATH)) {
String localPath = options.getString(OPTIONS_IMAGE_PATH);
File file = new File(localPath);
if (file.exists()) {
imageObject.setImagePath(localPath);
bitmap = BitmapFactory.decodeFile(localPath);
} else {
bitmap = null;
}
}
return imageObject;
}
/**
* 获取网页对象
* */
private WXWebpageObject getWebpageObj(ReadableMap options) {
WXWebpageObject webpageObject = new WXWebpageObject();
if (options.hasKey(OPTIONS_WEBPAGE_URL)) {
webpageObject.webpageUrl = options.getString(OPTIONS_WEBPAGE_URL);
}
if(options.hasKey(OPTIONS_THUMB_IMAGE)){
String thumbImage = options.getString(OPTIONS_THUMB_IMAGE);
try {
bitmap = BitmapFactory.decodeStream(new URL(thumbImage).openStream());
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
}
}
return webpageObject;
}
/**
* 获取音乐对象
* */
private WXMusicObject getMusicObj(ReadableMap options) {
WXMusicObject musicObject = new WXMusicObject();
if (options.hasKey(OPTIONS_MUSIC_URL)) {
musicObject.musicUrl = options.getString(OPTIONS_MUSIC_URL);
}
if (options.hasKey(OPTIONS_MUSIC_LOW_BAND_URL)) {
musicObject.musicLowBandUrl = options.getString(OPTIONS_MUSIC_LOW_BAND_URL);
}
if(options.hasKey(OPTIONS_THUMB_IMAGE)){
String thumbImage = options.getString(OPTIONS_THUMB_IMAGE);
try {
bitmap = BitmapFactory.decodeStream(new URL(thumbImage).openStream());
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
}
}
return musicObject;
}
/**
* 获取视频对象
* */
private WXVideoObject getVideoObj(ReadableMap options){
WXVideoObject videoObject = new WXVideoObject();
if(options.hasKey(OPTIONS_VIDEO_URL)){
videoObject.videoUrl = options.getString(OPTIONS_VIDEO_URL);
}
if(options.hasKey(OPTIONS_VIDEO_LOW_BAND_URL)){
videoObject.videoLowBandUrl = options.getString(OPTIONS_VIDEO_LOW_BAND_URL);
}
if(options.hasKey(OPTIONS_THUMB_IMAGE)){
String thumbImage = options.getString(OPTIONS_THUMB_IMAGE);
try {
bitmap = BitmapFactory.decodeStream(new URL(thumbImage).openStream());
} catch (IOException e) {
bitmap = null;
e.printStackTrace();
}
}
return videoObject;
}
/**
* 分享到微信
* */
@ReactMethod
public void shareToSession(ReadableMap data, Callback callback){
_share(SendMessageToWX.Req.WXSceneSession, data, callback);
}
private String _getErrorMsg(int errCode) {
switch (errCode) {
case BaseResp.ErrCode.ERR_OK:
return "成功";
case BaseResp.ErrCode.ERR_COMM:
return "普通错误类型";
case BaseResp.ErrCode.ERR_USER_CANCEL:
return "用户点击取消并返回";
case BaseResp.ErrCode.ERR_SENT_FAILED:
return "发送失败";
case BaseResp.ErrCode.ERR_AUTH_DENIED:
return "授权失败";
case BaseResp.ErrCode.ERR_UNSUPPORT:
return "微信不支持";
default:
return "失败";
}
}
private void _share(final int scene, final ReadableMap data, final Callback callBack){
if (data.hasKey(RCTWXShareImageUrl)) {
String imageUrl = data.getString(RCTWXShareImageUrl);
DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber =
new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
// isFinished must be obtained before image, otherwise we might set intermediate result
// as final image.
boolean isFinished = dataSource.isFinished();
// float progress = dataSource.getProgress();
CloseableReference<CloseableImage> image = dataSource.getResult();
if (image != null) {
Drawable drawable = _createDrawable(image);
Bitmap bitmap = _drawable2Bitmap(drawable);
_share(scene, data, bitmap, callBack);
} else if (isFinished) {
_share(scene, data, null, callBack);
}
dataSource.close();
}
@Override
public void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
dataSource.close();
_share(scene, data, null, callBack);
}
@Override
public void onProgressUpdate(DataSource<CloseableReference<CloseableImage>> dataSource) {
}
};
ResizeOptions resizeOptions = null;
if (!(data.hasKey(RCTWXShareType) && data.getString(RCTWXShareType).equals(RCTWXShareTypeImage))) {
int size = 80;
if (data.hasKey(RCTWXShareThumbImageSize)) {
size = data.getInt(RCTWXShareThumbImageSize);
}
resizeOptions = new ResizeOptions(size, size);
}
_downloadImage(imageUrl, resizeOptions, dataSubscriber);
}
else {
_share(scene, data, null, callBack);
}
}
private void _share(int scene, ReadableMap data, Bitmap image, Callback callback) {
WXMediaMessage message = new WXMediaMessage();
if (data.hasKey(RCTWXShareTitle)){
message.title = data.getString(RCTWXShareTitle);
}
if (data.hasKey(RCTWXShareDescription)) {
message.description = data.getString(RCTWXShareDescription);
}
String type = RCTWXShareTypeNews;
if (data.hasKey(RCTWXShareType)) {
type = data.getString(RCTWXShareType);
}
if (type.equals(RCTWXShareTypeText)) {
WXTextObject object = new WXTextObject();
if (data.hasKey(RCTWXShareText)) {
object.text = data.getString(RCTWXShareText);
}
message.mediaObject = object;
}
else if (type.equals(RCTWXShareTypeImage)) {
WXImageObject object = new WXImageObject();
if (data.hasKey(RCTWXShareImageUrl)) {
if (image != null) {
object.imageData = _Bitmap2Bytes(image);
Bitmap thumb = Bitmap.createScaledBitmap(image, 80, 80, true);
message.thumbData = _Bitmap2Bytes(thumb);
}
}
message.mediaObject = object;
}
else {
if (type.equals(RCTWXShareTypeNews)) {
WXWebpageObject object = new WXWebpageObject();
if (data.hasKey(RCTWXShareWebpageUrl)){
object.webpageUrl = data.getString(RCTWXShareWebpageUrl);
}
if (data.hasKey("extInfo")){
object.extInfo = data.getString("extInfo");
}
message.mediaObject = object;
}
else if (type.equals(RCTWXShareTypeVideo)) {
WXMusicObject object = new WXMusicObject();
if (data.hasKey(RCTWXShareWebpageUrl)) {
object.musicUrl = data.getString(RCTWXShareWebpageUrl);
}
message.mediaObject = object;
}
else if (type.equals(RCTWXShareTypeAudio)) {
WXVideoObject object = new WXVideoObject();
if (data.hasKey(RCTWXShareWebpageUrl)) {
object.videoUrl = data.getString(RCTWXShareWebpageUrl);
}
message.mediaObject = object;
}
if (image != null) {
Log.e("share", "image no null");
message.setThumbImage(image);
}else {
Log.e("share", "image null");
}
}
//TODO: create Thumb Data.
if (data.hasKey("mediaTagName")) {
message.mediaTagName = data.getString("mediaTagName");
}
if (data.hasKey("messageAction")) {
message.mediaTagName = data.getString("messageAction");
}
if (data.hasKey("messageExt")) {
message.mediaTagName = data.getString("messageExt");
}
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.message = message;
req.scene = scene;
boolean success = WeChatModule.wxApi.sendReq(req);
if (success == false) {
callback.invoke("INVOKE_FAILED");
}else {
callback.invoke();
}
}
private static @Nullable
Uri getResourceDrawableUri(Context context, @Nullable String name) {
if (name == null || name.isEmpty()) {
return null;
}
name = name.toLowerCase().replace("-", "_");
int resId = context.getResources().getIdentifier(
name,
"drawable",
context.getPackageName());
return new Uri.Builder()
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
.path(String.valueOf(resId))
.build();
}
private void _downloadImage(String imageUrl, ResizeOptions resizeOptions, DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber) {
Uri uri = null;
try {
uri = Uri.parse(imageUrl);
// Verify scheme is set, so that relative uri (used by static resources) are not handled.
if (uri.getScheme() == null) {
uri = null;
}
} catch (Exception e) {
// ignore malformed uri, then attempt to extract resource ID.
}
if (uri == null) {
uri = getResourceDrawableUri(getReactApplicationContext(), imageUrl);
} else {
}
ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
if (resizeOptions != null) {
builder = builder.setResizeOptions(resizeOptions);
}
ImageRequest imageRequest = builder.build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, null);
dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance());
}
private Drawable _createDrawable(CloseableReference<CloseableImage> image) {
Preconditions.checkState(CloseableReference.isValid(image));
CloseableImage closeableImage = image.get();
if (closeableImage instanceof CloseableStaticBitmap) {
CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
BitmapDrawable bitmapDrawable = new BitmapDrawable(
getReactApplicationContext().getResources(),
closeableStaticBitmap.getUnderlyingBitmap());
if (closeableStaticBitmap.getRotationAngle() == 0 ||
closeableStaticBitmap.getRotationAngle() == EncodedImage.UNKNOWN_ROTATION_ANGLE) {
return bitmapDrawable;
} else {
return new OrientedDrawable(bitmapDrawable, closeableStaticBitmap.getRotationAngle());
}
} else {
throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
}
}
private Bitmap _drawable2Bitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}
}
private byte[] _Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 70, baos);
return baos.toByteArray();
}
}