diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt index 15accc4..3a098be 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt @@ -6,6 +6,7 @@ import android.net.ConnectivityManager import android.os.Bundle import android.provider.Settings +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import com.casic.app.smartwell.sanxi.R import com.casic.app.smartwell.sanxi.extensions.convertColor @@ -43,14 +44,21 @@ .addAction(ConnectivityManager.CONNECTIVITY_ACTION, object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (!context!!.isNetworkConnected()) { - NoNetworkDialog.Builder() - .setContext(this@BaseActivity) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) - } - }).build().show() + /** + * 捕获用户在有网情况下登录进入APP,后来又处于断网状态会导致闪退的Bug + * */ + try { + NoNetworkDialog.Builder() + .setContext(this@BaseActivity) + .setOnDialogButtonClickListener(object : + NoNetworkDialog.OnDialogButtonClickListener { + override fun onButtonClick() { + startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } } } }) diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt index 15accc4..3a098be 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt @@ -6,6 +6,7 @@ import android.net.ConnectivityManager import android.os.Bundle import android.provider.Settings +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import com.casic.app.smartwell.sanxi.R import com.casic.app.smartwell.sanxi.extensions.convertColor @@ -43,14 +44,21 @@ .addAction(ConnectivityManager.CONNECTIVITY_ACTION, object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (!context!!.isNetworkConnected()) { - NoNetworkDialog.Builder() - .setContext(this@BaseActivity) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) - } - }).build().show() + /** + * 捕获用户在有网情况下登录进入APP,后来又处于断网状态会导致闪退的Bug + * */ + try { + NoNetworkDialog.Builder() + .setContext(this@BaseActivity) + .setOnDialogButtonClickListener(object : + NoNetworkDialog.OnDialogButtonClickListener { + override fun onButtonClick() { + startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } } } }) diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt index e0b3f6a..82995f2 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt @@ -1,7 +1,6 @@ package com.casic.app.smartwell.sanxi.extensions import android.graphics.Color -import android.os.Looper import android.view.Gravity import android.widget.TextView import android.widget.Toast @@ -30,12 +29,6 @@ //将Toast扩展到String,任何线程都可以显示 fun String.show() { - var toastLooper = Looper.myLooper() - if (toastLooper == null) { - Looper.prepare() - toastLooper = Looper.myLooper() - } - val context = BaseApplication.obtainInstance() val toast = Toast(context) val textView = TextView(context) @@ -53,11 +46,6 @@ toast.view = textView toast.duration = Toast.LENGTH_SHORT toast.show() - - if (toastLooper != null) { - Looper.loop() - toastLooper.quit() - } } fun String.isLetterAndDigit(): Boolean { diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt index 15accc4..3a098be 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt @@ -6,6 +6,7 @@ import android.net.ConnectivityManager import android.os.Bundle import android.provider.Settings +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import com.casic.app.smartwell.sanxi.R import com.casic.app.smartwell.sanxi.extensions.convertColor @@ -43,14 +44,21 @@ .addAction(ConnectivityManager.CONNECTIVITY_ACTION, object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (!context!!.isNetworkConnected()) { - NoNetworkDialog.Builder() - .setContext(this@BaseActivity) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) - } - }).build().show() + /** + * 捕获用户在有网情况下登录进入APP,后来又处于断网状态会导致闪退的Bug + * */ + try { + NoNetworkDialog.Builder() + .setContext(this@BaseActivity) + .setOnDialogButtonClickListener(object : + NoNetworkDialog.OnDialogButtonClickListener { + override fun onButtonClick() { + startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } } } }) diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt index e0b3f6a..82995f2 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt @@ -1,7 +1,6 @@ package com.casic.app.smartwell.sanxi.extensions import android.graphics.Color -import android.os.Looper import android.view.Gravity import android.widget.TextView import android.widget.Toast @@ -30,12 +29,6 @@ //将Toast扩展到String,任何线程都可以显示 fun String.show() { - var toastLooper = Looper.myLooper() - if (toastLooper == null) { - Looper.prepare() - toastLooper = Looper.myLooper() - } - val context = BaseApplication.obtainInstance() val toast = Toast(context) val textView = TextView(context) @@ -53,11 +46,6 @@ toast.view = textView toast.duration = Toast.LENGTH_SHORT toast.show() - - if (toastLooper != null) { - Looper.loop() - toastLooper.quit() - } } fun String.isLetterAndDigit(): Boolean { diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 87cab8c..12cdd2d 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,10 +2,12 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log +import android.os.CountDownTimer import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider @@ -20,6 +22,7 @@ import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant +import com.casic.app.smartwell.sanxi.utils.DialogHelper import com.casic.app.smartwell.sanxi.vm.AlarmViewModel import com.casic.app.smartwell.sanxi.vm.DeviceViewModel import com.casic.app.smartwell.sanxi.vm.WellViewModel @@ -42,7 +45,7 @@ import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, - AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { + AMap.OnMarkerClickListener, AMap.InfoWindowAdapter { private val kTag = "HomePageFragment" private lateinit var homeView: View @@ -113,6 +116,8 @@ workOrderViewModel = ViewModelProvider(this).get(WorkOrderViewModel::class.java) //获取所有窨井数据 + httpCountDownTimer.start() + DialogHelper.showLoadingDialog(requireActivity(), "数据加载中,请稍后...") wellViewModel.obtainAllWell() wellViewModel.allWellModel.observe(viewLifecycleOwner, { if (it.code == 200) { @@ -139,10 +144,10 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 @@ -150,7 +155,7 @@ //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) - aMap.animateCamera(cameraUpdate, 3000, null) + aMap.animateCamera(cameraUpdate, 1500, null) } }) @@ -332,8 +337,6 @@ aMap.addOnMarkerClickListener(this) // 点击marker弹出自定义popup aMap.setInfoWindowAdapter(this) - //信息窗点击事件 - aMap.addOnInfoWindowClickListener(this) } override fun onMapLoaded() { @@ -345,9 +348,25 @@ } + /** + * http请求计时器 + * */ + private val httpCountDownTimer = object : CountDownTimer(15 * 1000, 1000) { + override fun onFinish() { + "请求服务器超时,请退出后重试".show() + DialogHelper.dismissLoadingDialog() + } + + override fun onTick(millisUntilFinished: Long) { + + } + } + //获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker override fun onCameraChangeFinish(p0: CameraPosition?) { //地图缩放之后显示聚合点数据 + DialogHelper.dismissLoadingDialog() + httpCountDownTimer.cancel() initClustersMarkers() } @@ -409,15 +428,16 @@ } override fun getInfoWindow(marker: Marker?): View? { + val availableWidth = (SizeUtil.getScreenWidth(requireContext()) * 0.8).toInt() if (infoWindow == null) { infoWindow = LayoutInflater.from(requireContext()).inflate(R.layout.popu_map_info, null) } - val v = infoWindow!! //反射得到popup里面的控件对象 - val wellTypeView = v.findViewById(R.id.wellTypeView) - val wellCodeView = v.findViewById(R.id.wellCodeView) - val ownerShipView = v.findViewById(R.id.ownerShipView) - val locationView = v.findViewById(R.id.locationView) + val wellTypeView = infoWindow!!.findViewById(R.id.wellTypeView) + val wellCodeView = infoWindow!!.findViewById(R.id.wellCodeView) + val ownerShipView = infoWindow!!.findViewById(R.id.ownerShipView) + val locationView = infoWindow!!.findViewById(R.id.locationView) + val navigateView = infoWindow!!.findViewById(R.id.navigateView) //绑定数据 val clickedLatLng = marker?.position!! @@ -428,7 +448,69 @@ wellTypeView.text = String.format("井类型: ${well.wellType.toTypeName()}") wellCodeView.text = String.format("井编号: ${well.wellCode}") ownerShipView.text = String.format("权属单位: ${well.deptid.toDeptName()}") - locationView.text = String.format("详细位置: ${well.position}") + val temp = String.format("详细位置: ${well.position}") + + /** + * 20是xml里面设置的window的内边距 + * */ + val textPaint = ownerShipView.paint + if (textPaint.measureText(temp) <= availableWidth) { + //如果整行宽度在控件可用宽度之内,就不处理了 + locationView.text = temp + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } else { + //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 + val wordWidth = textPaint.measureText(temp, 0, 1) + val index = (availableWidth / wordWidth).toInt() + val strings = temp.split("") + val newTextBuilder = StringBuilder() + for (i in strings.indices) { + if (i == index) { + newTextBuilder.append(strings[i]).append("\n") + } else { + newTextBuilder.append(strings[i]) + } + } + locationView.text = newTextBuilder + //动态设置infoWindow宽高 + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + availableWidth, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + navigateView.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("操作提示") + .setMessage("确定要前往吗") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onConfirmClick() { + val latLng = marker.position + val lat = latLng.latitude.toString() + val lng = latLng.longitude.toString() + if (lat.isBlank() || lng.isBlank()) { + "窨井经纬度异常,无法开启导航".show() + return + } + Poi( + marker.snippet, + LatLng(lat.toDouble(), lng.toDouble()), + "" + ).showRouteOnMap( + requireContext() + ) + } + + override fun onCancelClick() { + + } + }).build().show() + } return infoWindow } } @@ -441,36 +523,6 @@ * */ override fun getInfoContents(p0: Marker?): View? = null - override fun onInfoWindowClick(p0: Marker?) { - if (p0 != null) { - AlertControlDialog.Builder() - .setContext(requireContext()) - .setTitle("操作提示") - .setMessage("确定要前往吗") - .setNegativeButton("取消") - .setPositiveButton("确定") - .setOnDialogButtonClickListener(object : - AlertControlDialog.OnDialogButtonClickListener { - override fun onConfirmClick() { - val latLng = p0.position - val lat = latLng.latitude.toString() - val lng = latLng.longitude.toString() - if (lat.isBlank() || lng.isBlank()) { - "窨井经纬度异常,无法开启导航".show() - return - } - Poi(p0.snippet, LatLng(lat.toDouble(), lng.toDouble()), "").showRouteOnMap( - requireContext() - ) - } - - override fun onCancelClick() { - - } - }).build().show() - } - } - /***以下是地图生命周期管理************************************************************************/ override fun onResume() { super.onResume() diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt index 15accc4..3a098be 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt @@ -6,6 +6,7 @@ import android.net.ConnectivityManager import android.os.Bundle import android.provider.Settings +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import com.casic.app.smartwell.sanxi.R import com.casic.app.smartwell.sanxi.extensions.convertColor @@ -43,14 +44,21 @@ .addAction(ConnectivityManager.CONNECTIVITY_ACTION, object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (!context!!.isNetworkConnected()) { - NoNetworkDialog.Builder() - .setContext(this@BaseActivity) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) - } - }).build().show() + /** + * 捕获用户在有网情况下登录进入APP,后来又处于断网状态会导致闪退的Bug + * */ + try { + NoNetworkDialog.Builder() + .setContext(this@BaseActivity) + .setOnDialogButtonClickListener(object : + NoNetworkDialog.OnDialogButtonClickListener { + override fun onButtonClick() { + startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } } } }) diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt index e0b3f6a..82995f2 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt @@ -1,7 +1,6 @@ package com.casic.app.smartwell.sanxi.extensions import android.graphics.Color -import android.os.Looper import android.view.Gravity import android.widget.TextView import android.widget.Toast @@ -30,12 +29,6 @@ //将Toast扩展到String,任何线程都可以显示 fun String.show() { - var toastLooper = Looper.myLooper() - if (toastLooper == null) { - Looper.prepare() - toastLooper = Looper.myLooper() - } - val context = BaseApplication.obtainInstance() val toast = Toast(context) val textView = TextView(context) @@ -53,11 +46,6 @@ toast.view = textView toast.duration = Toast.LENGTH_SHORT toast.show() - - if (toastLooper != null) { - Looper.loop() - toastLooper.quit() - } } fun String.isLetterAndDigit(): Boolean { diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 87cab8c..12cdd2d 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,10 +2,12 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log +import android.os.CountDownTimer import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider @@ -20,6 +22,7 @@ import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant +import com.casic.app.smartwell.sanxi.utils.DialogHelper import com.casic.app.smartwell.sanxi.vm.AlarmViewModel import com.casic.app.smartwell.sanxi.vm.DeviceViewModel import com.casic.app.smartwell.sanxi.vm.WellViewModel @@ -42,7 +45,7 @@ import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, - AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { + AMap.OnMarkerClickListener, AMap.InfoWindowAdapter { private val kTag = "HomePageFragment" private lateinit var homeView: View @@ -113,6 +116,8 @@ workOrderViewModel = ViewModelProvider(this).get(WorkOrderViewModel::class.java) //获取所有窨井数据 + httpCountDownTimer.start() + DialogHelper.showLoadingDialog(requireActivity(), "数据加载中,请稍后...") wellViewModel.obtainAllWell() wellViewModel.allWellModel.observe(viewLifecycleOwner, { if (it.code == 200) { @@ -139,10 +144,10 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 @@ -150,7 +155,7 @@ //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) - aMap.animateCamera(cameraUpdate, 3000, null) + aMap.animateCamera(cameraUpdate, 1500, null) } }) @@ -332,8 +337,6 @@ aMap.addOnMarkerClickListener(this) // 点击marker弹出自定义popup aMap.setInfoWindowAdapter(this) - //信息窗点击事件 - aMap.addOnInfoWindowClickListener(this) } override fun onMapLoaded() { @@ -345,9 +348,25 @@ } + /** + * http请求计时器 + * */ + private val httpCountDownTimer = object : CountDownTimer(15 * 1000, 1000) { + override fun onFinish() { + "请求服务器超时,请退出后重试".show() + DialogHelper.dismissLoadingDialog() + } + + override fun onTick(millisUntilFinished: Long) { + + } + } + //获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker override fun onCameraChangeFinish(p0: CameraPosition?) { //地图缩放之后显示聚合点数据 + DialogHelper.dismissLoadingDialog() + httpCountDownTimer.cancel() initClustersMarkers() } @@ -409,15 +428,16 @@ } override fun getInfoWindow(marker: Marker?): View? { + val availableWidth = (SizeUtil.getScreenWidth(requireContext()) * 0.8).toInt() if (infoWindow == null) { infoWindow = LayoutInflater.from(requireContext()).inflate(R.layout.popu_map_info, null) } - val v = infoWindow!! //反射得到popup里面的控件对象 - val wellTypeView = v.findViewById(R.id.wellTypeView) - val wellCodeView = v.findViewById(R.id.wellCodeView) - val ownerShipView = v.findViewById(R.id.ownerShipView) - val locationView = v.findViewById(R.id.locationView) + val wellTypeView = infoWindow!!.findViewById(R.id.wellTypeView) + val wellCodeView = infoWindow!!.findViewById(R.id.wellCodeView) + val ownerShipView = infoWindow!!.findViewById(R.id.ownerShipView) + val locationView = infoWindow!!.findViewById(R.id.locationView) + val navigateView = infoWindow!!.findViewById(R.id.navigateView) //绑定数据 val clickedLatLng = marker?.position!! @@ -428,7 +448,69 @@ wellTypeView.text = String.format("井类型: ${well.wellType.toTypeName()}") wellCodeView.text = String.format("井编号: ${well.wellCode}") ownerShipView.text = String.format("权属单位: ${well.deptid.toDeptName()}") - locationView.text = String.format("详细位置: ${well.position}") + val temp = String.format("详细位置: ${well.position}") + + /** + * 20是xml里面设置的window的内边距 + * */ + val textPaint = ownerShipView.paint + if (textPaint.measureText(temp) <= availableWidth) { + //如果整行宽度在控件可用宽度之内,就不处理了 + locationView.text = temp + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } else { + //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 + val wordWidth = textPaint.measureText(temp, 0, 1) + val index = (availableWidth / wordWidth).toInt() + val strings = temp.split("") + val newTextBuilder = StringBuilder() + for (i in strings.indices) { + if (i == index) { + newTextBuilder.append(strings[i]).append("\n") + } else { + newTextBuilder.append(strings[i]) + } + } + locationView.text = newTextBuilder + //动态设置infoWindow宽高 + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + availableWidth, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + navigateView.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("操作提示") + .setMessage("确定要前往吗") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onConfirmClick() { + val latLng = marker.position + val lat = latLng.latitude.toString() + val lng = latLng.longitude.toString() + if (lat.isBlank() || lng.isBlank()) { + "窨井经纬度异常,无法开启导航".show() + return + } + Poi( + marker.snippet, + LatLng(lat.toDouble(), lng.toDouble()), + "" + ).showRouteOnMap( + requireContext() + ) + } + + override fun onCancelClick() { + + } + }).build().show() + } return infoWindow } } @@ -441,36 +523,6 @@ * */ override fun getInfoContents(p0: Marker?): View? = null - override fun onInfoWindowClick(p0: Marker?) { - if (p0 != null) { - AlertControlDialog.Builder() - .setContext(requireContext()) - .setTitle("操作提示") - .setMessage("确定要前往吗") - .setNegativeButton("取消") - .setPositiveButton("确定") - .setOnDialogButtonClickListener(object : - AlertControlDialog.OnDialogButtonClickListener { - override fun onConfirmClick() { - val latLng = p0.position - val lat = latLng.latitude.toString() - val lng = latLng.longitude.toString() - if (lat.isBlank() || lng.isBlank()) { - "窨井经纬度异常,无法开启导航".show() - return - } - Poi(p0.snippet, LatLng(lat.toDouble(), lng.toDouble()), "").showRouteOnMap( - requireContext() - ) - } - - override fun onCancelClick() { - - } - }).build().show() - } - } - /***以下是地图生命周期管理************************************************************************/ override fun onResume() { super.onResume() diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt index e8970c2..266cd18 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt @@ -1,5 +1,6 @@ package com.casic.app.smartwell.sanxi.utils.retrofit +import android.os.Looper import android.util.Log import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.utils.Constant @@ -37,14 +38,19 @@ HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger { override fun log(@NotNull message: String) { Log.d(kTag, ">>>>> $message") - if (message.contains("Exception")) { - "请求服务器异常,请检查网络或者服务器配置".show() + if (message.contains("SocketTimeoutException")) { + /** + * 子线程需要自己管理Looper + * */ + Looper.prepare() + "请求服务器超时,请检查网络或者服务器配置".show() + Looper.loop() } } }) interceptor.setLevel(HttpLoggingInterceptor.Level.BODY) val builder = OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS)// 连接时间:30s超时 + .connectTimeout(15, TimeUnit.SECONDS)// 连接时间:15s超时 .readTimeout(10, TimeUnit.SECONDS)// 读取时间:10s超时 .writeTimeout(10, TimeUnit.SECONDS)// 写入时间:10s超时 return builder.addInterceptor(interceptor).build() diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt index 15accc4..3a098be 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt @@ -6,6 +6,7 @@ import android.net.ConnectivityManager import android.os.Bundle import android.provider.Settings +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import com.casic.app.smartwell.sanxi.R import com.casic.app.smartwell.sanxi.extensions.convertColor @@ -43,14 +44,21 @@ .addAction(ConnectivityManager.CONNECTIVITY_ACTION, object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (!context!!.isNetworkConnected()) { - NoNetworkDialog.Builder() - .setContext(this@BaseActivity) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) - } - }).build().show() + /** + * 捕获用户在有网情况下登录进入APP,后来又处于断网状态会导致闪退的Bug + * */ + try { + NoNetworkDialog.Builder() + .setContext(this@BaseActivity) + .setOnDialogButtonClickListener(object : + NoNetworkDialog.OnDialogButtonClickListener { + override fun onButtonClick() { + startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } } } }) diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt index e0b3f6a..82995f2 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt @@ -1,7 +1,6 @@ package com.casic.app.smartwell.sanxi.extensions import android.graphics.Color -import android.os.Looper import android.view.Gravity import android.widget.TextView import android.widget.Toast @@ -30,12 +29,6 @@ //将Toast扩展到String,任何线程都可以显示 fun String.show() { - var toastLooper = Looper.myLooper() - if (toastLooper == null) { - Looper.prepare() - toastLooper = Looper.myLooper() - } - val context = BaseApplication.obtainInstance() val toast = Toast(context) val textView = TextView(context) @@ -53,11 +46,6 @@ toast.view = textView toast.duration = Toast.LENGTH_SHORT toast.show() - - if (toastLooper != null) { - Looper.loop() - toastLooper.quit() - } } fun String.isLetterAndDigit(): Boolean { diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 87cab8c..12cdd2d 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,10 +2,12 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log +import android.os.CountDownTimer import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider @@ -20,6 +22,7 @@ import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant +import com.casic.app.smartwell.sanxi.utils.DialogHelper import com.casic.app.smartwell.sanxi.vm.AlarmViewModel import com.casic.app.smartwell.sanxi.vm.DeviceViewModel import com.casic.app.smartwell.sanxi.vm.WellViewModel @@ -42,7 +45,7 @@ import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, - AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { + AMap.OnMarkerClickListener, AMap.InfoWindowAdapter { private val kTag = "HomePageFragment" private lateinit var homeView: View @@ -113,6 +116,8 @@ workOrderViewModel = ViewModelProvider(this).get(WorkOrderViewModel::class.java) //获取所有窨井数据 + httpCountDownTimer.start() + DialogHelper.showLoadingDialog(requireActivity(), "数据加载中,请稍后...") wellViewModel.obtainAllWell() wellViewModel.allWellModel.observe(viewLifecycleOwner, { if (it.code == 200) { @@ -139,10 +144,10 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 @@ -150,7 +155,7 @@ //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) - aMap.animateCamera(cameraUpdate, 3000, null) + aMap.animateCamera(cameraUpdate, 1500, null) } }) @@ -332,8 +337,6 @@ aMap.addOnMarkerClickListener(this) // 点击marker弹出自定义popup aMap.setInfoWindowAdapter(this) - //信息窗点击事件 - aMap.addOnInfoWindowClickListener(this) } override fun onMapLoaded() { @@ -345,9 +348,25 @@ } + /** + * http请求计时器 + * */ + private val httpCountDownTimer = object : CountDownTimer(15 * 1000, 1000) { + override fun onFinish() { + "请求服务器超时,请退出后重试".show() + DialogHelper.dismissLoadingDialog() + } + + override fun onTick(millisUntilFinished: Long) { + + } + } + //获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker override fun onCameraChangeFinish(p0: CameraPosition?) { //地图缩放之后显示聚合点数据 + DialogHelper.dismissLoadingDialog() + httpCountDownTimer.cancel() initClustersMarkers() } @@ -409,15 +428,16 @@ } override fun getInfoWindow(marker: Marker?): View? { + val availableWidth = (SizeUtil.getScreenWidth(requireContext()) * 0.8).toInt() if (infoWindow == null) { infoWindow = LayoutInflater.from(requireContext()).inflate(R.layout.popu_map_info, null) } - val v = infoWindow!! //反射得到popup里面的控件对象 - val wellTypeView = v.findViewById(R.id.wellTypeView) - val wellCodeView = v.findViewById(R.id.wellCodeView) - val ownerShipView = v.findViewById(R.id.ownerShipView) - val locationView = v.findViewById(R.id.locationView) + val wellTypeView = infoWindow!!.findViewById(R.id.wellTypeView) + val wellCodeView = infoWindow!!.findViewById(R.id.wellCodeView) + val ownerShipView = infoWindow!!.findViewById(R.id.ownerShipView) + val locationView = infoWindow!!.findViewById(R.id.locationView) + val navigateView = infoWindow!!.findViewById(R.id.navigateView) //绑定数据 val clickedLatLng = marker?.position!! @@ -428,7 +448,69 @@ wellTypeView.text = String.format("井类型: ${well.wellType.toTypeName()}") wellCodeView.text = String.format("井编号: ${well.wellCode}") ownerShipView.text = String.format("权属单位: ${well.deptid.toDeptName()}") - locationView.text = String.format("详细位置: ${well.position}") + val temp = String.format("详细位置: ${well.position}") + + /** + * 20是xml里面设置的window的内边距 + * */ + val textPaint = ownerShipView.paint + if (textPaint.measureText(temp) <= availableWidth) { + //如果整行宽度在控件可用宽度之内,就不处理了 + locationView.text = temp + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } else { + //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 + val wordWidth = textPaint.measureText(temp, 0, 1) + val index = (availableWidth / wordWidth).toInt() + val strings = temp.split("") + val newTextBuilder = StringBuilder() + for (i in strings.indices) { + if (i == index) { + newTextBuilder.append(strings[i]).append("\n") + } else { + newTextBuilder.append(strings[i]) + } + } + locationView.text = newTextBuilder + //动态设置infoWindow宽高 + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + availableWidth, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + navigateView.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("操作提示") + .setMessage("确定要前往吗") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onConfirmClick() { + val latLng = marker.position + val lat = latLng.latitude.toString() + val lng = latLng.longitude.toString() + if (lat.isBlank() || lng.isBlank()) { + "窨井经纬度异常,无法开启导航".show() + return + } + Poi( + marker.snippet, + LatLng(lat.toDouble(), lng.toDouble()), + "" + ).showRouteOnMap( + requireContext() + ) + } + + override fun onCancelClick() { + + } + }).build().show() + } return infoWindow } } @@ -441,36 +523,6 @@ * */ override fun getInfoContents(p0: Marker?): View? = null - override fun onInfoWindowClick(p0: Marker?) { - if (p0 != null) { - AlertControlDialog.Builder() - .setContext(requireContext()) - .setTitle("操作提示") - .setMessage("确定要前往吗") - .setNegativeButton("取消") - .setPositiveButton("确定") - .setOnDialogButtonClickListener(object : - AlertControlDialog.OnDialogButtonClickListener { - override fun onConfirmClick() { - val latLng = p0.position - val lat = latLng.latitude.toString() - val lng = latLng.longitude.toString() - if (lat.isBlank() || lng.isBlank()) { - "窨井经纬度异常,无法开启导航".show() - return - } - Poi(p0.snippet, LatLng(lat.toDouble(), lng.toDouble()), "").showRouteOnMap( - requireContext() - ) - } - - override fun onCancelClick() { - - } - }).build().show() - } - } - /***以下是地图生命周期管理************************************************************************/ override fun onResume() { super.onResume() diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt index e8970c2..266cd18 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt @@ -1,5 +1,6 @@ package com.casic.app.smartwell.sanxi.utils.retrofit +import android.os.Looper import android.util.Log import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.utils.Constant @@ -37,14 +38,19 @@ HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger { override fun log(@NotNull message: String) { Log.d(kTag, ">>>>> $message") - if (message.contains("Exception")) { - "请求服务器异常,请检查网络或者服务器配置".show() + if (message.contains("SocketTimeoutException")) { + /** + * 子线程需要自己管理Looper + * */ + Looper.prepare() + "请求服务器超时,请检查网络或者服务器配置".show() + Looper.loop() } } }) interceptor.setLevel(HttpLoggingInterceptor.Level.BODY) val builder = OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS)// 连接时间:30s超时 + .connectTimeout(15, TimeUnit.SECONDS)// 连接时间:15s超时 .readTimeout(10, TimeUnit.SECONDS)// 读取时间:10s超时 .writeTimeout(10, TimeUnit.SECONDS)// 写入时间:10s超时 return builder.addInterceptor(interceptor).build() diff --git a/app/src/main/res/drawable/ic_navigate.xml b/app/src/main/res/drawable/ic_navigate.xml new file mode 100644 index 0000000..fe7e38a --- /dev/null +++ b/app/src/main/res/drawable/ic_navigate.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt index 15accc4..3a098be 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt @@ -6,6 +6,7 @@ import android.net.ConnectivityManager import android.os.Bundle import android.provider.Settings +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import com.casic.app.smartwell.sanxi.R import com.casic.app.smartwell.sanxi.extensions.convertColor @@ -43,14 +44,21 @@ .addAction(ConnectivityManager.CONNECTIVITY_ACTION, object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (!context!!.isNetworkConnected()) { - NoNetworkDialog.Builder() - .setContext(this@BaseActivity) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) - } - }).build().show() + /** + * 捕获用户在有网情况下登录进入APP,后来又处于断网状态会导致闪退的Bug + * */ + try { + NoNetworkDialog.Builder() + .setContext(this@BaseActivity) + .setOnDialogButtonClickListener(object : + NoNetworkDialog.OnDialogButtonClickListener { + override fun onButtonClick() { + startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } } } }) diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt index e0b3f6a..82995f2 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt @@ -1,7 +1,6 @@ package com.casic.app.smartwell.sanxi.extensions import android.graphics.Color -import android.os.Looper import android.view.Gravity import android.widget.TextView import android.widget.Toast @@ -30,12 +29,6 @@ //将Toast扩展到String,任何线程都可以显示 fun String.show() { - var toastLooper = Looper.myLooper() - if (toastLooper == null) { - Looper.prepare() - toastLooper = Looper.myLooper() - } - val context = BaseApplication.obtainInstance() val toast = Toast(context) val textView = TextView(context) @@ -53,11 +46,6 @@ toast.view = textView toast.duration = Toast.LENGTH_SHORT toast.show() - - if (toastLooper != null) { - Looper.loop() - toastLooper.quit() - } } fun String.isLetterAndDigit(): Boolean { diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 87cab8c..12cdd2d 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,10 +2,12 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log +import android.os.CountDownTimer import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider @@ -20,6 +22,7 @@ import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant +import com.casic.app.smartwell.sanxi.utils.DialogHelper import com.casic.app.smartwell.sanxi.vm.AlarmViewModel import com.casic.app.smartwell.sanxi.vm.DeviceViewModel import com.casic.app.smartwell.sanxi.vm.WellViewModel @@ -42,7 +45,7 @@ import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, - AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { + AMap.OnMarkerClickListener, AMap.InfoWindowAdapter { private val kTag = "HomePageFragment" private lateinit var homeView: View @@ -113,6 +116,8 @@ workOrderViewModel = ViewModelProvider(this).get(WorkOrderViewModel::class.java) //获取所有窨井数据 + httpCountDownTimer.start() + DialogHelper.showLoadingDialog(requireActivity(), "数据加载中,请稍后...") wellViewModel.obtainAllWell() wellViewModel.allWellModel.observe(viewLifecycleOwner, { if (it.code == 200) { @@ -139,10 +144,10 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 @@ -150,7 +155,7 @@ //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) - aMap.animateCamera(cameraUpdate, 3000, null) + aMap.animateCamera(cameraUpdate, 1500, null) } }) @@ -332,8 +337,6 @@ aMap.addOnMarkerClickListener(this) // 点击marker弹出自定义popup aMap.setInfoWindowAdapter(this) - //信息窗点击事件 - aMap.addOnInfoWindowClickListener(this) } override fun onMapLoaded() { @@ -345,9 +348,25 @@ } + /** + * http请求计时器 + * */ + private val httpCountDownTimer = object : CountDownTimer(15 * 1000, 1000) { + override fun onFinish() { + "请求服务器超时,请退出后重试".show() + DialogHelper.dismissLoadingDialog() + } + + override fun onTick(millisUntilFinished: Long) { + + } + } + //获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker override fun onCameraChangeFinish(p0: CameraPosition?) { //地图缩放之后显示聚合点数据 + DialogHelper.dismissLoadingDialog() + httpCountDownTimer.cancel() initClustersMarkers() } @@ -409,15 +428,16 @@ } override fun getInfoWindow(marker: Marker?): View? { + val availableWidth = (SizeUtil.getScreenWidth(requireContext()) * 0.8).toInt() if (infoWindow == null) { infoWindow = LayoutInflater.from(requireContext()).inflate(R.layout.popu_map_info, null) } - val v = infoWindow!! //反射得到popup里面的控件对象 - val wellTypeView = v.findViewById(R.id.wellTypeView) - val wellCodeView = v.findViewById(R.id.wellCodeView) - val ownerShipView = v.findViewById(R.id.ownerShipView) - val locationView = v.findViewById(R.id.locationView) + val wellTypeView = infoWindow!!.findViewById(R.id.wellTypeView) + val wellCodeView = infoWindow!!.findViewById(R.id.wellCodeView) + val ownerShipView = infoWindow!!.findViewById(R.id.ownerShipView) + val locationView = infoWindow!!.findViewById(R.id.locationView) + val navigateView = infoWindow!!.findViewById(R.id.navigateView) //绑定数据 val clickedLatLng = marker?.position!! @@ -428,7 +448,69 @@ wellTypeView.text = String.format("井类型: ${well.wellType.toTypeName()}") wellCodeView.text = String.format("井编号: ${well.wellCode}") ownerShipView.text = String.format("权属单位: ${well.deptid.toDeptName()}") - locationView.text = String.format("详细位置: ${well.position}") + val temp = String.format("详细位置: ${well.position}") + + /** + * 20是xml里面设置的window的内边距 + * */ + val textPaint = ownerShipView.paint + if (textPaint.measureText(temp) <= availableWidth) { + //如果整行宽度在控件可用宽度之内,就不处理了 + locationView.text = temp + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } else { + //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 + val wordWidth = textPaint.measureText(temp, 0, 1) + val index = (availableWidth / wordWidth).toInt() + val strings = temp.split("") + val newTextBuilder = StringBuilder() + for (i in strings.indices) { + if (i == index) { + newTextBuilder.append(strings[i]).append("\n") + } else { + newTextBuilder.append(strings[i]) + } + } + locationView.text = newTextBuilder + //动态设置infoWindow宽高 + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + availableWidth, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + navigateView.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("操作提示") + .setMessage("确定要前往吗") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onConfirmClick() { + val latLng = marker.position + val lat = latLng.latitude.toString() + val lng = latLng.longitude.toString() + if (lat.isBlank() || lng.isBlank()) { + "窨井经纬度异常,无法开启导航".show() + return + } + Poi( + marker.snippet, + LatLng(lat.toDouble(), lng.toDouble()), + "" + ).showRouteOnMap( + requireContext() + ) + } + + override fun onCancelClick() { + + } + }).build().show() + } return infoWindow } } @@ -441,36 +523,6 @@ * */ override fun getInfoContents(p0: Marker?): View? = null - override fun onInfoWindowClick(p0: Marker?) { - if (p0 != null) { - AlertControlDialog.Builder() - .setContext(requireContext()) - .setTitle("操作提示") - .setMessage("确定要前往吗") - .setNegativeButton("取消") - .setPositiveButton("确定") - .setOnDialogButtonClickListener(object : - AlertControlDialog.OnDialogButtonClickListener { - override fun onConfirmClick() { - val latLng = p0.position - val lat = latLng.latitude.toString() - val lng = latLng.longitude.toString() - if (lat.isBlank() || lng.isBlank()) { - "窨井经纬度异常,无法开启导航".show() - return - } - Poi(p0.snippet, LatLng(lat.toDouble(), lng.toDouble()), "").showRouteOnMap( - requireContext() - ) - } - - override fun onCancelClick() { - - } - }).build().show() - } - } - /***以下是地图生命周期管理************************************************************************/ override fun onResume() { super.onResume() diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt index e8970c2..266cd18 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt @@ -1,5 +1,6 @@ package com.casic.app.smartwell.sanxi.utils.retrofit +import android.os.Looper import android.util.Log import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.utils.Constant @@ -37,14 +38,19 @@ HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger { override fun log(@NotNull message: String) { Log.d(kTag, ">>>>> $message") - if (message.contains("Exception")) { - "请求服务器异常,请检查网络或者服务器配置".show() + if (message.contains("SocketTimeoutException")) { + /** + * 子线程需要自己管理Looper + * */ + Looper.prepare() + "请求服务器超时,请检查网络或者服务器配置".show() + Looper.loop() } } }) interceptor.setLevel(HttpLoggingInterceptor.Level.BODY) val builder = OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS)// 连接时间:30s超时 + .connectTimeout(15, TimeUnit.SECONDS)// 连接时间:15s超时 .readTimeout(10, TimeUnit.SECONDS)// 读取时间:10s超时 .writeTimeout(10, TimeUnit.SECONDS)// 写入时间:10s超时 return builder.addInterceptor(interceptor).build() diff --git a/app/src/main/res/drawable/ic_navigate.xml b/app/src/main/res/drawable/ic_navigate.xml new file mode 100644 index 0000000..fe7e38a --- /dev/null +++ b/app/src/main/res/drawable/ic_navigate.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/dialog_no_network.xml b/app/src/main/res/layout/dialog_no_network.xml index aad7f7b..cad8b3d 100644 --- a/app/src/main/res/layout/dialog_no_network.xml +++ b/app/src/main/res/layout/dialog_no_network.xml @@ -41,7 +41,7 @@ android:paddingVertical="@dimen/dp_10" android:text="未发现网络,内容走丢了..." android:textColor="@color/hintTextColor" - android:textSize="@dimen/sp_18" + android:textSize="@dimen/sp_14" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt index 15accc4..3a098be 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/base/BaseActivity.kt @@ -6,6 +6,7 @@ import android.net.ConnectivityManager import android.os.Bundle import android.provider.Settings +import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity import com.casic.app.smartwell.sanxi.R import com.casic.app.smartwell.sanxi.extensions.convertColor @@ -43,14 +44,21 @@ .addAction(ConnectivityManager.CONNECTIVITY_ACTION, object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (!context!!.isNetworkConnected()) { - NoNetworkDialog.Builder() - .setContext(this@BaseActivity) - .setOnDialogButtonClickListener(object : - NoNetworkDialog.OnDialogButtonClickListener { - override fun onButtonClick() { - startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) - } - }).build().show() + /** + * 捕获用户在有网情况下登录进入APP,后来又处于断网状态会导致闪退的Bug + * */ + try { + NoNetworkDialog.Builder() + .setContext(this@BaseActivity) + .setOnDialogButtonClickListener(object : + NoNetworkDialog.OnDialogButtonClickListener { + override fun onButtonClick() { + startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)) + } + }).build().show() + } catch (e: WindowManager.BadTokenException) { + e.printStackTrace() + } } } }) diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt index e0b3f6a..82995f2 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/String.kt @@ -1,7 +1,6 @@ package com.casic.app.smartwell.sanxi.extensions import android.graphics.Color -import android.os.Looper import android.view.Gravity import android.widget.TextView import android.widget.Toast @@ -30,12 +29,6 @@ //将Toast扩展到String,任何线程都可以显示 fun String.show() { - var toastLooper = Looper.myLooper() - if (toastLooper == null) { - Looper.prepare() - toastLooper = Looper.myLooper() - } - val context = BaseApplication.obtainInstance() val toast = Toast(context) val textView = TextView(context) @@ -53,11 +46,6 @@ toast.view = textView toast.duration = Toast.LENGTH_SHORT toast.show() - - if (toastLooper != null) { - Looper.loop() - toastLooper.quit() - } } fun String.isLetterAndDigit(): Boolean { diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 87cab8c..12cdd2d 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,10 +2,12 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log +import android.os.CountDownTimer import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider @@ -20,6 +22,7 @@ import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant +import com.casic.app.smartwell.sanxi.utils.DialogHelper import com.casic.app.smartwell.sanxi.vm.AlarmViewModel import com.casic.app.smartwell.sanxi.vm.DeviceViewModel import com.casic.app.smartwell.sanxi.vm.WellViewModel @@ -42,7 +45,7 @@ import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, - AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { + AMap.OnMarkerClickListener, AMap.InfoWindowAdapter { private val kTag = "HomePageFragment" private lateinit var homeView: View @@ -113,6 +116,8 @@ workOrderViewModel = ViewModelProvider(this).get(WorkOrderViewModel::class.java) //获取所有窨井数据 + httpCountDownTimer.start() + DialogHelper.showLoadingDialog(requireActivity(), "数据加载中,请稍后...") wellViewModel.obtainAllWell() wellViewModel.allWellModel.observe(viewLifecycleOwner, { if (it.code == 200) { @@ -139,10 +144,10 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 @@ -150,7 +155,7 @@ //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) - aMap.animateCamera(cameraUpdate, 3000, null) + aMap.animateCamera(cameraUpdate, 1500, null) } }) @@ -332,8 +337,6 @@ aMap.addOnMarkerClickListener(this) // 点击marker弹出自定义popup aMap.setInfoWindowAdapter(this) - //信息窗点击事件 - aMap.addOnInfoWindowClickListener(this) } override fun onMapLoaded() { @@ -345,9 +348,25 @@ } + /** + * http请求计时器 + * */ + private val httpCountDownTimer = object : CountDownTimer(15 * 1000, 1000) { + override fun onFinish() { + "请求服务器超时,请退出后重试".show() + DialogHelper.dismissLoadingDialog() + } + + override fun onTick(millisUntilFinished: Long) { + + } + } + //获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker override fun onCameraChangeFinish(p0: CameraPosition?) { //地图缩放之后显示聚合点数据 + DialogHelper.dismissLoadingDialog() + httpCountDownTimer.cancel() initClustersMarkers() } @@ -409,15 +428,16 @@ } override fun getInfoWindow(marker: Marker?): View? { + val availableWidth = (SizeUtil.getScreenWidth(requireContext()) * 0.8).toInt() if (infoWindow == null) { infoWindow = LayoutInflater.from(requireContext()).inflate(R.layout.popu_map_info, null) } - val v = infoWindow!! //反射得到popup里面的控件对象 - val wellTypeView = v.findViewById(R.id.wellTypeView) - val wellCodeView = v.findViewById(R.id.wellCodeView) - val ownerShipView = v.findViewById(R.id.ownerShipView) - val locationView = v.findViewById(R.id.locationView) + val wellTypeView = infoWindow!!.findViewById(R.id.wellTypeView) + val wellCodeView = infoWindow!!.findViewById(R.id.wellCodeView) + val ownerShipView = infoWindow!!.findViewById(R.id.ownerShipView) + val locationView = infoWindow!!.findViewById(R.id.locationView) + val navigateView = infoWindow!!.findViewById(R.id.navigateView) //绑定数据 val clickedLatLng = marker?.position!! @@ -428,7 +448,69 @@ wellTypeView.text = String.format("井类型: ${well.wellType.toTypeName()}") wellCodeView.text = String.format("井编号: ${well.wellCode}") ownerShipView.text = String.format("权属单位: ${well.deptid.toDeptName()}") - locationView.text = String.format("详细位置: ${well.position}") + val temp = String.format("详细位置: ${well.position}") + + /** + * 20是xml里面设置的window的内边距 + * */ + val textPaint = ownerShipView.paint + if (textPaint.measureText(temp) <= availableWidth) { + //如果整行宽度在控件可用宽度之内,就不处理了 + locationView.text = temp + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } else { + //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 + val wordWidth = textPaint.measureText(temp, 0, 1) + val index = (availableWidth / wordWidth).toInt() + val strings = temp.split("") + val newTextBuilder = StringBuilder() + for (i in strings.indices) { + if (i == index) { + newTextBuilder.append(strings[i]).append("\n") + } else { + newTextBuilder.append(strings[i]) + } + } + locationView.text = newTextBuilder + //动态设置infoWindow宽高 + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + availableWidth, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + navigateView.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("操作提示") + .setMessage("确定要前往吗") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onConfirmClick() { + val latLng = marker.position + val lat = latLng.latitude.toString() + val lng = latLng.longitude.toString() + if (lat.isBlank() || lng.isBlank()) { + "窨井经纬度异常,无法开启导航".show() + return + } + Poi( + marker.snippet, + LatLng(lat.toDouble(), lng.toDouble()), + "" + ).showRouteOnMap( + requireContext() + ) + } + + override fun onCancelClick() { + + } + }).build().show() + } return infoWindow } } @@ -441,36 +523,6 @@ * */ override fun getInfoContents(p0: Marker?): View? = null - override fun onInfoWindowClick(p0: Marker?) { - if (p0 != null) { - AlertControlDialog.Builder() - .setContext(requireContext()) - .setTitle("操作提示") - .setMessage("确定要前往吗") - .setNegativeButton("取消") - .setPositiveButton("确定") - .setOnDialogButtonClickListener(object : - AlertControlDialog.OnDialogButtonClickListener { - override fun onConfirmClick() { - val latLng = p0.position - val lat = latLng.latitude.toString() - val lng = latLng.longitude.toString() - if (lat.isBlank() || lng.isBlank()) { - "窨井经纬度异常,无法开启导航".show() - return - } - Poi(p0.snippet, LatLng(lat.toDouble(), lng.toDouble()), "").showRouteOnMap( - requireContext() - ) - } - - override fun onCancelClick() { - - } - }).build().show() - } - } - /***以下是地图生命周期管理************************************************************************/ override fun onResume() { super.onResume() diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt index e8970c2..266cd18 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt @@ -1,5 +1,6 @@ package com.casic.app.smartwell.sanxi.utils.retrofit +import android.os.Looper import android.util.Log import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.utils.Constant @@ -37,14 +38,19 @@ HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger { override fun log(@NotNull message: String) { Log.d(kTag, ">>>>> $message") - if (message.contains("Exception")) { - "请求服务器异常,请检查网络或者服务器配置".show() + if (message.contains("SocketTimeoutException")) { + /** + * 子线程需要自己管理Looper + * */ + Looper.prepare() + "请求服务器超时,请检查网络或者服务器配置".show() + Looper.loop() } } }) interceptor.setLevel(HttpLoggingInterceptor.Level.BODY) val builder = OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS)// 连接时间:30s超时 + .connectTimeout(15, TimeUnit.SECONDS)// 连接时间:15s超时 .readTimeout(10, TimeUnit.SECONDS)// 读取时间:10s超时 .writeTimeout(10, TimeUnit.SECONDS)// 写入时间:10s超时 return builder.addInterceptor(interceptor).build() diff --git a/app/src/main/res/drawable/ic_navigate.xml b/app/src/main/res/drawable/ic_navigate.xml new file mode 100644 index 0000000..fe7e38a --- /dev/null +++ b/app/src/main/res/drawable/ic_navigate.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/dialog_no_network.xml b/app/src/main/res/layout/dialog_no_network.xml index aad7f7b..cad8b3d 100644 --- a/app/src/main/res/layout/dialog_no_network.xml +++ b/app/src/main/res/layout/dialog_no_network.xml @@ -41,7 +41,7 @@ android:paddingVertical="@dimen/dp_10" android:text="未发现网络,内容走丢了..." android:textColor="@color/hintTextColor" - android:textSize="@dimen/sp_18" + android:textSize="@dimen/sp_14" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" diff --git a/app/src/main/res/layout/popu_map_info.xml b/app/src/main/res/layout/popu_map_info.xml index 7755ff3..6b92ae1 100644 --- a/app/src/main/res/layout/popu_map_info.xml +++ b/app/src/main/res/layout/popu_map_info.xml @@ -1,15 +1,30 @@ - + + + + + + [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 @@ -150,7 +155,7 @@ //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) - aMap.animateCamera(cameraUpdate, 3000, null) + aMap.animateCamera(cameraUpdate, 1500, null) } }) @@ -332,8 +337,6 @@ aMap.addOnMarkerClickListener(this) // 点击marker弹出自定义popup aMap.setInfoWindowAdapter(this) - //信息窗点击事件 - aMap.addOnInfoWindowClickListener(this) } override fun onMapLoaded() { @@ -345,9 +348,25 @@ } + /** + * http请求计时器 + * */ + private val httpCountDownTimer = object : CountDownTimer(15 * 1000, 1000) { + override fun onFinish() { + "请求服务器超时,请退出后重试".show() + DialogHelper.dismissLoadingDialog() + } + + override fun onTick(millisUntilFinished: Long) { + + } + } + //获取视野内的marker 根据聚合算法合成自定义的marker 显示视野内的marker override fun onCameraChangeFinish(p0: CameraPosition?) { //地图缩放之后显示聚合点数据 + DialogHelper.dismissLoadingDialog() + httpCountDownTimer.cancel() initClustersMarkers() } @@ -409,15 +428,16 @@ } override fun getInfoWindow(marker: Marker?): View? { + val availableWidth = (SizeUtil.getScreenWidth(requireContext()) * 0.8).toInt() if (infoWindow == null) { infoWindow = LayoutInflater.from(requireContext()).inflate(R.layout.popu_map_info, null) } - val v = infoWindow!! //反射得到popup里面的控件对象 - val wellTypeView = v.findViewById(R.id.wellTypeView) - val wellCodeView = v.findViewById(R.id.wellCodeView) - val ownerShipView = v.findViewById(R.id.ownerShipView) - val locationView = v.findViewById(R.id.locationView) + val wellTypeView = infoWindow!!.findViewById(R.id.wellTypeView) + val wellCodeView = infoWindow!!.findViewById(R.id.wellCodeView) + val ownerShipView = infoWindow!!.findViewById(R.id.ownerShipView) + val locationView = infoWindow!!.findViewById(R.id.locationView) + val navigateView = infoWindow!!.findViewById(R.id.navigateView) //绑定数据 val clickedLatLng = marker?.position!! @@ -428,7 +448,69 @@ wellTypeView.text = String.format("井类型: ${well.wellType.toTypeName()}") wellCodeView.text = String.format("井编号: ${well.wellCode}") ownerShipView.text = String.format("权属单位: ${well.deptid.toDeptName()}") - locationView.text = String.format("详细位置: ${well.position}") + val temp = String.format("详细位置: ${well.position}") + + /** + * 20是xml里面设置的window的内边距 + * */ + val textPaint = ownerShipView.paint + if (textPaint.measureText(temp) <= availableWidth) { + //如果整行宽度在控件可用宽度之内,就不处理了 + locationView.text = temp + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } else { + //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 + val wordWidth = textPaint.measureText(temp, 0, 1) + val index = (availableWidth / wordWidth).toInt() + val strings = temp.split("") + val newTextBuilder = StringBuilder() + for (i in strings.indices) { + if (i == index) { + newTextBuilder.append(strings[i]).append("\n") + } else { + newTextBuilder.append(strings[i]) + } + } + locationView.text = newTextBuilder + //动态设置infoWindow宽高 + infoWindow!!.layoutParams = LinearLayout.LayoutParams( + availableWidth, ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + navigateView.setOnClickListener { + AlertControlDialog.Builder() + .setContext(requireContext()) + .setTitle("操作提示") + .setMessage("确定要前往吗") + .setNegativeButton("取消") + .setPositiveButton("确定") + .setOnDialogButtonClickListener(object : + AlertControlDialog.OnDialogButtonClickListener { + override fun onConfirmClick() { + val latLng = marker.position + val lat = latLng.latitude.toString() + val lng = latLng.longitude.toString() + if (lat.isBlank() || lng.isBlank()) { + "窨井经纬度异常,无法开启导航".show() + return + } + Poi( + marker.snippet, + LatLng(lat.toDouble(), lng.toDouble()), + "" + ).showRouteOnMap( + requireContext() + ) + } + + override fun onCancelClick() { + + } + }).build().show() + } return infoWindow } } @@ -441,36 +523,6 @@ * */ override fun getInfoContents(p0: Marker?): View? = null - override fun onInfoWindowClick(p0: Marker?) { - if (p0 != null) { - AlertControlDialog.Builder() - .setContext(requireContext()) - .setTitle("操作提示") - .setMessage("确定要前往吗") - .setNegativeButton("取消") - .setPositiveButton("确定") - .setOnDialogButtonClickListener(object : - AlertControlDialog.OnDialogButtonClickListener { - override fun onConfirmClick() { - val latLng = p0.position - val lat = latLng.latitude.toString() - val lng = latLng.longitude.toString() - if (lat.isBlank() || lng.isBlank()) { - "窨井经纬度异常,无法开启导航".show() - return - } - Poi(p0.snippet, LatLng(lat.toDouble(), lng.toDouble()), "").showRouteOnMap( - requireContext() - ) - } - - override fun onCancelClick() { - - } - }).build().show() - } - } - /***以下是地图生命周期管理************************************************************************/ override fun onResume() { super.onResume() diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt index e8970c2..266cd18 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitFactory.kt @@ -1,5 +1,6 @@ package com.casic.app.smartwell.sanxi.utils.retrofit +import android.os.Looper import android.util.Log import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.utils.Constant @@ -37,14 +38,19 @@ HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger { override fun log(@NotNull message: String) { Log.d(kTag, ">>>>> $message") - if (message.contains("Exception")) { - "请求服务器异常,请检查网络或者服务器配置".show() + if (message.contains("SocketTimeoutException")) { + /** + * 子线程需要自己管理Looper + * */ + Looper.prepare() + "请求服务器超时,请检查网络或者服务器配置".show() + Looper.loop() } } }) interceptor.setLevel(HttpLoggingInterceptor.Level.BODY) val builder = OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS)// 连接时间:30s超时 + .connectTimeout(15, TimeUnit.SECONDS)// 连接时间:15s超时 .readTimeout(10, TimeUnit.SECONDS)// 读取时间:10s超时 .writeTimeout(10, TimeUnit.SECONDS)// 写入时间:10s超时 return builder.addInterceptor(interceptor).build() diff --git a/app/src/main/res/drawable/ic_navigate.xml b/app/src/main/res/drawable/ic_navigate.xml new file mode 100644 index 0000000..fe7e38a --- /dev/null +++ b/app/src/main/res/drawable/ic_navigate.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/dialog_no_network.xml b/app/src/main/res/layout/dialog_no_network.xml index aad7f7b..cad8b3d 100644 --- a/app/src/main/res/layout/dialog_no_network.xml +++ b/app/src/main/res/layout/dialog_no_network.xml @@ -41,7 +41,7 @@ android:paddingVertical="@dimen/dp_10" android:text="未发现网络,内容走丢了..." android:textColor="@color/hintTextColor" - android:textSize="@dimen/sp_18" + android:textSize="@dimen/sp_14" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" diff --git a/app/src/main/res/layout/popu_map_info.xml b/app/src/main/res/layout/popu_map_info.xml index 7755ff3..6b92ae1 100644 --- a/app/src/main/res/layout/popu_map_info.xml +++ b/app/src/main/res/layout/popu_map_info.xml @@ -1,15 +1,30 @@ - + + + + + +