Add option to require unlocking the device (#18)

This commit is contained in:
Maksim Karasev 2024-06-21 12:58:45 +03:00
parent 347781b61e
commit 3275548201
5 changed files with 90 additions and 36 deletions

View file

@ -7,6 +7,7 @@ import android.service.quicksettings.TileService
import ru.karasevm.privatednstoggle.utils.PreferenceHelper
import ru.karasevm.privatednstoggle.utils.PreferenceHelper.autoMode
import ru.karasevm.privatednstoggle.utils.PreferenceHelper.dns_servers
import ru.karasevm.privatednstoggle.utils.PreferenceHelper.requireUnlock
import ru.karasevm.privatednstoggle.utils.PrivateDNSUtils
import ru.karasevm.privatednstoggle.utils.PrivateDNSUtils.AUTO_MODE_OPTION_AUTO
import ru.karasevm.privatednstoggle.utils.PrivateDNSUtils.AUTO_MODE_OPTION_OFF_AUTO
@ -27,17 +28,14 @@ class DnsTileService : TileService() {
qsTile.updateTile()
}
override fun onClick() {
super.onClick()
if (!checkForPermission(this)) {
return
}
/**
* Set's the state of the tile to the next state
*/
private fun cycleState() {
val dnsMode = Settings.Global.getString(contentResolver, "private_dns_mode")
val dnsProvider = Settings.Global.getString(contentResolver, "private_dns_specifier")
val sharedPrefs = PreferenceHelper.defaultPreference(this)
if (dnsMode.equals(DNS_MODE_OFF, ignoreCase = true)) {
if (sharedPrefs.autoMode == AUTO_MODE_OPTION_AUTO || sharedPrefs.autoMode == AUTO_MODE_OPTION_OFF_AUTO) {
changeTileState(
@ -101,6 +99,21 @@ class DnsTileService : TileService() {
)
}
}
}
override fun onClick() {
super.onClick()
if (!checkForPermission(this)) {
return
}
val sharedPrefs = PreferenceHelper.defaultPreference(this)
val requireUnlock = sharedPrefs.requireUnlock
if (isLocked && requireUnlock) {
unlockAndRun(this::cycleState)
} else {
cycleState()
}
}
@ -150,7 +163,7 @@ class DnsTileService : TileService() {
}
/**
* Updates tile to specified parameters
* Updates tile to specified parameters
*
* @param tile tile to update
* @param state tile state

View file

@ -7,6 +7,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import ru.karasevm.privatednstoggle.databinding.DialogOptionsBinding
import ru.karasevm.privatednstoggle.utils.PreferenceHelper
import ru.karasevm.privatednstoggle.utils.PreferenceHelper.autoMode
import ru.karasevm.privatednstoggle.utils.PreferenceHelper.requireUnlock
import ru.karasevm.privatednstoggle.utils.PrivateDNSUtils
class OptionsDialogFragment : DialogFragment() {
@ -46,5 +47,11 @@ class OptionsDialogFragment : DialogFragment() {
PrivateDNSUtils.AUTO_MODE_OPTION_OFF_AUTO
}
}
val requireUnlock = sharedPrefs.requireUnlock
binding.requireUnlockSwitch.isChecked = requireUnlock
binding.requireUnlockSwitch.setOnCheckedChangeListener { _, isChecked ->
sharedPrefs.requireUnlock = isChecked
}
}
}

View file

@ -7,6 +7,8 @@ object PreferenceHelper {
private const val DNS_SERVERS = "dns_servers"
private const val AUTO_MODE = "auto_mode"
private const val REQUIRE_UNLOCK = "require_unlock"
fun defaultPreference(context: Context): SharedPreferences =
context.getSharedPreferences("app_prefs", 0)
@ -44,4 +46,12 @@ object PreferenceHelper {
}
}
var SharedPreferences.requireUnlock
get() = getBoolean(REQUIRE_UNLOCK, false)
set(value) {
editMe {
it.put(REQUIRE_UNLOCK to value)
}
}
}

View file

@ -1,35 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/autoOptionRadioGroup"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="17dp"
android:paddingTop="10dp"
android:paddingEnd="17dp">
android:orientation="vertical">
<TextView
android:id="@+id/autoOptionHeader"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/autoOptionRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:text="@string/auto_option_description"
android:paddingStart="17dp"
android:paddingTop="10dp"
android:paddingEnd="17dp">
<TextView
android:id="@+id/autoOptionHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:text="@string/auto_option_description"
android:textSize="16sp" />
<RadioButton
android:id="@+id/autoOptionOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_option_only_off" />
<RadioButton
android:id="@+id/autoOptionAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_option_only_auto" />
<RadioButton
android:id="@+id/autoOptionOffAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_option_off_and_auto" />
</RadioGroup>
<com.google.android.material.divider.MaterialDivider
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginVertical="5dp" />
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/requireUnlockSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:text="@string/require_unlock_setting"
android:textSize="16sp" />
<RadioButton
android:id="@+id/autoOptionOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_option_only_off" />
<RadioButton
android:id="@+id/autoOptionAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_option_only_auto" />
<RadioButton
android:id="@+id/autoOptionOffAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/auto_option_off_and_auto" />
</RadioGroup>
</LinearLayout>

View file

@ -26,4 +26,5 @@
<string name="set_to_off_toast">Private DNS turned off</string>
<string name="set_to_auto_toast">Private DNS set to auto</string>
<string name="set_to_provider_toast">Private DNS set to %1$s</string>
<string name="require_unlock_setting">Require unlocking the device to change server</string>
</resources>