From 795810be52adeb500e287017f4b048cb4747e1c0 Mon Sep 17 00:00:00 2001 From: xiaowusky Date: Mon, 29 May 2023 15:50:50 +0800 Subject: [PATCH] =?UTF-8?q?desc:=E7=9B=91=E5=90=ACsim=E5=8D=A1=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=8F=AF=E7=94=A8=E5=8F=8A=E4=BF=A1=E5=8F=B7=E5=BC=BA?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 1 + .../safetywatcher/watcher/HomeActivity.kt | 3 + .../safetywatcher/watcher/utils/SimHelper.kt | 109 ++++++++++++++++++ .../watcher/view/CommonTopBar.kt | 22 ++++ app/src/main/res/layout/layout_topbar.xml | 5 +- buildCommon/commonLibConfig.gradle | 2 +- 6 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/yinuo/safetywatcher/watcher/utils/SimHelper.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 029b143..259138a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ + diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/HomeActivity.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/HomeActivity.kt index a419403..4c5b1e7 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/HomeActivity.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/HomeActivity.kt @@ -7,6 +7,7 @@ import androidx.annotation.RequiresApi import com.yinuo.safetywatcher.databinding.ActivityHomeBinding import com.yinuo.safetywatcher.watcher.base.BaseActivity import com.yinuo.safetywatcher.watcher.utils.BatteryHelper +import com.yinuo.safetywatcher.watcher.utils.SimHelper import com.yinuo.safetywatcher.watcher.utils.WifiHelper class HomeActivity : BaseActivity() { @@ -23,6 +24,7 @@ class HomeActivity : BaseActivity() { override fun initView() { BatteryHelper.init(this@HomeActivity) WifiHelper.init(this@HomeActivity) + SimHelper.init(this@HomeActivity) mBinding.apply { itemSetting.setOnClickListener { startActivity(Intent(this@HomeActivity, SettingActivity::class.java)) @@ -38,5 +40,6 @@ class HomeActivity : BaseActivity() { super.onDestroy() BatteryHelper.release(this@HomeActivity) WifiHelper.release(this@HomeActivity) + SimHelper.release(this@HomeActivity) } } \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/SimHelper.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/SimHelper.kt new file mode 100644 index 0000000..7002f8c --- /dev/null +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/utils/SimHelper.kt @@ -0,0 +1,109 @@ +package com.yinuo.safetywatcher.watcher.utils + +import android.annotation.SuppressLint +import android.app.Service +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.telephony.PhoneStateListener +import android.telephony.SignalStrength +import android.telephony.SubscriptionInfo +import android.telephony.SubscriptionManager +import android.telephony.TelephonyManager +import android.util.Log + + +object SimHelper { + private var callbacks = mutableListOf() + private var mSimState = -1; + private var mRssi = -1; + + private val receiver = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + if (intent?.action.equals("android.intent.action.SIM_STATE_CHANGED")) { + val tm = context!!.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager + mSimState = tm.simState + } + Log.i( + this@SimHelper.javaClass.name, + "onReceive action = ${intent?.action}, mSimState = $mSimState, mRssi = $mRssi" + ) + callbacks.forEach { + it.onLevel( + mSimState == TelephonyManager.SIM_STATE_READY, mRssi + ) + } + } + } + + fun init(context: Context) { + watchSimState(context) + watchSimRssi(context) + } + + private fun watchSimRssi(context: Context) { + val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager + //获取指定卡槽的信号强度 + val info = getNewerSlotSubscriptionInfo(context, 0) + info?.let { + val mTelephonyManagerForSubscriptionId = + tm.createForSubscriptionId(info!!.subscriptionId) + mTelephonyManagerForSubscriptionId.listen(object : PhoneStateListener() { + override fun onSignalStrengthsChanged(signalStrength: SignalStrength) { + super.onSignalStrengthsChanged(signalStrength) + mRssi = signalStrength.level + callbacks.forEach { + it.onLevel( + mSimState == TelephonyManager.SIM_STATE_READY, mRssi + ) + } + } + }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS) + } + } + + private fun watchSimState(context: Context) { + val filter = IntentFilter("android.intent.action.SIM_STATE_CHANGED") + context.registerReceiver(receiver, filter) + } + + fun addCallBack(callBack: OnSimLevelCallback) { + callbacks.add(callBack) + if (mSimState != -1) { + callBack.onLevel(mSimState == TelephonyManager.SIM_STATE_READY, mRssi) + } + } + + fun removeCallback(callBack: OnSimLevelCallback) { + callbacks.remove(callBack) + } + + fun release(context: Context) { + callbacks.clear() + context.unregisterReceiver(receiver) + } + + interface OnSimLevelCallback { + fun onLevel(enable: Boolean, level: Int) + } + + + @SuppressLint("MissingPermission") + @Synchronized + fun getNewerSlotSubscriptionInfo(context: Context, slot: Int): SubscriptionInfo? { + val list = SubscriptionManager.from(context).activeSubscriptionInfoList + if (list != null) { + for (info in list) { + if (slot == info.simSlotIndex) { + Log.i( + this@SimHelper.javaClass.name, + "SlotIndex : " + info.simSlotIndex + " displayName : " + info.displayName + ) + return info + } + } + } + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/view/CommonTopBar.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/view/CommonTopBar.kt index c9a3a33..d2bb3df 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/view/CommonTopBar.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/view/CommonTopBar.kt @@ -9,6 +9,7 @@ import android.widget.LinearLayout import com.yinuo.safetywatcher.R import com.yinuo.safetywatcher.databinding.LayoutTopbarBinding import com.yinuo.safetywatcher.watcher.utils.BatteryHelper +import com.yinuo.safetywatcher.watcher.utils.SimHelper import com.yinuo.safetywatcher.watcher.utils.WifiHelper class CommonTopBar : LinearLayout { @@ -39,6 +40,7 @@ class CommonTopBar : LinearLayout { // watch battery watchBattery() watchWifi() + watchSim() } } @@ -71,6 +73,25 @@ class CommonTopBar : LinearLayout { WifiHelper.addCallBack(wifiCallback) } + + private val simCallback = object : SimHelper.OnSimLevelCallback { + override fun onLevel(enable: Boolean, level: Int) { + Log.i( + this@CommonTopBar.javaClass.name, + "simCallback onEnable = $enable , level = $level" + ) + if (enable) { + mBinding?.fourG?.visibility = VISIBLE + } else { + mBinding?.fourG?.visibility = GONE + } + } + } + + private fun watchSim() { + SimHelper.addCallBack(simCallback) + } + open fun setTitle(title: String) { mBinding?.backArea?.visibility = VISIBLE mBinding?.title?.text = title @@ -80,5 +101,6 @@ class CommonTopBar : LinearLayout { super.onDetachedFromWindow() BatteryHelper.removeCallback(batteryCallback) WifiHelper.removeCallback(wifiCallback) + SimHelper.removeCallback(simCallback) } } \ No newline at end of file diff --git a/app/src/main/res/layout/layout_topbar.xml b/app/src/main/res/layout/layout_topbar.xml index 16c52ce..083675a 100644 --- a/app/src/main/res/layout/layout_topbar.xml +++ b/app/src/main/res/layout/layout_topbar.xml @@ -62,7 +62,10 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/_40dp" - android:layout_marginEnd="@dimen/_36dp" android:src="@mipmap/ic_setting"/> + + \ No newline at end of file diff --git a/buildCommon/commonLibConfig.gradle b/buildCommon/commonLibConfig.gradle index ffb89b5..5ef6240 100644 --- a/buildCommon/commonLibConfig.gradle +++ b/buildCommon/commonLibConfig.gradle @@ -13,7 +13,7 @@ project.ext { versions = [ compileSdkVersion : 33, buildToolsVersion : "33.0.3", - minSdkVersion : 23, + minSdkVersion : 24, targetSdkVersion : 28, versionCode : 1, versionName : "1.0",