新增:来电提醒(响铃立即转发,无卡槽信息) #213

This commit is contained in:
pppscn 2022-08-29 23:26:37 +08:00
parent e0d358a16b
commit 242fb332bd
9 changed files with 98 additions and 56 deletions

View File

@ -13,7 +13,7 @@ data class CallInfo(
var dateLong: Long = 0L,
//获取通话时长,值为多少秒
var duration: Int = 0,
//通话类型1=呼入, 2=呼出, 3=未接
//通话类型1=呼入, 2=呼出, 3=未接, 4=未接提醒
var type: Int = 1,
//被呼号码
@SerializedName("via_number")

View File

@ -78,7 +78,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
//转发短信广播
switchEnableSms(binding!!.sbEnableSms)
//转发通话记录
switchEnablePhone(binding!!.sbEnablePhone, binding!!.scbCallType1, binding!!.scbCallType2, binding!!.scbCallType3)
switchEnablePhone(binding!!.sbEnablePhone, binding!!.scbCallType1, binding!!.scbCallType2, binding!!.scbCallType3, binding!!.scbCallType4)
//转发应用通知
switchEnableAppNotify(binding!!.sbEnableAppNotify, binding!!.scbCancelAppNotify, binding!!.scbNotUserPresent)
//启动时异步获取已安装App信息
@ -266,13 +266,14 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
//转发通话
@SuppressLint("UseSwitchCompatOrMaterialCode")
fun switchEnablePhone(sbEnablePhone: SwitchButton, scbCallType1: SmoothCheckBox, scbCallType2: SmoothCheckBox, scbCallType3: SmoothCheckBox) {
fun switchEnablePhone(sbEnablePhone: SwitchButton, scbCallType1: SmoothCheckBox, scbCallType2: SmoothCheckBox, scbCallType3: SmoothCheckBox, scbCallType4: SmoothCheckBox) {
sbEnablePhone.isChecked = SettingUtils.enablePhone
scbCallType1.isChecked = SettingUtils.enableCallType1
scbCallType2.isChecked = SettingUtils.enableCallType2
scbCallType3.isChecked = SettingUtils.enableCallType3
scbCallType4.isChecked = SettingUtils.enableCallType4
sbEnablePhone.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
if (isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3) {
if (isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3 && !SettingUtils.enableCallType4) {
XToastUtils.info(R.string.enable_phone_fw_tips)
SettingUtils.enablePhone = false
sbEnablePhone.isChecked = false
@ -315,7 +316,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
}
scbCallType1.setOnCheckedChangeListener { _: SmoothCheckBox, isChecked: Boolean ->
SettingUtils.enableCallType1 = isChecked
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3) {
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3 && !SettingUtils.enableCallType4) {
XToastUtils.info(R.string.enable_phone_fw_tips)
SettingUtils.enablePhone = false
sbEnablePhone.isChecked = false
@ -323,7 +324,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
}
scbCallType2.setOnCheckedChangeListener { _: SmoothCheckBox, isChecked: Boolean ->
SettingUtils.enableCallType2 = isChecked
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3) {
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3 && !SettingUtils.enableCallType4) {
XToastUtils.info(R.string.enable_phone_fw_tips)
SettingUtils.enablePhone = false
sbEnablePhone.isChecked = false
@ -331,7 +332,15 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
}
scbCallType3.setOnCheckedChangeListener { _: SmoothCheckBox, isChecked: Boolean ->
SettingUtils.enableCallType3 = isChecked
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3) {
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3 && !SettingUtils.enableCallType4) {
XToastUtils.info(R.string.enable_phone_fw_tips)
SettingUtils.enablePhone = false
sbEnablePhone.isChecked = false
}
}
scbCallType4.setOnCheckedChangeListener { _: SmoothCheckBox, isChecked: Boolean ->
SettingUtils.enableCallType4 = isChecked
if (!isChecked && !SettingUtils.enableCallType1 && !SettingUtils.enableCallType2 && !SettingUtils.enableCallType3 && !SettingUtils.enableCallType4) {
XToastUtils.info(R.string.enable_phone_fw_tips)
SettingUtils.enablePhone = false
sbEnablePhone.isChecked = false

View File

@ -21,6 +21,7 @@ import com.idormy.sms.forwarder.utils.PhoneUtils
import com.idormy.sms.forwarder.utils.SettingUtils
import com.idormy.sms.forwarder.utils.Worker
import com.idormy.sms.forwarder.workers.SendWorker
import com.xuexiang.xutil.resource.ResUtils
import com.xuexiang.xutil.resource.ResUtils.getString
import java.util.*
@ -42,27 +43,6 @@ class PhoneStateReceiver : BroadcastReceiver() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) return
//获取来电号码
/*val phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)
val state = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Core.telephonyManager.callStateForSubscription
} else {
Core.telephonyManager.callState
}
Log.d(TAG, "来电信息state=$state phoneNumber = $phoneNumber")
if (TextUtils.isEmpty(phoneNumber)) return
when (state) {
TelephonyManager.CALL_STATE_RINGING -> {}
TelephonyManager.CALL_STATE_IDLE -> {
*//*(Core.app as App).applicationScope.launch {
delay(500L) //延时0.5秒获取通话记录
sendReceiveCallMsg(context, phoneNumber)
}*//*
sendReceiveCallMsg(context, phoneNumber)
}
TelephonyManager.CALL_STATE_OFFHOOK -> {}
}*/
val number = intent.extras!!.getString(TelephonyManager.EXTRA_INCOMING_NUMBER)
val stateStr = intent.extras!!.getString(TelephonyManager.EXTRA_STATE)
var state = 0
@ -71,6 +51,8 @@ class PhoneStateReceiver : BroadcastReceiver() {
TelephonyManager.EXTRA_STATE_OFFHOOK -> state = TelephonyManager.CALL_STATE_OFFHOOK
TelephonyManager.EXTRA_STATE_RINGING -> state = TelephonyManager.CALL_STATE_RINGING
}
Log.d(TAG, "state=$state, number=$number")
onCallStateChanged(context, state, number)
} catch (e: Exception) {
@ -82,17 +64,39 @@ class PhoneStateReceiver : BroadcastReceiver() {
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
private fun onCallStateChanged(context: Context, state: Int, number: String?) {
val lastState = MMKVUtils.getInt("CALL_LAST_STATE", TelephonyManager.CALL_STATE_IDLE)
if (lastState == state) {
if (lastState == state || (state == TelephonyManager.CALL_STATE_RINGING && number == null)) {
//No change, debounce extras
return
}
MMKVUtils.put("CALL_LAST_STATE", state)
when (state) {
TelephonyManager.CALL_STATE_RINGING -> {
Log.d(TAG, "来电响铃")
MMKVUtils.put("CALL_IS_INCOMING", true)
//MMKVUtils.put("CALL_START_TIME", Date())
MMKVUtils.put("CALL_SAVED_NUMBER", number)
//来电提醒
if (!TextUtils.isEmpty(number) && SettingUtils.enableCallType4) {
val contacts = PhoneUtils.getContactByNumber(number)
val contactName = if (contacts.isNotEmpty()) contacts[0].name else getString(R.string.unknown_number)
val sb = StringBuilder()
sb.append(getString(R.string.linkman)).append(contactName).append("\n")
sb.append(ResUtils.getString(R.string.mandatory_type))
sb.append(getString(R.string.incoming_call))
val msgInfo = MsgInfo("call", number.toString(), sb.toString(), Date(), "", -1)
val request = OneTimeWorkRequestBuilder<SendWorker>()
.setInputData(
workDataOf(
Worker.sendMsgInfo to Gson().toJson(msgInfo)
)
)
.build()
WorkManager.getInstance(context).enqueue(request)
}
}
TelephonyManager.CALL_STATE_OFFHOOK ->
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
@ -125,7 +129,6 @@ class PhoneStateReceiver : BroadcastReceiver() {
}
}
}
MMKVUtils.put("CALL_LAST_STATE", state)
}
private fun sendReceiveCallMsg(context: Context, callType: Int, phoneNumber: String?) {

View File

@ -29,6 +29,7 @@ const val SP_ENABLE_PHONE = "enable_phone"
const val SP_ENABLE_CALL_TYPE_1 = "enable_call_type_1"
const val SP_ENABLE_CALL_TYPE_2 = "enable_call_type_2"
const val SP_ENABLE_CALL_TYPE_3 = "enable_call_type_3"
const val SP_ENABLE_CALL_TYPE_4 = "enable_call_type_4"
const val SP_ENABLE_APP_NOTIFY = "enable_app_notify"
const val SP_ENABLE_CANCEL_APP_NOTIFY = "enable_cancel_app_notify"

View File

@ -307,13 +307,21 @@ class PhoneUtils private constructor() {
sb.append(ResUtils.getString(R.string.linkman)).append(callInfo.name).append("\n")
if (!TextUtils.isEmpty(callInfo.viaNumber)) sb.append(ResUtils.getString(R.string.via_number)).append(callInfo.viaNumber).append("\n")
if (callInfo.dateLong > 0L) sb.append(ResUtils.getString(R.string.call_date)).append(DateUtils.millis2String(callInfo.dateLong, SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()))).append("\n")
if (callInfo.duration > 0) sb.append(ResUtils.getString(R.string.call_duration)).append(callInfo.duration).append("s\n")
if (callInfo.duration > 0) {
if (callInfo.type == 3) {
sb.append(ResUtils.getString(R.string.ring_duration))
} else {
sb.append(ResUtils.getString(R.string.call_duration))
}
sb.append(callInfo.duration).append("s\n")
}
sb.append(ResUtils.getString(R.string.mandatory_type))
//通话类型1.呼入 2.呼出 3.未接
//通话类型1.呼入 2.呼出 3.未接 4.来电提醒
when (callInfo.type) {
1 -> sb.append(ResUtils.getString(R.string.received_call))
2 -> sb.append(ResUtils.getString(R.string.local_outgoing_call))
else -> sb.append(ResUtils.getString(R.string.missed_call))
3 -> sb.append(ResUtils.getString(R.string.missed_call))
else -> sb.append(ResUtils.getString(R.string.incoming_call))
}
return sb.toString()
}

View File

@ -61,6 +61,14 @@ class SettingUtils private constructor() {
MMKVUtils.put(SP_ENABLE_CALL_TYPE_3, enableCallType3)
}
//是否转发通话——来电提醒
@JvmStatic
var enableCallType4: Boolean
get() = MMKVUtils.getBoolean(SP_ENABLE_CALL_TYPE_4, false)
set(enableCallType4) {
MMKVUtils.put(SP_ENABLE_CALL_TYPE_4, enableCallType4)
}
//是否转发应用通知
@JvmStatic
var enableAppNotify: Boolean

View File

@ -107,7 +107,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mandatory_type"
android:text="@string/type"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
@ -121,37 +121,51 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:singleLine="true"
android:text="@string/missed_call"
android:textSize="11sp" />
android:textSize="10sp" />
<com.xuexiang.xui.widget.button.SmoothCheckBox
android:id="@+id/scb_call_type1"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="5dp"
android:layout_marginStart="3dp"
app:scb_color_checked="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:singleLine="true"
android:text="@string/received_call"
android:textSize="11sp" />
android:textSize="10sp" />
<com.xuexiang.xui.widget.button.SmoothCheckBox
android:id="@+id/scb_call_type2"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="5dp"
android:layout_marginStart="3dp"
app:scb_color_checked="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:singleLine="true"
android:text="@string/local_outgoing_call"
android:textSize="11sp" />
android:textSize="10sp" />
<com.xuexiang.xui.widget.button.SmoothCheckBox
android:id="@+id/scb_call_type4"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="3dp"
app:scb_color_checked="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/incoming_call"
android:textSize="10sp" />
</LinearLayout>
@ -215,9 +229,8 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/cancel_app_notify"
android:textSize="11sp" />
android:textSize="10sp" />
<com.xuexiang.xui.widget.button.SmoothCheckBox
android:id="@+id/scb_not_user_present"
@ -229,9 +242,8 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/not_user_present"
android:textSize="11sp" />
android:textSize="10sp" />
</LinearLayout>
@ -293,10 +305,9 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:checked="true"
android:text="@string/user_app"
android:textSize="11sp" />
android:textSize="10sp" />
<com.xuexiang.xui.widget.button.SmoothCheckBox
android:id="@+id/scb_load_system_app"
@ -308,9 +319,8 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/system_app"
android:textSize="11sp" />
android:textSize="10sp" />
</LinearLayout>
@ -531,9 +541,8 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/keep_reminding"
android:textSize="11sp" />
android:textSize="10sp" />
</LinearLayout>
@ -804,9 +813,8 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/play_silence_music"
android:textSize="11sp" />
android:textSize="10sp" />
<LinearLayout
android:id="@+id/layout_one_pixel_activity"
@ -824,9 +832,8 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:text="@string/one_pixel_activity"
android:textSize="11sp" />
android:textSize="10sp" />
</LinearLayout>

View File

@ -503,8 +503,11 @@
<string name="forwarding_function_tips">Main switch: Enable the forwarding function as required</string>
<string name="call_date">Call date: </string>
<string name="call_duration">Call duration: </string>
<string name="ring_duration">Ring duration: </string>
<string name="type">Type: </string>
<string name="mandatory_type">Call type: </string>
<string name="missed_call">Missed</string>
<string name="incoming_call">Incoming</string>
<string name="received_call">Received</string>
<string name="local_outgoing_call">Call out</string>
<string name="optional_action">Optional: </string>

View File

@ -504,8 +504,11 @@
<string name="forwarding_function_tips">总开关,请根据实际需要,启用对应的转发功能</string>
<string name="call_date">通话时间:</string>
<string name="call_duration">通话时长:</string>
<string name="ring_duration">响铃时长:</string>
<string name="type">类型:</string>
<string name="mandatory_type">通话类型:</string>
<string name="missed_call">未接来电</string>
<string name="incoming_call">来电提醒</string>
<string name="received_call">已接来电</string>
<string name="local_outgoing_call">本机去电</string>
<string name="optional_action">可选操作:</string>