From b1d822d6272dc33928586ffa18b9ed3247f32522 Mon Sep 17 00:00:00 2001
From: lmw <125975490@qq.com>
Date: 星期五, 07 六月 2024 14:48:20 +0800
Subject: [PATCH] 修改部分bug

---
 app/src/main/java/com/future/driver/ui/main/MainActivity.kt |  124 +++++++++++++++++++++++++++++++++++-----
 1 files changed, 107 insertions(+), 17 deletions(-)

diff --git a/app/src/main/java/com/future/driver/ui/main/MainActivity.kt b/app/src/main/java/com/future/driver/ui/main/MainActivity.kt
index f1b5833..de08945 100644
--- a/app/src/main/java/com/future/driver/ui/main/MainActivity.kt
+++ b/app/src/main/java/com/future/driver/ui/main/MainActivity.kt
@@ -6,8 +6,8 @@
 import android.os.Handler
 import android.os.Looper
 import android.provider.Settings
+import android.util.Log
 import android.widget.Toast
-import androidx.core.app.ActivityCompat
 import cn.sinata.rxnetty.NettyClient
 import cn.sinata.xldutils.utils.*
 import com.amap.api.location.AMapLocation
@@ -30,8 +30,10 @@
 import com.future.driver.ui.main.event.MineEventActivity
 import com.future.driver.ui.mine.MineActivity
 import com.future.driver.ui.mine.MsgActivity
+import com.future.driver.utils.AudioRecordUtils
 import com.future.driver.utils.Cache.CacheKey
 import com.future.driver.utils.DateUtil
+import com.future.driver.utils.OSSUtil
 import com.future.driver.utils.download.DownloadUtil
 import com.google.gson.Gson
 import com.lljjcoder.style.citylist.Toast.ToastUtils
@@ -42,8 +44,8 @@
 import org.jetbrains.anko.startActivity
 import org.jetbrains.anko.toast
 
