升级微博SDK

This commit is contained in:
v7lin 2022-07-26 10:51:38 +08:00
parent 748de42ea9
commit 7426b83456
6 changed files with 222 additions and 106 deletions

View File

@ -56,7 +56,6 @@ android {
} }
dependencies { dependencies {
// v10.10.0
vendorImplementation 'androidx.appcompat:appcompat:1.0.0' vendorImplementation 'androidx.appcompat:appcompat:1.0.0'
vendorImplementation 'io.github.sinaweibosdk:core:11.12.0@aar' vendorImplementation 'io.github.sinaweibosdk:core:12.5.0@aar'
} }

View File

@ -14,6 +14,7 @@ import androidx.annotation.Nullable;
import com.sina.weibo.sdk.api.ImageObject; import com.sina.weibo.sdk.api.ImageObject;
import com.sina.weibo.sdk.api.MultiImageObject; import com.sina.weibo.sdk.api.MultiImageObject;
import com.sina.weibo.sdk.api.TextObject; import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.VideoSourceObject;
import com.sina.weibo.sdk.api.WebpageObject; import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage; import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.auth.AuthInfo; import com.sina.weibo.sdk.auth.AuthInfo;
@ -27,7 +28,9 @@ import com.sina.weibo.sdk.share.WbShareCallback;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -164,21 +167,35 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
iwbapi.registerApp(applicationContext, new AuthInfo(applicationContext, appKey, redirectUrl, scope)); iwbapi.registerApp(applicationContext, new AuthInfo(applicationContext, appKey, redirectUrl, scope));
result.success(null); result.success(null);
} else if ("isInstalled".equals(call.method)) { } else if ("isInstalled".equals(call.method)) {
if (iwbapi != null) {
result.success(iwbapi.isWBAppInstalled()); result.success(iwbapi.isWBAppInstalled());
} else {
result.error("FAILED", "请先调用registerApp", null);
}
} else if ("isSupportMultipleImage".equals(call.method)) {
if (iwbapi != null) {
result.success(iwbapi.isWBAppSupportMultipleImage());
} else {
result.error("FAILED", "请先调用registerApp", null);
}
} else if ("auth".equals(call.method)) { } else if ("auth".equals(call.method)) {
if (iwbapi != null) {
handleAuthCall(call, result); handleAuthCall(call, result);
} else if ("shareText".equals(call.method)) { } else {
handleShareTextCall(call, result); result.error("FAILED", "请先调用registerApp", null);
} else if ("shareImage".equals(call.method) || }
"shareWebpage".equals(call.method)) { } else if (Arrays.asList("shareText", "shareImage", "shareMultiImage", "shareVideo", "shareWebpage").contains(call.method)) {
handleShareMediaCall(call, result); if (iwbapi != null) {
handleShareCall(call, result);
} else {
result.error("FAILED", "请先调用registerApp", null);
}
} else { } else {
result.notImplemented(); result.notImplemented();
} }
} }
private void handleAuthCall(@NonNull MethodCall call, @NonNull Result result) { private void handleAuthCall(@NonNull MethodCall call, @NonNull Result result) {
if (iwbapi != null) {
iwbapi.authorize(activityPluginBinding.getActivity(), new WbAuthListener() { iwbapi.authorize(activityPluginBinding.getActivity(), new WbAuthListener() {
@Override @Override
public void onComplete(Oauth2AccessToken token) { public void onComplete(Oauth2AccessToken token) {
@ -216,39 +233,39 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
} }
} }
}); });
}
result.success(null); result.success(null);
} }
private void handleShareTextCall(@NonNull MethodCall call, @NonNull Result result) { private void handleShareCall(@NonNull MethodCall call, @NonNull Result result) {
final WeiboMultiMessage message = new WeiboMultiMessage(); final WeiboMultiMessage message = new WeiboMultiMessage();
if ("shareText".equals(call.method)) {
final TextObject object = new TextObject(); final TextObject object = new TextObject();
object.text = call.argument("text");// 1024 object.text = call.argument("text");// 1024
message.textObject = object; message.textObject = object;
} else if ("shareImage".equals(call.method)) {
if (iwbapi != null) {
iwbapi.shareMessage(activityPluginBinding.getActivity(), message, false);
}
result.success(null);
}
private void handleShareMediaCall(@NonNull MethodCall call, @NonNull Result result) {
final WeiboMultiMessage message = new WeiboMultiMessage();
if ("shareImage".equals(call.method)) {
if (call.hasArgument("text")) { if (call.hasArgument("text")) {
final TextObject object = new TextObject(); final TextObject object = new TextObject();
object.text = call.argument("text");// 1024 object.text = call.argument("text");// 1024
message.textObject = object; message.textObject = object;
} }
final ImageObject object = new ImageObject();
if (iwbapi != null && iwbapi.isWBAppSupportMultipleImage() && call.hasArgument("imageUri")) { if (call.hasArgument("imageData")) {
final MultiImageObject object = new MultiImageObject(); object.imageData = call.argument("imageData");// 2 * 1024 * 1024
} else if (call.hasArgument("imageUri")) {
final String imageUri = call.argument("imageUri"); final String imageUri = call.argument("imageUri");
object.imagePath = Uri.parse(imageUri).getPath();// 512 - 10 * 1024 * 1024
}
message.imageObject = object;
} else if ("shareMultiImage".equals(call.method)) {
if (call.hasArgument("text")) {
final TextObject object = new TextObject();
object.text = call.argument("text");// 1024
message.textObject = object;
}
final MultiImageObject object = new MultiImageObject();
final List<String> imageUris = call.argument("imageUris");
final ArrayList<Uri> images = new ArrayList<>(); final ArrayList<Uri> images = new ArrayList<>();
for (String imageUri : imageUris) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try { try {
final ProviderInfo providerInfo = applicationContext.getPackageManager().getProviderInfo(new ComponentName(applicationContext, FileProvider.class), PackageManager.MATCH_DEFAULT_ONLY); final ProviderInfo providerInfo = applicationContext.getPackageManager().getProviderInfo(new ComponentName(applicationContext, FileProvider.class), PackageManager.MATCH_DEFAULT_ONLY);
@ -261,18 +278,30 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
} else { } else {
images.add(Uri.parse(imageUri)); images.add(Uri.parse(imageUri));
} }
}
object.imageList = images; object.imageList = images;
message.mediaObject = object; message.multiImageObject = object;
} else if ("shareVideo".equals(call.method)) {
if (call.hasArgument("text")) {
final TextObject object = new TextObject();
object.text = call.argument("text");// 1024
message.textObject = object;
}
final VideoSourceObject object = new VideoSourceObject();
final String videoUri = call.argument("videoUri");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
final ProviderInfo providerInfo = applicationContext.getPackageManager().getProviderInfo(new ComponentName(applicationContext, FileProvider.class), PackageManager.MATCH_DEFAULT_ONLY);
final Uri shareFileUri = FileProvider.getUriForFile(applicationContext, providerInfo.authority, new File(Uri.parse(videoUri).getPath()));
applicationContext.grantUriPermission("com.sina.weibo", shareFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
object.videoPath = shareFileUri;
} catch (PackageManager.NameNotFoundException e) {
object.videoPath = Uri.parse(videoUri);
}
} else { } else {
final ImageObject object = new ImageObject(); object.videoPath = Uri.parse(videoUri);
if (call.hasArgument("imageData")) {
object.imageData = call.argument("imageData");// 2 * 1024 * 1024
} else if (call.hasArgument("imageUri")) {
final String imageUri = call.argument("imageUri");
object.imagePath = Uri.parse(imageUri).getPath();// 512 - 10 * 1024 * 1024
}
message.mediaObject = object;
} }
message.videoSourceObject = object;
} else if ("shareWebpage".equals(call.method)) { } else if ("shareWebpage".equals(call.method)) {
final WebpageObject object = new WebpageObject(); final WebpageObject object = new WebpageObject();
object.identify = UUID.randomUUID().toString(); object.identify = UUID.randomUUID().toString();
@ -285,9 +314,9 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis
message.mediaObject = object; message.mediaObject = object;
} }
if (iwbapi != null) { final boolean clientOnly = call.argument("clientOnly");
iwbapi.shareMessage(activityPluginBinding.getActivity(), message, false);
} iwbapi.shareMessage(activityPluginBinding.getActivity(), message, clientOnly);
result.success(null); result.success(null);
} }
} }

