|
| 1 | +package com.tencent.iot.explorer.link.demo.video |
| 2 | + |
| 3 | +import android.text.InputType |
| 4 | +import android.text.TextUtils |
| 5 | +import android.util.Log |
| 6 | +import android.view.View |
| 7 | +import android.widget.Toast |
| 8 | +import androidx.recyclerview.widget.LinearLayoutManager |
| 9 | +import com.alibaba.fastjson.JSONArray |
| 10 | +import com.tencent.iot.explorer.link.core.utils.SharePreferenceUtil |
| 11 | +import com.tencent.iot.explorer.link.demo.App |
| 12 | +import com.tencent.iot.explorer.link.demo.R |
| 13 | +import com.tencent.iot.explorer.link.demo.VideoBaseActivity |
| 14 | +import com.tencent.iot.explorer.link.demo.video.preview.WlanVideoPreviewActivity |
| 15 | +import com.tencent.iot.video.link.callback.OnWlanDevicesDetectedCallback |
| 16 | +import com.tencent.iot.video.link.consts.VideoConst |
| 17 | +import com.tencent.iot.video.link.entity.DeviceServerInfo |
| 18 | +import com.tencent.iot.video.link.entity.WlanDetectBody |
| 19 | +import com.tencent.iot.video.link.entity.WlanRespBody |
| 20 | +import com.tencent.iot.video.link.service.DetectService |
| 21 | +import kotlinx.android.synthetic.main.activity_video_detect_devs.* |
| 22 | +import kotlinx.android.synthetic.main.activity_video_detect_devs.product_id_layout |
| 23 | +import kotlinx.android.synthetic.main.activity_video_input_authorize.* |
| 24 | +import kotlinx.android.synthetic.main.blue_title_layout.* |
| 25 | +import kotlinx.android.synthetic.main.fragment_video_device.* |
| 26 | +import kotlinx.android.synthetic.main.input_item_layout.view.* |
| 27 | +import kotlinx.coroutines.* |
| 28 | +import java.util.* |
| 29 | +import kotlin.collections.ArrayList |
| 30 | + |
| 31 | +class VideoWlanDetectActivity : VideoBaseActivity() , CoroutineScope by MainScope() { |
| 32 | + |
| 33 | + var datas: MutableList<DeviceServerInfo> = ArrayList() |
| 34 | + var adapter: WlanDevsAdapter? = null |
| 35 | + |
| 36 | + override fun getContentView(): Int { |
| 37 | + return R.layout.activity_video_detect_devs |
| 38 | + } |
| 39 | + |
| 40 | + override fun initView() { |
| 41 | + |
| 42 | + tv_title.setText(R.string.video_wlan) |
| 43 | + product_id_layout.tv_tip.setText(R.string.product_id) |
| 44 | + product_id_layout.ev_content.setHint(R.string.hint_product_id) |
| 45 | + product_id_layout.ev_content.inputType = InputType.TYPE_CLASS_TEXT |
| 46 | + product_id_layout.iv_more.visibility = View.GONE |
| 47 | + |
| 48 | + client_token_layout.tv_tip.setText(R.string.video_client_token) |
| 49 | + client_token_layout.ev_content.setHint(R.string.hint_client_token) |
| 50 | + client_token_layout.ev_content.inputType = InputType.TYPE_CLASS_TEXT |
| 51 | + client_token_layout.iv_more.visibility = View.GONE |
| 52 | + |
| 53 | + launch (Dispatchers.Main) { |
| 54 | + var jsonArrStr = SharePreferenceUtil.getString(this@VideoWlanDetectActivity, VideoConst.VIDEO_WLAN_CONFIG, VideoConst.VIDEO_ACCESS_INFOS) |
| 55 | + jsonArrStr?.let { |
| 56 | + var accessInfos = JSONArray.parseArray(jsonArrStr, AccessInfo::class.java) |
| 57 | + accessInfos?.let { |
| 58 | + if (accessInfos.size > 0) { |
| 59 | + var accessInfo = accessInfos.get(accessInfos.size - 1) |
| 60 | + client_token_layout.ev_content.setText(accessInfo.accessToken) |
| 61 | + product_id_layout.ev_content.setText(accessInfo.productId) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + var layoutManager = LinearLayoutManager(this@VideoWlanDetectActivity) |
| 68 | + adapter = WlanDevsAdapter(this@VideoWlanDetectActivity, datas) |
| 69 | + devs_lv.setLayoutManager(layoutManager) |
| 70 | + devs_lv.setAdapter(adapter) |
| 71 | + adapter?.setOnItemClicked(onItemClicked) |
| 72 | + } |
| 73 | + |
| 74 | + private var onItemClicked = object : WlanDevsAdapter.OnItemClicked { |
| 75 | + override fun onItemClicked(pos: Int) { |
| 76 | + App.data.accessInfo = AccessInfo() |
| 77 | + App.data.accessInfo?.productId = product_id_layout.ev_content.text.toString() |
| 78 | + |
| 79 | + var dev = DevInfo() |
| 80 | + dev.deviceName = datas.get(pos).deviceName |
| 81 | + dev.channel = 0 |
| 82 | + dev.Status = 1 |
| 83 | + dev.address = datas.get(pos).address |
| 84 | + dev.port = datas.get(pos).port |
| 85 | + WlanVideoPreviewActivity.startPreviewActivity(this@VideoWlanDetectActivity, dev) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + override fun setListener() { |
| 90 | + iv_back.setOnClickListener { finish() } |
| 91 | + btn_detect.setOnClickListener(searchClickedListener) |
| 92 | + } |
| 93 | + |
| 94 | + override fun onDestroy() { |
| 95 | + super.onDestroy() |
| 96 | + cancel() |
| 97 | + } |
| 98 | + |
| 99 | + override fun onPause() { |
| 100 | + super.onPause() |
| 101 | + DetectService.getInstance().clearAllTask() |
| 102 | + } |
| 103 | + |
| 104 | + var searchClickedListener = object : View.OnClickListener { |
| 105 | + override fun onClick(v: View?) { |
| 106 | + if (TextUtils.isEmpty(product_id_layout.ev_content.text)) { |
| 107 | + Toast.makeText(this@VideoWlanDetectActivity, R.string.hint_product_id, Toast.LENGTH_SHORT).show() |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + if (TextUtils.isEmpty(client_token_layout.ev_content.text)) { |
| 112 | + Toast.makeText(this@VideoWlanDetectActivity, R.string.hint_client_token, Toast.LENGTH_SHORT).show() |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + var accessInfo = AccessInfo() |
| 117 | + accessInfo.accessToken = client_token_layout.ev_content.text.toString() |
| 118 | + accessInfo.productId = product_id_layout.ev_content.text.toString() |
| 119 | + |
| 120 | + launch (Dispatchers.Main) { |
| 121 | + checkAccessInfo(accessInfo) |
| 122 | + } |
| 123 | + |
| 124 | + datas.clear() |
| 125 | + var detectBody = WlanDetectBody() |
| 126 | + detectBody.productId = accessInfo.productId |
| 127 | + detectBody.clientToken = accessInfo.accessToken |
| 128 | + DetectService.getInstance().onWlanDevicesDetectedCallback = detectMesssageCallback |
| 129 | + DetectService.getInstance().startSendBroadcast(detectBody, 30) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private var detectMesssageCallback = object: OnWlanDevicesDetectedCallback { |
| 134 | + override fun onMessage(version: String, resp: WlanRespBody): Boolean { |
| 135 | + if (!datas.contains(resp.params)) { |
| 136 | + datas.add(resp.params) |
| 137 | + runOnUiThread(Runnable { |
| 138 | + adapter?.notifyDataSetChanged() |
| 139 | + }) |
| 140 | + } |
| 141 | + return true |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private fun checkAccessInfo(accessInfo: AccessInfo) { |
| 146 | + var accessInfos: MutableList<AccessInfo> = ArrayList() |
| 147 | + accessInfos.add(accessInfo) |
| 148 | + SharePreferenceUtil.saveString(this@VideoWlanDetectActivity, VideoConst.VIDEO_WLAN_CONFIG, VideoConst.VIDEO_ACCESS_INFOS, JSONArray.toJSONString(accessInfos)) |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments