提交 06a05a58 authored 作者: JarvanMo's avatar JarvanMo

android:refactor,add checkWeChat method

上级 5df021f8
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jarvan.fluwx">
</manifest>
package="com.jarvan.fluwx"></manifest>
package com.jarvan.fluwx
import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.constant.WeChatPluginMethods.IS_WE_CHAT_INSTALLED
import com.jarvan.fluwx.handler.FluwxLoginHandler
import com.jarvan.fluwx.handler.FluwxResponseHandler
import com.jarvan.fluwx.handler.FluwxShareHandler
import com.jarvan.fluwx.handler.WXAPiHandler
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
......@@ -14,33 +17,40 @@ class FluwxPlugin(private var registrar: Registrar) : MethodCallHandler {
@JvmStatic
fun registerWith(registrar: Registrar): Unit {
val channel = MethodChannel(registrar.messenger(), "fluwx")
WXAPiHandler.setRegistrar(registrar)
FluwxShareHandler.setRegistrar(registrar)
FluwxShareHandler.setMethodChannel(channel)
FluwxResponseHandler.setMethodChannel(channel)
channel.setMethodCallHandler(FluwxPlugin(registrar))
}
}
override fun onMethodCall(call: MethodCall, result: Result): Unit {
if(call.method == WeChatPluginMethods.REGISTER_APP ){
FluwxShareHandler.registerApp(call,result)
if (call.method == WeChatPluginMethods.REGISTER_APP) {
WXAPiHandler.registerApp(call, result)
return
}
if(call.method == WeChatPluginMethods.UNREGISTER_APP){
FluwxShareHandler.unregisterApp(call)
result.success(true)
if (call.method == WeChatPluginMethods.UNREGISTER_APP) {
// FluwxShareHandler.unregisterApp(call)
// result.success(true)
return
}
if ("sendAuth" == call.method){
FluwxLoginHandler.sendAuth(call,result)
if(call.method == IS_WE_CHAT_INSTALLED){
WXAPiHandler.checkWeChatInstallation(result)
return
}
if( call.method.startsWith("share")){
if ("sendAuth" == call.method) {
FluwxLoginHandler.sendAuth(call, result)
return
}
if (call.method.startsWith("share")) {
FluwxShareHandler.handle(call, result)
}else{
} else {
result.notImplemented()
}
......
......@@ -10,6 +10,8 @@ public class WeChatPluginMethods {
public static final String UNREGISTER_APP = "unregisterApp";
public static final String WE_CHAT_SHARE_RESPONSE = "onShareResponse";
public static final String IS_WE_CHAT_INSTALLED = "isWeChatInstalled";
public static final String SHARE_TEXT = "shareText";
public static final String SHARE_IMAGE = "shareImage";
public static final String SHARE_MUSIC = "shareMusic";
......
package com.jarvan.fluwx.handler
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
object FluwxLoginHandler {
private var channel: MethodChannel? = null
fun setMethodChannel(channel: MethodChannel) {
FluwxLoginHandler.channel = channel
}
internal object FluwxLoginHandler {
fun sendAuth(call: MethodCall, result: MethodChannel.Result) {
val req = SendAuth.Req()
req.scope = call.argument("scope")
req.state = call.argument("state")
result.success(WXAPiHandler.wxApi!!.sendReq(req))
result.success(WXAPiHandler.wxApi?.sendReq(req))
}
fun handleResponse(response:BaseResp){
fun hehe(){
}
}
\ No newline at end of file
package com.jarvan.fluwx.handler
import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.constant.WechatPluginKeys
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import io.flutter.plugin.common.MethodChannel
object FluwxResponseHandler {
private var channel: MethodChannel? = null
private const val errStr = "errStr"
private const val errCode = "errCode"
private const val openId ="openId"
private const val type = "type"
fun setMethodChannel(channel: MethodChannel) {
FluwxResponseHandler.channel = channel
}
fun handleResponse(response: BaseResp) {
if (response is SendAuth.Resp) {
handleAuthResponse(response)
}else if (response is SendMessageToWX.Resp){
handleSendMessageResp(response)
}
}
private fun handleSendMessageResp(response: SendMessageToWX.Resp) {
val result = mapOf(
errStr to response.errStr,
WechatPluginKeys.TRANSACTION to response.transaction,
type to response.type,
errCode to response.errCode,
openId to response.openId,
WechatPluginKeys.PLATFORM to "android"
)
channel?.invokeMethod(WeChatPluginMethods.WE_CHAT_SHARE_RESPONSE, result)
}
private fun handleAuthResponse(response:SendAuth.Resp){
val result = mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
errCode to response.errCode,
"code" to response.code,
"state" to response.state,
"lang" to response.lang,
"country" to response.country,
errStr to response.errStr,
openId to response.openId,
"url" to response.url,
type to response.type,
WechatPluginKeys.TRANSACTION to response.transaction
)
channel?.invokeMethod("onAuthResponse", result)
}
}
\ No newline at end of file
......@@ -5,10 +5,7 @@ import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.constant.WechatPluginKeys
import com.jarvan.fluwx.utils.ShareImageUtil
import com.jarvan.fluwx.utils.WeChatThumbnailUtil
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.*
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
......@@ -23,8 +20,8 @@ import kotlinx.coroutines.experimental.launch
* 冷风如刀,以大地为砧板,视众生为鱼肉。
* 万里飞雪,将穹苍作烘炉,熔万物为白银。
**/
object FluwxShareHandler {
private var wxApi: IWXAPI? = null
internal object FluwxShareHandler {
private var channel: MethodChannel? = null
......@@ -35,50 +32,6 @@ object FluwxShareHandler {
FluwxShareHandler.channel = channel
}
fun setWXApi(wxApi:IWXAPI){
this.wxApi = wxApi
}
fun registerApp(call: MethodCall, result: MethodChannel.Result) {
if(!call.argument<Boolean>(WechatPluginKeys.ANDROID)){
result.success(mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
WechatPluginKeys.RESULT to false
))
return
}
if (wxApi != null) {
result.success(mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
WechatPluginKeys.RESULT to true
))
return
}
val appId:String? = call.argument(WechatPluginKeys.APP_ID)
if (appId.isNullOrBlank()) {
result.error("invalid app id", "are you sure your app id is correct ?", appId)
return
}
val api = WXAPIFactory.createWXAPI(registrar!!.context().applicationContext, appId)
val registered = api.registerApp(appId)
wxApi = api
result.success(mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
WechatPluginKeys.RESULT to registered
))
}
fun unregisterApp(call: MethodCall) {
if(!call.argument<Boolean>(WechatPluginKeys.ANDROID)){
return
}
wxApi?.unregisterApp()
wxApi = null
}
fun setRegistrar(registrar: PluginRegistry.Registrar) {
FluwxShareHandler.registrar = registrar
......@@ -86,12 +39,12 @@ object FluwxShareHandler {
fun handle(call: MethodCall, result: MethodChannel.Result) {
if (wxApi == null) {
if (WXAPiHandler.wxApi == null) {
result.error(CallResult.RESULT_API_NULL, "please config wxapi first", null)
return
}
if (!wxApi!!.isWXAppInstalled) {
if (!WXAPiHandler.wxApi!!.isWXAppInstalled) {
result.error(CallResult.RESULT_WE_CHAT_NOT_INSTALLED, CallResult.RESULT_WE_CHAT_NOT_INSTALLED, null)
return
}
......@@ -124,7 +77,7 @@ object FluwxShareHandler {
msg.mediaTagName = call.argument<String>(WechatPluginKeys.MEDIA_TAG_NAME)
setCommonArguments(call, req, msg)
val done = wxApi?.sendReq(req)
val done = WXAPiHandler.wxApi?.sendReq(req)
result.success(
mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
......@@ -156,7 +109,7 @@ object FluwxShareHandler {
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
val done = wxApi?.sendReq(req)
val done = WXAPiHandler.wxApi?.sendReq(req)
result.success(
mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
......@@ -239,7 +192,7 @@ object FluwxShareHandler {
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
val done = wxApi?.sendReq(req)
val done = WXAPiHandler.wxApi?.sendReq(req)
result.success(
mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
......@@ -273,7 +226,7 @@ object FluwxShareHandler {
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
val done = wxApi?.sendReq(req)
val done = WXAPiHandler.wxApi?.sendReq(req)
result.success(
mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
......@@ -307,7 +260,7 @@ object FluwxShareHandler {
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
val done = wxApi?.sendReq(req)
val done = WXAPiHandler.wxApi?.sendReq(req)
result.success(
mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
......@@ -336,7 +289,7 @@ object FluwxShareHandler {
val req = SendMessageToWX.Req()
setCommonArguments(call, req, msg)
req.message = msg
val done = wxApi?.sendReq(req)
val done = WXAPiHandler.wxApi?.sendReq(req)
result.success(
mapOf(
WechatPluginKeys.PLATFORM to WechatPluginKeys.ANDROID,
......@@ -365,19 +318,7 @@ object FluwxShareHandler {
//
// return imgObj
// }
fun onResp(resp: BaseResp) {
val result = mapOf(
"errStr" to resp.errStr,
"transaction" to resp.transaction,
"type" to resp.type,
"errCode" to resp.errCode,
"openId" to resp.openId,
WechatPluginKeys.PLATFORM to "android"
)
channel?.invokeMethod(WeChatPluginMethods.WE_CHAT_SHARE_RESPONSE, result)
}
private fun getScene(value: String) = when (value) {
WechatPluginKeys.SCENE_TIMELINE -> SendMessageToWX.Req.WXSceneTimeline
......
package com.jarvan.fluwx.handler
import com.jarvan.fluwx.constant.CallResult
import com.jarvan.fluwx.constant.WechatPluginKeys
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
......@@ -13,7 +14,6 @@ object WXAPiHandler {
var wxApi: IWXAPI? = null
fun setRegistrar(registrar: PluginRegistry.Registrar) {
WXAPiHandler.registrar = registrar
}
......@@ -21,7 +21,7 @@ object WXAPiHandler {
fun registerApp(call: MethodCall, result: MethodChannel.Result) {
if(!call.argument<Boolean>(WechatPluginKeys.ANDROID)){
if (!call.argument<Boolean>(WechatPluginKeys.ANDROID)) {
return
}
......@@ -33,7 +33,7 @@ object WXAPiHandler {
return
}
val appId:String? = call.argument(WechatPluginKeys.APP_ID)
val appId: String? = call.argument(WechatPluginKeys.APP_ID)
if (appId.isNullOrBlank()) {
result.error("invalid app id", "are you sure your app id is correct ?", appId)
return
......@@ -47,4 +47,14 @@ object WXAPiHandler {
WechatPluginKeys.RESULT to registered
))
}
fun checkWeChatInstallation(result: MethodChannel.Result){
if (wxApi == null) {
result.error(CallResult.RESULT_API_NULL, "please config wxapi first", null)
return
}else{
result.success(wxApi!!.isWXAppInstalled)
}
}
}
\ No newline at end of file
......@@ -52,7 +52,6 @@ public class ShareImageUtil {
}
private static byte[] streamToByteArray(InputStream inputStream) {
Bitmap bmp = null;
bmp = BitmapFactory.decodeStream(inputStream);
......
......@@ -88,28 +88,27 @@ public class ThumbnailCompressUtil {
// 重置baos即清空baos
baos.reset();
// 这里压缩options%,把压缩后的数据存放到baos中
if(options <= 0){
if (options <= 0) {
options = 0;
}
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
if(options == 0 ){
if (options == 0) {
break;
}
}
return bitmap;
}
public static Bitmap compress(String nativeImagePath){
public static Bitmap compress(String nativeImagePath) {
Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
if (nativeImagePath.toLowerCase().endsWith(".png")) {
format = Bitmap.CompressFormat.PNG;
}
Log.e("tag",nativeImagePath);
Log.e("tag", nativeImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeFile(nativeImagePath);
bitmap.compress(format, 90, baos);
......@@ -124,6 +123,7 @@ public class ThumbnailCompressUtil {
return result;
}
public static Bitmap createScaledBitmapWithRatio(Bitmap bitmap, float thumbWidth, boolean recycle) {
Bitmap thumb;
int imagw = bitmap.getWidth();
......@@ -156,20 +156,20 @@ public class ThumbnailCompressUtil {
}
public static Bitmap createScaledBitmapWithRatio(Bitmap bitmap , int maxLength, boolean recycle){
public static Bitmap createScaledBitmapWithRatio(Bitmap bitmap, int maxLength, boolean recycle) {
Bitmap result = bitmap;
while (true){
double ratio =((double) maxLength )/ result.getByteCount();
while (true) {
double ratio = ((double) maxLength) / result.getByteCount();
double width = result.getWidth() * Math.sqrt(ratio);
double height = result.getHeight() * Math.sqrt(ratio);
Bitmap tmp = Bitmap.createScaledBitmap(result, (int) width, (int)height, true);
if (result != bitmap){
Bitmap tmp = Bitmap.createScaledBitmap(result, (int) width, (int) height, true);
if (result != bitmap) {
result.recycle();
}
result = tmp;
if (result.getByteCount() < maxLength ) {
if (result.getByteCount() < maxLength) {
break;
}
......@@ -179,7 +179,7 @@ public class ThumbnailCompressUtil {
bitmap.recycle();
}
return result;
return result;
}
......
......@@ -81,7 +81,7 @@ class Util {
}
public static byte[] getHtmlByteArray(final String url) {
public static byte[] getHtmlByteArray(final String url) {
URL htmlUrl = null;
InputStream inStream = null;
try {
......
......@@ -30,7 +30,7 @@ import okio.Source;
import top.zibin.luban.Luban;
public class WeChatThumbnailUtil {
public static final int SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH = 120 * 1024;
public static final int SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH = 120 * 1024;
public static final int SHARE_IMAGE_THUMB_LENGTH = 32 * 1024;
private static final int COMMON_THUMB_WIDTH = 150;
......@@ -46,7 +46,7 @@ public class WeChatThumbnailUtil {
} else {
file = downloadImage(thumbnail);
}
return compress(file, registrar,SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH);
return compress(file, registrar, SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH);
}
......@@ -59,7 +59,7 @@ public class WeChatThumbnailUtil {
} else {
file = downloadImage(thumbnail);
}
return compress(file, registrar,SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH);
return compress(file, registrar, SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH);
}
public static byte[] thumbnailForCommon(String thumbnail, PluginRegistry.Registrar registrar) {
......@@ -71,10 +71,10 @@ public class WeChatThumbnailUtil {
} else {
file = downloadImage(thumbnail);
}
return compress(file, registrar,SHARE_IMAGE_THUMB_LENGTH);
return compress(file, registrar, SHARE_IMAGE_THUMB_LENGTH);
}
private static byte[] compress(File file, PluginRegistry.Registrar registrar,int resultMaxLength) {
private static byte[] compress(File file, PluginRegistry.Registrar registrar, int resultMaxLength) {
if (file == null) {
return new byte[]{};
}
......@@ -86,7 +86,7 @@ public class WeChatThumbnailUtil {
.ignoreBy(resultMaxLength)
.setTargetDir(registrar.context().getCacheDir().getAbsolutePath())
.get(file.getAbsolutePath());
if (compressedFile.length() < resultMaxLength ) {
if (compressedFile.length() < resultMaxLength) {
Source source = Okio.source(compressedFile);
BufferedSource bufferedSource = Okio.buffer(source);
byte[] bytes = bufferedSource.readByteArray();
......@@ -94,9 +94,7 @@ public class WeChatThumbnailUtil {
bufferedSource.close();
return bytes;
}
return createScaledBitmapWithRatio(compressedFile,resultMaxLength);
return createScaledBitmapWithRatio(compressedFile, resultMaxLength);
} catch (IOException e) {
......@@ -105,7 +103,7 @@ public class WeChatThumbnailUtil {
return new byte[]{};
}
private static byte[] createScaledBitmapWithRatio(File file,int resultMaxLength) {
private static byte[] createScaledBitmapWithRatio(File file, int resultMaxLength) {
Bitmap originBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Bitmap result = ThumbnailCompressUtil.createScaledBitmap(originBitmap, resultMaxLength, true);
......@@ -117,18 +115,18 @@ public class WeChatThumbnailUtil {
}
private static byte[] createScaledBitmap(File file,int resultMaxLength,int scaledWidth) {
private static byte[] createScaledBitmap(File file, int resultMaxLength, int scaledWidth) {
Bitmap originBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Bitmap result = null;
int width =scaledWidth;
while (width > 10){
int width = scaledWidth;
while (width > 10) {
result = ThumbnailCompressUtil.createScaledBitmap(originBitmap, width, false);
if (result.getByteCount() < resultMaxLength* 1024){
if (result.getByteCount() < resultMaxLength * 1024) {
break;
}else {
width = width -10;
} else {
width = width - 10;
}
}
......@@ -170,12 +168,12 @@ public class WeChatThumbnailUtil {
File result = null;
int endIndex = thumbnail.length();
int indexOfPackage = thumbnail.indexOf(WechatPluginKeys.PACKAGE);
if(indexOfPackage > 0){
if (indexOfPackage > 0) {
endIndex = indexOfPackage;
}
String key = thumbnail.substring(WeChatPluginImageSchema.SCHEMA_ASSETS.length(), endIndex);
// flutter_assets/packages/flutter_gallery_assets/ali_connors.jpg?package=flutter_gallery_assets
AssetFileDescriptor fileDescriptor = AssetManagerUtil.openAsset(registrar, key, getPackage(thumbnail));
AssetFileDescriptor fileDescriptor = AssetManagerUtil.openAsset(registrar, key, getPackage(thumbnail));
if (fileDescriptor != null) {
try {
......@@ -201,6 +199,7 @@ public class WeChatThumbnailUtil {
}
return packageStr;
}
private static File downloadImage(String url) {
File result = null;
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
......@@ -225,7 +224,7 @@ public class WeChatThumbnailUtil {
}
private static File inputStreamToFile(InputStream inputStream,String suffix) {
private static File inputStreamToFile(InputStream inputStream, String suffix) {
File result = null;
try {
result = File.createTempFile(UUID.randomUUID().toString(), suffix);
......
......@@ -5,7 +5,7 @@ import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import com.jarvan.fluwx.handler.FluwxShareHandler;
import com.jarvan.fluwx.handler.FluwxLoginHandler;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
......@@ -35,7 +35,6 @@ public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
@Override
public void onResp(BaseResp resp) {
FluwxShareHandler.INSTANCE.onResp(resp);
}
......
......@@ -20,8 +20,11 @@ class Fluwx {
StreamController<Map> _responseFromShareController =
new StreamController.broadcast();
Stream<Map> get responseFromShare => _responseFromShareController.stream;
StreamController<Map> _responseFromAuthController =
new StreamController.broadcast();
Stream<Map> get responseFromShare => _responseFromShareController.stream;
Stream<Map> get responseFromAuth => _responseFromAuthController.stream;
///the [model] should not be null
static Future registerApp(RegisterModel model) async {
return await _channel.invokeMethod("registerApp", model.toMap());
......@@ -37,12 +40,17 @@ class Fluwx {
void disposeAll() {
_responseFromShareController.close();
_responseFromAuthController.close();
}
void disposeResponseFromShare(){
_responseFromShareController.close();
}
void disposeResponseFromAuth(){
_responseFromAuthController.close();
}
///the [model] can not be null
///see [WeChatShareWebPageModel]
/// [WeChatShareTextModel]
......@@ -62,9 +70,15 @@ class Fluwx {
return await _channel.invokeMethod("sendAuth", model.toMap());
}
Future isWeChatInstalled() async{
return await _channel.invokeMethod("isWeChatInstalled");
}
Future<dynamic> _handler(MethodCall methodCall) {
if ("onShareResponse" == methodCall.method) {
_responseFromShareController.add(methodCall.arguments);
}else if("onAuthResponse" == methodCall.method){
_responseFromAuthController.add(methodCall.arguments);
}
return Future.value(true);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论