diff --git a/app/build.gradle b/app/build.gradle index 274c4daf3..53f042baf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index e1af01e98..42385ad44 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index f4544e52a..17a04bad7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -415,20 +415,39 @@ class ReaderActivity : BaseRxActivity() { 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) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsDialog.kt index 9ccaae478..b1a60f024 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsDialog.kt @@ -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() { diff --git a/app/src/main/res/layout/activity_reader.xml b/app/src/main/res/layout/activity_reader.xml index 54db3108f..8812abbbd 100644 --- a/app/src/main/res/layout/activity_reader.xml +++ b/app/src/main/res/layout/activity_reader.xml @@ -99,4 +99,10 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_reader_settings.xml b/app/src/main/res/layout/dialog_reader_settings.xml index 10c6e8cdd..fa384b4f2 100644 --- a/app/src/main/res/layout/dialog_reader_settings.xml +++ b/app/src/main/res/layout/dialog_reader_settings.xml @@ -1,6 +1,7 @@ - + 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" /> \ No newline at end of file diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 4565d56a0..75b71b6f3 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -27,7 +27,7 @@ pref_show_page_number_key pref_keep_screen_on_key pref_custom_brightness_key - pref_custom_brightness_value_key + custom_brightness_value pref_reader_theme_key pref_image_decoder_key reader_volume_keys