Skip to content

Commit

Permalink
新增:自定义模板可用标签 {{定位信息}}(英文系统:{{LOCATION}}#341 #343
Browse files Browse the repository at this point in the history
  • Loading branch information
pppscn committed Nov 22, 2023
1 parent 5741cdf commit 3dd2e41
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 204 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/com/idormy/sms/forwarder/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ class App : Application(), CactusCallback, Configuration.Provider by Core {
}

//启动前台服务
Intent(this, ForegroundService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(it)
} else {
startService(it)
}
val serviceIntent = Intent(this, ForegroundService::class.java)
serviceIntent.action = "START"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent)
} else {
startService(serviceIntent)
}

//网络状态监听
Expand Down
26 changes: 13 additions & 13 deletions app/src/main/java/com/idormy/sms/forwarder/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),

//启动前台服务
if (!ForegroundService.isRunning) {
Intent(this, ForegroundService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(it)
} else {
startService(it)
}
val serviceIntent = Intent(this, ForegroundService::class.java)
serviceIntent.action = "START"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent)
} else {
startService(serviceIntent)
}
}
})
Expand Down Expand Up @@ -204,6 +204,7 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),
.show()
}
}

R.id.nav_app_list -> openNewPage(AppListFragment::class.java)
//R.id.nav_logcat -> openNewPage(LogcatFragment::class.java)
R.id.nav_help -> AgentWebActivity.goWeb(this, getString(R.string.url_help))
Expand Down Expand Up @@ -232,12 +233,15 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),
getString(R.string.menu_rules) -> binding!!.includeMain.toolbar.inflateMenu(
R.menu.menu_rules
)

getString(R.string.menu_senders) -> binding!!.includeMain.toolbar.inflateMenu(
R.menu.menu_senders
)

getString(R.string.menu_settings) -> binding!!.includeMain.toolbar.inflateMenu(
R.menu.menu_settings
)

else -> binding!!.includeMain.toolbar.inflateMenu(R.menu.menu_logs)
}
item.isChecked = true
Expand All @@ -255,13 +259,6 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),
LiveEventBus.get(EVENT_UPDATE_RULE_TYPE, String::class.java).observe(this) { type: String ->
ruleType = type
}

//更新通知栏文案
LiveEventBus.get(EVENT_UPDATE_NOTIFY, String::class.java).observe(this) { notify: String ->
cactusUpdateNotification {
setContent(notify)
}
}
}

/**
Expand All @@ -287,6 +284,7 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),
R.id.action_notifications -> {
showTipsForce(this)
}

R.id.action_clear_logs -> {
MaterialDialog.Builder(this)
.content(R.string.delete_type_log_tips)
Expand All @@ -311,6 +309,7 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),
}
.show()
}

R.id.action_add_sender -> {
val dialog = BottomSheetDialog(this)
val view: View =
Expand All @@ -328,6 +327,7 @@ class MainActivity : BaseActivity<ActivityMainBinding?>(),
dialog.show()
WidgetUtils.transparentBottomSheetDialogBackground(dialog)
}

R.id.action_add_rule -> {
PageOption.to(RulesEditFragment::class.java)
.putString(KEY_RULE_TYPE, ruleType)
Expand Down
24 changes: 0 additions & 24 deletions app/src/main/java/com/idormy/sms/forwarder/core/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ object Core : Configuration.Provider {
val logs: LogsRepository by lazy { (app as App).logsRepository }
val rule: RuleRepository by lazy { (app as App).ruleRepository }
val sender: SenderRepository by lazy { (app as App).senderRepository }
/*
val telephonyManager: TelephonyManager by lazy { app.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager }
val smsManager: SmsManager by lazy { app.getSystemService(SmsManager::class.java) }
val subscriptionManager: SubscriptionManager by lazy {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
SubscriptionManager.from(app)
} else {
app.getSystemService(SubscriptionManager::class.java)
}
}
val user by lazy { app.getSystemService<UserManager>()!! }*/


