This commit is contained in:
xianghairui
2021-06-29 18:17:11 +08:00
parent 6ec9f9e8a1
commit 6b5c6bc1fe
117 changed files with 4593 additions and 201 deletions

8
android/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

52
android/build.gradle Normal file
View File

@@ -0,0 +1,52 @@
group 'io.openim.flutter_openim_sdk'
version '1.0'
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
google()
maven {
url 'http://127.0.0.1:8081/repository/maven-releases/' //这个路径就是Browse下maven-release右边有一个copy按钮复制出来的。
}
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
}
}
rootProject.allprojects {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin' }
google()
maven {
url 'http://127.0.0.1:8081/repository/maven-releases' //这个路径就是Browse下maven-release右边有一个copy按钮复制出来的。
}
mavenCentral()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 21
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.alibaba:fastjson:1.1.72.android'
implementation 'io.openim:localsdk:1.1.4@aar'//graoupId:artifactId:version@aar
}

View File

@@ -0,0 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.enableDexingArtifactTransform=false

View File

@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

1
android/settings.gradle Normal file
View File

@@ -0,0 +1 @@
rootProject.name = 'flutter_openim_sdk'

View File

@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.openim.flutter_openim_sdk">
</manifest>

View File

@@ -0,0 +1,82 @@
package io.openim.flutter_openim_sdk;
import android.content.Context;
import androidx.annotation.NonNull;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.openim.flutter_openim_sdk.manager.ConversationManager;
import io.openim.flutter_openim_sdk.manager.FriendshipManager;
import io.openim.flutter_openim_sdk.manager.IMManager;
import io.openim.flutter_openim_sdk.manager.MessageManager;
import io.openim.flutter_openim_sdk.util.CommonUtil;
/**
* FlutterOpenimSdkPlugin
*/
public class FlutterOpenimSdkPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
public static MethodChannel channel;
private static IMManager imManager;
private static FriendshipManager friendshipManager;
private static MessageManager messageManager;
private static ConversationManager conversationManager;
public FlutterOpenimSdkPlugin() {
}
private FlutterOpenimSdkPlugin(Context context, MethodChannel channel) {
FlutterOpenimSdkPlugin.imManager = new IMManager(channel);
FlutterOpenimSdkPlugin.friendshipManager = new FriendshipManager(channel);
FlutterOpenimSdkPlugin.messageManager = new MessageManager(channel);
FlutterOpenimSdkPlugin.conversationManager = new ConversationManager(channel);
}
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_openim_sdk");
channel.setMethodCallHandler(new FlutterOpenimSdkPlugin(flutterPluginBinding.getApplicationContext(), channel));
// channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
parse(call, result);
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
void parse(@NonNull MethodCall call, @NonNull Result result) {
try {
String managerName = CommonUtil.getParamValue(call, "ManagerName");
Field field = FlutterOpenimSdkPlugin.class.getDeclaredField(managerName);
Method method = field.get(new Object()).getClass().getDeclaredMethod(call.method, MethodCall.class, Result.class);
System.out.println("flutter call native { class:" + managerName + ", method:" + method.getName() + " }");
method.invoke(field.get(new Object()), call, result);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,36 @@
package io.openim.flutter_openim_sdk.listener;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.OnAdvancedMsgListener;
public class AdvancedMsgListenerImpl implements OnAdvancedMsgListener {
final MethodChannel channel;
final Map<String, String> values = new HashMap<>();
public AdvancedMsgListenerImpl(MethodChannel channel, String id) {
this.channel = channel;
this.values.put("id", id);
}
@Override
public void onRecvC2CReadReceipt(String s) {
values.put("message", s);
CommonUtil.emitEvent(channel, "advancedMsgListener", "onRecvC2CReadReceipt", values);
}
@Override
public void onRecvMessageRevoked(String s) {
values.put("message", s);
CommonUtil.emitEvent(channel, "advancedMsgListener", "onRecvMessageRevoked", values);
}
@Override
public void onRecvNewMessage(String s) {
values.put("message", s);
CommonUtil.emitEvent(channel, "advancedMsgListener", "onRecvNewMessage", values);
}
}

View File

@@ -0,0 +1,27 @@
package io.openim.flutter_openim_sdk.listener;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.Base;
public class BaseImpl implements Base {
MethodChannel.Result result;
public BaseImpl(MethodChannel.Result result) {
this.result = result;
}
@Override
public void onError(long l, String s) {
System.out.println("BaseImpl:" + s);
CommonUtil.runMainThreadReturnError(result, l, s, null);
}
@Override
public void onSuccess(String s) {
System.out.println("BaseImpl:" + s);
CommonUtil.runMainThreadReturn(result, s);
}
}

View File

@@ -0,0 +1,43 @@
package io.openim.flutter_openim_sdk.listener;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.OnConversationListener;
public class ConversationListenerImpl implements OnConversationListener {
private final MethodChannel channel;
public ConversationListenerImpl(MethodChannel channel) {
this.channel = channel;
}
@Override
public void onConversationChanged(String s) {
CommonUtil.emitEvent(channel, "conversationListener", "onConversationChanged", s);
}
@Override
public void onNewConversation(String s) {
CommonUtil.emitEvent(channel, "conversationListener", "onNewConversation", s);
}
@Override
public void onSyncServerFailed() {
CommonUtil.emitEvent(channel, "conversationListener", "onSyncServerFailed", null);
}
@Override
public void onSyncServerFinish() {
CommonUtil.emitEvent(channel, "conversationListener", "onSyncServerFinish", null);
}
@Override
public void onSyncServerStart() {
CommonUtil.emitEvent(channel, "conversationListener", "onSyncServerStart", null);
}
@Override
public void onTotalUnreadMessageCountChanged(int i) {
CommonUtil.emitEvent(channel, "conversationListener", "onTotalUnreadMessageCountChanged", i);
}
}

View File

@@ -0,0 +1,58 @@
package io.openim.flutter_openim_sdk.listener;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.OnFriendshipListener;
public class FriendshipListenerImpl implements OnFriendshipListener {
private final MethodChannel channel;
public FriendshipListenerImpl(MethodChannel channel) {
this.channel = channel;
}
@Override
public void onBlackListAdd(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onBlackListAdd", s);
}
@Override
public void onBlackListDeleted(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onBlackListDeleted", s);
}
@Override
public void onFriendApplicationListAccept(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onFriendApplicationListAccept", s);
}
@Override
public void onFriendApplicationListAdded(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onFriendApplicationListAdded", s);
}
@Override
public void onFriendApplicationListDeleted(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onFriendApplicationListDeleted", s);
}
@Override
public void onFriendApplicationListReject(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onFriendApplicationListReject", s);
}
@Override
public void onFriendInfoChanged(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onFriendInfoChanged", s);
}
@Override
public void onFriendListAdded(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onFriendListAdded", s);
}
@Override
public void onFriendListDeleted(String s) {
CommonUtil.emitEvent(channel, "friendListener", "onFriendListDeleted", s);
}
}

View File

@@ -0,0 +1,44 @@
package io.openim.flutter_openim_sdk.listener;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.IMSDKListener;
public class SDKListenerImpl implements IMSDKListener {
final private MethodChannel channel;
public SDKListenerImpl(MethodChannel channel) {
this.channel = channel;
}
@Override
public void onConnectFailed(long l, String s) {
CommonUtil.emitEvent(channel, "initSDKListener", "onConnectFailed", l, s, null);
}
@Override
public void onConnectSuccess() {
CommonUtil.emitEvent(channel, "initSDKListener", "onConnectSuccess", null);
}
@Override
public void onConnecting() {
CommonUtil.emitEvent(channel, "initSDKListener", "onConnecting", null);
}
@Override
public void onKickedOffline() {
CommonUtil.emitEvent(channel, "initSDKListener", "onKickedOffline", null);
}
@Override
public void onSelfInfoUpdated(String s) {
CommonUtil.emitEvent(channel, "initSDKListener", "onSelfInfoUpdated", s);
}
@Override
public void onUserTokenExpired() {
CommonUtil.emitEvent(channel, "initSDKListener", "onUserTokenExpired", null);
}
}

View File

@@ -0,0 +1,45 @@
package io.openim.flutter_openim_sdk.listener;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.SendMsgCallBack;
public class SendMsgProgressListener implements SendMsgCallBack {
public SendMsgProgressListener(MethodChannel.Result result, MethodChannel channel, MethodCall call) {
this.result = result;
this.channel = channel;
this.call = call;
}
final private MethodChannel.Result result;
final private MethodChannel channel;
final private MethodCall call;
final Map<String, Object> values = new HashMap<>();
@Override
public void onError(long l, String s) {
System.out.println("=================onError============" + s);
CommonUtil.runMainThreadReturnError(result, l, s, null);
}
@Override
public void onProgress(long l) {
System.out.println("================onProgress============" + l);
values.put("clientMsgID", CommonUtil.getSendMessageClientMsgID(call));
values.put("progress", l);
CommonUtil.emitEvent(channel, "messageProgressListener", "onProgress", values);
}
@Override
public void onSuccess(String s) {
System.out.println("=================onSuccess============" + s);
CommonUtil.runMainThreadReturn(result, s);
}
}

View File

@@ -0,0 +1,48 @@
package io.openim.flutter_openim_sdk.manager;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.listener.BaseImpl;
import io.openim.flutter_openim_sdk.listener.ConversationListenerImpl;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.Open_im_sdk;
public class ConversationManager {
final private MethodChannel channel;
public ConversationManager(MethodChannel channel) {
this.channel = channel;
}
public void setConversationListener(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.setConversationListener(new ConversationListenerImpl(channel));
}
public void getAllConversationList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getAllConversationList(new BaseImpl(result));
}
public void getOneConversation(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getOneConversation(CommonUtil.getConversationId(methodCall), new BaseImpl(result));
}
public void getMultipleConversation(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getMultipleConversation(CommonUtil.getConversationIds(methodCall), new BaseImpl(result));
}
public void deleteConversation(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.deleteConversation(CommonUtil.getConversationId(methodCall), new BaseImpl(result));
}
public void setConversationDraft(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.setConversationDraft(CommonUtil.getConversationId(methodCall), CommonUtil.getConversationDraft(methodCall), new BaseImpl(result));
}
public void pinConversation(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.pinConversation(CommonUtil.getConversationId(methodCall), CommonUtil.isPinnedConversation(methodCall), new BaseImpl(result));
}
public void getTotalUnreadMsgCount(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getTotalUnreadMsgCount(new BaseImpl(result));
}
}

View File

@@ -0,0 +1,81 @@
package io.openim.flutter_openim_sdk.manager;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.listener.BaseImpl;
import io.openim.flutter_openim_sdk.listener.FriendshipListenerImpl;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.Open_im_sdk;
public class FriendshipManager {
final private MethodChannel channel;
public FriendshipManager(MethodChannel channel) {
this.channel = channel;
}
public void setFriendListener(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.setFriendListener(new FriendshipListenerImpl(channel));
}
public void getFriendsInfo(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getFriendsInfo(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void addFriend(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.addFriend(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void getFriendApplicationList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getFriendApplicationList(new BaseImpl(result));
}
public void getFriendList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getFriendList(new BaseImpl(result));
}
public void setFriendInfo(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.setFriendInfo(CommonUtil.getSDKJsonParam(methodCall), new BaseImpl(result));
}
public void addToBlackList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.addToBlackList(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void getBlackList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getBlackList(new BaseImpl(result));
}
public void deleteFromBlackList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.deleteFromBlackList(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void checkFriend(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.checkFriend(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void deleteFromFriendList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.deleteFromFriendList(CommonUtil.getSDKJsonParam(methodCall), new BaseImpl(result));
}
public void acceptFriendApplication(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.acceptFriendApplication(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void refuseFriendApplication(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.refuseFriendApplication(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void forceSyncFriendApplication(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.forceSyncFriendApplication();
}
public void forceSyncFriend(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.forceSyncFriend();
}
public void forceSyncBlackList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.forceSyncBlackList();
}
}

View File

@@ -0,0 +1,50 @@
package io.openim.flutter_openim_sdk.manager;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.listener.BaseImpl;
import io.openim.flutter_openim_sdk.listener.SDKListenerImpl;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.Open_im_sdk;
public class IMManager {
final private MethodChannel channel;
public IMManager(MethodChannel channel) {
this.channel = channel;
}
public void initSDK(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.initSDK(CommonUtil.getSDKJsonParam(methodCall), new SDKListenerImpl(channel));
CommonUtil.runMainThreadReturn(result, null);
}
public void login(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.login(CommonUtil.getUid(methodCall), CommonUtil.getToken(methodCall), new BaseImpl(result));
}
public void logout(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.logout(new BaseImpl(result));
}
public void getLoginStatus(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.getLoginStatus());
}
public void getLoginUid(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.getLoginUid());
}
public void getUsersInfo(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getUsersInfo(CommonUtil.getSDKJsonParam(methodCall), new BaseImpl(result));
}
public void setSelfInfo(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.setSelfInfo(CommonUtil.getSDKJsonParam(methodCall), new BaseImpl(result));
}
public void forceSyncLoginUerInfo(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.forceSyncLoginUerInfo();
}
}

View File

@@ -0,0 +1,108 @@
package io.openim.flutter_openim_sdk.manager;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.listener.AdvancedMsgListenerImpl;
import io.openim.flutter_openim_sdk.listener.BaseImpl;
import io.openim.flutter_openim_sdk.listener.SendMsgProgressListener;
import io.openim.flutter_openim_sdk.util.CommonUtil;
import open_im_sdk.OnAdvancedMsgListener;
import open_im_sdk.Open_im_sdk;
public class MessageManager {
private final static String KEY_ID = "id";
private final MethodChannel channel;
private final static Map<String, OnAdvancedMsgListener> listeners = new HashMap<>();
public MessageManager(MethodChannel channel) {
this.channel = channel;
}
public void addAdvancedMsgListener(MethodCall methodCall, MethodChannel.Result result) {
String key = methodCall.argument(KEY_ID);
if (!listeners.containsKey(key)) {
AdvancedMsgListenerImpl listener = new AdvancedMsgListenerImpl(channel, key);
listeners.put(methodCall.argument(KEY_ID), listener);
Open_im_sdk.addAdvancedMsgListener(listener);
System.out.println("=================add msg listener======" + listener);
}
}
public void removeAdvancedMsgListener(MethodCall methodCall, MethodChannel.Result result) {
String key = methodCall.argument(KEY_ID);
OnAdvancedMsgListener listener = listeners.remove(key);
Open_im_sdk.removeAdvancedMsgListener(listener);
System.out.println("=================remove msg listener======" + listener);
}
public void sendMessage(MethodCall methodCall, MethodChannel.Result result) {
SendMsgProgressListener listener = new SendMsgProgressListener(result, channel, methodCall);
System.out.println("===============sendMessage===============");
Open_im_sdk.sendMessage(listener, CommonUtil.getSendMessageContent(methodCall), CommonUtil.getSendMessageReceiver(methodCall), CommonUtil.geSendMessageGroupId(methodCall), CommonUtil.getSendMessageOnlineOnly(methodCall));
}
public void getHistoryMessageList(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.getHistoryMessageList(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void revokeMessage(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.revokeMessage(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void deleteMessageFromLocalStorage(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.deleteMessageFromLocalStorage(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void deleteMessages(MethodCall methodCall, MethodChannel.Result result) {
// Open_im_sdk.deleteMessages(new BaseImpl(result), CommonUtil.getSDKJsonParam(methodCall));
}
public void insertSingleMessageToLocalStorage(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.insertSingleMessageToLocalStorage(new BaseImpl(result), CommonUtil.getSingleMessageContent(methodCall), CommonUtil.getSingleMessageUserid(methodCall), CommonUtil.getSingleMessageSender(methodCall));
}
public void findMessages(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.findMessages(new BaseImpl(result), CommonUtil.getFindMessageIds(methodCall));
}
public void markSingleMessageHasRead(MethodCall methodCall, MethodChannel.Result result) {
Open_im_sdk.markSingleMessageHasRead(new BaseImpl(result), CommonUtil.getSingleMessageUserid(methodCall));
}
public void createTextMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createTextMessage(CommonUtil.getMessageText(methodCall)));
}
public void createTextAtMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createTextAtMessage(CommonUtil.getMessageText(methodCall), CommonUtil.getAtUserList(methodCall)));
}
public void createImageMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createImageMessage(CommonUtil.getImagePath(methodCall)));
}
public void createSoundMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createSoundMessage(CommonUtil.getSoundPath(methodCall), CommonUtil.getSoundDuration(methodCall)));
}
public void createVideoMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createVideoMessage(CommonUtil.getVideoPath(methodCall), CommonUtil.getVideoType(methodCall), CommonUtil.getVideoDuration(methodCall), CommonUtil.getVideoSnapshotPath(methodCall)));
}
public void createFileMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createFileMessage(CommonUtil.getFilePath(methodCall), CommonUtil.getFileName(methodCall)));
}
public void createMergerMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createMergerMessage(CommonUtil.getMergerMessageList(methodCall), CommonUtil.getMergerMessageTitle(methodCall), CommonUtil.getSummaryList(methodCall)));
}
public void createForwardMessage(MethodCall methodCall, MethodChannel.Result result) {
CommonUtil.runMainThreadReturn(result, Open_im_sdk.createForwardMessage(CommonUtil.getForwardMessage(methodCall)));
}
}

