Skip to content

Commit

Permalink
更新夜间模式设置
Browse files Browse the repository at this point in the history
  • Loading branch information
ZYFDroid committed Oct 5, 2024
1 parent 4c15bfd commit bfc237c
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 57 deletions.
15 changes: 14 additions & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ android {
minSdkVersion 21
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 28
versionCode 1080002
versionName "1.8.0.2"
versionCode 1080003
versionName "1.8.0.3"
signingConfig signingConfigs.debug
}

Expand Down
130 changes: 103 additions & 27 deletions app/src/main/java/com/zyfdroid/epub/ReadingActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.zyfdroid.epub;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.view.*;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -115,6 +118,39 @@ public boolean onMenuOpened(int featureId, Menu menu) {
return super.onMenuOpened(featureId, menu);
}

@Override
protected void attachBaseContext(Context newBase) {
Configuration config = newBase.getResources().getConfiguration();
int nightMode = SpUtils.getInstance(newBase).getNightMode();

if(nightMode == 0){
super.attachBaseContext(newBase);
return;
}

if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O){

int oldInt = config.uiMode;

// day mode
if(nightMode == 1){
oldInt = oldInt & (~Configuration.UI_MODE_NIGHT_YES);
oldInt = oldInt & (~Configuration.UI_MODE_NIGHT_UNDEFINED);
oldInt = oldInt | Configuration.UI_MODE_NIGHT_NO;
}
// night mode
if(nightMode == 2){
oldInt = oldInt & (~Configuration.UI_MODE_NIGHT_NO);
oldInt = oldInt & (~Configuration.UI_MODE_NIGHT_UNDEFINED);
oldInt = oldInt | Configuration.UI_MODE_NIGHT_YES;
}
config.uiMode = oldInt;
}

Context context = newBase.createConfigurationContext(config);
super.attachBaseContext(context);
}

float readActionBarSize = 0;
View readActionBar = null;
@Override
Expand Down Expand Up @@ -162,9 +198,7 @@ public void onClick(View v) {
bookView = findViewById(R.id.webEpub);

if(getString(R.string.isnightmode).contains("yes")){
if(SpUtils.getInstance(ReadingActivity.this).getAllowNightMode()){
bookView.setBackgroundColor(0);
}
bookView.setBackgroundColor(0);
}

bookmarkAdapter = new BookmarkAdapter();
Expand Down Expand Up @@ -330,6 +364,46 @@ public boolean onCreateOptionsMenu(Menu menu) {



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if(keyCode==KeyEvent.KEYCODE_VOLUME_UP || keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) {

if (event.getRepeatCount() > 0) {
return true;
}
if (isDrawerOpen()) {
if (SpUtils.getInstance(this).getEinkMode()) {
if (displayingEinkPage != null) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
displayingEinkPage.pageUp();
return true;
}

if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
displayingEinkPage.pageDown();
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}

if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
evaluteJavascriptFunction("prev");
return true;
}

if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
evaluteJavascriptFunction("next");
return true;
}
}
return super.onKeyDown(keyCode, event);
}



@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(ViewUtils.sourceIsGamepad(event.getSource())){
Expand Down Expand Up @@ -785,9 +859,9 @@ public void run(String arg) {
@Override
public void run(String arg) {
if(getString(R.string.isnightmode).contains("yes")){
if(SpUtils.getInstance(ReadingActivity.this).getAllowNightMode()){

evaluteJavascriptFunction("setNight()");
}

}
evaluteJavascriptFunction("loadBookAtUrl", contentOpfPath, DBUtils.autoLoad(ReadingActivity.this,readingBook.getUUID()).getEpubcft(),SpUtils.getInstance(ReadingActivity.this).getTextSize());
}
Expand Down Expand Up @@ -1101,32 +1175,34 @@ protected void onPause() {
}
bookView.onPause();
}
}


