diff --git a/app/src/main/java/com/casic/detector/utils/LocationHub.kt b/app/src/main/java/com/casic/detector/utils/LocationHub.kt index 658d019..d1fed79 100644 --- a/app/src/main/java/com/casic/detector/utils/LocationHub.kt +++ b/app/src/main/java/com/casic/detector/utils/LocationHub.kt @@ -13,15 +13,23 @@ import com.amap.api.services.geocoder.RegeocodeResult import com.casic.detector.callback.IAddressListener import com.casic.detector.callback.ILocationListener +import com.pengxh.kt.lite.base.BaseSingleton -object LocationHub { - private const val kTag = "LocationHub" +class LocationHub private constructor(context: Context) { - fun getCurrentLocation(context: Context, listener: ILocationListener) { - val locationClient = AMapLocationClient(context) + companion object : BaseSingleton() { + override val creator: (Context) -> LocationHub + get() = ::LocationHub + } + + private val kTag = "LocationHub" + private val locationClient by lazy { AMapLocationClient(context) } + private val codeSearch by lazy { GeocodeSearch(context) } + + fun getCurrentLocation(isOnceLocation: Boolean, listener: ILocationListener) { val locationOption = AMapLocationClientOption() locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy - locationOption.isOnceLocation = true + locationOption.isOnceLocation = isOnceLocation locationClient.setLocationOption(locationOption) locationClient.setLocationListener { if (it.errorCode == 0) { @@ -36,9 +44,8 @@ locationClient.startLocation() } - fun antiCodingLocation(context: Context, location: Location, listener: IAddressListener) { + fun antiCodingLocation(location: Location, listener: IAddressListener) { try { - val codeSearch = GeocodeSearch(context) // 第一个参数表示一个LatLonPoint,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 val query = RegeocodeQuery( LatLonPoint(location.latitude, location.longitude), 10f, GeocodeSearch.AMAP @@ -55,4 +62,8 @@ e.printStackTrace() } } + + fun stopLocation() { + locationClient.stopLocation() + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/utils/LocationHub.kt b/app/src/main/java/com/casic/detector/utils/LocationHub.kt index 658d019..d1fed79 100644 --- a/app/src/main/java/com/casic/detector/utils/LocationHub.kt +++ b/app/src/main/java/com/casic/detector/utils/LocationHub.kt @@ -13,15 +13,23 @@ import com.amap.api.services.geocoder.RegeocodeResult import com.casic.detector.callback.IAddressListener import com.casic.detector.callback.ILocationListener +import com.pengxh.kt.lite.base.BaseSingleton -object LocationHub { - private const val kTag = "LocationHub" +class LocationHub private constructor(context: Context) { - fun getCurrentLocation(context: Context, listener: ILocationListener) { - val locationClient = AMapLocationClient(context) + companion object : BaseSingleton() { + override val creator: (Context) -> LocationHub + get() = ::LocationHub + } + + private val kTag = "LocationHub" + private val locationClient by lazy { AMapLocationClient(context) } + private val codeSearch by lazy { GeocodeSearch(context) } + + fun getCurrentLocation(isOnceLocation: Boolean, listener: ILocationListener) { val locationOption = AMapLocationClientOption() locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy - locationOption.isOnceLocation = true + locationOption.isOnceLocation = isOnceLocation locationClient.setLocationOption(locationOption) locationClient.setLocationListener { if (it.errorCode == 0) { @@ -36,9 +44,8 @@ locationClient.startLocation() } - fun antiCodingLocation(context: Context, location: Location, listener: IAddressListener) { + fun antiCodingLocation(location: Location, listener: IAddressListener) { try { - val codeSearch = GeocodeSearch(context) // 第一个参数表示一个LatLonPoint,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 val query = RegeocodeQuery( LatLonPoint(location.latitude, location.longitude), 10f, GeocodeSearch.AMAP @@ -55,4 +62,8 @@ e.printStackTrace() } } + + fun stopLocation() { + locationClient.stopLocation() + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index 7b94099..99905ec 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -53,6 +53,7 @@ private lateinit var soundPool: SoundPool private var soundResId = 0 private val gpioManager by lazy { GpioManager() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var weakReferenceHandler: WeakReferenceHandler override fun initViewBinding(): ActivityInstallLabelBinding { @@ -82,14 +83,10 @@ weakReferenceHandler = WeakReferenceHandler(this) - val audioAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() - soundPool = SoundPool.Builder() - .setMaxStreams(16) - .setAudioAttributes(audioAttributes) - .build() + val audioAttributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build() + soundPool = + SoundPool.Builder().setMaxStreams(16).setAudioAttributes(audioAttributes).build() } override fun initEvent() { @@ -100,9 +97,7 @@ binding.objectInclude.materialSpinner.show(this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0) binding.objectInclude.downPipeTypeSpinner.show(this, LocaleConstant.DOWN_PIPE_TYPE_ARRAY, 0) binding.objectInclude.downPipeMaterialSpinner.show( - this, - LocaleConstant.PIPE_MATERIAL_ARRAY, - 0 + this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 ) binding.objectInclude.buryMethodSpinner.show(this, LocaleConstant.BURY_METHOD_ARRAY, 0) @@ -137,14 +132,12 @@ } binding.identifierInclude.identifierTypeSpinner.show( - this, - LocaleConstant.IDENTIFIER_TYPE_ARRAY, - 0 + this, LocaleConstant.IDENTIFIER_TYPE_ARRAY, 0 ) binding.identifierInclude.installTimeView.text = System.currentTimeMillis().timestampToCompleteDate() - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { binding.identifierInclude.lngView.text = location.longitude.toString() @@ -301,8 +294,7 @@ "identifierDeepView".setDefaultValue(binding.identifierInclude.identifierDeepView.text.toString()) "personDeptView".setDefaultValue(binding.identifierInclude.personDeptView.text.toString()) } else { - NoNetworkDialog.Builder() - .setContext(context) + NoNetworkDialog.Builder().setContext(context) .setOnDialogButtonClickListener(object : NoNetworkDialog.OnDialogButtonClickListener { override fun onButtonClick() { @@ -456,9 +448,7 @@ binding.titleInclude.titleView.text = "安装新标识器" binding.titleInclude.titleView.setTextColor(R.color.themeColor.convertColor(this)) - ImmersionBar.with(this) - .statusBarDarkFont(true) - .statusBarColor(R.color.mainBackground) + ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.mainBackground) .init() initLayoutImmersionBar(binding.rootView) } diff --git a/app/src/main/java/com/casic/detector/utils/LocationHub.kt b/app/src/main/java/com/casic/detector/utils/LocationHub.kt index 658d019..d1fed79 100644 --- a/app/src/main/java/com/casic/detector/utils/LocationHub.kt +++ b/app/src/main/java/com/casic/detector/utils/LocationHub.kt @@ -13,15 +13,23 @@ import com.amap.api.services.geocoder.RegeocodeResult import com.casic.detector.callback.IAddressListener import com.casic.detector.callback.ILocationListener +import com.pengxh.kt.lite.base.BaseSingleton -object LocationHub { - private const val kTag = "LocationHub" +class LocationHub private constructor(context: Context) { - fun getCurrentLocation(context: Context, listener: ILocationListener) { - val locationClient = AMapLocationClient(context) + companion object : BaseSingleton() { + override val creator: (Context) -> LocationHub + get() = ::LocationHub + } + + private val kTag = "LocationHub" + private val locationClient by lazy { AMapLocationClient(context) } + private val codeSearch by lazy { GeocodeSearch(context) } + + fun getCurrentLocation(isOnceLocation: Boolean, listener: ILocationListener) { val locationOption = AMapLocationClientOption() locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy - locationOption.isOnceLocation = true + locationOption.isOnceLocation = isOnceLocation locationClient.setLocationOption(locationOption) locationClient.setLocationListener { if (it.errorCode == 0) { @@ -36,9 +44,8 @@ locationClient.startLocation() } - fun antiCodingLocation(context: Context, location: Location, listener: IAddressListener) { + fun antiCodingLocation(location: Location, listener: IAddressListener) { try { - val codeSearch = GeocodeSearch(context) // 第一个参数表示一个LatLonPoint,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 val query = RegeocodeQuery( LatLonPoint(location.latitude, location.longitude), 10f, GeocodeSearch.AMAP @@ -55,4 +62,8 @@ e.printStackTrace() } } + + fun stopLocation() { + locationClient.stopLocation() + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index 7b94099..99905ec 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -53,6 +53,7 @@ private lateinit var soundPool: SoundPool private var soundResId = 0 private val gpioManager by lazy { GpioManager() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var weakReferenceHandler: WeakReferenceHandler override fun initViewBinding(): ActivityInstallLabelBinding { @@ -82,14 +83,10 @@ weakReferenceHandler = WeakReferenceHandler(this) - val audioAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() - soundPool = SoundPool.Builder() - .setMaxStreams(16) - .setAudioAttributes(audioAttributes) - .build() + val audioAttributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build() + soundPool = + SoundPool.Builder().setMaxStreams(16).setAudioAttributes(audioAttributes).build() } override fun initEvent() { @@ -100,9 +97,7 @@ binding.objectInclude.materialSpinner.show(this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0) binding.objectInclude.downPipeTypeSpinner.show(this, LocaleConstant.DOWN_PIPE_TYPE_ARRAY, 0) binding.objectInclude.downPipeMaterialSpinner.show( - this, - LocaleConstant.PIPE_MATERIAL_ARRAY, - 0 + this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 ) binding.objectInclude.buryMethodSpinner.show(this, LocaleConstant.BURY_METHOD_ARRAY, 0) @@ -137,14 +132,12 @@ } binding.identifierInclude.identifierTypeSpinner.show( - this, - LocaleConstant.IDENTIFIER_TYPE_ARRAY, - 0 + this, LocaleConstant.IDENTIFIER_TYPE_ARRAY, 0 ) binding.identifierInclude.installTimeView.text = System.currentTimeMillis().timestampToCompleteDate() - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { binding.identifierInclude.lngView.text = location.longitude.toString() @@ -301,8 +294,7 @@ "identifierDeepView".setDefaultValue(binding.identifierInclude.identifierDeepView.text.toString()) "personDeptView".setDefaultValue(binding.identifierInclude.personDeptView.text.toString()) } else { - NoNetworkDialog.Builder() - .setContext(context) + NoNetworkDialog.Builder().setContext(context) .setOnDialogButtonClickListener(object : NoNetworkDialog.OnDialogButtonClickListener { override fun onButtonClick() { @@ -456,9 +448,7 @@ binding.titleInclude.titleView.text = "安装新标识器" binding.titleInclude.titleView.setTextColor(R.color.themeColor.convertColor(this)) - ImmersionBar.with(this) - .statusBarDarkFont(true) - .statusBarColor(R.color.mainBackground) + ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.mainBackground) .init() initLayoutImmersionBar(binding.rootView) } diff --git a/app/src/main/java/com/casic/detector/view/MainActivity.kt b/app/src/main/java/com/casic/detector/view/MainActivity.kt index dd4a788..7d6b5c0 100644 --- a/app/src/main/java/com/casic/detector/view/MainActivity.kt +++ b/app/src/main/java/com/casic/detector/view/MainActivity.kt @@ -84,6 +84,7 @@ private var latitude: Double = 0.0 private var longitude: Double = 0.0 private val gson by lazy { Gson() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var wakeLock: PowerManager.WakeLock private lateinit var taskBean: TaskBean @@ -330,7 +331,7 @@ longitude = it.longitude latitude = it.latitude //经纬度逆编码 - LocationHub.antiCodingLocation(context, it) { address -> + locationHub.antiCodingLocation(it) { address -> binding.currentLocationView.text = address } } @@ -353,7 +354,7 @@ } private fun moveToCurrentLocation() { - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { aMap.moveCamera( diff --git a/app/src/main/java/com/casic/detector/utils/LocationHub.kt b/app/src/main/java/com/casic/detector/utils/LocationHub.kt index 658d019..d1fed79 100644 --- a/app/src/main/java/com/casic/detector/utils/LocationHub.kt +++ b/app/src/main/java/com/casic/detector/utils/LocationHub.kt @@ -13,15 +13,23 @@ import com.amap.api.services.geocoder.RegeocodeResult import com.casic.detector.callback.IAddressListener import com.casic.detector.callback.ILocationListener +import com.pengxh.kt.lite.base.BaseSingleton -object LocationHub { - private const val kTag = "LocationHub" +class LocationHub private constructor(context: Context) { - fun getCurrentLocation(context: Context, listener: ILocationListener) { - val locationClient = AMapLocationClient(context) + companion object : BaseSingleton() { + override val creator: (Context) -> LocationHub + get() = ::LocationHub + } + + private val kTag = "LocationHub" + private val locationClient by lazy { AMapLocationClient(context) } + private val codeSearch by lazy { GeocodeSearch(context) } + + fun getCurrentLocation(isOnceLocation: Boolean, listener: ILocationListener) { val locationOption = AMapLocationClientOption() locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy - locationOption.isOnceLocation = true + locationOption.isOnceLocation = isOnceLocation locationClient.setLocationOption(locationOption) locationClient.setLocationListener { if (it.errorCode == 0) { @@ -36,9 +44,8 @@ locationClient.startLocation() } - fun antiCodingLocation(context: Context, location: Location, listener: IAddressListener) { + fun antiCodingLocation(location: Location, listener: IAddressListener) { try { - val codeSearch = GeocodeSearch(context) // 第一个参数表示一个LatLonPoint,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 val query = RegeocodeQuery( LatLonPoint(location.latitude, location.longitude), 10f, GeocodeSearch.AMAP @@ -55,4 +62,8 @@ e.printStackTrace() } } + + fun stopLocation() { + locationClient.stopLocation() + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index 7b94099..99905ec 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -53,6 +53,7 @@ private lateinit var soundPool: SoundPool private var soundResId = 0 private val gpioManager by lazy { GpioManager() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var weakReferenceHandler: WeakReferenceHandler override fun initViewBinding(): ActivityInstallLabelBinding { @@ -82,14 +83,10 @@ weakReferenceHandler = WeakReferenceHandler(this) - val audioAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() - soundPool = SoundPool.Builder() - .setMaxStreams(16) - .setAudioAttributes(audioAttributes) - .build() + val audioAttributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build() + soundPool = + SoundPool.Builder().setMaxStreams(16).setAudioAttributes(audioAttributes).build() } override fun initEvent() { @@ -100,9 +97,7 @@ binding.objectInclude.materialSpinner.show(this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0) binding.objectInclude.downPipeTypeSpinner.show(this, LocaleConstant.DOWN_PIPE_TYPE_ARRAY, 0) binding.objectInclude.downPipeMaterialSpinner.show( - this, - LocaleConstant.PIPE_MATERIAL_ARRAY, - 0 + this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 ) binding.objectInclude.buryMethodSpinner.show(this, LocaleConstant.BURY_METHOD_ARRAY, 0) @@ -137,14 +132,12 @@ } binding.identifierInclude.identifierTypeSpinner.show( - this, - LocaleConstant.IDENTIFIER_TYPE_ARRAY, - 0 + this, LocaleConstant.IDENTIFIER_TYPE_ARRAY, 0 ) binding.identifierInclude.installTimeView.text = System.currentTimeMillis().timestampToCompleteDate() - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { binding.identifierInclude.lngView.text = location.longitude.toString() @@ -301,8 +294,7 @@ "identifierDeepView".setDefaultValue(binding.identifierInclude.identifierDeepView.text.toString()) "personDeptView".setDefaultValue(binding.identifierInclude.personDeptView.text.toString()) } else { - NoNetworkDialog.Builder() - .setContext(context) + NoNetworkDialog.Builder().setContext(context) .setOnDialogButtonClickListener(object : NoNetworkDialog.OnDialogButtonClickListener { override fun onButtonClick() { @@ -456,9 +448,7 @@ binding.titleInclude.titleView.text = "安装新标识器" binding.titleInclude.titleView.setTextColor(R.color.themeColor.convertColor(this)) - ImmersionBar.with(this) - .statusBarDarkFont(true) - .statusBarColor(R.color.mainBackground) + ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.mainBackground) .init() initLayoutImmersionBar(binding.rootView) } diff --git a/app/src/main/java/com/casic/detector/view/MainActivity.kt b/app/src/main/java/com/casic/detector/view/MainActivity.kt index dd4a788..7d6b5c0 100644 --- a/app/src/main/java/com/casic/detector/view/MainActivity.kt +++ b/app/src/main/java/com/casic/detector/view/MainActivity.kt @@ -84,6 +84,7 @@ private var latitude: Double = 0.0 private var longitude: Double = 0.0 private val gson by lazy { Gson() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var wakeLock: PowerManager.WakeLock private lateinit var taskBean: TaskBean @@ -330,7 +331,7 @@ longitude = it.longitude latitude = it.latitude //经纬度逆编码 - LocationHub.antiCodingLocation(context, it) { address -> + locationHub.antiCodingLocation(it) { address -> binding.currentLocationView.text = address } } @@ -353,7 +354,7 @@ } private fun moveToCurrentLocation() { - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { aMap.moveCamera( diff --git a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt index dd0c048..4a5eea2 100644 --- a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt @@ -6,12 +6,15 @@ import android.os.Bundle import android.os.Handler import android.os.Message +import android.util.Log import android.view.animation.Animation import android.view.animation.RotateAnimation import androidx.lifecycle.ViewModelProvider +import com.amap.api.location.AMapLocation import com.casic.detector.R import com.casic.detector.base.SerialPortActivity import com.casic.detector.bean.TaskBean +import com.casic.detector.callback.ILocationListener import com.casic.detector.databinding.ActivitySearchLabelBinding import com.casic.detector.extensions.hexToString import com.casic.detector.extensions.initLayoutImmersionBar @@ -20,6 +23,7 @@ import com.casic.detector.utils.GpioManager import com.casic.detector.utils.LoadingDialogHub import com.casic.detector.utils.LocaleConstant +import com.casic.detector.utils.LocationHub import com.casic.detector.vm.TaskViewModel import com.gyf.immersionbar.ImmersionBar import com.pengxh.kt.lite.extensions.navigatePageTo @@ -47,6 +51,7 @@ private val decimal by lazy { DecimalFormat("0.0") } private var identifierId = "" private val lastDegreeMap by lazy { HashMap() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private var isPlaying = false private var isDetectDepth = false private lateinit var taskViewModel: TaskViewModel @@ -59,26 +64,30 @@ weakReferenceHandler = WeakReferenceHandler(this) taskViewModel = ViewModelProvider(this)[TaskViewModel::class.java] - val audioAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() + val audioAttributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build() /** * 声音效果速度 * ling ${location.longitude},${location.latitude}") + //116.265904,39.914204 + } + } + }) + //搜索信号 detectTimer.schedule(object : TimerTask() { override fun run() { @@ -187,9 +196,12 @@ binding.energyValueView.text = decimal.format(degree) val animation = RotateAnimation( - lastDegreeMap["lastDegree"]!!, degree, - Animation.RELATIVE_TO_SELF, 0.5f, - Animation.RELATIVE_TO_SELF, 0.5f + lastDegreeMap["lastDegree"]!!, + degree, + Animation.RELATIVE_TO_SELF, + 0.5f, + Animation.RELATIVE_TO_SELF, + 0.5f ) animation.duration = 0 animation.fillAfter = true @@ -265,11 +277,8 @@ //53转为String为S,代表深度 val depthResponse = hex.substring(0, 10).hexToString() val depth = depthResponse.substring(1).toInt() - AlertMessageDialog.Builder() - .setContext(this) - .setTitle("温馨提示") - .setMessage("标识器埋深:${depth}厘米") - .setPositiveButton("知道了") + AlertMessageDialog.Builder().setContext(this).setTitle("温馨提示") + .setMessage("标识器埋深:${depth}厘米").setPositiveButton("知道了") .setOnDialogButtonClickListener(object : AlertMessageDialog.OnDialogButtonClickListener { override fun onConfirmClick() { @@ -323,5 +332,6 @@ super.onDestroy() gpioManager.setGpioLow("18") detectTimer.cancel() + locationHub.stopLocation() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/utils/LocationHub.kt b/app/src/main/java/com/casic/detector/utils/LocationHub.kt index 658d019..d1fed79 100644 --- a/app/src/main/java/com/casic/detector/utils/LocationHub.kt +++ b/app/src/main/java/com/casic/detector/utils/LocationHub.kt @@ -13,15 +13,23 @@ import com.amap.api.services.geocoder.RegeocodeResult import com.casic.detector.callback.IAddressListener import com.casic.detector.callback.ILocationListener +import com.pengxh.kt.lite.base.BaseSingleton -object LocationHub { - private const val kTag = "LocationHub" +class LocationHub private constructor(context: Context) { - fun getCurrentLocation(context: Context, listener: ILocationListener) { - val locationClient = AMapLocationClient(context) + companion object : BaseSingleton() { + override val creator: (Context) -> LocationHub + get() = ::LocationHub + } + + private val kTag = "LocationHub" + private val locationClient by lazy { AMapLocationClient(context) } + private val codeSearch by lazy { GeocodeSearch(context) } + + fun getCurrentLocation(isOnceLocation: Boolean, listener: ILocationListener) { val locationOption = AMapLocationClientOption() locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy - locationOption.isOnceLocation = true + locationOption.isOnceLocation = isOnceLocation locationClient.setLocationOption(locationOption) locationClient.setLocationListener { if (it.errorCode == 0) { @@ -36,9 +44,8 @@ locationClient.startLocation() } - fun antiCodingLocation(context: Context, location: Location, listener: IAddressListener) { + fun antiCodingLocation(location: Location, listener: IAddressListener) { try { - val codeSearch = GeocodeSearch(context) // 第一个参数表示一个LatLonPoint,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 val query = RegeocodeQuery( LatLonPoint(location.latitude, location.longitude), 10f, GeocodeSearch.AMAP @@ -55,4 +62,8 @@ e.printStackTrace() } } + + fun stopLocation() { + locationClient.stopLocation() + } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt index 7b94099..99905ec 100644 --- a/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/InstallLabelActivity.kt @@ -53,6 +53,7 @@ private lateinit var soundPool: SoundPool private var soundResId = 0 private val gpioManager by lazy { GpioManager() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var weakReferenceHandler: WeakReferenceHandler override fun initViewBinding(): ActivityInstallLabelBinding { @@ -82,14 +83,10 @@ weakReferenceHandler = WeakReferenceHandler(this) - val audioAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() - soundPool = SoundPool.Builder() - .setMaxStreams(16) - .setAudioAttributes(audioAttributes) - .build() + val audioAttributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build() + soundPool = + SoundPool.Builder().setMaxStreams(16).setAudioAttributes(audioAttributes).build() } override fun initEvent() { @@ -100,9 +97,7 @@ binding.objectInclude.materialSpinner.show(this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0) binding.objectInclude.downPipeTypeSpinner.show(this, LocaleConstant.DOWN_PIPE_TYPE_ARRAY, 0) binding.objectInclude.downPipeMaterialSpinner.show( - this, - LocaleConstant.PIPE_MATERIAL_ARRAY, - 0 + this, LocaleConstant.PIPE_MATERIAL_ARRAY, 0 ) binding.objectInclude.buryMethodSpinner.show(this, LocaleConstant.BURY_METHOD_ARRAY, 0) @@ -137,14 +132,12 @@ } binding.identifierInclude.identifierTypeSpinner.show( - this, - LocaleConstant.IDENTIFIER_TYPE_ARRAY, - 0 + this, LocaleConstant.IDENTIFIER_TYPE_ARRAY, 0 ) binding.identifierInclude.installTimeView.text = System.currentTimeMillis().timestampToCompleteDate() - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { binding.identifierInclude.lngView.text = location.longitude.toString() @@ -301,8 +294,7 @@ "identifierDeepView".setDefaultValue(binding.identifierInclude.identifierDeepView.text.toString()) "personDeptView".setDefaultValue(binding.identifierInclude.personDeptView.text.toString()) } else { - NoNetworkDialog.Builder() - .setContext(context) + NoNetworkDialog.Builder().setContext(context) .setOnDialogButtonClickListener(object : NoNetworkDialog.OnDialogButtonClickListener { override fun onButtonClick() { @@ -456,9 +448,7 @@ binding.titleInclude.titleView.text = "安装新标识器" binding.titleInclude.titleView.setTextColor(R.color.themeColor.convertColor(this)) - ImmersionBar.with(this) - .statusBarDarkFont(true) - .statusBarColor(R.color.mainBackground) + ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.mainBackground) .init() initLayoutImmersionBar(binding.rootView) } diff --git a/app/src/main/java/com/casic/detector/view/MainActivity.kt b/app/src/main/java/com/casic/detector/view/MainActivity.kt index dd4a788..7d6b5c0 100644 --- a/app/src/main/java/com/casic/detector/view/MainActivity.kt +++ b/app/src/main/java/com/casic/detector/view/MainActivity.kt @@ -84,6 +84,7 @@ private var latitude: Double = 0.0 private var longitude: Double = 0.0 private val gson by lazy { Gson() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var wakeLock: PowerManager.WakeLock private lateinit var taskBean: TaskBean @@ -330,7 +331,7 @@ longitude = it.longitude latitude = it.latitude //经纬度逆编码 - LocationHub.antiCodingLocation(context, it) { address -> + locationHub.antiCodingLocation(it) { address -> binding.currentLocationView.text = address } } @@ -353,7 +354,7 @@ } private fun moveToCurrentLocation() { - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { aMap.moveCamera( diff --git a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt index dd0c048..4a5eea2 100644 --- a/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt +++ b/app/src/main/java/com/casic/detector/view/SearchLabelActivity.kt @@ -6,12 +6,15 @@ import android.os.Bundle import android.os.Handler import android.os.Message +import android.util.Log import android.view.animation.Animation import android.view.animation.RotateAnimation import androidx.lifecycle.ViewModelProvider +import com.amap.api.location.AMapLocation import com.casic.detector.R import com.casic.detector.base.SerialPortActivity import com.casic.detector.bean.TaskBean +import com.casic.detector.callback.ILocationListener import com.casic.detector.databinding.ActivitySearchLabelBinding import com.casic.detector.extensions.hexToString import com.casic.detector.extensions.initLayoutImmersionBar @@ -20,6 +23,7 @@ import com.casic.detector.utils.GpioManager import com.casic.detector.utils.LoadingDialogHub import com.casic.detector.utils.LocaleConstant +import com.casic.detector.utils.LocationHub import com.casic.detector.vm.TaskViewModel import com.gyf.immersionbar.ImmersionBar import com.pengxh.kt.lite.extensions.navigatePageTo @@ -47,6 +51,7 @@ private val decimal by lazy { DecimalFormat("0.0") } private var identifierId = "" private val lastDegreeMap by lazy { HashMap() } + private val locationHub by lazy { LocationHub.obtainInstance(this) } private var isPlaying = false private var isDetectDepth = false private lateinit var taskViewModel: TaskViewModel @@ -59,26 +64,30 @@ weakReferenceHandler = WeakReferenceHandler(this) taskViewModel = ViewModelProvider(this)[TaskViewModel::class.java] - val audioAttributes = AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_MEDIA) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() + val audioAttributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA) + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build() /** * 声音效果速度 * ling ${location.longitude},${location.latitude}") + //116.265904,39.914204 + } + } + }) + //搜索信号 detectTimer.schedule(object : TimerTask() { override fun run() { @@ -187,9 +196,12 @@ binding.energyValueView.text = decimal.format(degree) val animation = RotateAnimation( - lastDegreeMap["lastDegree"]!!, degree, - Animation.RELATIVE_TO_SELF, 0.5f, - Animation.RELATIVE_TO_SELF, 0.5f + lastDegreeMap["lastDegree"]!!, + degree, + Animation.RELATIVE_TO_SELF, + 0.5f, + Animation.RELATIVE_TO_SELF, + 0.5f ) animation.duration = 0 animation.fillAfter = true @@ -265,11 +277,8 @@ //53转为String为S,代表深度 val depthResponse = hex.substring(0, 10).hexToString() val depth = depthResponse.substring(1).toInt() - AlertMessageDialog.Builder() - .setContext(this) - .setTitle("温馨提示") - .setMessage("标识器埋深:${depth}厘米") - .setPositiveButton("知道了") + AlertMessageDialog.Builder().setContext(this).setTitle("温馨提示") + .setMessage("标识器埋深:${depth}厘米").setPositiveButton("知道了") .setOnDialogButtonClickListener(object : AlertMessageDialog.OnDialogButtonClickListener { override fun onConfirmClick() { @@ -323,5 +332,6 @@ super.onDestroy() gpioManager.setGpioLow("18") detectTimer.cancel() + locationHub.stopLocation() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/view/UploadEventActivity.kt b/app/src/main/java/com/casic/detector/view/UploadEventActivity.kt index a9a7f7f..0118b89 100644 --- a/app/src/main/java/com/casic/detector/view/UploadEventActivity.kt +++ b/app/src/main/java/com/casic/detector/view/UploadEventActivity.kt @@ -33,6 +33,7 @@ class UploadEventActivity : KotlinBaseActivity() { private val context: Context = this@UploadEventActivity + private val locationHub by lazy { LocationHub.obtainInstance(this) } private lateinit var imageAdapter: EditableImageAdapter private val realPaths: ArrayList = ArrayList() //真实图片路径 private lateinit var taskViewModel: TaskViewModel @@ -78,7 +79,7 @@ binding.uploadTimeView.text = System.currentTimeMillis().timestampToCompleteDate() - LocationHub.getCurrentLocation(this, object : ILocationListener { + locationHub.getCurrentLocation(true, object : ILocationListener { override fun onAMapLocationGet(location: AMapLocation?) { if (location != null) { binding.lngView.text = "${location.longitude}"