Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
Bright level control for reader (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulhaag committed Jan 1, 2019
1 parent 014ba51 commit 84ba85b
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 33 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/ar/rulosoft/mimanganu/ReaderFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,16 @@ public void onSaveInstanceState(Bundle savedInstanceState) {

@Override
public void onDestroy() {
if (mReader != null) mReader.freeMemory();
if (readerOptions != null) {
readerOptions.startStopBrightLevel(false);
}
if (mReader != null) {
mReader.freeMemory();
}
super.onDestroy();
}


@Override
public void onResume() {
super.onResume();
Expand Down Expand Up @@ -749,7 +755,7 @@ public void onOptionChange(ReaderOptions.OptionType optionType) {
readerType = readerOptions.getReaderType();
setReader();
break;
case AJUST:
case ADJUST:
mReader.setScreenFit(readerOptions.getScreenFit());
break;
case DIRECTION:
Expand Down
122 changes: 92 additions & 30 deletions app/src/main/java/ar/rulosoft/mimanganu/componentes/ReaderOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class ReaderOptions extends FrameLayout {

public static final String BLUE_FILTER_CHECK = "blue_filter_switch_";
public static final String BLUE_FILTER_LEVEL = "blue_filter_level_";
public static final String BRIGHT_CHECK = "bright_filter_switch__";
public static final String BRIGHT_LEVEL = "bright_filter_level__";

Reader.Direction mDirection;
ImageViewTouchBase.DisplayType mScreenFit;
Expand All @@ -49,10 +51,10 @@ public class ReaderOptions extends FrameLayout {
Reader reader;
Manga manga;
int d = -1;
Button type, direction, ajust, keep_screen, rotate;
CheckBox blueCheckbox;
Button type, direction, adjust, keep_screen, rotate;
CheckBox blueCheckbox, brightCheckbox;
RelativeLayout root;
SeekBar blueLevel;
SeekBar blueLevel, brightLevel;
SharedPreferences pm;
private int readerType;
private boolean mKeepOn;
Expand Down Expand Up @@ -89,11 +91,13 @@ private void initialize() {
optionsRoot = findViewById(R.id.option_root);
type = findViewById(R.id.reader_type);
direction = findViewById(R.id.reader_direction);
ajust = findViewById(R.id.reader_ajust);
adjust = findViewById(R.id.reader_ajust);
rotate = findViewById(R.id.reader_rotate);
keep_screen = findViewById(R.id.reader_stay_awake);
blueCheckbox = findViewById(R.id.blue_cb);
blueLevel = findViewById(R.id.blue_sb);
brightCheckbox = findViewById(R.id.ux_cb);
brightLevel = findViewById(R.id.ux_sb);
ImageButton back = findViewById(R.id.reader_options_close);

type.setOnClickListener(new OnClickListener() {
Expand All @@ -117,11 +121,11 @@ public void onClick(View v) {
optionListener.onOptionChange(OptionType.TYPE);
}
if (readerType == 2) {
ajust.setEnabled(false);
ajust.setAlpha(0.5f);
adjust.setEnabled(false);
adjust.setAlpha(0.5f);
} else {
ajust.setEnabled(true);
ajust.setAlpha(1.f);
adjust.setEnabled(true);
adjust.setAlpha(1.f);
}
}
});
Expand Down Expand Up @@ -155,16 +159,16 @@ public void onClick(View v) {
}
});

ajust.setOnClickListener(new OnClickListener() {
adjust.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mScreenFit = mScreenFit.getNext();
SharedPreferences.Editor editor = pm.edit();
editor.putString(ADJUST_KEY, mScreenFit.toString());
editor.apply();
updateIconAjust(mScreenFit);
updateIconAdjust(mScreenFit);
if (optionListener != null) {
optionListener.onOptionChange(OptionType.AJUST);
optionListener.onOptionChange(OptionType.ADJUST);
}
}
});
Expand Down Expand Up @@ -207,10 +211,10 @@ public void onClick(View v) {
}
});

updateValues();
blueCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
pm.edit().putBoolean(BLUE_FILTER_CHECK, isChecked).apply();
if (isChecked) {
if (reader != null)
reader.setBlueFilter((100f - blueLevel.getProgress()) / 100f);
Expand Down Expand Up @@ -242,6 +246,33 @@ public void onStopTrackingTouch(SeekBar seekBar) {
}
});

brightCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
startStopBrightLevel(isChecked);
if (isChecked)
setBrightLevel(brightLevel.getProgress());

brightLevel.setEnabled(isChecked);
}
});

brightLevel.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setBrightLevel(brightLevel.getProgress());
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
setBrightLevel(brightLevel.getProgress());
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -287,13 +318,13 @@ public void setValues() {
type.setText(R.string.paged_reader);
}
if (readerType == 2) {
ajust.setEnabled(false);
ajust.setAlpha(0.5f);
adjust.setEnabled(false);
adjust.setAlpha(0.5f);
}

// Ajust
mScreenFit = ImageViewTouchBase.DisplayType.valueOf(pm.getString(ADJUST_KEY, ImageViewTouchBase.DisplayType.FIT_TO_WIDTH.toString()));
updateIconAjust(mScreenFit);
updateIconAdjust(mScreenFit);

// KeepOn
mKeepOn = pm.getBoolean(ReaderFragment.KEEP_SCREEN_ON, false);
Expand All @@ -306,26 +337,26 @@ public void setValues() {
// Orientation
mOrientation = pm.getInt(ORIENTATION, 0);
updateIconOrientation();

updateValues();
}