View File

@ -32,6 +32,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
path: ^1.8.1
path_provider: ^2.0.10
image: ^3.0.1 image: ^3.0.1
flutter_cache_manager: ^3.0.0 flutter_cache_manager: ^3.0.0

View File

@ -12,21 +12,16 @@ import 'package:weibo_kit/src/weibo_kit_platform_interface.dart';
class MethodChannelWeiboKit extends WeiboKitPlatform { class MethodChannelWeiboKit extends WeiboKitPlatform {
/// The method channel used to interact with the native platform. /// The method channel used to interact with the native platform.
@visibleForTesting @visibleForTesting
late final MethodChannel methodChannel = late final MethodChannel methodChannel = const MethodChannel('v7lin.github.io/weibo_kit')..setMethodCallHandler(_handleMethod);
const MethodChannel('v7lin.github.io/weibo_kit') final StreamController<BaseResp> _respStreamController = StreamController<BaseResp>.broadcast();
..setMethodCallHandler(_handleMethod);
final StreamController<BaseResp> _respStreamController =
StreamController<BaseResp>.broadcast();
Future<dynamic> _handleMethod(MethodCall call) async { Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) { switch (call.method) {
case 'onAuthResp': case 'onAuthResp':
_respStreamController.add(AuthResp.fromJson( _respStreamController.add(AuthResp.fromJson((call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break; break;
case 'onShareMsgResp': case 'onShareMsgResp':
_respStreamController.add(ShareMsgResp.fromJson( _respStreamController.add(ShareMsgResp.fromJson((call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break; break;
} }
} }
@ -60,6 +55,11 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
return await methodChannel.invokeMethod<bool>('isInstalled') ?? false; return await methodChannel.invokeMethod<bool>('isInstalled') ?? false;
} }
@override
Future<bool> isSupportMultipleImage() async {
return await methodChannel.invokeMethod<bool>('isSupportMultipleImage') ?? false;
}
@override @override
Future<void> auth({ Future<void> auth({
required String appKey, required String appKey,
@ -79,11 +79,13 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
@override @override
Future<void> shareText({ Future<void> shareText({
required String text, required String text,
bool clientOnly = false,
}) { }) {
return methodChannel.invokeMethod<void>( return methodChannel.invokeMethod<void>(
'shareText', 'shareText',
<String, dynamic>{ <String, dynamic>{
'text': text, 'text': text,
'clientOnly': clientOnly,
}, },
); );
} }
@ -93,19 +95,54 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
String? text, String? text,
Uint8List? imageData, Uint8List? imageData,
Uri? imageUri, Uri? imageUri,
bool clientOnly = false,
}) { }) {
assert(text == null || text.length <= 1024); assert(text == null || text.length <= 1024);
assert((imageData != null && imageData.lengthInBytes <= 2 * 1024 * 1024) || assert((imageData != null && imageData.lengthInBytes <= 2 * 1024 * 1024) ||
(imageUri != null && (imageUri != null && imageUri.isScheme('file') && imageUri.toFilePath().length <= 512 && File.fromUri(imageUri).lengthSync() <= 10 * 1024 * 1024));
imageUri.isScheme('file') &&
imageUri.toFilePath().length <= 512 &&
File.fromUri(imageUri).lengthSync() <= 10 * 1024 * 1024));
return methodChannel.invokeMethod<void>( return methodChannel.invokeMethod<void>(
'shareImage', 'shareImage',
<String, dynamic>{ <String, dynamic>{
if (text != null && text.isNotEmpty) 'text': text, if (text != null && text.isNotEmpty) 'text': text,
if (imageData != null) 'imageData': imageData, if (imageData != null) 'imageData': imageData,
if (imageUri != null) 'imageUri': imageUri.toString(), if (imageUri != null) 'imageUri': imageUri.toString(),
'clientOnly': clientOnly,
},
);
}
@override
Future<void> shareMultiImage({
String? text,
required List<Uri> imageUris,
bool clientOnly = false,
}) {
assert(text == null || text.length <= 1024);
assert(imageUris.isNotEmpty && imageUris.every((Uri element) => element.isScheme('file')));
return methodChannel.invokeMethod<void>(
'shareMultiImage',
<String, dynamic>{
if (text != null && text.isNotEmpty) 'text': text,
'imageUris': imageUris.map((Uri element) => element.toString()).toList(),
'clientOnly': clientOnly,
},
);
}
@override
Future<void> shareVideo({
String? text,
required Uri videoUri,
bool clientOnly = false,
}) {
assert(text == null || text.length <= 1024);
assert(videoUri.isScheme('file'));
return methodChannel.invokeMethod<void>(
'shareVideo',
<String, dynamic>{
if (text != null && text.isNotEmpty) 'text': text,
'videoUri': videoUri.toString(),
'clientOnly': clientOnly,
}, },
); );
} }
@ -116,6 +153,7 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
required String description, required String description,
required Uint8List thumbData, required Uint8List thumbData,
required String webpageUrl, required String webpageUrl,
bool clientOnly = false,
}) { }) {
assert(title.length <= 512); assert(title.length <= 512);
assert(description.isNotEmpty && description.length <= 1024); assert(description.isNotEmpty && description.length <= 1024);
@ -128,6 +166,7 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
'description': description, 'description': description,
'thumbData': thumbData, 'thumbData': thumbData,
'webpageUrl': webpageUrl, 'webpageUrl': webpageUrl,
'clientOnly': clientOnly,
}, },
); );
} }

