From d3145d0ed2fc4b5b7754f610c76cbdc3c79e6d82 Mon Sep 17 00:00:00 2001
From: xiaowusky <chenyangyang3858@dingtalk.com>
Date: Tue, 14 Nov 2023 16:16:03 +0800
Subject: [PATCH] =?UTF-8?q?desc:wifi=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../watcher/ui/view/CommonTopBar.kt           |  4 +--
 .../safetywatcher/watcher/utils/LztekUtil.kt  | 10 ++----
 .../safetywatcher/watcher/utils/WifiHelper.kt | 17 ++++++++++
 .../common/commonlib/utils/StorageUtils.kt    | 34 ++++++++++++++++---
 .../library/vlc/encoder/AndroidMuxer.java     | 11 +++---
 .../library/vlc/encoder/CameraHelper.java     | 13 ++++---
 6 files changed, 65 insertions(+), 24 deletions(-)

diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/view/CommonTopBar.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/view/CommonTopBar.kt
index 922d5c4..b0a393b 100644
--- a/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/view/CommonTopBar.kt
+++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/ui/view/CommonTopBar.kt
@@ -75,13 +75,13 @@ class CommonTopBar : LinearLayout {
             if (!enable) {
                 mBinding?.wifi?.visibility = GONE
             } else {
-//                mBinding?.wifi?.visibility = VISIBLE
+                mBinding?.wifi?.visibility = VISIBLE
             }
         }
 
         override fun onLevel(level: Int) {
             Log.i(this@CommonTopBar.javaClass.name, "wifiCallback onLevel = $level")
-            mBinding?.wifi?.visibility = if (level > 0) VISIBLE else GONE
+//            mBinding?.wifi?.visibility = if (level > 0) VISIBLE else GONE
             val showLevel = (level + 1) * 20
             mBinding?.wifi?.setImageLevel(showLevel)
         }
diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/LztekUtil.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/LztekUtil.kt
index d7fe079..33fd227 100644
--- a/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/LztekUtil.kt
+++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/LztekUtil.kt
@@ -67,25 +67,19 @@ object LztekUtil {
         return mLztek?.storageCardPath
     }
 
