Add an overlay on top of the reader to simulate a lower brightness. Closes #362

This commit is contained in:
len 2016-07-07 23:18:22 +02:00
parent a32e0e4ec5
commit f15df40a54
7 changed files with 52 additions and 20 deletions

View File

@ -163,6 +163,7 @@ dependencies {
compile 'com.afollestad.material-dialogs:core:0.8.6.1'
compile 'net.xpece.android:support-preference:0.8.1'
compile 'me.zhanghai.android.systemuihelper:library:1.0.0'
compile 'org.adw.library:discrete-seekbar:1.0.1'
// Tests
testCompile 'junit:junit:4.12'

View File

@ -48,7 +48,7 @@ class PreferencesHelper(context: Context) {
fun customBrightness() = rxPrefs.getBoolean(keys.customBrightness, false)
fun customBrightnessValue() = rxPrefs.getFloat(keys.customBrightnessValue, 0f)
fun customBrightnessValue() = rxPrefs.getInteger(keys.customBrightnessValue, 0)
fun defaultViewer() = prefs.getInt(keys.defaultViewer, 1)

View File

@ -415,20 +415,39 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
private fun setCustomBrightness(enabled: Boolean) {
if (enabled) {
customBrightnessSubscription = preferences.customBrightnessValue().asObservable()
.map { Math.max(0.01f, it) }
.subscribe { setCustomBrightnessValue(it) }
subscriptions.add(customBrightnessSubscription)
} else {
if (customBrightnessSubscription != null) {
subscriptions.remove(customBrightnessSubscription)
}
setCustomBrightnessValue(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE)
customBrightnessSubscription?.let { subscriptions.remove(it) }
setCustomBrightnessValue(0)
}
}
private fun setCustomBrightnessValue(value: Float) {
window.attributes = window.attributes.apply { screenBrightness = value }
/**
* Sets the brightness of the screen. Range is [-50, 100].
* From -50 to -1 a semi-transparent black view is shown at the top with the minimum brightness.
* From 1 to 100 it sets that value as brightness.
* 0 sets system brightness and hides the overlay.
*/
private fun setCustomBrightnessValue(value: Int) {
// Calculate and set reader brightness.
val readerBrightness = if (value > 0) {
value / 100f
} else if (value < 0) {
0.01f
} else WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
window.attributes = window.attributes.apply { screenBrightness = readerBrightness }
// Set black overlay visibility.
if (value < 0) {
brightness_overlay.visibility = View.VISIBLE
val alpha = (Math.abs(value) * 2.56).toInt()
brightness_overlay.setBackgroundColor(Color.argb(alpha, 0, 0, 0))
} else {
brightness_overlay.visibility = View.GONE
}
}
private fun applyTheme(theme: Int) {

View File

@ -4,15 +4,14 @@ import android.app.Dialog
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.view.View
import android.widget.SeekBar
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.util.plusAssign
import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
import kotlinx.android.synthetic.main.dialog_reader_settings.view.*
import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.subscriptions.CompositeSubscription
@ -108,17 +107,18 @@ class ReaderSettingsDialog : DialogFragment() {
preferences.customBrightness().set(isChecked)
}
brightness_seekbar.max = 100
brightness_seekbar.progress = Math.round(
preferences.customBrightnessValue().getOrDefault() * brightness_seekbar.max)
brightness_seekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
brightness_seekbar.progress = preferences.customBrightnessValue().getOrDefault()
brightness_seekbar.setOnProgressChangeListener(object : DiscreteSeekBar.OnProgressChangeListener {
override fun onProgressChanged(seekBar: DiscreteSeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
preferences.customBrightnessValue().set(progress.toFloat() / seekBar.max)
preferences.customBrightnessValue().set(value)
}
}
})
override fun onStartTrackingTouch(seekBar: DiscreteSeekBar) {}
override fun onStopTrackingTouch(seekBar: DiscreteSeekBar) {}
})
}
override fun onDestroyView() {

View File

@ -99,4 +99,10 @@
</FrameLayout>
<View
android:id="@+id/brightness_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</FrameLayout>

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@ -188,10 +189,15 @@
android:layout_height="wrap_content"
android:text="@string/pref_custom_brightness"/>
<SeekBar
<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
android:id="@+id/brightness_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextAppearance.Regular.Body1.Light"/>
app:dsb_min="-50"
app:dsb_max="100"
app:dsb_indicatorFormatter="%d%%"
app:dsb_indicatorTextAppearance="@style/TextAppearance.Regular"
app:dsb_indicatorColor="?colorAccent"
app:dsb_progressColor="?colorAccent" />
</LinearLayout>

View File

@ -27,7 +27,7 @@
<string name="pref_show_page_number_key">pref_show_page_number_key</string>
<string name="pref_keep_screen_on_key">pref_keep_screen_on_key</string>
<string name="pref_custom_brightness_key">pref_custom_brightness_key</string>
<string name="pref_custom_brightness_value_key">pref_custom_brightness_value_key</string>
<string name="pref_custom_brightness_value_key">custom_brightness_value</string>
<string name="pref_reader_theme_key">pref_reader_theme_key</string>
<string name="pref_image_decoder_key">pref_image_decoder_key</string>
<string name="pref_read_with_volume_keys_key">reader_volume_keys</string>