class TocEntry implements Cloneable {
public String id;
public String href;
public String label;
public TocEntry[] subitems;
public static class TocEntry implements Cloneable {
public String id;
public String href;
public String label;
public TocEntry[] subitems;

@Override
protected Object clone() {
TocEntry obj = new TocEntry();
obj.href = href;
obj.id = id;
obj.label = label;
obj.subitems = null;
return obj;
@Override
protected Object clone() {
TocEntry obj = new TocEntry();
obj.href = href;
obj.id = id;
obj.label = label;
obj.subitems = null;
return obj;
}
}

public static class BookSpine {
public String idref;
public int index;
public String href;
}
}

class BookSpine {
public String idref;
public int index;
public String href;
public interface Action<T> {
public void run(T arg);
}
}

interface Action<T> {
public void run(T arg);
}

30 changes: 19 additions & 11 deletions app/src/main/java/com/zyfdroid/epub/SettingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.zyfdroid.epub.utils.CinematicProgressDialog;
import com.zyfdroid.epub.utils.DBUtils;
Expand All @@ -38,14 +36,29 @@ public class SettingActivity extends AppCompatActivity {
CheckBox chkEink;
CheckBox chkFullscreen;
CheckBox chkShowStatusBar;

RadioGroup readingThemes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
chkOpenExternal = findViewById(R.id.chkExternalOpen);
chkOpenExternal.setChecked(SpUtils.getInstance(this).shouldOpenWithExternalReader());
chkAllowNight = findViewById(R.id.chkAllowNight);
chkAllowNight.setChecked(SpUtils.getInstance(this).getAllowNightMode());
readingThemes = findViewById(R.id.radNightModes);

readingThemes.check(new int[]{
R.id.chkReadingNightFollowSystem,
R.id.chkReadingNightAlwaysLight,
R.id.chkReadingNightAlwaysDark}
[SpUtils.getInstance(this).getNightMode()]);
readingThemes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.chkReadingNightFollowSystem){SpUtils.getInstance(SettingActivity.this).setNightMode(0);}
if(checkedId == R.id.chkReadingNightAlwaysLight){SpUtils.getInstance(SettingActivity.this).setNightMode(1);}
if(checkedId == R.id.chkReadingNightAlwaysDark){SpUtils.getInstance(SettingActivity.this).setNightMode(2);}
}
});
chkEink = findViewById(R.id.chkEink);
chkEink.setChecked(SpUtils.getInstance(this).getEinkMode());
chkFullscreen = findViewById(R.id.chkFullscreen);
Expand All @@ -60,12 +73,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SpUtils.getInstance(SettingActivity.this).setOpenWithExternalReader(isChecked);
}
});
chkAllowNight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SpUtils.getInstance(SettingActivity.this).setAllowNightMode(isChecked);
}
});

chkEink.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/zyfdroid/epub/utils/SpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public int getTextSize(){
public boolean getEinkMode(){return sp.getBoolean("eink",false);}
public void setEinkMode(boolean b){sp.edit().putBoolean("eink",b).apply();}

public boolean getAllowNightMode(){
return sp.getBoolean("nightmode",true);

public int getNightMode(){
return sp.getInt("nightmode2",0);
}
public void setAllowNightMode(boolean value){
sp.edit().putBoolean("nightmode",value).apply();
public void setNightMode(int value){
sp.edit().putInt("nightmode2",value).apply();
}
public void setOpenWithExternalReader(boolean b){
sp.edit().putBoolean("openWithExternalReader",b).apply();
Expand Down
39 changes: 31 additions & 8 deletions app/src/main/res/layout/activity_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,43 @@
android:gravity="center_vertical"
android:id="@+id/chkExternalOpen"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/allownight"
android:gravity="center_vertical"
android:id="@+id/chkAllowNight"
/>
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radNightModes"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_reading_theme"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_reading_theme_daynight"
android:id="@+id/chkReadingNightFollowSystem"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_reading_theme_always_light"
android:id="@+id/chkReadingNightAlwaysLight"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_reading_theme_always_dark"
android:id="@+id/chkReadingNightAlwaysDark"
/>
</RadioGroup>

<CheckBox
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/eink"
android:gravity="center_vertical"
android:id="@+id/chkEink"
android:visibility="gone"
android:visibility="visible"
/>
<TextView
android:layout_width="wrap_content"
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@
<string name="menu_add_to_desktop">要放到桌面的那个位置</string>
<string name="tm_added_to_desktop">已添加到桌面</string>
<string name="tm_removed_from_desktop">已从桌面移除</string>
<string name="settings_reading_theme">阅读页面主题 (Android 8+)</string>
<string name="settings_reading_theme_daynight">跟随系统</string>
<string name="settings_reading_theme_always_light">亮色主题(适配电子书模式)</string>
<string name="settings_reading_theme_always_dark">暗色主题(在OLED/MiniLED上更省电)</string>
</resources>
Loading

0 comments on commit bfc237c

Please sign in to comment.