View File

@ -31,11 +31,9 @@ abstract class WeiboKitPlatform extends PlatformInterface {
required String appKey, required String appKey,
required String? universalLink, required String? universalLink,
required List<String> scope, required List<String> scope,
String redirectUrl = WeiboRegister String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL, // -> -> -> -> OAuth2.0
.DEFAULT_REDIRECTURL, // -> -> -> -> OAuth2.0
}) { }) {
throw UnimplementedError( throw UnimplementedError('registerApp({required appKey, required universalLink, required scope, redirectUrl}) has not been implemented.');
'registerApp({required appKey, required universalLink, required scope, redirectUrl}) has not been implemented.');
} }
/// ///
@ -48,43 +46,66 @@ abstract class WeiboKitPlatform extends PlatformInterface {
throw UnimplementedError('isInstalled() has not been implemented.'); throw UnimplementedError('isInstalled() has not been implemented.');
} }
///
Future<bool> isSupportMultipleImage() {
throw UnimplementedError('isSupportMultipleImage() has not been implemented.');
}
/// ///
Future<void> auth({ Future<void> auth({
required String appKey, required String appKey,
required List<String> scope, required List<String> scope,
String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL, String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL,
}) { }) {
throw UnimplementedError( throw UnimplementedError('auth({required appKey, required scope, redirectUrl}) has not been implemented.');
'auth({required appKey, required scope, redirectUrl}) has not been implemented.');
} }
/// - /// -
Future<void> shareText({ Future<void> shareText({
required String text, required String text,
bool clientOnly = false,
}) { }) {
throw UnimplementedError( throw UnimplementedError('shareText({required text, clientOnly}) has not been implemented.');
'shareText({required text}) has not been implemented.');
} }
/// - /// -
/// shareMultiImage
Future<void> shareImage({ Future<void> shareImage({
String? text, String? text,
Uint8List? imageData, Uint8List? imageData,
Uri? imageUri, Uri? imageUri,
bool clientOnly = false,
}) { }) {
throw UnimplementedError( throw UnimplementedError('shareImage({text, imageData, imageUri, clientOnly}) has not been implemented.');
'shareImage({text, imageData, imageUri}) has not been implemented.'); }
/// -
Future<void> shareMultiImage({
String? text,
required List<Uri> imageUris,
bool clientOnly = false,
}) {
throw UnimplementedError('shareMultiImage({text, required imageUris, clientOnly}) has not been implemented.');
}
/// -
Future<void> shareVideo({
String? text,
required Uri videoUri,
bool clientOnly = false,
}) {
throw UnimplementedError('shareVideo({text, required videoUri, clientOnly}) has not been implemented.');
} }
/// - /// -
@Deprecated('iOS分享多媒体已经弃用 请不要用相关api') /// iOS api
Future<void> shareWebpage({ Future<void> shareWebpage({
required String title, required String title,
required String description, required String description,
required Uint8List thumbData, required Uint8List thumbData,
required String webpageUrl, required String webpageUrl,
bool clientOnly = false,
}) { }) {
throw UnimplementedError( throw UnimplementedError('shareWebpage({required title, required description, required thumbData, required webpageUrl, clientOnly}) has not been implemented.');
'shareWebpage({required title, required description, required thumbData, required webpageUrl}) has not been implemented.');
} }
} }