View File

@@ -0,0 +1,286 @@
package io.openim.flutter_openim_sdk.util;
import android.os.Handler;
import android.os.Looper;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.openim.flutter_openim_sdk.FlutterOpenimSdkPlugin;
public class CommonUtil {
/**
* 主线程处理器
*/
private final static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
public static <T> T getParamValue(MethodCall methodCall, String param) {
return methodCall.argument(param);
}
public static String getSDKJsonParam(MethodCall methodCall, String key) {
return JsonUtil.toString(methodCall.argument(key));
}
public static String getSDKJsonParam(MethodCall methodCall) {
return JsonUtil.toString(methodCall.arguments);
}
public static void runMainThreadReturn(final MethodChannel.Result result, final Object param) {
MAIN_HANDLER.post(new Runnable() {
@Override
public void run() {
result.success(param);
}
});
}
public static void runMainThread(Runnable runnable) {
MAIN_HANDLER.post(runnable);
}
/**
* 运行主线程返回错误结果执行
*
* @param result 返回结果对象
* @param errorCode 错误码
* @param errorMessage 错误信息
* @param errorDetails 错误内容
*/
public static void runMainThreadReturnError(final MethodChannel.Result result, final String errorCode, final String errorMessage, final Object errorDetails) {
MAIN_HANDLER.post(new Runnable() {
@Override
public void run() {
result.error(errorCode, errorMessage, errorDetails);
}
});
}
public static void runMainThreadReturnError(final MethodChannel.Result result, final long errorCode, final String errorMessage, final Object errorDetails) {
runMainThreadReturnError(result, String.valueOf(errorCode), errorMessage, errorDetails);
}
/*
public static void returnError(final MethodChannel.Result result, int i, String s) {
HashMap<String, Object> err = new HashMap<String, Object>();
err.put("code", i);
err.put("desc", s);
result.success(err);
}
public static void returnError(final MethodChannel.Result result, int i, String s, HashMap<String, Object> data) {
HashMap<String, Object> err = new HashMap<String, Object>();
err.put("code", i);
err.put("desc", s);
err.put("data", data);
result.success(err);
}
public static <T> void returnSuccess(final MethodChannel.Result result, T data) {
HashMap<String, Object> succ = new HashMap<String, Object>();
succ.put("code", 0);
succ.put("desc", "ok");
succ.put("data", data);
result.success(succ);
}
*/
public static <T> void emitEvent(MethodChannel channel, String method, String type, Long errCode, String errMsg, T data) {
runMainThread(new Runnable() {
@Override
public void run() {
HashMap<String, Object> res = new HashMap<String, Object>();
// if (null != type) {
res.put("type", type);
// }
if (null != data) {
res.put("data", data);
}
if (null != errCode) {
res.put("errCode", errCode);
}
if (null != errMsg) {
res.put("errMsg", errMsg);
}
System.out.println("native call flutter { method:" + method + ", type:" + type + " }");
FlutterOpenimSdkPlugin.channel.invokeMethod(method, res);
}
});
}
public static <T> void emitEvent(MethodChannel channel, String method, String type, T data) {
emitEvent(channel, method, type, null, null, data);
}
///////////////////////////////////////
//////////////////////////////////////
public static String getUid(MethodCall methodCall) {
return getParamValue(methodCall, KEY_LOGIN_UID);
}
public static String getToken(MethodCall methodCall) {
return getParamValue(methodCall, KEY_LOGIN_TOKEN);
}
public static String getMessageText(MethodCall methodCall) {
return getParamValue(methodCall, KEY_TEXT);
}
public static String getAtUserList(MethodCall methodCall) {
// return methodCall.argument(KEY_AT_USER_LIST);
return getSDKJsonParam(methodCall, KEY_AT_USER_LIST);
}
public static String getImagePath(MethodCall methodCall) {
return getParamValue(methodCall, KEY_IMAGE_PATH);
}
public static String getSoundPath(MethodCall methodCall) {
return getParamValue(methodCall, KEY_SOUND_PATH);
}
public static long getSoundDuration(MethodCall methodCall) {
Integer i = getParamValue(methodCall, KEY_SOUND_DURATION);
return Long.valueOf(i);
}
public static String getVideoPath(MethodCall methodCall) {
return getParamValue(methodCall, KEY_VIDEO_PATH);
}
public static String getVideoType(MethodCall methodCall) {
return getParamValue(methodCall, KEY_VIDEO_TYPE);
}
public static long getVideoDuration(MethodCall methodCall) {
Integer i = getParamValue(methodCall, KEY_VIDEO_DURATION);
return Long.valueOf(i);
}
public static String getVideoSnapshotPath(MethodCall methodCall) {
return getParamValue(methodCall, KEY_VIDEO_SNAPSHOT_PATH);
}
public static String getFilePath(MethodCall methodCall) {
return getParamValue(methodCall, KEY_FILE_PATH);
}
public static String getFileName(MethodCall methodCall) {
return getParamValue(methodCall, KEY_FILE_NAME);
}
public static String getMergerMessageList(MethodCall methodCall) {
// return methodCall.argument(KEY_MERGER_MESSAGE_LIST);
return getSDKJsonParam(methodCall, KEY_MERGER_MESSAGE_LIST);
}
public static String getMergerMessageTitle(MethodCall methodCall) {
return getParamValue(methodCall, KEY_MERGER_MESSAGE_TITLE);
}
public static String getSummaryList(MethodCall methodCall) {
// return methodCall.argument(KEY_SUMMARY_LIST);
return getSDKJsonParam(methodCall, KEY_SUMMARY_LIST);
}
public static String getForwardMessage(MethodCall methodCall) {
// return methodCall.argument(KEY_FORWARD_MESSAGE);
return getSDKJsonParam(methodCall, KEY_FORWARD_MESSAGE);
}
public static String getSendMessageContent(MethodCall methodCall) {
return getSDKJsonParam(methodCall, KEY_SEND_MESSAGE_CONTENT);
}
public static Object getSendMessageClientMsgID(MethodCall methodCall) {
Map<String, Object> map = getParamValue(methodCall, KEY_SEND_MESSAGE_CONTENT);
return map.get(KEY_SEND_MESSAGE_CONTENT_CLIENT_ID);
}
public static String getSendMessageReceiver(MethodCall methodCall) {
return getParamValue(methodCall, KEY_SEND_MESSAGE_RECEIVER);
}
public static String geSendMessageGroupId(MethodCall methodCall) {
return getParamValue(methodCall, KEY_SEND_MESSAGE_GROUP_ID);
}
public static boolean getSendMessageOnlineOnly(MethodCall methodCall) {
return getParamValue(methodCall, KEY_SEND_MESSAGE_ONLINE_ONLY);
}
public static String getSingleMessageContent(MethodCall methodCall) {
return getParamValue(methodCall, KEY_SINGLE_MESSAGE_CONTENT);
}
public static String getSingleMessageUserid(MethodCall methodCall) {
return getParamValue(methodCall, KEY_SINGLE_MESSAGE_USERID);
}
public static String getSingleMessageSender(MethodCall methodCall) {
return getParamValue(methodCall, KEY_SINGLE_MESSAGE_SENDER);
}
public static String getFindMessageIds(MethodCall methodCall) {
return getSDKJsonParam(methodCall, KEY_FIND_MESSAGE_IDS);
}
public static String getConversationId(MethodCall methodCall) {
return getParamValue(methodCall, KEY_CONVERSATION_ID);
}
public static String getConversationIds(MethodCall methodCall) {
return getSDKJsonParam(methodCall, KEY_CONVERSATION_IDS);
}
public static String getConversationDraft(MethodCall methodCall) {
return getParamValue(methodCall, KEY_CONVERSATION_DRAFT);
}
public static boolean isPinnedConversation(MethodCall methodCall) {
return getParamValue(methodCall, KEY_CONVERSATION_PINNED);
}
//login
final static String KEY_LOGIN_UID = "uid";
final static String KEY_LOGIN_TOKEN = "token";
//create message body
final static String KEY_TEXT = "text";
final static String KEY_AT_USER_LIST = "atUserList";
final static String KEY_IMAGE_PATH = "imagePath";
final static String KEY_SOUND_PATH = "soundPath";
final static String KEY_SOUND_DURATION = "duration";
final static String KEY_VIDEO_PATH = "videoPath";
final static String KEY_VIDEO_TYPE = "videoType";
final static String KEY_VIDEO_DURATION = "duration";
final static String KEY_VIDEO_SNAPSHOT_PATH = "snapshotPath";
final static String KEY_FILE_PATH = "filePath";
final static String KEY_FILE_NAME = "fileName";
final static String KEY_MERGER_MESSAGE_LIST = "messageList";
final static String KEY_MERGER_MESSAGE_TITLE = "title";
final static String KEY_SUMMARY_LIST = "summaryList";
final static String KEY_FORWARD_MESSAGE = "message";
//send message
final static String KEY_SEND_MESSAGE_CONTENT = "message";
final static String KEY_SEND_MESSAGE_CONTENT_CLIENT_ID = "clientMsgID";
final static String KEY_SEND_MESSAGE_RECEIVER = "receiver";
final static String KEY_SEND_MESSAGE_GROUP_ID = "groupID";
final static String KEY_SEND_MESSAGE_ONLINE_ONLY = "onlineUserOnly";
//single chat
final static String KEY_SINGLE_MESSAGE_CONTENT = "message";
final static String KEY_SINGLE_MESSAGE_USERID = "userID";
final static String KEY_SINGLE_MESSAGE_SENDER = "sender";
// find message
final static String KEY_FIND_MESSAGE_IDS = "messageIDList";
// conversation
final static String KEY_CONVERSATION_ID = "conversationID";
final static String KEY_CONVERSATION_IDS = "conversationIDList";
final static String KEY_CONVERSATION_DRAFT = "draftText";
final static String KEY_CONVERSATION_PINNED = "isPinned";
}

View File

@@ -0,0 +1,70 @@
package io.openim.flutter_openim_sdk.util;
import android.text.TextUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.List;
import java.util.Map;
import static com.alibaba.fastjson.JSON.parseObject;
public class JsonUtil {
public static String toString(Object object) {
if (object == null) {
return "";
}
try {
return JSON.toJSONString(object);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static <T> T toObj(String jsonData, Class<T> clazz) {
T t = null;
if (TextUtils.isEmpty(jsonData)) {
return null;
}
try {
t = parseObject(jsonData, clazz);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public static <T> List<T> toList(String jsonData, Class<T> clazz) {
List<T> list = null;
try {
list = JSON.parseArray(jsonData, clazz);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* json转化为Map fastjson 使用方式
*/
public static Map toMap(String jsonData) {
if (TextUtils.isEmpty(jsonData)) {
return null;
}
Map map = null;
try {
map = parseObject(jsonData, new TypeReference<Map>() {
});
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}