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

android:refactor,add checkWeChat method

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