-
-class MainActivity : MyBaseActivity(), AMapLocationListener {
+class MainActivity : MyBaseActivity(), AMapLocationListener,
+    AudioRecordUtils.OnAudioStatusUpdateListener {
 
     val mainFragment by lazy {
         MainFragment()
@@ -55,23 +57,34 @@
         setContentView(R.layout.activity_main)
     }
 
+    private var recorder:AudioRecordUtils? = null  //录音器
+    private val VOICE_LENGTH = 1000L*60*5 //实际5分钟一个片段
+//    private val VOICE_LENGTH = 1000L*15 //测试15秒一个片段
+
     override fun initView() {
         MyApplication.canPlayVoice = true
 //        var packageName = "com.android.providers.downloads";
 //        var intent =  Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
 //    intent.setData(Uri.parse("package:" + packageName));
 //    startActivity(intent);
-        if (CacheKey.getKeyStr(Const.REFUSE_LOCATION).isNotEmpty()&&!RxPermissions(this).isGranted(Manifest.permission.ACCESS_FINE_LOCATION)){
+        if (CacheKey.getKeyStr(Const.REFUSE_LOCATION).isNotEmpty()&&!RxPermissions(this).isGranted(
+                Manifest.permission.ACCESS_FINE_LOCATION
+            )){
             toast("没有定位权限,您将无法收到新订单推送")
         }else{
             if (!RxPermissions(this).isGranted(Manifest.permission.ACCESS_FINE_LOCATION)){
-                DialogUtil.getDelAndSureDialog(this,"去授权","否","《未来出行司机》需要获取您的当前位置信息,用于查询当前城市是否开通服务,我们会将您的位置实时上传至后端为您匹配和推送附近订单。如果拒绝定位权限,您将无法正常接单。",{
-                }){
+                DialogUtil.getDelAndSureDialog(
+                    this,
+                    "去授权",
+                    "否",
+                    "《未来出行司机》需要获取您的当前位置信息,用于查询当前城市是否开通服务,我们会将您的位置实时上传至后端为您匹配和推送附近订单。如果拒绝定位权限,您将无法正常接单。",
+                    {
+                    }){
                     val subscribe =
                         RxPermissions(this).request(Manifest.permission.ACCESS_FINE_LOCATION).subscribe {
                             if (!it){
                                 toast("没有定位权限,您将无法收到新订单推送")
-                                CacheKey.putKeyStr(Const.REFUSE_LOCATION,"1")
+                                CacheKey.putKeyStr(Const.REFUSE_LOCATION, "1")
                             }
                         }
                 }
@@ -96,7 +109,7 @@
         override fun orderInfo(data: OrderSimpleData?) {
             data?.let {
                 when(it.orderType){
-                    1,2,3 -> {
+                    1, 2, 3 -> {
                         if (it.status == 10 || it.status == 12) {
                             Handler(Looper.getMainLooper()).post {
                                 mainFragment?.refresh()
@@ -105,14 +118,13 @@
                         }
                     }
                 }
-
             }
         }
     }
 
     private fun checkGps() {
         if (!AMapKit.isLocServiceEnable(this)){
-            DialogUtil.getDelAndSureDialog(this,"查看定位","退出","您暂未开启手机定位GPS,是否前往开启?",{
+            DialogUtil.getDelAndSureDialog(this, "查看定位", "退出", "您暂未开启手机定位GPS,是否前往开启?", {
                 onBackPressed()
             }){
                 val intent = Intent()
@@ -137,7 +149,7 @@
 //        map["id"] = CacheKey.getUserId()
         callNet(Api.queryNewData, map) {
             var data = gson.fromJson<VersionBean>(it, VersionBean::class.java)
-            if (data.data.version != "" && data.data.version != Utils.getAppVersion(this)) {
+            if (data.data.version != "" && data.data.version > Utils.getAppVersion(this)) {
                 if (data.data.mandatory == 2) {
                     DialogUtil.getDelAndSureDialog(this, "有新版本是否更新?", {}, {
                         DownloadUtil(this).downloadAPK(data.data.url, "未来出行司机端")
@@ -163,7 +175,7 @@
         mainFragment?.refresh()
     }
 
-    fun beOnDuty(b:Boolean){
+    fun beOnDuty(b: Boolean){
         if (b){
             avv_anim.visible()
             tv_up_avv.visible()
@@ -174,7 +186,7 @@
     }
 
 
-    private fun initCall() {
+    private fun initCall(playVoice:Boolean = true) {
         var map = getMapByAny()
         callNet(Api.queryHomeData, map) {
             var data = gson.fromJson<HomeDataBean>(it, HomeDataBean::class.java)
@@ -187,10 +199,12 @@
             tv_lien_name.text = data.data.brand.toString()
             tv_car.text = if (data.data.work == 1) "收车" else "出车"
             if (data.data.work == 1){
-                MyApplication.getTTsManager().setVideoText("您正在上班中")
+                if (playVoice)
+                    MyApplication.getTTsManager().setVideoText("您正在上班中")
                 beOnDuty(true)
             }else{
-                MyApplication.getTTsManager().setVideoText("您已收车")
+                if (playVoice)
+                    MyApplication.getTTsManager().setVideoText("您已收车")
                 beOnDuty(false)
             }
             tv_time.text = DateUtil.getTime(
@@ -274,10 +288,14 @@
                     }else{
                         var map= getMapByAny()
                         map["type"] = bean[0].id
-                        callNet(Api.work,map) {
+                        callNet(Api.work, map,{
                             toast("上班成功")
                             beOnDuty(true)
                             EventBus.getDefault().post(BaseEvent(BaseEvent.START_CAR))
+                        }){
+                            if (it?.contains("人脸") == true){
+                                tv_face.visible()
+                            }
                         }
                     }
                 }
@@ -324,6 +342,14 @@
             }
             BaseEvent.FACE_FAILED -> {
                 tv_face.visible()
+                initCall(false)
+            }
+            BaseEvent.START_RECORD -> {
+                if (!MyApplication.isRecording)
+                    startRecord()
+            }
+            BaseEvent.FINISH_RECORD -> {
+                recorder?.stopRecord()
             }
         }
     }
@@ -331,10 +357,74 @@
     override fun onLocationChanged(p0: AMapLocation?) {
         if (p0?.errorCode == 0) {
             MyApplication.aMapLocation = p0
-            CacheKey.putKeyStr("location",Gson().toJson(p0))
+            CacheKey.putKeyStr("location", Gson().toJson(p0))
         }
     }
 
+    private fun startRecord(){
+        if (recorder == null){
+            recorder = AudioRecordUtils(getExternalFilesDir(null)?.path+"/")
+            recorder?.setOnAudioStatusUpdateListener(this)
+        }
+        if (RxPermissions(this).isGranted(Manifest.permission.RECORD_AUDIO)&&RxPermissions(this).isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
+            Log.e("mmp","订单${MyApplication.recordOrderId} 开始录音...")
+            recorder?.startRecord()
+        }
+        else
+            toast("缺少录音相关权限")
+    }
+
+    private fun updateVoice(filePath: String) {
+        val ossUtil = OSSUtil(this)
+        ossUtil.uploadSingleWithSize(filePath, object : OSSUtil.OSSUploadCallBack() {
+            override fun onFinishWithSize(url: String?, size: Long) {
+                super.onFinishWithSize(url, size)
+                Log.e("mmp","录音保存路径:${url},大小:${size/1024}kb")
+                val mapByAny = getMapByAny()
+                mapByAny["fileFormat"] = "caf"
+                mapByAny["fileLink"] = url
+                mapByAny["fileSize"] = size/1024
+                mapByAny["orderId"] = MyApplication.recordOrderId
+                mapByAny["orderType"] = MyApplication.recordOrderType
+                callNet(false,Api.tape,mapByAny){
+
+                }
+            }
+
+            override fun onFial(message: String?) {
+                super.onFial(message)
+                runOnUiThread {
+                    Toast.makeText(this@MainActivity, "录音上传失败,请检查网络", Toast.LENGTH_SHORT).show()
+                }
+            }
+        })
+    }
+
+    override fun onUpdate(db: Double, time: Long) {
+        MyApplication.isRecording = true
+        if (time >= VOICE_LENGTH) {
+            Log.e("mmp","订单${MyApplication.recordOrderId} 录音片段完成")
+            recorder?.stopRecord() //结束录音
+        }
+    }
+
+    override fun onStop(filePath: String) {
+        MyApplication.isRecording = false
+        Thread {
+            updateVoice(filePath)
+        }.start()
+        if (MyApplication.currentOrderId.isNotEmpty()){//行程未结束 继续录制下一段
+            Log.e("mmp","订单${MyApplication.recordOrderId} 开始下一片段录音")
+            recorder?.startRecord()
+        }
+    }
+
+    override fun onStartPlay() {
+    }
+
+    override fun onFinishPlay() {
+    }
+
     override fun onDestroy() {
         super.onDestroy()
         MyApplication.canPlayVoice = false

--
Gitblit v1.7.1