/*val directBootAware: Boolean get() = directBootSupported && dataStore.canToggleLocked
val directBootSupported by lazy {
Build.VERSION.SDK_INT >= 24 && try {
app.getSystemService<DevicePolicyManager>()?.storageEncryptionStatus ==
DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER
} catch (_: RuntimeException) {
false
}
}*/

fun init(app: Application) {
this.app = app
Expand All @@ -54,6 +32,4 @@ object Core : Configuration.Provider {
setTaskExecutor { (app as App).applicationScope.launch { it.run() } }
}.build()
}

fun startService() = ContextCompat.startForegroundService(app, Intent(app, ForegroundService::class.java))
}
14 changes: 12 additions & 2 deletions app/src/main/java/com/idormy/sms/forwarder/entity/MsgInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.Log
import com.idormy.sms.forwarder.App
import com.idormy.sms.forwarder.R
import com.idormy.sms.forwarder.utils.CALL_TYPE_MAP
import com.idormy.sms.forwarder.utils.HttpServerUtils
import com.idormy.sms.forwarder.utils.SettingUtils
import com.idormy.sms.forwarder.utils.SettingUtils.Companion.enableSmsTemplate
import com.idormy.sms.forwarder.utils.SettingUtils.Companion.extraDeviceMark
Expand Down Expand Up @@ -58,7 +59,7 @@ data class MsgInfo(
.replace(getString(R.string.tag_app_version), versionName)
.replace(getString(R.string.tag_call_type), CALL_TYPE_MAP[callType.toString()] ?: getString(R.string.unknown_call))
.trim()
return replaceAppName(regexReplace(titleForSend, regexReplace), from)
return replaceLocationTag(replaceAppName(regexReplace(titleForSend, regexReplace), from))
}

val smsVoForSend: String
Expand Down Expand Up @@ -106,7 +107,7 @@ data class MsgInfo(
.replace(getString(R.string.tag_app_version), versionName)
.replace(getString(R.string.tag_call_type), CALL_TYPE_MAP[callType.toString()] ?: getString(R.string.unknown_call))
.trim()
return replaceAppName(regexReplace(smsVoForSend, regexReplace), from)
return replaceLocationTag(replaceAppName(regexReplace(smsVoForSend, regexReplace), from))
}

//正则替换内容
Expand Down Expand Up @@ -154,6 +155,15 @@ data class MsgInfo(
return content.replace(getString(R.string.tag_app_name), appName)
}

//替换 {{定位信息}} 标签
private fun replaceLocationTag(content: String): String {
if (TextUtils.isEmpty(content)) return content
if (content.indexOf(getString(R.string.tag_location)) == -1) return content

val location = HttpServerUtils.apiLocationCache.toString()
return content.replace(getString(R.string.tag_location), location)
}

