Skip to content

Commit

Permalink
1.11.2
Browse files Browse the repository at this point in the history
add mobile group
add allow calls from short numbers
  • Loading branch information
lucky committed Jul 15, 2022
1 parent f00da9a commit 0620914
Show file tree
Hide file tree
Showing 35 changed files with 378 additions and 243 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "me.lucky.silence"
minSdk 29
targetSdk 32
versionCode 45
versionName "1.11.1"
versionCode 46
versionName "1.11.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/me/lucky/silence/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ open class MainActivity : AppCompatActivity() {
R.id.nav_groups -> GroupsFragment()
R.id.nav_repeated -> RepeatedFragment()
R.id.nav_messages -> MessagesFragment()
R.id.nav_extra -> ExtraFragment()
else -> MainFragment()
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/me/lucky/silence/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Preferences(ctx: Context) {

private const val RESPONSE_OPTIONS = "call_screening_response_options"
private const val UNKNOWN_NUMBERS_CHECKED = "unknown_numbers_checked"
private const val SHORT_NUMBERS_CHECKED = "short_numbers_checked"
private const val SIM = "sim"

private const val DEFAULT_REPEATED_COUNT = 3
Expand Down Expand Up @@ -90,6 +91,10 @@ class Preferences(ctx: Context) {
)
set(value) = prefs.edit { putBoolean(UNKNOWN_NUMBERS_CHECKED, value) }

var isShortNumbersChecked: Boolean
get() = prefs.getBoolean(SHORT_NUMBERS_CHECKED, false)
set(value) = prefs.edit { putBoolean(SHORT_NUMBERS_CHECKED, value) }

var responseOptions: Int
get() = prefs.getInt(
RESPONSE_OPTIONS,
Expand All @@ -111,6 +116,7 @@ enum class Group(val value: Int) {
TOLL_FREE(1),
LOCAL(1 shl 1),
NOT_LOCAL(1 shl 2),
MOBILE(1 shl 3),
}

enum class Message(val value: Int) {
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/me/lucky/silence/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,28 @@ import me.lucky.silence.text.NotificationListenerService

class Utils {
companion object {
fun setMessagesTextState(ctx: Context, value: Boolean) =
setComponentState(ctx, NotificationListenerService::class.java, value)
fun setMessagesTextEnabled(ctx: Context, value: Boolean) =
setComponentEnabled(ctx, NotificationListenerService::class.java, value)

fun updateMessagesTextState(ctx: Context, prefs: Preferences) =
setMessagesTextState(
fun updateMessagesTextEnabled(ctx: Context) {
val prefs = Preferences(ctx)
setMessagesTextEnabled(
ctx,
prefs.isEnabled &&
prefs.isMessagesChecked &&
prefs.messages.and(Message.TEXT.value) != 0,
)
}

fun setComponentState(ctx: Context, cls: Class<*>, value: Boolean) =
fun setComponentEnabled(ctx: Context, cls: Class<*>, value: Boolean) =
ctx.packageManager.setComponentEnabledSetting(
ComponentName(ctx, cls),
if (value) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
)

fun getComponentState(ctx: Context, cls: Class<*>) =
fun isComponentEnabled(ctx: Context, cls: Class<*>) =
ctx.packageManager.getComponentEnabledSetting(ComponentName(ctx, cls)) ==
PackageManager.COMPONENT_ENABLED_STATE_ENABLED

Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/me/lucky/silence/ctl/BroadcastReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ class BroadcastReceiver : BroadcastReceiver() {
}

private fun setGlobalState(ctx: Context, state: Boolean) {
val prefs = Preferences(ctx)
prefs.isEnabled = state
Utils.updateMessagesTextState(ctx, prefs)
Preferences(ctx).isEnabled = state
Utils.updateMessagesTextEnabled(ctx)
}

private fun setUnknownNumbersState(ctx: Context, state: Boolean) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/me/lucky/silence/ctl/TileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TileService : TileService() {
super.onClick()
val state = qsTile.state == Tile.STATE_INACTIVE
prefs.isEnabled = state
Utils.updateMessagesTextState(this, prefs)
Utils.updateMessagesTextEnabled(this)
update()
}

Expand Down
82 changes: 82 additions & 0 deletions app/src/main/java/me/lucky/silence/fragment/ExtraFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package me.lucky.silence.fragment

import android.Manifest
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment

import me.lucky.silence.Preferences
import me.lucky.silence.Sim
import me.lucky.silence.Utils
import me.lucky.silence.databinding.FragmentExtraBinding

class ExtraFragment : Fragment() {
private lateinit var binding: FragmentExtraBinding
private lateinit var ctx: Context
private lateinit var prefs: Preferences

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentExtraBinding.inflate(inflater, container, false)
init()
setup()
return binding.root
}

private fun init() {
ctx = this.requireContext()
prefs = Preferences(ctx)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) disableStir()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || Utils.getModemCount(ctx) < 2)
disableSim()
binding.apply {
shortNumbers.isChecked = prefs.isShortNumbersChecked
unknownNumbers.isChecked = prefs.isUnknownNumbersChecked
stir.isChecked = prefs.isStirChecked
val opts = prefs.sim
sim1.isChecked = opts.and(Sim.SIM_1.value) != 0
sim2.isChecked = opts.and(Sim.SIM_2.value) != 0
}
}

private fun setup() = binding.apply {
shortNumbers.setOnCheckedChangeListener { _, isChecked ->
prefs.isShortNumbersChecked = isChecked
}
unknownNumbers.setOnCheckedChangeListener { _, isChecked ->
prefs.isUnknownNumbersChecked = isChecked
}
stir.setOnCheckedChangeListener { _, isChecked ->
prefs.isStirChecked = isChecked
}
sim1.setOnCheckedChangeListener { _, isChecked ->
prefs.sim = Utils.setFlag(prefs.sim, Sim.SIM_1.value, isChecked)
if (isChecked) requestSimPermissions()
}
sim2.setOnCheckedChangeListener { _, isChecked ->
prefs.sim = Utils.setFlag(prefs.sim, Sim.SIM_2.value, isChecked)
if (isChecked) requestSimPermissions()
}
}

private fun disableStir() { binding.stir.isEnabled = false }

private fun disableSim() = binding.apply {
sim1.isEnabled = false
sim2.isEnabled = false
}

private val registerForSimPermissions =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {}

private fun requestSimPermissions() =
registerForSimPermissions.launch(Manifest.permission.READ_PHONE_STATE)
}
4 changes: 4 additions & 0 deletions app/src/main/java/me/lucky/silence/fragment/GroupsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class GroupsFragment : Fragment() {
local.isChecked = value.and(Group.LOCAL.value) != 0
notLocal.isChecked = value.and(Group.NOT_LOCAL.value) != 0
tollFree.isChecked = value.and(Group.TOLL_FREE.value) != 0
mobile.isChecked = value.and(Group.MOBILE.value) != 0
}
}

Expand All @@ -49,5 +50,8 @@ class GroupsFragment : Fragment() {
tollFree.setOnCheckedChangeListener { _, isChecked ->
prefs.groups = Utils.setFlag(prefs.groups, Group.TOLL_FREE.value, isChecked)
}
mobile.setOnCheckedChangeListener { _, isChecked ->
prefs.groups = Utils.setFlag(prefs.groups, Group.MOBILE.value, isChecked)
}
}
}
11 changes: 2 additions & 9 deletions app/src/main/java/me/lucky/silence/fragment/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package me.lucky.silence.fragment
import android.Manifest
import android.app.role.RoleManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -42,17 +41,14 @@ class MainFragment : Fragment() {
private fun init() {
ctx = this.requireContext()
prefs = Preferences(ctx)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) disableStir()
binding.apply {
contacted.isChecked = prefs.isContactedChecked
groups.isChecked = prefs.isGroupsChecked
repeated.isChecked = prefs.isRepeatedChecked
messages.isChecked = prefs.isMessagesChecked
stir.isChecked = prefs.isStirChecked
}
}

private fun disableStir() { binding.stir.isEnabled = false }
private fun update() { binding.toggle.isChecked = prefs.isEnabled }

private fun setup() = binding.apply {
Expand All @@ -70,15 +66,12 @@ class MainFragment : Fragment() {
messages.setOnCheckedChangeListener { _, isChecked ->
prefs.isMessagesChecked = isChecked
if (isChecked) requestMessagesPermissions()
Utils.updateMessagesTextState(ctx, prefs)
}
stir.setOnCheckedChangeListener { _, isChecked ->
prefs.isStirChecked = isChecked
Utils.updateMessagesTextEnabled(ctx)
}
toggle.setOnCheckedChangeListener { _, isChecked ->
prefs.isEnabled = isChecked
if (isChecked) requestCallScreeningRole()
Utils.updateMessagesTextState(ctx, prefs)
Utils.updateMessagesTextEnabled(ctx)
}
}

Expand Down
40 changes: 6 additions & 34 deletions app/src/main/java/me/lucky/silence/fragment/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package me.lucky.silence.fragment

import android.Manifest
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment

import me.lucky.silence.*
import me.lucky.silence.ControlReceiver
import me.lucky.silence.Preferences
import me.lucky.silence.ResponseOption
import me.lucky.silence.Utils
import me.lucky.silence.databinding.FragmentSettingsBinding

class SettingsFragment : Fragment() {
Expand All @@ -32,34 +32,20 @@ class SettingsFragment : Fragment() {
private fun init() {
ctx = this.requireContext()
prefs = Preferences(ctx)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || Utils.getModemCount(ctx) < 2)
disableSim()
binding.apply {
controller.isChecked = Utils.getComponentState(ctx, ControlReceiver::class.java)
unknownNumbers.isChecked = prefs.isUnknownNumbersChecked
controller.isChecked = Utils.isComponentEnabled(ctx, ControlReceiver::class.java)
val opts = prefs.responseOptions
disallowCall.isChecked = opts.and(ResponseOption.DisallowCall.value) != 0
rejectCall.isChecked = opts.and(ResponseOption.RejectCall.value) != 0
silenceCall.isChecked = opts.and(ResponseOption.SilenceCall.value) != 0
skipCallLog.isChecked = opts.and(ResponseOption.SkipCallLog.value) != 0
skipNotification.isChecked = opts.and(ResponseOption.SkipNotification.value) != 0
val simOpts = prefs.sim
sim1.isChecked = simOpts.and(Sim.SIM_1.value) != 0
sim2.isChecked = simOpts.and(Sim.SIM_2.value) != 0
}
}

private fun disableSim() = binding.apply {
sim1.isEnabled = false
sim2.isEnabled = false
}

private fun setup() = binding.apply {
controller.setOnCheckedChangeListener { _, isChecked ->
Utils.setComponentState(ctx, ControlReceiver::class.java, isChecked)
}
unknownNumbers.setOnCheckedChangeListener { _, isChecked ->
prefs.isUnknownNumbersChecked = isChecked
Utils.setComponentEnabled(ctx, ControlReceiver::class.java, isChecked)
}
disallowCall.setOnCheckedChangeListener { _, isChecked ->
prefs.responseOptions = Utils.setFlag(
Expand Down Expand Up @@ -96,19 +82,5 @@ class SettingsFragment : Fragment() {
isChecked,
)
}
sim1.setOnCheckedChangeListener { _, isChecked ->
prefs.sim = Utils.setFlag(prefs.sim, Sim.SIM_1.value, isChecked)
if (isChecked) requestSimPermissions()
}
sim2.setOnCheckedChangeListener { _, isChecked ->
prefs.sim = Utils.setFlag(prefs.sim, Sim.SIM_2.value, isChecked)
if (isChecked) requestSimPermissions()
}
}

private val registerForSimPermissions =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {}

private fun requestSimPermissions() =
registerForSimPermissions.launch(Manifest.permission.READ_PHONE_STATE)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PanicResponderActivity : AppCompatActivity() {
return
}
Preferences(this).isEnabled = false
Utils.setMessagesTextState(this, false)
Utils.setMessagesTextEnabled(this, false)
if (PanicResponder.receivedTriggerFromConnectedApp(this))
AppDatabase.getInstance(this).allowNumberDao().deleteAll()
finishAndRemoveTask()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ class CallScreeningHelper(private val ctx: Context) {
number,
telephonyManager?.networkCountryIso?.uppercase(),
) }
val numberType by lazy { phoneNumberUtil.getNumberType(number) }
for (group in Group.values().asSequence().filter { groups.and(it.value) != 0 }) {
result = when (group) {
Group.TOLL_FREE -> phoneNumberUtil.getNumberType(number) ==
PhoneNumberUtil.PhoneNumberType.TOLL_FREE
Group.TOLL_FREE -> numberType == PhoneNumberUtil.PhoneNumberType.TOLL_FREE
Group.MOBILE -> numberType == PhoneNumberUtil.PhoneNumberType.MOBILE
Group.LOCAL -> isLocal
Group.NOT_LOCAL -> !isLocal
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.telecom.Connection
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import androidx.annotation.RequiresApi
import androidx.core.text.isDigitsOnly
import com.google.i18n.phonenumbers.NumberParseException
import com.google.i18n.phonenumbers.PhoneNumberUtil
import com.google.i18n.phonenumbers.Phonenumber
Expand Down Expand Up @@ -42,13 +43,14 @@ class CallScreeningService : CallScreeningService() {
return
} else if (checkEmergency(callDetails)) {
prefs.isEnabled = false
Utils.setMessagesTextState(this, false)
Utils.setMessagesTextEnabled(this, false)
respondAllow(callDetails)
return
} else if (
callDetails.callDirection != Call.Details.DIRECTION_INCOMING ||
(prefs.isStirChecked && checkStir(callDetails)) ||
checkUnknownNumber(callDetails) ||
(prefs.isUnknownNumbersChecked && checkUnknownNumber(callDetails)) ||
(prefs.isShortNumbersChecked && checkShortNumber(callDetails)) ||
checkSim()
) {
respondAllow(callDetails)
Expand Down Expand Up @@ -126,8 +128,11 @@ class CallScreeningService : CallScreeningService() {
} catch (exc: SecurityException) { false }
}

private fun checkUnknownNumber(callDetails: Call.Details): Boolean {
return callDetails.handle?.schemeSpecificPart == null &&
prefs.isUnknownNumbersChecked
private fun checkUnknownNumber(callDetails: Call.Details) =
callDetails.handle?.schemeSpecificPart == null

private fun checkShortNumber(callDetails: Call.Details): Boolean {
val v = callDetails.handle?.schemeSpecificPart?.trimStart('+') ?: return false
return v.length in 3..5 && v.isDigitsOnly()
}
}
Loading

0 comments on commit 0620914

Please sign in to comment.