Преглед изворни кода

1.解决新手机放置一段时间之后tts服务断开导致无法语音播报问题
2.解决呼叫或通话界面按了home键后进入悬浮窗界面,但是对方挂断没有更新状态问题
3.修改通话悬浮窗显示不全问题

weizhengliang пре 2 година
родитељ
комит
6a02604461

+ 23 - 7
home/src/main/code/com/wdkl/ncs/android/component/home/service/FloatingService.java

@@ -13,6 +13,7 @@ import android.view.MotionEvent;
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.TextView;
+import android.widget.Toast;
 
 import androidx.annotation.Nullable;
 
@@ -43,17 +44,21 @@ public class FloatingService extends Service {
     private TextView mCallView;
 
     private long clickTime = 0;
+    private boolean callResumed = false;
 
     @Override
     public void onCreate() {
         super.onCreate();
         initWindow();//设置悬浮窗基本参数(位置、宽高等)
-        EventBus.getDefault().register(this);
+        //EventBus.getDefault().register(this);
     }
 
     @Nullable
     @Override
     public IBinder onBind(Intent intent) {
+        if (!EventBus.getDefault().isRegistered(this)) {
+            EventBus.getDefault().register(this);
+        }
         initFloating();//悬浮框点击事件的处理
 
         return new MyBinder();
@@ -79,7 +84,10 @@ public class FloatingService extends Service {
             mFloatingLayout = null;
             Constants.Companion.setShowFloatWindow(false);
         }
-        EventBus.getDefault().unregister(this);
+
+        if (EventBus.getDefault().isRegistered(this)) {
+            EventBus.getDefault().unregister(this);
+        }
         Log.d(TAG, "floating service destroyed");
     }
 
@@ -109,10 +117,10 @@ public class FloatingService extends Service {
         wmParams.gravity = Gravity.LEFT | Gravity.TOP;
         //wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
         //设置悬浮窗口长宽数据
-        wmParams.width = 220;
-        wmParams.height = 248;
-        wmParams.x = 320;
-        wmParams.y = 240;
+        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
+        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
+        wmParams.x = 200;
+        wmParams.y = 200;
         return wmParams;
     }
 
@@ -128,7 +136,13 @@ public class FloatingService extends Service {
             public void onClick(View v) {
                 //在这里实现点击重新回到Activity
                 if (System.currentTimeMillis() - clickTime > 3000) {
-                    resumeCallActivity();
+                    if (!callResumed) {
+                        resumeCallActivity();
+                    } else {
+                        Toast.makeText(getApplicationContext(), "请稍后...", Toast.LENGTH_LONG).show();
+                    }
+                } else {
+                    Toast.makeText(getApplicationContext(), "请勿频繁点击", Toast.LENGTH_LONG).show();
                 }
                 clickTime = System.currentTimeMillis();
             }
@@ -136,9 +150,11 @@ public class FloatingService extends Service {
     }
 
     private void resumeCallActivity() {
+        callResumed = true;
         Intent intent = new Intent(FloatingService.this, CallSingleActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
+        Toast.makeText(getApplicationContext(), "恢复通话", Toast.LENGTH_LONG).show();
     }
 
     //开始触控的坐标,移动时的坐标(相对于屏幕左上角的坐标)

+ 9 - 6
home/src/main/code/com/wdkl/ncs/android/component/home/service/WdKeepAliveService.kt

@@ -91,7 +91,9 @@ class WdKeepAliveService : AbsWorkService() {
     }
 
     override fun startWork() {
-        EventBus.getDefault().register(this)
+        if (!EventBus.getDefault().isRegistered(this)) {
+            EventBus.getDefault().register(this)
+        }
 
         //全局调用一次
         Thread {
@@ -211,7 +213,9 @@ class WdKeepAliveService : AbsWorkService() {
 
         SoundPoolManager.getInstance().release()
         SpeechUtil.getInstance().release()
-        EventBus.getDefault().unregister(this)
+        if (EventBus.getDefault().isRegistered(this)) {
+            EventBus.getDefault().unregister(this);
+        }
         if (netWorkChangeReceiver != null) {
             try {
                 unregisterReceiver(netWorkChangeReceiver)
@@ -227,11 +231,11 @@ class WdKeepAliveService : AbsWorkService() {
     }
 
     override fun onBindService(p0: Intent?, p1: Void?): IBinder {
-        return Messenger(Handler()).binder
+        return Messenger(Handler(Looper.myLooper()!!)).binder
     }
 
     override fun onServiceKilled() {
-        EventBus.getDefault().unregister(this)
+        //EventBus.getDefault().unregister(this)
         if (netWorkChangeReceiver != null) {
             try {
                 unregisterReceiver(netWorkChangeReceiver)
@@ -250,11 +254,10 @@ class WdKeepAliveService : AbsWorkService() {
                 Util.wakeUpAndUnlock()
 
                 SpeechUtil.getInstance().stopSpeak()
-                MediaPlayHelper.getInstance().releaseMusic();
+                MediaPlayHelper.getInstance().releaseMusic()
 
                 val tcpModel = messageEvent.getMessage() as TcpModel
                 if (tcpModel.type == TcpType.VOICE) {
-//                var gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                     val interactionVO = Gson().fromJson(tcpModel.data.toString(), InteractionVO::class.java)
 
                     if (tcpModel.action == TcpAction.VoiceAction.CALL) {  //有来电

+ 14 - 0
home/src/main/code/com/wdkl/ncs/android/component/home/util/SpeechUtil.java

@@ -135,6 +135,20 @@ public class SpeechUtil {
             params.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_MUSIC);
             int speakStatus = textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, "uniqueId");
             Log.d(TAG, "startSpeak status: " + speakStatus + ", text: " + text);
+            if(speakStatus != TextToSpeech.SUCCESS){
+                SpeechUtil.getInstance().init(BaseApplication.appContext, new ISpeechCallback() {
+                    @Override
+                    public void initSuccess() {
+                        Log.d(TAG, "TTS init success");
+                        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, "uniqueId");
+                    }
+
+                    @Override
+                    public void initFailed() {
+                        Log.d(TAG, "TTS init failed");
+                    }
+                });
+            }
         } else {
             Log.d(TAG, "startSpeak fail: " + text);
         }

+ 4 - 4
home/src/main/res/layout/float_call_window_layout.xml

@@ -1,15 +1,15 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
     android:gravity="center_horizontal"
     android:background="@drawable/av_float_bg"
     android:orientation="vertical">
 
     <TextView
         android:id="@+id/float_call_view"
-        android:layout_width="72dp"
-        android:layout_height="72dp"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
         android:background="@drawable/av_float_audio"/>
 
     <TextView

+ 4 - 0
middleware/src/main/code/com/wdkl/ncs/android/middleware/common/Constants.kt

@@ -58,6 +58,10 @@ class Constants {
 
         var sosActive = false
 
+        const val call_action = "com.wdkl.call_action"
+        const val action_name = "action_name"
+        const val interaction_id = "interaction_id"
+
         const val SYSTEM_REGISTERED = "SYSTEM_REGISTERED"
 
         const val EVENT_BATTERY_PERCENT = 0x07 //电量

+ 18 - 20
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/channel/DeviceChannel.java

@@ -1,8 +1,10 @@
 package com.wdkl.ncs.android.middleware.tcp.channel;
 
+import android.content.Intent;
 import android.util.Log;
 
 import com.google.gson.Gson;
+import com.wdkl.ncs.android.lib.base.BaseApplication;
 import com.wdkl.ncs.android.middleware.common.Constants;
 import com.wdkl.ncs.android.middleware.model.vo.InteractionVO;
 import com.wdkl.ncs.android.middleware.tcp.dto.TcpModel;
@@ -27,37 +29,21 @@ public class DeviceChannel {
     public static int callId = 0;
 
     public static TcpModel handleTcpReceived(TcpModel tcpModel){
-        /*TcpModel responseTcpModel = new TcpModel();
-        responseTcpModel.setType(TcpType.CALLBACK);
-        responseTcpModel.setAction(TcpAction.CallbackAction.SUCCESS);
-        responseTcpModel.setFromId(Constants.Companion.getDeviceId());
-        responseTcpModel.setToId(tcpModel.getFromId());
-
-        //处理重复
-        String tcpJsonStr = tcpModel.toJson();
-        Long currentTime = System.currentTimeMillis();
-        if (lastTcp.get(tcpJsonStr)!=null && currentTime/1000 - lastTcp.get(tcpJsonStr)/1000 <1){
-            return responseTcpModel;
-        }
-        lastTcp.clear();
-        lastTcp.put(tcpModel.toJson(), System.currentTimeMillis());*/
 
         TcpModel responseTcpModel = null;
 
-//        Log.e(TAG,"收到tcp消息 DeviceChannel "+tcpJsonStr);
+        //Log.d(TAG,"收到tcp消息 DeviceChannel "+tcpModel.toJson());
 
         switch (tcpModel.getType()){
             case VOICE:
                 if (tcpModel.getAction()==TcpAction.VoiceAction.SUCCESS) {
                     EventBus.getDefault().post(new MessageEvent(tcpModel, Constants.EVENT_NEW_TCP));
-                    //EventBus.getDefault().post(new MessageEvent(tcpModel, 1));
                 } else if(tcpModel.getAction() == TcpAction.VoiceAction.CALL){
                     if (calling) {
                         Log.d(TAG,"通话中,来电 " + tcpModel.getFromId() + "<>" + callId);
                         //相同来源,重新建立通话
                         if (tcpModel.getFromId().equals(callId)){
                             EventBus.getDefault().post(new MessageEvent(tcpModel, Constants.EVENT_NEW_TCP));
-                            //EventBus.getDefault().post(new MessageEvent(tcpModel, 11));
                         } else {
                             InteractionVO interactionVO = new Gson().fromJson(tcpModel.getData().toString(), InteractionVO.class);
                             responseTcpModel = VoiceUtil.voiceCalling(Constants.Companion.getDeviceId(), tcpModel.getFromId(), interactionVO.getId());
@@ -66,7 +52,6 @@ public class DeviceChannel {
                         }
                     } else { //得到通话
                         EventBus.getDefault().post(new MessageEvent(tcpModel, Constants.EVENT_NEW_TCP));
-                        //EventBus.getDefault().post(new MessageEvent(tcpModel, 1));
                     }
                 } else if ((tcpModel.getAction() == TcpAction.VoiceAction.ACCEPT)  //我方呼出,对方接受
                         || (tcpModel.getAction() == TcpAction.VoiceAction.REJECT) //我方呼出,对方拒绝
@@ -85,12 +70,12 @@ public class DeviceChannel {
                                     e.printStackTrace();
                                 }
                                 EventBus.getDefault().post(new MessageEvent(tm, Constants.EVENT_NEW_TCP));
-                                //EventBus.getDefault().post(new MessageEvent(tm, 2));
                             }
                         }).start();
                     } else {
                         EventBus.getDefault().post(new MessageEvent(tcpModel, Constants.EVENT_NEW_TCP));
-                        //EventBus.getDefault().post(new MessageEvent(tcpModel, 2));
+                        //发广播通知
+                        //sendCallBroadcast(tcpModel);
                     }
                 }
                 break;
@@ -156,4 +141,17 @@ public class DeviceChannel {
 
         return responseTcpModel;
     }
+
+
+    private static void sendCallBroadcast(TcpModel tcpModel) {
+        Intent intent = new Intent();
+        intent.setAction(Constants.call_action);
+        intent.putExtra(Constants.action_name, tcpModel.getAction().getName());
+        if (tcpModel.getData() != null) {
+            InteractionVO interactionVO = new Gson().fromJson(tcpModel.getData().toString(), InteractionVO.class);
+            intent.putExtra(Constants.interaction_id, interactionVO.getId());
+        }
+
+        BaseApplication.appContext.sendBroadcast(intent);
+    }
 }