Add menu entry to keep screen on

This commit is contained in:
Fabian Zobrist
2020-08-25 14:51:07 +02:00
parent dfc17b4068
commit dd9c8d56ad
5 changed files with 37 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import android.os.Bundle
import android.text.InputType import android.text.InputType
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.WindowManager
import android.widget.ScrollView import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
@@ -31,6 +32,12 @@ class MainActivity : AppCompatActivity() {
inputTeamA.requestFocus() inputTeamA.requestFocus()
disableSubmitButton() disableSubmitButton()
updateTheme(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getInt("Theme", 2)) updateTheme(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getInt("Theme", 2))
keepScreenOn(
this.getSharedPreferences("Settings", Context.MODE_PRIVATE)
.getBoolean("Screen_On", false)
)
history = savedInstanceState?.getParcelable("history") ?: History() history = savedInstanceState?.getParcelable("history") ?: History()
updateView() updateView()
@@ -265,9 +272,14 @@ class MainActivity : AppCompatActivity() {
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu) menuInflater.inflate(R.menu.menu_main, menu)
menu.findItem(R.id.action_screenOn).isChecked =
this.getSharedPreferences("Settings", Context.MODE_PRIVATE)
.getBoolean("Screen_On", false)
return true return true
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.action_clear -> { R.id.action_clear -> {
@@ -282,6 +294,11 @@ class MainActivity : AppCompatActivity() {
chooseThemeDialog() chooseThemeDialog()
true true
} }
R.id.action_screenOn -> {
item.isChecked = !item.isChecked
keepScreenOn(item.isChecked)
true
}
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
} }
} }
@@ -381,4 +398,16 @@ class MainActivity : AppCompatActivity() {
} }
delegate.applyDayNight() delegate.applyDayNight()
} }
private fun keepScreenOn(keepOn: Boolean) {
if (keepOn) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
val prefs = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).edit()
prefs.putBoolean("Screen_On", keepOn)
prefs.apply()
}
} }

View File

@@ -17,4 +17,9 @@
android:id="@+id/action_theme" android:id="@+id/action_theme"
android:orderInCategory="15" android:orderInCategory="15"
android:title="@string/choose_theme_text" /> android:title="@string/choose_theme_text" />
<item
android:id="@+id/action_screenOn"
android:checkable="true"
android:orderInCategory="20"
android:title="@string/keep_screen_on" />
</menu> </menu>

View File

@@ -3,4 +3,5 @@
<string name="clear">Neues Spiel Starten</string> <string name="clear">Neues Spiel Starten</string>
<string name="undo">Letzte Runde Löschen</string> <string name="undo">Letzte Runde Löschen</string>
<string name="choose_theme_text">Theme auswählen</string> <string name="choose_theme_text">Theme auswählen</string>
<string name="keep_screen_on">Bildschirm eingeschaltet lassen</string>
</resources> </resources>

View File

@@ -3,4 +3,5 @@
<string name="clear">Neus Spil Starte</string> <string name="clear">Neus Spil Starte</string>
<string name="undo">Letschti Rundi Lösche</string> <string name="undo">Letschti Rundi Lösche</string>
<string name="choose_theme_text">Usgsehe ändere</string> <string name="choose_theme_text">Usgsehe ändere</string>
<string name="keep_screen_on">Bildschirm igschalted la</string>
</resources> </resources>

View File

@@ -7,4 +7,5 @@
<string name="clear">Start New Game</string> <string name="clear">Start New Game</string>
<string name="undo">Undo Last Round</string> <string name="undo">Undo Last Round</string>
<string name="choose_theme_text">Choose theme</string> <string name="choose_theme_text">Choose theme</string>
<string name="keep_screen_on">Keep screen on</string>
</resources> </resources>