private void updateIconAjust(ImageViewTouchBase.DisplayType displayType) {
private void updateIconAdjust(ImageViewTouchBase.DisplayType displayType) {
switch (displayType) {
case NONE:
ajust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_original), null, null);
ajust.setText(R.string.no_scale);
adjust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_original), null, null);
adjust.setText(R.string.no_scale);
break;
case FIT_TO_HEIGHT:
ajust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_ajustar_alto), null, null);
ajust.setText(R.string.ajuste_alto);
adjust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_ajustar_alto), null, null);
adjust.setText(R.string.ajuste_alto);
break;
case FIT_TO_WIDTH:
ajust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_ajustar_ancho), null, null);
ajust.setText(R.string.ajuste_ancho);
adjust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_ajustar_ancho), null, null);
adjust.setText(R.string.ajuste_ancho);
break;
case FIT_TO_SCREEN:
ajust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_ajustar_diagonal), null, null);
ajust.setText(R.string.mejor_ajuste);
adjust.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.ic_action_ajustar_diagonal), null, null);
adjust.setText(R.string.mejor_ajuste);
break;
default:
break;
Expand Down Expand Up @@ -358,12 +389,19 @@ private void updateIconOrientation() {
private void saveValues() {
pm.edit().putInt(BLUE_FILTER_LEVEL, blueLevel.getProgress()).apply();
pm.edit().putBoolean(BLUE_FILTER_CHECK, blueCheckbox.isChecked()).apply();

pm.edit().putInt(BRIGHT_LEVEL, brightLevel.getProgress()).apply();
pm.edit().putBoolean(BRIGHT_CHECK, brightCheckbox.isChecked()).apply();
}

private void updateValue() {
public void updateValues() {
blueCheckbox.setChecked(pm.getBoolean(BLUE_FILTER_CHECK, false));
blueLevel.setProgress(pm.getInt(BLUE_FILTER_LEVEL, 30));
blueLevel.setEnabled(blueCheckbox.isChecked());

brightCheckbox.setChecked(pm.getBoolean(BRIGHT_CHECK, false));
brightLevel.setProgress(pm.getInt(BRIGHT_LEVEL, 125));
brightLevel.setEnabled(brightCheckbox.isChecked());
}

public void switchOptions() {
Expand All @@ -379,7 +417,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
if (root.getVisibility() == GONE) {
optionsRoot.setAlpha(0f);
root.setVisibility(VISIBLE);
updateValue();
updateValues();
vA.start();
} else {
saveValues();
Expand Down Expand Up @@ -423,6 +461,10 @@ public void setReader(Reader reader) {
if (blueCheckbox.isChecked()) {
reader.setBlueFilter((100f - blueLevel.getProgress()) / 100f);
}
if (brightCheckbox.isChecked()) {
startStopBrightLevel(true);
setBrightLevel(brightLevel.getProgress());
}
}
}

Expand All @@ -446,18 +488,38 @@ public Reader.Direction getDirection() {
}

public boolean isVisible() {
return root.getVisibility() == VISIBLE;
return root.getVisibility() == VISIBLE;
}

public void setBrightLevel(int level) {
if (mActivity == null) return;
WindowManager.LayoutParams layoutParams = mActivity.getWindow().getAttributes();
layoutParams.screenBrightness = ((float) level / 255.0f);
mActivity.getWindow().setAttributes(layoutParams);
}

public void startStopBrightLevel(boolean start) {
if (mActivity == null) return;
WindowManager.LayoutParams layoutParams = mActivity.getWindow().getAttributes(); // Get Params
if (start) {
layoutParams.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
} else {
layoutParams.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
}
mActivity.getWindow().setAttributes(layoutParams); // Set params
}



public enum OptionType {
TYPE,
DIRECTION,
AJUST,
ADJUST,
KEEP_SCREEN,
ROTATE
}

public interface OptionListener {
void onOptionChange(OptionType optionType);
}
}
}
33 changes: 32 additions & 1 deletion app/src/main/res/layout/reader_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,45 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blue_filter"
android:text="@string/bright_level"
android:textColor="@android:color/white" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<SeekBar
android:id="@+id/ux_sb"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_weight="1"
android:max="255"
android:progressBackgroundTint="@android:color/white"
android:progressTint="@android:color/white"
android:thumbTint="@android:color/white" />

<CheckBox
android:id="@+id/ux_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:buttonTint="@android:color/white"
android:maxLines="1" />

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blue_filter"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<SeekBar
android:id="@+id/blue_sb"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -447,5 +447,6 @@
<string name="processed">" precesado(s)"</string>
<string name="server_update_process_finished">Acualizacion de datos del servidor finalizada.</string>
<string name="update_server_information">Ejecutando una actualización de datos del servidor, puede tardar 5 -10 min, por favor espera hasta que el mensaje de terminado sea mostrado (su base de datos esta en juego).</string>
<string name="bright_level">Brillo</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,5 @@
<string name="update_server_information">Running server update, it can take a while (5-10 min), please wait until finish message is displayed!</string>
<string name="server_update_process_finished">Server update process finished.</string>
<string name="processed">" processed"</string>
<string name="bright_level">Bright level</string>
</resources>

0 comments on commit 84ba85b

Please sign in to comment.