override fun toString(): String {
return "MsgInfo{" +
"mobile='" + from + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ class FrpcFragment : BaseFragment<FragmentFrpcsBinding?>(), FrpcPagingAdapter.On

R.id.iv_play -> {
if (!ForegroundService.isRunning) {
Intent(requireContext(), ForegroundService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requireContext().startForegroundService(it)
} else {
requireContext().startService(it)
}
val serviceIntent = Intent(requireContext(), ForegroundService::class.java)
serviceIntent.action = "START"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requireContext().startForegroundService(serviceIntent)
} else {
requireContext().startService(serviceIntent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class ServerFragment : BaseFragment<FragmentServerBinding?>(), View.OnClickListe
})
}

//联系人权限
//定位权限
private fun checkLocationPermission() {
XXPermissions.with(this).permission(Permission.ACCESS_COARSE_LOCATION).permission(Permission.ACCESS_FINE_LOCATION).permission(Permission.ACCESS_BACKGROUND_LOCATION).request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, all: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
Expand All @@ -32,6 +33,7 @@ import com.idormy.sms.forwarder.core.BaseFragment
import com.idormy.sms.forwarder.databinding.FragmentSettingsBinding
import com.idormy.sms.forwarder.entity.SimInfo
import com.idormy.sms.forwarder.receiver.BootReceiver
import com.idormy.sms.forwarder.service.ForegroundService
import com.idormy.sms.forwarder.utils.*
import com.idormy.sms.forwarder.workers.LoadAppListWorker
import com.jeremyliao.liveeventbus.LiveEventBus
Expand Down Expand Up @@ -172,6 +174,9 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL

//纯客户端模式
switchDirectlyToClient(binding!!.sbDirectlyToClient)

//启用 {{定位信息}} 标签
switchEnableLocationTag(binding!!.sbEnableLocationTag)
}

override fun onResume() {
Expand Down Expand Up @@ -918,21 +923,25 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
SettingUtils.notifyContent = etNotifyContent.text.toString().trim()
LiveEventBus.get(EVENT_UPDATE_NOTIFY, String::class.java).post(SettingUtils.notifyContent)
val notifyContent = etNotifyContent.text.toString().trim()
SettingUtils.notifyContent = notifyContent
val updateIntent = Intent(context, ForegroundService::class.java)
updateIntent.action = "UPDATE_NOTIFICATION"
updateIntent.putExtra("UPDATED_CONTENT", notifyContent)
context?.let { ContextCompat.startForegroundService(it, updateIntent) }
}
})
}

//设置转发时启用自定义模版
@SuppressLint("UseSwitchCompatOrMaterialCode", "SetTextI18n")
fun switchSmsTemplate(sb_sms_template: SwitchButton) {
fun switchSmsTemplate(sbSmsTemplate: SwitchButton) {
val isOn: Boolean = SettingUtils.enableSmsTemplate
sb_sms_template.isChecked = isOn
sbSmsTemplate.isChecked = isOn
val layoutSmsTemplate: LinearLayout = binding!!.layoutSmsTemplate
layoutSmsTemplate.visibility = if (isOn) View.VISIBLE else View.GONE
val etSmsTemplate: EditText = binding!!.etSmsTemplate
sb_sms_template.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
sbSmsTemplate.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
layoutSmsTemplate.visibility = if (isChecked) View.VISIBLE else View.GONE
SettingUtils.enableSmsTemplate = isChecked
if (!isChecked) {
Expand Down Expand Up @@ -983,6 +992,40 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding?>(), View.OnClickL
}
}

//启用 {{定位信息}} 标签
private fun switchEnableLocationTag(@SuppressLint("UseSwitchCompatOrMaterialCode") switchEnableLocationTag: SwitchButton) {
switchEnableLocationTag.isChecked = SettingUtils.enableLocationTag
switchEnableLocationTag.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
SettingUtils.enableLocationTag = isChecked
if (isChecked) {
XXPermissions.with(this).permission(Permission.ACCESS_COARSE_LOCATION).permission(Permission.ACCESS_FINE_LOCATION).permission(Permission.ACCESS_BACKGROUND_LOCATION).request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, all: Boolean) {
//重启前台服务
val serviceIntent = Intent(requireContext(), ForegroundService::class.java)
serviceIntent.action = "START"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requireContext().startForegroundService(serviceIntent)
} else {
requireContext().startService(serviceIntent)
}
}

override fun onDenied(permissions: List<String>, never: Boolean) {
if (never) {
XToastUtils.error(R.string.toast_denied_never)
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity(requireContext(), permissions)
} else {
XToastUtils.error(R.string.toast_denied)
}
SettingUtils.enableLocationTag = false
switchEnableLocationTag.isChecked = false
}
})
}
}
}

//获取当前手机品牌
private fun getAutoStartTips(): String {
return when (Build.BRAND.lowercase(Locale.ROOT)) {
Expand Down
Loading

0 comments on commit 3dd2e41

Please sign in to comment.