desc:wifi优化

main
xiaowusky 1 year ago
parent 945705b211
commit d3145d0ed2

@ -75,13 +75,13 @@ class CommonTopBar : LinearLayout {
if (!enable) { if (!enable) {
mBinding?.wifi?.visibility = GONE mBinding?.wifi?.visibility = GONE
} else { } else {
// mBinding?.wifi?.visibility = VISIBLE mBinding?.wifi?.visibility = VISIBLE
} }
} }
override fun onLevel(level: Int) { override fun onLevel(level: Int) {
Log.i(this@CommonTopBar.javaClass.name, "wifiCallback onLevel = $level") 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 val showLevel = (level + 1) * 20
mBinding?.wifi?.setImageLevel(showLevel) mBinding?.wifi?.setImageLevel(showLevel)
} }

@ -67,25 +67,19 @@ object LztekUtil {
return mLztek?.storageCardPath return mLztek?.storageCardPath
} }
private const val BYTE_GB = 1024 * 1024 * 1024
fun getCardAvailableSizeWeak(): Boolean { fun getCardAvailableSizeWeak(): Boolean {
val context = CommonApplication.getContext() val context = CommonApplication.getContext()
var hasNvme = false var hasNvme = false
var cardPath = Environment.getExternalStorageDirectory().absolutePath var cardPath = Environment.getExternalStorageDirectory().absolutePath
context?.let { context?.let {
val nvmePath = StorageUtils.getStoragePath(it, NVME_KEYWORDS) val nvmePath = StorageUtils.getStoragePath(it, false)
if (!nvmePath.isNullOrEmpty()) { if (!nvmePath.isNullOrEmpty()) {
hasNvme = true hasNvme = true
cardPath = nvmePath cardPath = nvmePath
} }
} }
cardPath?.let { cardPath?.let {
val statfs = StatFs(it) val gb = StorageUtils.getAvailableSizeSizeInGB(it)
val blocSize = statfs.blockSizeLong
val availableSize = statfs.availableBlocksLong * blocSize
val totalSize = statfs.blockCountLong * blocSize
val gb = (availableSize / BYTE_GB).toInt()
return if (hasNvme) gb < STORAGE_WARN_THRESHOLD_NVME else gb < STORAGE_WARN_THRESHOLD return if (hasNvme) gb < STORAGE_WARN_THRESHOLD_NVME else gb < STORAGE_WARN_THRESHOLD
} }
return false return false

@ -5,6 +5,8 @@ import android.content.Context
import android.content.Context.WIFI_SERVICE import android.content.Context.WIFI_SERVICE
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.util.Log import android.util.Log
@ -28,6 +30,20 @@ object WifiHelper {
callbacks.forEach { callbacks.forEach {
it.onLevel(mRssi) 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( Log.i(
this@WifiHelper.javaClass.name, this@WifiHelper.javaClass.name,
@ -49,6 +65,7 @@ object WifiHelper {
private fun watchWifi(context: Context) { private fun watchWifi(context: Context) {
val filter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION) val filter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)
filter.addAction(WifiManager.RSSI_CHANGED_ACTION) filter.addAction(WifiManager.RSSI_CHANGED_ACTION)
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
context.registerReceiver(receiver, filter) context.registerReceiver(receiver, filter)
} }

@ -1,13 +1,12 @@
package com.common.commonlib.utils package com.common.commonlib.utils
import android.content.Context import android.content.Context
import android.os.StatFs
import android.os.storage.StorageManager import android.os.storage.StorageManager
import android.os.storage.StorageVolume import android.os.storage.StorageVolume
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
val USB_KEYWORDS = arrayOf("USB", "U 盘") val USB_KEYWORDS = arrayOf("USB", "U 盘")
@JvmField
val NVME_KEYWORDS = arrayOf("新加卷") val NVME_KEYWORDS = arrayOf("新加卷")
object StorageUtils { object StorageUtils {
@ -17,7 +16,7 @@ object StorageUtils {
* @param keyword SD = "内部存储"; EXT = "SD卡"; USB = "U盘" * @param keyword SD = "内部存储"; EXT = "SD卡"; USB = "U盘"
* @return * @return
*/ */
fun getStoragePath(mContext: Context, keywords: Array<String> = USB_KEYWORDS): String? { fun getStoragePath(mContext: Context, isUsb: Boolean = true): String? {
var targetpath: String? = "" var targetpath: String? = ""
val mStorageManager = mContext val mStorageManager = mContext
.getSystemService(Context.STORAGE_SERVICE) as StorageManager .getSystemService(Context.STORAGE_SERVICE) as StorageManager
@ -34,8 +33,16 @@ object StorageUtils {
val storageVolumeElement: StorageVolume = result[i] val storageVolumeElement: StorageVolume = result[i]
val userLabel = getUserLabel.invoke(storageVolumeElement) as String val userLabel = getUserLabel.invoke(storageVolumeElement) as String
val path = getPath.invoke(storageVolumeElement) as String val path = getPath.invoke(storageVolumeElement) as String
if (keywords.contains(userLabel)) if (isUsb) {
targetpath = path if (USB_KEYWORDS.contains(userLabel)) {
targetpath = path
}
} else {
// 大于400GB的也当硬盘看待
if (NVME_KEYWORDS.contains(userLabel) || getSizeInGB(path) > 400) {
targetpath = path
}
}
} }
} catch (e: ClassNotFoundException) { } catch (e: ClassNotFoundException) {
e.printStackTrace() e.printStackTrace()
@ -48,4 +55,21 @@ object StorageUtils {
} }
return targetpath 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
}
} }

@ -42,10 +42,12 @@ public class AndroidMuxer {
try { try {
long timeMillis = System.currentTimeMillis(); long timeMillis = System.currentTimeMillis();
File output = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO, timeMillis, DEFAULT_RECORD_DURATION); File output = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO, timeMillis, DEFAULT_RECORD_DURATION);
mCurrentPath = output.getAbsolutePath(); if (output != null) {
LogUtils.v(String.format("startRecording: %s", output)); mCurrentPath = output.getAbsolutePath();
mMuxer = new MediaMuxer(mCurrentPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); LogUtils.v(String.format("startRecording: %s", output));
mStarted = false; mMuxer = new MediaMuxer(mCurrentPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
mStarted = false;
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -123,6 +125,7 @@ public class AndroidMuxer {
if (mStartRecordTime > 0 && mStartRecordTime < System.currentTimeMillis() && !TextUtils.isEmpty(mCurrentPath)) { if (mStartRecordTime > 0 && mStartRecordTime < System.currentTimeMillis() && !TextUtils.isEmpty(mCurrentPath)) {
insertToDB(mStartRecordTime, mCurrentPath); insertToDB(mStartRecordTime, mCurrentPath);
} }
mMuxer = null;
} }
} }
} }

@ -1,7 +1,5 @@
package com.yinuo.library.vlc.encoder; package com.yinuo.library.vlc.encoder;
import static com.common.commonlib.utils.StorageUtilsKt.NVME_KEYWORDS;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@ -14,6 +12,7 @@ import android.view.Surface;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.common.commonlib.CommonApplication; import com.common.commonlib.CommonApplication;
import com.common.commonlib.utils.LogUtils;
import com.common.commonlib.utils.StorageUtils; import com.common.commonlib.utils.StorageUtils;
import java.io.File; import java.io.File;
@ -189,8 +188,7 @@ public class CameraHelper {
* Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
* is persistent and available to other applications like gallery. * is persistent and available to other applications like gallery.
* *
* @param type Media type. Can be video or image. * @param type Media type. Can be video or image.
* @param defaultRecordDuration
* @return A file object pointing to the newly created file. * @return A file object pointing to the newly created file.
*/ */
public static File getOutputMediaFile(int type, long time, long duration) { public static File getOutputMediaFile(int type, long time, long duration) {
@ -199,6 +197,11 @@ public class CameraHelper {
File mediaStorageDir = getMediaStorageDir(); File mediaStorageDir = getMediaStorageDir();
if (mediaStorageDir == null) return null; if (mediaStorageDir == null) return null;
Float availableSizeSizeInGB = StorageUtils.INSTANCE.getAvailableSizeSizeInGB(mediaStorageDir.getAbsolutePath());
if (availableSizeSizeInGB < 1f) {
LogUtils.e("存储空间即将耗尽");
return null;
}
// Create a media file name // Create a media file name
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
String timeStamp = format.format(time); String timeStamp = format.format(time);
@ -221,7 +224,7 @@ public class CameraHelper {
private static File getMediaStorageDir() { private static File getMediaStorageDir() {
Context context = CommonApplication.Companion.getContext(); Context context = CommonApplication.Companion.getContext();
if (context != null) { if (context != null) {
String nvmePath = StorageUtils.INSTANCE.getStoragePath(context, NVME_KEYWORDS); String nvmePath = StorageUtils.INSTANCE.getStoragePath(context, false);
if (!TextUtils.isEmpty(nvmePath)) { if (!TextUtils.isEmpty(nvmePath)) {
File mediaStorageDir = new File(nvmePath + File.separator + "video"); File mediaStorageDir = new File(nvmePath + File.separator + "video");
if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) { if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {

Loading…
Cancel
Save