Skip to content
This repository has been archived by the owner on Aug 26, 2019. It is now read-only.

Commit

Permalink
Add screen lightness
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Apr 1, 2016
1 parent 57a9865 commit 5fb3b61
Show file tree
Hide file tree
Showing 19 changed files with 648 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/src/main/java/com/hippo/ehviewer/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,28 @@ public static void putVolumePage(boolean value) {
putBoolean(KEY_VOLUME_PAGE, value);
}

private static final String KEY_CUSTOM_SCREEN_LIGHTNESS = "custom_screen_lightness";
private static final boolean DEFAULT_CUSTOM_SCREEN_LIGHTNESS = false;

public static boolean getCustomScreenLightness() {
return getBoolean(KEY_CUSTOM_SCREEN_LIGHTNESS, DEFAULT_CUSTOM_SCREEN_LIGHTNESS);
}

public static void putCustomScreenLightness(boolean value) {
putBoolean(KEY_CUSTOM_SCREEN_LIGHTNESS, value);
}

private static final String KEY_SCREEN_LIGHTNESS = "screen_lightness";
private static final int DEFAULT_SCREEN_LIGHTNESS = 50;

public static int getScreenLightness() {
return getInt(KEY_SCREEN_LIGHTNESS, DEFAULT_SCREEN_LIGHTNESS);
}

public static void putScreenLightness(int value) {
putInt(KEY_SCREEN_LIGHTNESS, value);
}

/********************
****** Download
********************/
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
Expand All @@ -63,8 +65,10 @@
import com.hippo.image.Image;
import com.hippo.unifile.UniFile;
import com.hippo.util.SystemUiHelper;
import com.hippo.widget.ColorView;
import com.hippo.yorozuya.AnimationUtils;
import com.hippo.yorozuya.ConcurrentPool;
import com.hippo.yorozuya.MathUtils;
import com.hippo.yorozuya.SimpleAnimatorListener;
import com.hippo.yorozuya.SimpleHandler;
import com.hippo.yorozuya.ViewUtils;
Expand Down Expand Up @@ -105,6 +109,8 @@ public class GalleryActivity extends EhActivity
private SystemUiHelper mSystemUiHelper;
private boolean mShowSystemUi;

@Nullable
private ColorView mMaskView;
@Nullable
private View mClock;
@Nullable
Expand Down Expand Up @@ -261,6 +267,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mSystemUiHelper.hide();
mShowSystemUi = false;

mMaskView = (ColorView) ViewUtils.$$(this, R.id.mask);
mClock = ViewUtils.$$(this, R.id.clock);
mBattery = ViewUtils.$$(this, R.id.battery);
mClock.setVisibility(Settings.getShowClock() ? View.VISIBLE : View.GONE);
Expand Down Expand Up @@ -300,6 +307,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}
setRequestedOrientation(orientation);

// Screen lightness
setScreenLightness(Settings.getCustomScreenLightness(), Settings.getScreenLightness());

if (Settings.getGuideGallery()) {
FrameLayout mainLayout = (FrameLayout) ViewUtils.$$(this, R.id.main);
mainLayout.addView(new GalleryGuideView(this));
Expand All @@ -320,6 +330,9 @@ protected void onDestroy() {
mGalleryProvider = null;
}

mMaskView = null;
mClock = null;
mBattery = null;
mSeekBarPanel = null;
mLeftText = null;
mRightText = null;
Expand Down Expand Up @@ -667,6 +680,34 @@ private void hideSlider(View sliderPanel) {
}
}

/**
* @param lightness 0 - 200
*/
private void setScreenLightness(boolean enable, int lightness) {
if (null == mMaskView) {
return;
}

Window w = getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
if (enable) {
lightness = MathUtils.clamp(lightness, 0, 200);
if (lightness > 100) {
mMaskView.setColor(0);
// Avoid BRIGHTNESS_OVERRIDE_OFF,
// screen may be off when lp.screenBrightness is 0.0f
lp.screenBrightness = Math.max((lightness - 100) / 100.0f, 0.01f);
} else {
mMaskView.setColor(MathUtils.lerp(0xde, 0x00, lightness / 100.0f) << 24);
lp.screenBrightness = 0.01f;
}
} else {
mMaskView.setColor(0);
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
}
w.setAttributes(lp);
}

private class GalleryMenuHelper implements DialogInterface.OnClickListener {

private final View mView;
Expand All @@ -678,6 +719,8 @@ private class GalleryMenuHelper implements DialogInterface.OnClickListener {
private final SwitchCompat mShowClock;
private final SwitchCompat mShowBattery;
private final SwitchCompat mVolumePage;
private final SwitchCompat mCustomScreenLightness;
private final SeekBar mScreenLightness;

@SuppressLint("InflateParams")
public GalleryMenuHelper(Context context) {
Expand All @@ -690,6 +733,8 @@ public GalleryMenuHelper(Context context) {
mShowClock = (SwitchCompat) mView.findViewById(R.id.show_clock);
mShowBattery = (SwitchCompat) mView.findViewById(R.id.show_battery);
mVolumePage = (SwitchCompat) mView.findViewById(R.id.volume_page);
mCustomScreenLightness = (SwitchCompat) mView.findViewById(R.id.custom_screen_lightness);
mScreenLightness = (SeekBar) mView.findViewById(R.id.screen_lightness);

mScreenRotation.setSelection(Settings.getScreenRotation());
mReadingDirection.setSelection(Settings.getReadingDirection());
Expand All @@ -699,6 +744,16 @@ public GalleryMenuHelper(Context context) {
mShowClock.setChecked(Settings.getShowClock());
mShowBattery.setChecked(Settings.getShowBattery());
mVolumePage.setChecked(Settings.getVolumePage());
mCustomScreenLightness.setChecked(Settings.getCustomScreenLightness());
mScreenLightness.setProgress(Settings.getScreenLightness());
mScreenLightness.setEnabled(Settings.getCustomScreenLightness());

mCustomScreenLightness.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mScreenLightness.setEnabled(isChecked);
}
});
}

public View getView() {
Expand All @@ -715,6 +770,8 @@ public void onClick(DialogInterface dialog, int which) {
boolean showClock = mShowClock.isChecked();
boolean showBattery = mShowBattery.isChecked();
boolean volumePage = mVolumePage.isChecked();
boolean customScreenLightness = mCustomScreenLightness.isChecked();
int screenLightness = mScreenLightness.getProgress();

Settings.putScreenRotation(screenRotation);
Settings.putReadingDirection(layoutMode);
Expand All @@ -724,6 +781,8 @@ public void onClick(DialogInterface dialog, int which) {
Settings.putShowClock(showClock);
Settings.putShowBattery(showBattery);
Settings.putVolumePage(volumePage);
Settings.putCustomScreenLightness(customScreenLightness);
Settings.putScreenLightness(screenLightness);

int orientation;
switch (screenRotation) {
Expand Down Expand Up @@ -755,6 +814,7 @@ public void onClick(DialogInterface dialog, int which) {
if (mBattery != null) {
mBattery.setVisibility(showBattery ? View.VISIBLE : View.GONE);
}
setScreenLightness(customScreenLightness, screenLightness);

// Update slider
mLayoutMode = layoutMode;
Expand Down
Loading

0 comments on commit 5fb3b61

Please sign in to comment.