redirection delay selector

This commit is contained in:
lucky 2022-02-07 13:35:33 +03:00
parent 48a9998c26
commit 53708dc554
11 changed files with 64 additions and 11 deletions

View file

@ -10,8 +10,8 @@ android {
applicationId "me.lucky.red" applicationId "me.lucky.red"
minSdk 29 minSdk 29
targetSdk 32 targetSdk 32
versionCode 2 versionCode 3
versionName "1.0.1" versionName "1.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View file

@ -21,7 +21,7 @@ class CallRedirectionService : CallRedirectionService() {
) )
} }
private lateinit var prefs: Preferences lateinit var prefs: Preferences
private lateinit var window: PopupWindow private lateinit var window: PopupWindow
private var connectivityManager: ConnectivityManager? = null private var connectivityManager: ConnectivityManager? = null
@ -30,6 +30,11 @@ class CallRedirectionService : CallRedirectionService() {
init() init()
} }
override fun onDestroy() {
super.onDestroy()
window.cancel()
}
private fun init() { private fun init() {
prefs = Preferences(this) prefs = Preferences(this)
window = PopupWindow(this) window = PopupWindow(this)

View file

@ -44,12 +44,19 @@ class MainActivity : AppCompatActivity() {
prefs = Preferences(this) prefs = Preferences(this)
roleManager = getSystemService(RoleManager::class.java) roleManager = getSystemService(RoleManager::class.java)
binding.apply { binding.apply {
redirectionDelay.value = (prefs.redirectionDelay / 1000).toFloat()
toggle.isChecked = prefs.isServiceEnabled toggle.isChecked = prefs.isServiceEnabled
} }
} }
private fun setup() { private fun setup() {
binding.apply { binding.apply {
redirectionDelay.setLabelFormatter {
String.format("%.1f", it)
}
redirectionDelay.addOnChangeListener { _, value, _ ->
prefs.redirectionDelay = (value * 1000).toLong()
}
toggle.setOnCheckedChangeListener { _, isChecked -> toggle.setOnCheckedChangeListener { _, isChecked ->
if (isChecked && !hasPermissions()) { if (isChecked && !hasPermissions()) {
toggle.isChecked = false toggle.isChecked = false

View file

@ -14,10 +14,6 @@ import java.util.*
import kotlin.concurrent.timerTask import kotlin.concurrent.timerTask
class PopupWindow(private val service: CallRedirectionService) { class PopupWindow(private val service: CallRedirectionService) {
companion object {
private const val CANCEL_DELAY = 2000L
}
private val windowManager = service private val windowManager = service
.applicationContext .applicationContext
.getSystemService(WindowManager::class.java) .getSystemService(WindowManager::class.java)
@ -41,9 +37,8 @@ class PopupWindow(private val service: CallRedirectionService) {
init { init {
view.setOnClickListener { view.setOnClickListener {
timer?.cancel() cancel()
service.placeCallUnmodified() service.placeCallUnmodified()
remove()
} }
} }
@ -70,7 +65,7 @@ class PopupWindow(private val service: CallRedirectionService) {
return@timerTask return@timerTask
} }
service.cancelCall() service.cancelCall()
}, CANCEL_DELAY) }, service.prefs.redirectionDelay)
view.findViewById<TextView>(R.id.description).text = String.format( view.findViewById<TextView>(R.id.description).text = String.format(
service.getString(R.string.popup), service.getString(R.string.popup),
service.getString(destinationId), service.getString(destinationId),
@ -104,4 +99,9 @@ class PopupWindow(private val service: CallRedirectionService) {
} catch (exc: WindowManager.BadTokenException) { return false } } catch (exc: WindowManager.BadTokenException) { return false }
return true return true
} }
fun cancel() {
timer?.cancel()
remove()
}
} }

View file

@ -7,6 +7,7 @@ import androidx.preference.PreferenceManager
class Preferences(ctx: Context) { class Preferences(ctx: Context) {
companion object { companion object {
private const val SERVICE_ENABLED = "service_enabled" private const val SERVICE_ENABLED = "service_enabled"
private const val REDIRECTION_DELAY = "redirection_delay"
} }
private val prefs = PreferenceManager.getDefaultSharedPreferences(ctx) private val prefs = PreferenceManager.getDefaultSharedPreferences(ctx)
@ -14,4 +15,8 @@ class Preferences(ctx: Context) {
var isServiceEnabled: Boolean var isServiceEnabled: Boolean
get() = prefs.getBoolean(SERVICE_ENABLED, false) get() = prefs.getBoolean(SERVICE_ENABLED, false)
set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) } set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) }
var redirectionDelay: Long
get() = prefs.getLong(REDIRECTION_DELAY, 2000L)
set(value) = prefs.edit { putLong(REDIRECTION_DELAY, value) }
} }

View file

@ -16,6 +16,39 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginVertical="16dp"
app:layout_constraintBottom_toTopOf="@+id/toggle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/description">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.slider.Slider
android:id="@+id/redirectionDelay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/redirection_delay_description"
android:stepSize="0.5"
android:valueFrom="2"
android:valueTo="4" />
<TextView
android:id="@+id/description2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/redirection_delay_description" />
</LinearLayout>
</ScrollView>
<ToggleButton <ToggleButton
android:id="@+id/toggle" android:id="@+id/toggle"
android:layout_width="0dp" android:layout_width="0dp"

View file

@ -5,4 +5,5 @@
<string name="popup">Перенаправление в %1$s</string> <string name="popup">Перенаправление в %1$s</string>
<string name="signal">Signal</string> <string name="signal">Signal</string>
<string name="telegram">Telegram</string> <string name="telegram">Telegram</string>
<string name="redirection_delay_description">Задержка до того, как звонок будет перенаправлен.</string>
</resources> </resources>

View file

@ -5,4 +5,5 @@
<string name="popup">Redirecting to %1$s</string> <string name="popup">Redirecting to %1$s</string>
<string name="signal">Signal</string> <string name="signal">Signal</string>
<string name="telegram">Telegram</string> <string name="telegram">Telegram</string>
<string name="redirection_delay_description">Delay before a call will be redirected.</string>
</resources> </resources>

View file

@ -5,7 +5,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:7.1.0' classpath 'com.android.tools.build:gradle:7.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong

View file

@ -0,0 +1 @@
redirection delay selector

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 72 KiB

Before After
Before After