View File

@ -31,6 +31,11 @@ class MockWeiboKitPlatform
throw UnimplementedError(); throw UnimplementedError();
} }
@override
Future<bool> isSupportMultipleImage() {
throw UnimplementedError();
}
@override @override
Future<void> auth({ Future<void> auth({
required String appKey, required String appKey,
@ -43,6 +48,7 @@ class MockWeiboKitPlatform
@override @override
Future<void> shareText({ Future<void> shareText({
required String text, required String text,
bool clientOnly = false,
}) { }) {
throw UnimplementedError(); throw UnimplementedError();
} }
@ -52,6 +58,25 @@ class MockWeiboKitPlatform
String? text, String? text,
Uint8List? imageData, Uint8List? imageData,
Uri? imageUri, Uri? imageUri,
bool clientOnly = false,
}) {
throw UnimplementedError();
}
@override
Future<void> shareMultiImage({
String? text,
required List<Uri> imageUris,
bool clientOnly = false,
}) {
throw UnimplementedError();
}
@override
Future<void> shareVideo({
String? text,
required Uri videoUri,
bool clientOnly = false,
}) { }) {
throw UnimplementedError(); throw UnimplementedError();
} }
@ -62,6 +87,7 @@ class MockWeiboKitPlatform
required String description, required String description,
required Uint8List thumbData, required Uint8List thumbData,
required String webpageUrl, required String webpageUrl,
bool clientOnly = false,
}) { }) {
throw UnimplementedError(); throw UnimplementedError();
} }