Add option to disable screen timeout

Before it was disabled by default

Closes #54
This commit is contained in:
Finn M Glas 2020-06-26 07:29:48 +02:00
parent 08d4a6c2ef
commit 54662ad4ab
No known key found for this signature in database
GPG key ID: 902A30146014DFBF
4 changed files with 73 additions and 12 deletions

View file

@ -14,9 +14,12 @@ import android.os.Bundle
import android.provider.Settings
import android.util.DisplayMetrics
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.animation.*
import android.widget.Button
import android.widget.ImageView
import android.widget.Switch
import android.widget.Toast
import com.finnmglas.launcher.list.ListActivity
import com.finnmglas.launcher.list.apps.AppsRecyclerAdapter
@ -50,6 +53,8 @@ const val PREF_VIBRANT = "custom_vibrant"
const val PREF_WALLPAPER = "background_uri"
const val PREF_THEME = "theme"
const val PREF_SCREEN_TIMEOUT_DISABLED = "disableTimeout"
const val PREF_STARTED = "startedBefore"
const val PREF_STARTED_TIME = "firstStartup"
@ -322,13 +327,12 @@ fun resetSettings(context: Context) {
dominantColor = context.resources.getColor(R.color.finnmglasTheme_background_color)
vibrantColor = context.resources.getColor(R.color.finnmglasTheme_accent_color)
launcherPreferences.edit()
editor
.putString(PREF_WALLPAPER, "")
.putInt(PREF_DOMINANT, dominantColor)
.putInt(PREF_VIBRANT, vibrantColor)
.apply()
saveTheme("finn")
.putString(PREF_THEME, "finn")
.putBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false)
// load action defaults
for (actionKey in ACTIONS)
@ -337,6 +341,17 @@ fun resetSettings(context: Context) {
editor.apply()
}
fun setWindowFlags(window: Window) {
window.setFlags(0, 0) // clear flags
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
if (launcherPreferences.getBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false))
window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
else window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
fun pickDefaultApp(action: String, context: Context) : String {
val arrayResource = when (action) {
ACTION_UP -> R.array.default_up
@ -383,6 +398,15 @@ fun setButtonColor(btn: Button, color: Int) {
// not setting it in any other case (yet), unable to find a good solution
}
fun setSwitchColor(sw: Switch, trackColor: Int) {
if (Build.VERSION.SDK_INT >= 29) {
sw.trackDrawable.colorFilter = BlendModeColorFilter(trackColor, BlendMode.MULTIPLY)
}
else if(Build.VERSION.SDK_INT >= 21) {
sw.trackDrawable.colorFilter = PorterDuffColorFilter(trackColor, PorterDuff.Mode.SRC_ATOP)
}
}
// Taken from: https://stackoverflow.com/a/33072575/12787264
fun manipulateColor(color: Int, factor: Float): Int {
val a = Color.alpha(color)

View file

@ -9,11 +9,7 @@ import android.view.WindowManager
*/
interface UIObject {
fun onStart() {
if (this is Activity){
window.setFlags(0,0) // clear flags
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
if (this is Activity) setWindowFlags(window)
applyTheme()
setOnClicks()

View file

@ -113,6 +113,8 @@ class SettingsFragmentTheme : Fragment(), UIObject {
settings_theme_custom_button_select.text = getString(R.string.settings_select_image)
}
setSwitchColor(settings_launcher_switch_screen_timeout, vibrantColor)
settings_theme_container.setBackgroundColor(dominantColor)
setButtonColor(settings_theme_finn_button_select, vibrantColor)
setButtonColor(settings_theme_dark_button_select, vibrantColor)
@ -154,5 +156,19 @@ class SettingsFragmentTheme : Fragment(), UIObject {
context!!
)
}
settings_launcher_switch_screen_timeout.setOnClickListener { // Toggle screen timeout
launcherPreferences.edit()
.putBoolean(PREF_SCREEN_TIMEOUT_DISABLED,
!launcherPreferences.getBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false))
.apply()
setWindowFlags(activity!!.window)
}
}
override fun adjustLayout() {
// visually load settings
settings_launcher_switch_screen_timeout.isChecked =
launcherPreferences.getBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false)
}
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
@ -17,7 +17,12 @@
<ScrollView
android:id="@+id/settings_theme_scroller"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="0dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="@id/settings_launcher_text_screen_timeout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
@ -134,4 +139,24 @@
</LinearLayout>
</ScrollView>
</LinearLayout>
<TextView
android:id="@+id/settings_launcher_text_screen_timeout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="Disable screen timeout"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="@+id/settings_launcher_switch_screen_timeout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@id/settings_launcher_text_screen_timeout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/settings_launcher_text_screen_timeout" />
</androidx.constraintlayout.widget.ConstraintLayout>