11package com.example.ivdemo
22
3+ import android.graphics.Bitmap
4+ import android.graphics.Canvas
35import android.graphics.SurfaceTexture
6+ import android.graphics.drawable.BitmapDrawable
7+ import android.graphics.drawable.Drawable
48import android.os.Build
59import android.os.Handler
610import android.os.Looper
711import android.util.Log
812import android.view.TextureView
9- import android.widget.Toast
1013import androidx.annotation.RequiresApi
14+ import androidx.core.content.ContextCompat
1115import androidx.core.view.isVisible
1216import androidx.lifecycle.lifecycleScope
17+ import com.example.ivdemo.popup.CustomCommandDialog
1318import com.example.ivdemo.popup.QualitySettingDialog
1419import com.tencent.iot.twcall.R
1520import com.tencent.iot.twcall.databinding.ActivityIpcBinding
1621import com.tencent.iot.video.device.VideoNativeInterface
1722import com.tencent.iot.video.device.annotations.CsChannelType
1823import com.tencent.iot.video.device.annotations.StreamType
1924import com.tencent.iot.video.device.model.AvDataInfo
20- import com.tencent.iot.video.device.model.CsBalanceInfo
2125import com.tencent.iotvideo.link.CameraRecorder
2226import com.tencent.iotvideo.link.SimplePlayer
2327import com.tencent.iotvideo.link.util.copyTextToClipboard
2428import kotlinx.coroutines.Dispatchers
2529import kotlinx.coroutines.launch
30+ import java.io.ByteArrayOutputStream
2631
2732class IPCActivity : BaseIPCActivity <ActivityIpcBinding >() {
2833
@@ -32,6 +37,7 @@ class IPCActivity : BaseIPCActivity<ActivityIpcBinding>() {
3237 private var localPreviewSurface: SurfaceTexture ? = null
3338 private val handler = Handler (Looper .getMainLooper())
3439 private val UPDATE_P2P_INFO_TOKEN = " update_p2p_info_token"
40+ private var customCommandDialog: CustomCommandDialog ? = null
3541
3642 private val listener = object : TextureView .SurfaceTextureListener {
3743 override fun onSurfaceTextureAvailable (surface : SurfaceTexture , width : Int , height : Int ) {
@@ -85,6 +91,38 @@ class IPCActivity : BaseIPCActivity<ActivityIpcBinding>() {
8591 tvCopy.setOnClickListener {
8692 copyTextToClipboard(this @IPCActivity, tvP2pInfo.text.toString().substringAfter(" :" ))
8793 }
94+ btnCloudStorageReport.setOnClickListener {
95+ if (! isOnline) {
96+ showToast(" 设备未上线" )
97+ return @setOnClickListener
98+ }
99+ VideoNativeInterface .getInstance()
100+ .startCsEvent(CsChannelType .CS_SINGLE_CH , 1 , " report test cs info" )
101+ }
102+ btnCloudWareReport.setOnClickListener {
103+ if (! isOnline) {
104+ showToast(" 设备未上线" )
105+ return @setOnClickListener
106+ }
107+ val drawable = ContextCompat .getDrawable(this @IPCActivity, R .drawable.mom)
108+ drawable?.let {
109+ VideoNativeInterface .getInstance().reportCsEventDirectly(
110+ CsChannelType .CS_SINGLE_CH ,
111+ 1 ,
112+ " report test cs info" ,
113+ 0 ,
114+ bitmapToByteArray(drawableToBitmap(it))
115+ )
116+ }
117+ }
118+ btnCustomCommand.setOnClickListener {
119+ if (! isOnline) {
120+ showToast(" 设备未上线" )
121+ return @setOnClickListener
122+ }
123+ customCommandDialog = CustomCommandDialog (this @IPCActivity, visitor)
124+ customCommandDialog?.show(supportFragmentManager)
125+ }
88126// btnIpcCall.setOnClickListener {
89127// val time = System.currentTimeMillis()
90128// val timeD = time - lastClickTime
@@ -107,12 +145,28 @@ class IPCActivity : BaseIPCActivity<ActivityIpcBinding>() {
107145 showToast(" P2PInfo 已更新" )
108146 }
109147 binding.tvP2pInfo.text = String .format(getString(R .string.text_p2p_info), p2pInfo)
110- handler.postDelayed(taskRunnable, UPDATE_P2P_INFO_TOKEN , 60000 )
148+ handler.postDelayed(taskRunnable, UPDATE_P2P_INFO_TOKEN , 10000 )
149+ }
150+
151+ private var shouldGetCs = true
152+ private fun updateCsState () {
153+ if (shouldGetCs) {
154+ val info =
155+ VideoNativeInterface .getInstance().getCsBalanceInfo(CsChannelType .CS_SINGLE_CH , 10 )
156+ if (info != null ) {
157+ shouldGetCs = false
158+ binding.tvCloudStorageState.text = String .format(
159+ getString(R .string.text_cloud_storage_state),
160+ if (info.csSwitch) " 已开通" else " 未开通"
161+ )
162+ }
163+ }
111164 }
112165
113166 @RequiresApi(Build .VERSION_CODES .P )
114167 private val taskRunnable = Runnable {
115168 updateP2pInfo()
169+ updateCsState()
116170 }
117171
118172 override fun onDestroy () {
@@ -125,14 +179,7 @@ class IPCActivity : BaseIPCActivity<ActivityIpcBinding>() {
125179 super .onOnline(netDateTime)
126180 lifecycleScope.launch(Dispatchers .Main ) {
127181 updateP2pInfo()
128- val info = VideoNativeInterface .getInstance()
129- .getCsBalanceInfo(CsChannelType .CS_MULTI_CH1 , 1000 )
130182 handler.postDelayed(taskRunnable, UPDATE_P2P_INFO_TOKEN , 60000 )
131- Log .d(
132- " hhh" ,
133- " info:${info.csDays} ${info.csType} ${info.csSwitch} ${info.freeTrialRemainingSec} "
134- )
135- // binding.tvCloudStorageState.text =info.
136183 }
137184 }
138185
@@ -196,4 +243,35 @@ class IPCActivity : BaseIPCActivity<ActivityIpcBinding>() {
196243 }
197244 return 0
198245 }
246+
247+ override fun onRecvCommand (
248+ command : Int ,
249+ visitor : Int ,
250+ channel : Int ,
251+ videoResType : Int ,
252+ args : String?
253+ ): String {
254+ return customCommandDialog?.receiveCommand(args ? : " null" ).toString()
255+ }
256+
257+ private fun drawableToBitmap (drawable : Drawable ): Bitmap {
258+ if (drawable is BitmapDrawable ) {
259+ return drawable.bitmap
260+ }
261+
262+ val width = drawable.intrinsicWidth
263+ val height = drawable.intrinsicHeight
264+ val bitmap = Bitmap .createBitmap(width, height, Bitmap .Config .ARGB_8888 )
265+ val canvas = Canvas (bitmap)
266+ drawable.setBounds(0 , 0 , canvas.width, canvas.height)
267+ drawable.draw(canvas)
268+ return bitmap
269+ }
270+
271+ // 将Bitmap转换为byte数组
272+ private fun bitmapToByteArray (bitmap : Bitmap ): ByteArray {
273+ val stream = ByteArrayOutputStream ()
274+ bitmap.compress(Bitmap .CompressFormat .PNG , 100 , stream)
275+ return stream.toByteArray()
276+ }
199277}
0 commit comments