StandAPP/app/src/main/java/com/example/kingway/ptt/CallBackUtil.java

366 lines
14 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.example.kingway.ptt;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import timber.log.Timber;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**指令返回类
* 说明操作UI请在主线程进行
* */
public class CallBackUtil {
private static String TAG = "TYT_CallBackUtil";
private static final long THROTTLE_INTERVAL_MS = 100L;
private static final Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
private static final ConcurrentHashMap<String, Long> lastPushAt = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Runnable> pendingRunnables = new ConcurrentHashMap<>();
private static void pushJsThrottled(String eventName, String script) {
long now = SystemClock.uptimeMillis();
Long last = lastPushAt.get(eventName);
if (last == null || now - last >= THROTTLE_INTERVAL_MS) {
lastPushAt.put(eventName, now);
cancelPending(eventName);
com.stand.standapp.MainActivity.executeJs(script);
return;
}
Runnable prev = pendingRunnables.get(eventName);
if (prev != null) {
MAIN_HANDLER.removeCallbacks(prev);
}
final long scheduledAt = now;
Runnable task = () -> {
lastPushAt.put(eventName, SystemClock.uptimeMillis());
pendingRunnables.remove(eventName);
com.stand.standapp.MainActivity.executeJs(script);
};
pendingRunnables.put(eventName, task);
MAIN_HANDLER.postDelayed(task, THROTTLE_INTERVAL_MS - (now - last));
}
private static void cancelPending(String eventName) {
Runnable r = pendingRunnables.remove(eventName);
if (r != null) {
MAIN_HANDLER.removeCallbacks(r);
}
}
private static void pushJsImmediate(String script) {
com.stand.standapp.MainActivity.executeJs(script);
}
/**
* 返回登录
* loginState 是否登录成功
* name 用户名
*/
public static void callBackLogin(boolean loginState, String name) {
Timber.tag(TAG).d("callBackLogin loginState=%s name=%s", loginState, name);
}
/**
* 登录状态通知
* state
* 0 : 离线
* 1 : 登陆中
* 2 : 登陆成功
* 3 : 注销中
* 说明:第一次登录 通知状态 先01再02
* 离线再上线 通知状态 先00再01
* <p>
* loginId 用户ID登录成功id有效
*/
public static void callBackLoinState(String state, String loginId) {
Timber.tag(TAG).d("callBackLoinState state=%s loginId=%s", state, loginId);
}
/**
* 判断账号登录是否冲突
* true登录冲突
* false登录不冲突
* */
public static void callBackLoginConflict(boolean loginConflict) {
Timber.tag(TAG).d("callBackLoginConflict: %s", loginConflict);
}
/**
* 登录账号有视频功能通知
*/
public static boolean callBackHaveVideo() {
Timber.tag(TAG).d("callBackHaveVideo invoked");
return true;
}
/**
* 查询群组返回
* state true-成功, false-失败
*/
public static void callBackOnGroupSuccess(boolean state) {
Timber.tag(TAG).d("callBackOnGroupSuccess state=%s", state);
}
/**
* 查询群组成员返回
* state true-成功, false-失败
*/
public static void callBackOnMemberSuccess(boolean state, java.util.Map<String, GroupMemberInfoDto> dtos) {
try {
org.json.JSONArray arr = new org.json.JSONArray();
if (dtos != null && state) {
for (GroupMemberInfoDto dto : dtos.values()) {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("memberId", dto.getMemberId());
obj.put("memberName", dto.getGmemberName());
obj.put("status", dto.getStatus());
arr.put(obj);
}
}
pushJsThrottled("MemberList", "if(window.onPttEvent){window.onPttEvent('MemberList', " + arr.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
/**
* 应用发起讲话返回
* state true-成功, false-失败
*/
public static void callBackSpeakPlaySuccess(boolean state) {
Timber.tag(TAG).d("callBackSpeakPlaySuccess state=%s", state);
if (state) {
AudioRecordManager.getInstance().startRecord();
Timber.tag(TAG).d("AudioRecord started");
}
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("success", state);
com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('SpeakResult', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
/**
* 应用结束讲话返回
* state true-成功, false-失败
*/
public static void callBackSpeakReleaseSuccess(boolean state) {
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("success", state);
com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('SpeakEnd', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
/**
* 返回群组信息
* groupId 群组ID
* groupName 群组名称
* groupNo 表示此组为第几个组
* groupCount 表示此组用户成员数
*/
public static void callBackGroupInfo(String groupId, String groupName, int groupNo, int groupCount) {
Timber.tag(TAG).d("callBackGroupInfo groupId=%s name=%s no=%d count=%d", groupId, groupName, groupNo, groupCount);
}
/**
* 返回群组集合信息
* GroupInfoDto 群组信息
* */
public static void callBackGroupInfo (java.util.Map<String, GroupInfoDto> groupInfoDtos) {
try {
org.json.JSONArray arr = new org.json.JSONArray();
for (GroupInfoDto dto : groupInfoDtos.values()) {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("groupId", dto.getGroupId());
obj.put("groupName", dto.getGroupName());
obj.put("groupNo", dto.getGroupNo());
arr.put(obj);
}
pushJsThrottled("GroupList", "if(window.onPttEvent){window.onPttEvent('GroupList', " + arr.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
/**
* 讲话用户信息通知
* speckType 讲话状态: 0 表示自己无法讲话; 1 表示自己可以中断讲话人的讲话,可以进行强插讲话
* talkId 讲话用户ID
* talkName 讲话用户名字
* */
public static void callBackNotifySpker ( int speckType, String groupId, String groupName, String talkId, String talkName){
Timber.tag(TAG).d("callBackNotifySpker speckType=%d groupId=%s talkId=%s talkName=%s", speckType, groupId, talkId, talkName);
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("speckType", speckType);
obj.put("groupId", groupId);
obj.put("groupName", groupName);
obj.put("talkId", talkId);
obj.put("talkName", talkName);
pushJsThrottled("SpeakerUpdate", "if(window.onPttEvent){window.onPttEvent('SpeakerUpdate', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
public static void callBackNotifyPlayStatus ( int playStatus, int speckType, String groupId, String groupName, String talkId, String talkName){
Timber.tag(TAG).d("callBackNotifyPlayStatus playStatus=%d talkId=%s", playStatus, talkId);
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("playStatus", playStatus);
obj.put("speckType", speckType);
obj.put("groupId", groupId);
obj.put("groupName", groupName);
obj.put("talkId", talkId);
obj.put("talkName", talkName);
pushJsThrottled("PlayStatus", "if(window.onPttEvent){window.onPttEvent('PlayStatus', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
public static void callBackTempCallNotify (String tempCallNotify){
Timber.tag(TAG).d("callBackTempCallNotify msg=%s", tempCallNotify);
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("msg", tempCallNotify);
com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('TempCall', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
public static void callBackNotifyUpdateGroup (String userGroupId, String userGroupName){
Timber.tag(TAG).d("callBackNotifyUpdateGroup groupId=%s groupName=%s", userGroupId, userGroupName);
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("groupId", userGroupId);
obj.put("groupName", userGroupName);
com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('UpdateGroup', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
/**
* 用户定位信息通知
* status 0获取成功 1获取失败
* userId 用户ID
* latitude 纬度
* longitude 经度
* time 时间
*/
public static void callBackNotifyLocationStatus ( int status, String userId,double latitude, double longitude, String time){
Timber.tag(TAG).d("callBackNotifyLocationStatus status=%d userId=%s lat=%f lng=%f", status, userId, latitude, longitude);
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("status", status);
obj.put("userId", userId);
obj.put("latitude", latitude);
obj.put("longitude", longitude);
obj.put("time", time);
pushJsThrottled("LocationStatus", "if(window.onPttEvent){window.onPttEvent('LocationStatus', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
/**
* 用户上报定位信息通知
* status true成功 false失败
* */
public static void callBackNotifyUploadLocation ( boolean status){
Timber.tag(TAG).d("callBackNotifyUploadLocation status=%s", status);
}
/**
* 空中写账号信息通知
* codeInfo ip=106.15.8.60;id=Android3;gps=1;inv=1
* ip=106.15.8.60 用户ip106.15.8.60
* id=Android3 用户账号Android3
* gps=11勾选定位0不勾选定位
* inv=11勾选单呼0不勾选单呼
* */
public static void callBackNotifyCodeInfo (String codeInfo){
Timber.tag(TAG).d("callBackNotifyCodeInfo: %s", codeInfo);
}
/**
* 空中写密码信息通知
* pwdInfo age=123456;pwd=111111
* age=123456 经销商密码123456
* pwd=111111 账号密码111111
* */
public static void callBackNotifyPwdInfo (String pwdInfo){
Timber.tag(TAG).d("callBackNotifyPwdInfo: %s", pwdInfo);
}
/**
* 进入写码模式通知
* status true成功false失败
* */
public static void callBackNotifyCodeOpenStatus ( boolean status){
Timber.tag(TAG).d("callBackNotifyCodeOpenStatus status=%s", status);
}
/**
* 退出写码模式通知
* status true成功false失败
* */
public static void callBackNotifyCodeExiteStatus ( boolean status){
Timber.tag(TAG).d("callBackNotifyCodeExiteStatus status=%s", status);
}
/**
* 上传经销商密码通知
* status true成功false失败
* */
public static void callBackNotifyUploadAgentPwd ( boolean status){
Timber.tag(TAG).d("callBackNotifyUploadAgentPwd status=%s", status);
}
/**
* 上报本机告警信号通知
* status true成功false失败
* */
public static void callBackRequestReportAlarm ( boolean status){
Timber.tag(TAG).d("callBackRequestReportAlarm status=%s", status);
}
/**
* 停止本机告警信号通知
* status true成功false失败
* */
public static void callBackRequestStopAlarm ( boolean status){
Timber.tag(TAG).d("callBackRequestStopAlarm status=%s", status);
}
/**
* 收到群成员警告通知
* memberName 群成员名称
* latitude 纬度
* longitude 经度
* */
public static void callBackEnotifyAlarm (String memberName,double latitude, double longitude){
Timber.tag(TAG).w("callBackEnotifyAlarm memberName=%s lat=%f lng=%f", memberName, latitude, longitude);
try {
org.json.JSONObject obj = new org.json.JSONObject();
obj.put("memberName", memberName);
obj.put("latitude", latitude);
obj.put("longitude", longitude);
com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('Alarm', " + obj.toString() + ");}");
} catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
}
/**
* 收到群成员停止警告通知
* */
public static void callBackEnotifyStopAlarm () {
Timber.tag(TAG).d("callBackEnotifyStopAlarm invoked");
com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('StopAlarm', {});}");
}
/**
* 上传NFC信息通知
* status true成功false失败
* */
public static void callBackRequestNfc ( boolean status){
Timber.tag(TAG).d("callBackRequestNfc status=%s", status);
}
}