-    private const val BYTE_GB = 1024 * 1024 * 1024
-
     fun getCardAvailableSizeWeak(): Boolean {
         val context = CommonApplication.getContext()
         var hasNvme = false
         var cardPath = Environment.getExternalStorageDirectory().absolutePath
         context?.let {
-            val nvmePath = StorageUtils.getStoragePath(it, NVME_KEYWORDS)
+            val nvmePath = StorageUtils.getStoragePath(it, false)
             if (!nvmePath.isNullOrEmpty()) {
                 hasNvme = true
                 cardPath = nvmePath
             }
         }
         cardPath?.let {
-            val statfs = StatFs(it)
-            val blocSize = statfs.blockSizeLong
-            val availableSize = statfs.availableBlocksLong * blocSize
-            val totalSize = statfs.blockCountLong * blocSize
-            val gb = (availableSize / BYTE_GB).toInt()
+            val gb = StorageUtils.getAvailableSizeSizeInGB(it)
             return if (hasNvme) gb < STORAGE_WARN_THRESHOLD_NVME else gb < STORAGE_WARN_THRESHOLD
         }
         return false
diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/WifiHelper.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/WifiHelper.kt
index 2073943..82ee64a 100644
--- a/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/WifiHelper.kt
+++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/WifiHelper.kt
@@ -5,6 +5,8 @@ import android.content.Context
 import android.content.Context.WIFI_SERVICE
 import android.content.Intent
 import android.content.IntentFilter
+import android.net.ConnectivityManager
+import android.net.NetworkInfo
 import android.net.wifi.WifiManager
 import android.util.Log
 
@@ -28,6 +30,20 @@ object WifiHelper {
                 callbacks.forEach {
                     it.onLevel(mRssi)
                 }
+            }else if (intent?.action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
+                val networkInfo = intent?.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO) as NetworkInfo?
+                networkInfo?.let {
+                    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
+                        if (networkInfo.detailedState == NetworkInfo.DetailedState.DISCONNECTED){
+                            mWifiState = -1
+                        }else if (networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED){
+                            mWifiState = WifiManager.WIFI_STATE_ENABLED
+                        }
+                        callbacks.forEach {
+                            it.onEnable(mWifiState == WifiManager.WIFI_STATE_ENABLED)
+                        }
+                    }
+                }
             }
             Log.i(
                 this@WifiHelper.javaClass.name,
@@ -49,6 +65,7 @@ object WifiHelper {
     private fun watchWifi(context: Context) {
         val filter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)
         filter.addAction(WifiManager.RSSI_CHANGED_ACTION)
+        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
         context.registerReceiver(receiver, filter)
     }
 
diff --git a/library-common/src/main/java/com/common/commonlib/utils/StorageUtils.kt b/library-common/src/main/java/com/common/commonlib/utils/StorageUtils.kt
index cafad80..e30ea9e 100644
--- a/library-common/src/main/java/com/common/commonlib/utils/StorageUtils.kt
+++ b/library-common/src/main/java/com/common/commonlib/utils/StorageUtils.kt
@@ -1,13 +1,12 @@
 package com.common.commonlib.utils
 
 import android.content.Context
+import android.os.StatFs
 import android.os.storage.StorageManager
 import android.os.storage.StorageVolume
 import java.lang.reflect.InvocationTargetException
 
-
 val USB_KEYWORDS = arrayOf("USB", "U 盘")
-@JvmField
 val NVME_KEYWORDS = arrayOf("新加卷")
 
 object StorageUtils {
@@ -17,7 +16,7 @@ object StorageUtils {
      * @param keyword SD = "内部存储"; EXT = "SD卡"; USB = "U盘"
      * @return
      */
-    fun getStoragePath(mContext: Context, keywords: Array<String> = USB_KEYWORDS): String? {
+    fun getStoragePath(mContext: Context, isUsb: Boolean = true): String? {
         var targetpath: String? = ""
         val mStorageManager = mContext
             .getSystemService(Context.STORAGE_SERVICE) as StorageManager
@@ -34,8 +33,16 @@ object StorageUtils {
                 val storageVolumeElement: StorageVolume = result[i]
                 val userLabel = getUserLabel.invoke(storageVolumeElement) as String
                 val path = getPath.invoke(storageVolumeElement) as String
-                if (keywords.contains(userLabel))
-                    targetpath = path
+                if (isUsb) {
+                    if (USB_KEYWORDS.contains(userLabel)) {
+                        targetpath = path
+                    }
+                } else {
+                    // 大于400GB的也当硬盘看待
+                    if (NVME_KEYWORDS.contains(userLabel) || getSizeInGB(path) > 400) {
+                        targetpath = path
+                    }
+                }
             }
         } catch (e: ClassNotFoundException) {
             e.printStackTrace()
@@ -48,4 +55,21 @@ object StorageUtils {
         }
         return targetpath
     }
+
+    private const val BYTE_GB = 1024 * 1024 * 1024f
+    private fun getSizeInGB(path: String): Int {
+        val statfs = StatFs(path)
+        val blocSize = statfs.blockSizeLong
+        val totalSize = statfs.blockCountLong * blocSize
+        val gb = (totalSize / BYTE_GB).toInt()
+        return gb;
+    }
+
+    fun getAvailableSizeSizeInGB(path: String): Float {
+        val statfs = StatFs(path)
+        val blocSize = statfs.blockSizeLong
+        val availableSize = statfs.availableBlocksLong * blocSize
+        val gb = (availableSize / BYTE_GB)
+        return gb
+    }
 }
\ No newline at end of file
diff --git a/library-push/src/main/java/com/yinuo/library/vlc/encoder/AndroidMuxer.java b/library-push/src/main/java/com/yinuo/library/vlc/encoder/AndroidMuxer.java
index e038a1b..acd711a 100644
--- a/library-push/src/main/java/com/yinuo/library/vlc/encoder/AndroidMuxer.java
+++ b/library-push/src/main/java/com/yinuo/library/vlc/encoder/AndroidMuxer.java
@@ -42,10 +42,12 @@ public class AndroidMuxer {
         try {
             long timeMillis = System.currentTimeMillis();
             File output = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO, timeMillis, DEFAULT_RECORD_DURATION);
-            mCurrentPath = output.getAbsolutePath();
-            LogUtils.v(String.format("startRecording: %s", output));
-            mMuxer = new MediaMuxer(mCurrentPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
-            mStarted = false;
+            if (output != null) {
+                mCurrentPath = output.getAbsolutePath();
+                LogUtils.v(String.format("startRecording: %s", output));
+                mMuxer = new MediaMuxer(mCurrentPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
+                mStarted = false;
+            }
         } catch (IOException e) {
             e.printStackTrace();
         }
@@ -123,6 +125,7 @@ public class AndroidMuxer {
             if (mStartRecordTime > 0 && mStartRecordTime < System.currentTimeMillis() && !TextUtils.isEmpty(mCurrentPath)) {
                 insertToDB(mStartRecordTime, mCurrentPath);
             }
+            mMuxer = null;
         }
     }
 }
diff --git a/library-push/src/main/java/com/yinuo/library/vlc/encoder/CameraHelper.java b/library-push/src/main/java/com/yinuo/library/vlc/encoder/CameraHelper.java
index 3aea6ad..d794e8e 100644
--- a/library-push/src/main/java/com/yinuo/library/vlc/encoder/CameraHelper.java
+++ b/library-push/src/main/java/com/yinuo/library/vlc/encoder/CameraHelper.java
@@ -1,7 +1,5 @@
 package com.yinuo.library.vlc.encoder;
 
-import static com.common.commonlib.utils.StorageUtilsKt.NVME_KEYWORDS;
-
 import android.annotation.TargetApi;
 import android.app.Activity;
 import android.content.Context;
@@ -14,6 +12,7 @@ import android.view.Surface;
 import androidx.annotation.Nullable;
 
 import com.common.commonlib.CommonApplication;
+import com.common.commonlib.utils.LogUtils;
 import com.common.commonlib.utils.StorageUtils;
 
 import java.io.File;
@@ -189,8 +188,7 @@ public class CameraHelper {
      * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
      * is persistent and available to other applications like gallery.
      *
-     * @param type                  Media type. Can be video or image.
-     * @param defaultRecordDuration
+     * @param type Media type. Can be video or image.
      * @return A file object pointing to the newly created file.
      */
     public static File getOutputMediaFile(int type, long time, long duration) {
@@ -199,6 +197,11 @@ public class CameraHelper {
         File mediaStorageDir = getMediaStorageDir();
         if (mediaStorageDir == null) return null;
 
+        Float availableSizeSizeInGB = StorageUtils.INSTANCE.getAvailableSizeSizeInGB(mediaStorageDir.getAbsolutePath());
+        if (availableSizeSizeInGB < 1f) {
+            LogUtils.e("存储空间即将耗尽");
+            return null;
+        }
         // Create a media file name
         SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
         String timeStamp = format.format(time);
@@ -221,7 +224,7 @@ public class CameraHelper {
     private static File getMediaStorageDir() {
         Context context = CommonApplication.Companion.getContext();
         if (context != null) {
-            String nvmePath = StorageUtils.INSTANCE.getStoragePath(context, NVME_KEYWORDS);
+            String nvmePath = StorageUtils.INSTANCE.getStoragePath(context, false);
             if (!TextUtils.isEmpty(nvmePath)) {
                 File mediaStorageDir = new File(nvmePath + File.separator + "video");
                 if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {