Add an option to use custom brightness

This commit is contained in:
inorichi 2015-11-22 20:03:48 +01:00
parent 9b504126d0
commit 85dcfd2beb
17 changed files with 309 additions and 14 deletions

View File

@ -57,6 +57,14 @@ public class PreferencesHelper {
return rxPrefs.getBoolean(getKey(R.string.pref_keep_screen_on_key), true); return rxPrefs.getBoolean(getKey(R.string.pref_keep_screen_on_key), true);
} }
public Preference<Boolean> customBrightness() {
return rxPrefs.getBoolean(getKey(R.string.pref_custom_brightness_key), false);
}
public Preference<Float> customBrightnessValue() {
return rxPrefs.getFloat(getKey(R.string.pref_custom_brightness_value_key), 0F);
}
public int getDefaultViewer() { public int getDefaultViewer() {
return Integer.parseInt(prefs.getString(getKey(R.string.pref_default_viewer_key), "1")); return Integer.parseInt(prefs.getString(getKey(R.string.pref_default_viewer_key), "1"));
} }

View File

@ -36,6 +36,7 @@ import eu.kanade.mangafeed.ui.reader.viewer.webtoon.WebtoonReader;
import eu.kanade.mangafeed.util.ToastUtil; import eu.kanade.mangafeed.util.ToastUtil;
import icepick.Icepick; import icepick.Icepick;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription; import rx.subscriptions.CompositeSubscription;
@RequiresPresenter(ReaderPresenter.class) @RequiresPresenter(ReaderPresenter.class)
@ -52,6 +53,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
private int uiFlags; private int uiFlags;
private CompositeSubscription subscriptions; private CompositeSubscription subscriptions;
private Subscription customBrightnessSubscription;
private static final int LEFT_TO_RIGHT = 1; private static final int LEFT_TO_RIGHT = 1;
private static final int RIGHT_TO_LEFT = 2; private static final int RIGHT_TO_LEFT = 2;
@ -166,6 +168,10 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
subscriptions.add(preferences.keepScreenOn() subscriptions.add(preferences.keepScreenOn()
.asObservable() .asObservable()
.subscribe(this::setKeepScreenOn)); .subscribe(this::setKeepScreenOn));
subscriptions.add(preferences.customBrightness()
.asObservable()
.subscribe(this::setCustomBrightness));
} }
private void setOrientation(boolean locked) { private void setOrientation(boolean locked) {
@ -205,6 +211,24 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
} }
} }
private void setCustomBrightness(boolean enabled) {
if (enabled) {
subscriptions.add(customBrightnessSubscription = preferences.customBrightnessValue()
.asObservable()
.subscribe(this::setCustomBrightnessValue));
} else {
if (customBrightnessSubscription != null)
subscriptions.remove(customBrightnessSubscription);
setCustomBrightnessValue(-1);
}
}
private void setCustomBrightnessValue(float value) {
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = value;
getWindow().setAttributes(layout);
}
private void setStatusBarVisibility(boolean hidden) { private void setStatusBarVisibility(boolean hidden) {
createUiHideFlags(hidden); createUiHideFlags(hidden);
setSystemUiVisibility(); setSystemUiVisibility();

View File

@ -40,12 +40,14 @@ public class ReaderMenu {
@Bind(R.id.lock_orientation) ImageButton lockOrientation; @Bind(R.id.lock_orientation) ImageButton lockOrientation;
@Bind(R.id.reader_selector) ImageButton readerSelector; @Bind(R.id.reader_selector) ImageButton readerSelector;
@Bind(R.id.reader_extra_settings) ImageButton extraSettings; @Bind(R.id.reader_extra_settings) ImageButton extraSettings;
@Bind(R.id.reader_brightness) ImageButton brightnessSettings;
private ReaderActivity activity; private ReaderActivity activity;
private PreferencesHelper preferences; private PreferencesHelper preferences;
@State boolean showing; @State boolean showing;
private PopupWindow popupWindow; private PopupWindow settingsPopup;
private PopupWindow brightnessPopup;
private DecimalFormat decimalFormat; private DecimalFormat decimalFormat;
@ -99,7 +101,8 @@ public class ReaderMenu {
Animation bottomMenuAnimation = AnimationUtils.loadAnimation(activity, R.anim.exit_to_bottom); Animation bottomMenuAnimation = AnimationUtils.loadAnimation(activity, R.anim.exit_to_bottom);
bottomMenu.startAnimation(bottomMenuAnimation); bottomMenu.startAnimation(bottomMenuAnimation);
popupWindow.dismiss(); settingsPopup.dismiss();
brightnessPopup.dismiss();
showing = false; showing = false;
} }
@ -152,14 +155,26 @@ public class ReaderMenu {
// Extra settings menu // Extra settings menu
final View popupView = activity.getLayoutInflater().inflate(R.layout.reader_popup, null); final View popupView = activity.getLayoutInflater().inflate(R.layout.reader_popup, null);
popupWindow = new SettingsPopupWindow(popupView); settingsPopup = new SettingsPopupWindow(popupView);
extraSettings.setOnClickListener(v -> { extraSettings.setOnClickListener(v -> {
if (!popupWindow.isShowing()) if (!settingsPopup.isShowing())
popupWindow.showAtLocation(extraSettings, settingsPopup.showAtLocation(extraSettings,
Gravity.BOTTOM | Gravity.RIGHT, 0, bottomMenu.getHeight()); Gravity.BOTTOM | Gravity.RIGHT, 0, bottomMenu.getHeight());
else else
popupWindow.dismiss(); settingsPopup.dismiss();
});
// Brightness popup
final View brightnessView = activity.getLayoutInflater().inflate(R.layout.reader_brightness, null);
brightnessPopup = new BrightnessPopupWindow(brightnessView);
brightnessSettings.setOnClickListener(v -> {
if (!brightnessPopup.isShowing())
brightnessPopup.showAtLocation(brightnessSettings,
Gravity.BOTTOM | Gravity.LEFT, 0, bottomMenu.getHeight());
else
brightnessPopup.dismiss();
}); });
} }
@ -208,9 +223,39 @@ public class ReaderMenu {
keepScreenOn.setOnCheckedChangeListener((view, isChecked) -> keepScreenOn.setOnCheckedChangeListener((view, isChecked) ->
preferences.keepScreenOn().set(isChecked)); preferences.keepScreenOn().set(isChecked));
}
} }
class BrightnessPopupWindow extends PopupWindow {
@Bind(R.id.custom_brightness) CheckBox customBrightness;
@Bind(R.id.brightness_seekbar) SeekBar brightnessSeekbar;
public BrightnessPopupWindow(View view) {
super(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
setAnimationStyle(R.style.reader_brightness_popup_animation);
ButterKnife.bind(this, view);
initializePopupMenu();
}
private void initializePopupMenu() {
subscriptions.add(preferences.customBrightness()
.asObservable()
.subscribe(isEnabled -> {
customBrightness.setChecked(isEnabled);
brightnessSeekbar.setEnabled(isEnabled);
}));
customBrightness.setOnCheckedChangeListener((view, isChecked) ->
preferences.customBrightness().set(isChecked));
brightnessSeekbar.setMax(100);
brightnessSeekbar.setProgress(Math.round(
preferences.customBrightnessValue().get() * brightnessSeekbar.getMax()));
brightnessSeekbar.setOnSeekBarChangeListener(new BrightnessSeekBarChangeListener());
}
} }
class PageSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener { class PageSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
@ -229,6 +274,26 @@ public class ReaderMenu {
public void onStopTrackingTouch(SeekBar seekBar) {} public void onStopTrackingTouch(SeekBar seekBar) {}
} }
class BrightnessSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
preferences.customBrightnessValue().set((float) progress / seekBar.getMax());
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
class HideMenuAnimationListener implements Animation.AnimationListener { class HideMenuAnimationListener implements Animation.AnimationListener {
@Override @Override

View File

@ -12,6 +12,7 @@ import eu.kanade.mangafeed.data.source.model.Page;
import eu.kanade.mangafeed.ui.reader.ReaderActivity; import eu.kanade.mangafeed.ui.reader.ReaderActivity;
import eu.kanade.mangafeed.ui.reader.viewer.base.BaseReader; import eu.kanade.mangafeed.ui.reader.viewer.base.BaseReader;
import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerReaderAdapter; import eu.kanade.mangafeed.ui.reader.viewer.common.ViewPagerReaderAdapter;
import rx.Subscription;
public class VerticalReader extends BaseReader { public class VerticalReader extends BaseReader {
@ -19,11 +20,17 @@ public class VerticalReader extends BaseReader {
private ViewPagerReaderAdapter adapter; private ViewPagerReaderAdapter adapter;
private boolean transitions;
private Subscription transitionsSubscription;
public VerticalReader(ReaderActivity activity) { public VerticalReader(ReaderActivity activity) {
super(activity); super(activity);
activity.getLayoutInflater().inflate(R.layout.reader_vertical, container); activity.getLayoutInflater().inflate(R.layout.reader_vertical, container);
ButterKnife.bind(this, container); ButterKnife.bind(this, container);
transitionsSubscription = activity.getPreferences().enableTransitions().asObservable()
.subscribe(value -> transitions = value);
viewPager.setOffscreenPageLimit(3); viewPager.setOffscreenPageLimit(3);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override @Override
@ -32,6 +39,33 @@ public class VerticalReader extends BaseReader {
updatePageNumber(); updatePageNumber();
} }
}); });
viewPager.setOnChapterBoundariesOutListener(new VerticalViewPager.OnChapterBoundariesOutListener() {
@Override
public void onFirstPageOutEvent() {
requestPreviousChapter();
}
@Override
public void onLastPageOutEvent() {
requestNextChapter();
}
});
viewPager.setOnChapterSingleTapListener(new VerticalViewPager.OnChapterSingleTapListener() {
@Override
public void onCenterTap() {
activity.onCenterSingleTap();
}
@Override
public void onLeftSideTap() {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, transitions);
}
@Override
public void onRightSideTap() {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, transitions);
}
});
} }
@Override @Override
@ -41,7 +75,7 @@ public class VerticalReader extends BaseReader {
@Override @Override
public void setSelectedPage(int pageNumber) { public void setSelectedPage(int pageNumber) {
viewPager.setCurrentItem(getCurrentPageIndex(pageNumber)); viewPager.setCurrentItem(getCurrentPageIndex(pageNumber), false);
} }
@Override @Override
@ -53,6 +87,11 @@ public class VerticalReader extends BaseReader {
@Override @Override
public boolean onImageTouch(MotionEvent motionEvent) { public boolean onImageTouch(MotionEvent motionEvent) {
return true; return viewPager.onImageTouch(motionEvent);
}
@Override
public void destroy() {
transitionsSubscription.unsubscribe();
} }
} }

View File

@ -3,17 +3,140 @@ package eu.kanade.mangafeed.ui.reader.viewer.vertical;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.MotionEvent;
public class VerticalViewPager extends fr.castorflex.android.verticalviewpager.VerticalViewPager { public class VerticalViewPager extends fr.castorflex.android.verticalviewpager.VerticalViewPager {
private GestureDetector gestureDetector; private GestureDetector gestureDetector;
private OnChapterBoundariesOutListener mOnChapterBoundariesOutListener;
private OnChapterSingleTapListener mOnChapterSingleTapListener;
private static final float LEFT_REGION = 0.33f;
private static final float RIGHT_REGION = 0.66f;
private static final float SWIPE_TOLERANCE = 0.25f;
private float startDragY;
public VerticalViewPager(Context context, AttributeSet attrs) { public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
gestureDetector = new GestureDetector(getContext(), new ReaderViewGestureListener()); gestureDetector = new GestureDetector(getContext(), new ReaderViewGestureListener());
} }
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
gestureDetector.onTouchEvent(ev);
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
if (this.getCurrentItem() == 0 || this.getCurrentItem() == this.getAdapter().getCount() - 1) {
startDragY = ev.getY();
}
}
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
// Do Nothing.
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
try {
if (mOnChapterBoundariesOutListener != null) {
if (this.getCurrentItem() == 0) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
float displacement = ev.getY() - startDragY;
if (ev.getY() > startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
mOnChapterBoundariesOutListener.onFirstPageOutEvent();
return true;
}
startDragY = 0;
}
} else if (this.getCurrentItem() == this.getAdapter().getCount() - 1) {
if ((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
float displacement = startDragY - ev.getY();
if (ev.getY() < startDragY && displacement > getHeight() * SWIPE_TOLERANCE) {
mOnChapterBoundariesOutListener.onLastPageOutEvent();
return true;
}
startDragY = 0;
}
}
}
return super.onTouchEvent(ev);
} catch (IllegalArgumentException e) {
// Do Nothing.
}
return false;
}
public boolean onImageTouch(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
public interface OnChapterBoundariesOutListener {
void onFirstPageOutEvent();
void onLastPageOutEvent();
}
public interface OnChapterSingleTapListener {
void onCenterTap();
void onLeftSideTap();
void onRightSideTap();
}
public void setOnChapterBoundariesOutListener(OnChapterBoundariesOutListener onChapterBoundariesOutListener) {
mOnChapterBoundariesOutListener = onChapterBoundariesOutListener;
}
public void setOnChapterSingleTapListener(OnChapterSingleTapListener onChapterSingleTapListener) {
mOnChapterSingleTapListener = onChapterSingleTapListener;
}
private class ReaderViewGestureListener extends GestureDetector.SimpleOnGestureListener { private class ReaderViewGestureListener extends GestureDetector.SimpleOnGestureListener {
// TODO
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
final int position = getCurrentItem();
final float positionX = e.getX();
if (positionX < getWidth() * LEFT_REGION) {
if (position != 0) {
if (mOnChapterSingleTapListener != null) {
mOnChapterSingleTapListener.onLeftSideTap();
}
} else {
if (mOnChapterBoundariesOutListener != null) {
mOnChapterBoundariesOutListener.onFirstPageOutEvent();
}
}
} else if (positionX > getWidth() * RIGHT_REGION) {
if (position != getAdapter().getCount() - 1) {
if (mOnChapterSingleTapListener != null) {
mOnChapterSingleTapListener.onRightSideTap();
}
} else {
if (mOnChapterBoundariesOutListener != null) {
mOnChapterBoundariesOutListener.onLastPageOutEvent();
}
}
} else {
if (mOnChapterSingleTapListener != null) {
mOnChapterSingleTapListener.onCenterTap();
}
}
return true;
}
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 570 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 816 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#333333"
android:paddingRight="10dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/grey_text"
android:text="@string/pref_custom_brightness"
android:id="@+id/custom_brightness" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/brightness_seekbar" />
</LinearLayout>

View File

@ -72,14 +72,16 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:id="@+id/lock_orientation" android:id="@+id/reader_brightness"
android:src="@drawable/ic_screen_rotation" android:src="@drawable/ic_brightness_high"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:background="?android:selectableItemBackground" /> android:background="?android:selectableItemBackground" />
<ImageButton <ImageButton
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:id="@+id/lock_orientation"
android:src="@drawable/ic_screen_rotation"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:background="?android:selectableItemBackground" /> android:background="?android:selectableItemBackground" />
<ImageButton <ImageButton

View File

@ -10,6 +10,8 @@
<string name="pref_enable_transitions_key">pref_enable_transitions_key</string> <string name="pref_enable_transitions_key">pref_enable_transitions_key</string>
<string name="pref_show_page_number_key">pref_show_page_number_key</string> <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_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_download_directory_key">pref_download_directory_key</string> <string name="pref_download_directory_key">pref_download_directory_key</string>
<string name="pref_download_threads_key">pref_download_threads_key</string> <string name="pref_download_threads_key">pref_download_threads_key</string>

View File

@ -28,15 +28,16 @@
<!-- Reader section --> <!-- Reader section -->
<string name="pref_hide_status_bar">Hide status bar</string> <string name="pref_hide_status_bar">Hide status bar</string>
<string name="pref_lock_orientation">Lock orientation</string>
<string name="pref_enable_transitions">Enable transitions</string>
<string name="pref_show_page_number">Show page number</string>
<string name="pref_custom_brightness">Use custom brightness</string>
<string name="pref_viewer_type">Default viewer</string> <string name="pref_viewer_type">Default viewer</string>
<string name="default_viewer">Default</string> <string name="default_viewer">Default</string>
<string name="left_to_right_viewer">Left to right</string> <string name="left_to_right_viewer">Left to right</string>
<string name="right_to_left_viewer">Right to left</string> <string name="right_to_left_viewer">Right to left</string>
<string name="vertical_viewer">Vertical</string> <string name="vertical_viewer">Vertical</string>
<string name="webtoon_viewer">Webtoon (experimental)</string> <string name="webtoon_viewer">Webtoon (experimental)</string>
<string name="pref_lock_orientation">Lock orientation</string>
<string name="pref_enable_transitions">Enable transitions</string>
<string name="pref_show_page_number">Show page number</string>
<!-- Downloads section --> <!-- Downloads section -->
<string name="pref_download_directory">Downloads directory</string> <string name="pref_download_directory">Downloads directory</string>

View File

@ -103,6 +103,10 @@
<item name="android:windowEnterAnimation">@anim/enter_from_right</item> <item name="android:windowEnterAnimation">@anim/enter_from_right</item>
<item name="android:windowExitAnimation">@anim/exit_to_right</item> <item name="android:windowExitAnimation">@anim/exit_to_right</item>
</style> </style>
<style name="reader_brightness_popup_animation">
<item name="android:windowEnterAnimation">@anim/enter_from_left</item>
<item name="android:windowExitAnimation">@anim/exit_to_left</item>
</style>
<style name="grey_text"> <style name="grey_text">
<item name="android:textColor">#e0e0e0</item> <item name="android:textColor">#e0e0e0</item>
</style> </style>

View File

@ -17,6 +17,10 @@
android:key="@string/pref_show_page_number_key" android:key="@string/pref_show_page_number_key"
android:defaultValue="true" /> android:defaultValue="true" />
<CheckBoxPreference android:title="@string/pref_custom_brightness"
android:key="@string/pref_custom_brightness_key"
android:defaultValue="false" />
<ListPreference android:title="@string/pref_viewer_type" <ListPreference android:title="@string/pref_viewer_type"
android:key="@string/pref_default_viewer_key" android:key="@string/pref_default_viewer_key"
android:entries="@array/viewers" android:entries="@array/viewers"