Forráskód Böngészése

优化电量显示

weizhengliang 2 éve
szülő
commit
cbeeaec7cb

+ 46 - 8
home/src/main/code/com/wdkl/ncs/android/component/home/activity/WatchHome2Activity.kt

@@ -6,6 +6,7 @@ import android.content.Intent
 import android.content.IntentFilter
 import android.graphics.Color
 import android.net.Uri
+import android.os.BatteryManager
 import android.os.Build
 import android.os.Bundle
 import android.os.CountDownTimer
@@ -61,6 +62,7 @@ import kotlinx.android.synthetic.main.watch_activity_register.*
 import org.greenrobot.eventbus.Subscribe
 import org.greenrobot.eventbus.ThreadMode
 
+
 //@Router(path = "/watch/home")
 class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivityHome2Binding>(), WatchHomeActivityContract.View, View.OnClickListener {
 
@@ -86,8 +88,10 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
         if (!isTaskRoot) {
             finish()
         }
-        loadingDialog = CommonTool.createLoadingDialog(this, R.layout.custom_loading, R.id.loadding_image)
-        netOffLoadingDialog = CommonTool.createLoadingDialog(this, R.layout.netoff_loading, R.id.netoff_loading_image)
+        loadingDialog =
+            CommonTool.createLoadingDialog(this, R.layout.custom_loading, R.id.loadding_image)
+        netOffLoadingDialog =
+            CommonTool.createLoadingDialog(this, R.layout.netoff_loading, R.id.netoff_loading_image)
 
         super.onCreate(savedInstanceState)
     }
@@ -117,7 +121,8 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
         initCountDownTimer()
 
         //网络强度监听
-        teleManager = (applicationContext.getSystemService(Context.TELEPHONY_SERVICE)) as TelephonyManager
+        teleManager =
+            (applicationContext.getSystemService(Context.TELEPHONY_SERVICE)) as TelephonyManager
         netType = NetHelper.getInstance().getNetworkState(applicationContext)
         Log.i(TAG, "网络类型:" + netType)
         /*
@@ -131,7 +136,9 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             if (teleManager != null) {
                 teleManager.listen(object : PhoneStateListener() {
                     override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
-                        if (NetHelper.getInstance().getNetworkState(applicationContext) == NetHelper.NETWORK_NONE) {
+                        if (NetHelper.getInstance()
+                                .getNetworkState(applicationContext) == NetHelper.NETWORK_NONE
+                        ) {
                             tv_signal_strength.text = "网络断开"
                         } else {
                             val signalinfo = signalStrength.toString()
@@ -171,8 +178,9 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
         if (Build.VERSION.SDK_INT >= 23) {
             if (!Settings.canDrawOverlays(this)) {
                 val intent = Intent(
-                        Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
-                        Uri.parse("package:$packageName"))
+                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
+                    Uri.parse("package:$packageName")
+                )
                 startActivityForResult(intent, 10)
             }
         }
@@ -640,6 +648,8 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             onTcpConnectFailed()
         }
 
+        updatePower()
+
         Constants.allowVoiceMsg = true
         tv_im_count.text = "" + WdKeepAliveService.channelImList.size
 
@@ -745,6 +755,32 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
         //super.onBackPressed()
     }
 
+    private fun getPower(): Int {
+        val mBatteryManager = getSystemService(BATTERY_SERVICE) as BatteryManager
+        val power = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
+        Log.d(TAG, "power energy: power percent: $power")
+
+        return power
+    }
+
+    private fun updatePower() {
+        val power = getPower()
+        if (10 < power && power < 20) {
+            electric_quantity_tv.text = "" + power
+            battery_warning_tv.setTextColor(Color.parseColor("#FF3030"))
+            battery_warning_tv.visibility = View.VISIBLE
+            battery_warning_tv.text = "低电量,请注意充电"
+        } else if (10 > power) {
+            electric_quantity_tv.text = "" + power
+            battery_warning_tv.setTextColor(Color.parseColor("#8B2323"))
+            battery_warning_tv.visibility = View.VISIBLE
+            battery_warning_tv.text = "电量过低,请充电"
+        } else {
+            battery_warning_tv.visibility = View.GONE
+            electric_quantity_tv.text = "" + power
+        }
+    }
+
     override fun onDestroy() {
         super.onDestroy()
         if (loadingDialog != null) {
@@ -762,7 +798,9 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             list_item_img_icon_num.text = "" + message.size
         } else if (messageEvent.tag == Constants.EVENT_BATTERY_PERCENT) {
             var message = messageEvent.getMessage() as Int
-            if (10 < message && message < 20) {
+            updatePower()
+
+            /*if (10 < message && message < 20) {
                 electric_quantity_tv.text = "" + message
                 battery_warning_tv.setTextColor(Color.parseColor("#FF3030"))
                 battery_warning_tv.visibility = View.VISIBLE
@@ -775,7 +813,7 @@ class WatchHome2Activity : BaseActivity<WatchHomeActivityPresenter, WatchActivit
             } else {
                 battery_warning_tv.visibility = View.GONE
                 electric_quantity_tv.text = "" + message
-            }
+            }*/
         } else if (messageEvent.tag == Constants.EVENT_RTC_STATE) {
             runOnUiThread(Runnable {
                 sip_state_tv.setBackgroundColor(Color.parseColor("#00FFFF"))