From 8cff9f700a0a01d22eab874ced5aed6b7a3d24f5 Mon Sep 17 00:00:00 2001 From: zlj <390057892@qq.com> Date: Tue, 12 Nov 2019 17:56:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E9=83=A8=E5=88=86java?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/novel/read/utlis/LocalManageUtil.java | 8 +- .../java/com/novel/read/utlis/MD5Utils.java | 43 ------ .../java/com/novel/read/utlis/MD5Utils.kt | 41 ++++++ .../java/com/novel/read/utlis/SpUtil.java | 5 +- .../java/com/novel/read/utlis/StringUtils.kt | 3 +- .../read/widget/page/LocalPageLoader.java | 2 +- .../novel/read/widget/page/PageLoader.java | 2 +- .../read/widget/page/ReadSettingManager.java | 124 ------------------ .../read/widget/page/ReadSettingManager.kt | 104 +++++++++++++++ 9 files changed, 152 insertions(+), 180 deletions(-) delete mode 100644 app/src/main/java/com/novel/read/utlis/MD5Utils.java create mode 100644 app/src/main/java/com/novel/read/utlis/MD5Utils.kt delete mode 100644 app/src/main/java/com/novel/read/widget/page/ReadSettingManager.java create mode 100644 app/src/main/java/com/novel/read/widget/page/ReadSettingManager.kt diff --git a/app/src/main/java/com/novel/read/utlis/LocalManageUtil.java b/app/src/main/java/com/novel/read/utlis/LocalManageUtil.java index dac2ad8..354e97b 100644 --- a/app/src/main/java/com/novel/read/utlis/LocalManageUtil.java +++ b/app/src/main/java/com/novel/read/utlis/LocalManageUtil.java @@ -39,11 +39,9 @@ public class LocalManageUtil { /** * 获取选择的语言设置 - * - * @param context - * @return + * @param context context */ - public static Locale getSetLanguageLocale(Context context) { + private static Locale getSetLanguageLocale(Context context) { switch (SpUtil.getInstance().getSelectLanguage()) { @@ -59,7 +57,7 @@ public class LocalManageUtil { public static void saveSelectLanguage(Context context, int select) { SpUtil.getInstance().saveLanguage(select); //本地国际化 - ReadSettingManager.getInstance().setConvertType(select); //网络数据国际化 + ReadSettingManager.Companion.getInstance().setConvertType(select); //网络数据国际化 setApplicationLanguage(context); } diff --git a/app/src/main/java/com/novel/read/utlis/MD5Utils.java b/app/src/main/java/com/novel/read/utlis/MD5Utils.java deleted file mode 100644 index 0a61b5a..0000000 --- a/app/src/main/java/com/novel/read/utlis/MD5Utils.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.novel.read.utlis; - -/** - * Created by newbiechen on 2018/1/1. - */ - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - *@Description: 将字符串转化为MD5 - */ - -public class MD5Utils { - - public static String strToMd5By32(String str){ - String reStr = null; - try { - MessageDigest md5 = MessageDigest.getInstance("MD5"); - byte[] bytes = md5.digest(str.getBytes()); - StringBuffer stringBuffer = new StringBuffer(); - for (byte b : bytes){ - int bt = b&0xff; - if (bt < 16){ - stringBuffer.append(0); - } - stringBuffer.append(Integer.toHexString(bt)); - } - reStr = stringBuffer.toString(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - return reStr; - } - - public static String strToMd5By16(String str){ - String reStr = strToMd5By32(str); - if (reStr != null){ - reStr = reStr.substring(8, 24); - } - return reStr; - } -} diff --git a/app/src/main/java/com/novel/read/utlis/MD5Utils.kt b/app/src/main/java/com/novel/read/utlis/MD5Utils.kt new file mode 100644 index 0000000..999e94f --- /dev/null +++ b/app/src/main/java/com/novel/read/utlis/MD5Utils.kt @@ -0,0 +1,41 @@ +package com.novel.read.utlis + +import java.security.MessageDigest +import java.security.NoSuchAlgorithmException +import kotlin.experimental.and + +/** + * 将字符串转化为MD5 + */ + +object MD5Utils { + + private fun strToMd5By32(str: String): String? { + var reStr: String? = null + try { + val md5 = MessageDigest.getInstance("MD5") + val bytes = md5.digest(str.toByteArray()) + val stringBuffer = StringBuilder() + for (b in bytes) { + val bt = b and 0xff.toByte() + if (bt < 16) { + stringBuffer.append(0) + } + stringBuffer.append(Integer.toHexString(bt.toInt())) + } + reStr = stringBuffer.toString() + } catch (e: NoSuchAlgorithmException) { + e.printStackTrace() + } + + return reStr + } + + fun strToMd5By16(str: String): String? { + var reStr = strToMd5By32(str) + if (reStr != null) { + reStr = reStr.substring(8, 24) + } + return reStr + } +} diff --git a/app/src/main/java/com/novel/read/utlis/SpUtil.java b/app/src/main/java/com/novel/read/utlis/SpUtil.java index 6c4696e..584ecc6 100644 --- a/app/src/main/java/com/novel/read/utlis/SpUtil.java +++ b/app/src/main/java/com/novel/read/utlis/SpUtil.java @@ -8,7 +8,7 @@ import java.util.Locale; public class SpUtil { - static SharedPreferences sp; + private static SharedPreferences sp; private static SpUtil sInstance; public static void init(Context context) { @@ -91,9 +91,6 @@ public class SpUtil { editor.apply(); } - - - private final String TAG_LANGUAGE = "language_select"; private Locale systemCurrentLocal = Locale.ENGLISH; diff --git a/app/src/main/java/com/novel/read/utlis/StringUtils.kt b/app/src/main/java/com/novel/read/utlis/StringUtils.kt index f9dadf2..e55ef6f 100644 --- a/app/src/main/java/com/novel/read/utlis/StringUtils.kt +++ b/app/src/main/java/com/novel/read/utlis/StringUtils.kt @@ -2,11 +2,10 @@ package com.novel.read.utlis import android.content.Context import android.text.TextUtils +import com.novel.read.widget.page.ReadSettingManager.Companion.SHARED_READ_CONVERT_TYPE import com.spreada.utils.chinese.ZHConverter -import com.novel.read.widget.page.ReadSettingManager.SHARED_READ_CONVERT_TYPE - object StringUtils { /** diff --git a/app/src/main/java/com/novel/read/widget/page/LocalPageLoader.java b/app/src/main/java/com/novel/read/widget/page/LocalPageLoader.java index bd58865..4a8630d 100644 --- a/app/src/main/java/com/novel/read/widget/page/LocalPageLoader.java +++ b/app/src/main/java/com/novel/read/widget/page/LocalPageLoader.java @@ -398,7 +398,7 @@ public class LocalPageLoader extends PageLoader { for (int i = 0; i < mChapterList.size(); ++i) { TxtChapter chapter = mChapterList.get(i); BookChapterBean bean = new BookChapterBean(); - bean.setId(MD5Utils.strToMd5By16(mBookFile.getAbsolutePath() + bean.setId(MD5Utils.INSTANCE.strToMd5By16(mBookFile.getAbsolutePath() + File.separator + chapter.title)); // 将路径+i 作为唯一值 bean.setTitle(chapter.getTitle()); bean.setStart(chapter.getStart()); diff --git a/app/src/main/java/com/novel/read/widget/page/PageLoader.java b/app/src/main/java/com/novel/read/widget/page/PageLoader.java index 6f881b5..823ab05 100644 --- a/app/src/main/java/com/novel/read/widget/page/PageLoader.java +++ b/app/src/main/java/com/novel/read/widget/page/PageLoader.java @@ -171,7 +171,7 @@ public abstract class PageLoader { private void initData() { // 获取配置管理器 - mSettingManager = ReadSettingManager.getInstance(); + mSettingManager = ReadSettingManager.Companion.getInstance(); // 获取配置参数 mPageMode = mSettingManager.getPageMode(); mPageStyle = mSettingManager.getPageStyle(); diff --git a/app/src/main/java/com/novel/read/widget/page/ReadSettingManager.java b/app/src/main/java/com/novel/read/widget/page/ReadSettingManager.java deleted file mode 100644 index 63b1609..0000000 --- a/app/src/main/java/com/novel/read/widget/page/ReadSettingManager.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.novel.read.widget.page; - - -import com.novel.read.utlis.ScreenUtils; -import com.novel.read.utlis.SpUtil; - -public class ReadSettingManager { - /*************实在想不出什么好记的命名方式。。******************/ - public static final int READ_BG_DEFAULT = 0; - public static final int READ_BG_1 = 1; - public static final int READ_BG_2 = 2; - public static final int READ_BG_3 = 3; - public static final int READ_BG_4 = 4; - public static final int NIGHT_MODE = 5; - - public static final String SHARED_READ_BG = "shared_read_bg"; - public static final String SHARED_READ_BRIGHTNESS = "shared_read_brightness"; - public static final String SHARED_READ_IS_BRIGHTNESS_AUTO = "shared_read_is_brightness_auto"; - public static final String SHARED_READ_TEXT_SIZE = "shared_read_text_size"; - public static final String SHARED_READ_IS_TEXT_DEFAULT = "shared_read_text_default"; - public static final String SHARED_READ_PAGE_MODE = "shared_read_mode"; - public static final String SHARED_READ_NIGHT_MODE = "shared_night_mode"; - public static final String SHARED_READ_VOLUME_TURN_PAGE = "shared_read_volume_turn_page"; - public static final String SHARED_READ_FULL_SCREEN = "shared_read_full_screen"; - public static final String SHARED_READ_CONVERT_TYPE = "shared_read_convert_type"; - - private static volatile ReadSettingManager sInstance; - - public static ReadSettingManager getInstance() { - if (sInstance == null) { - synchronized (ReadSettingManager.class) { - if (sInstance == null) { - sInstance = new ReadSettingManager(); - } - } - } - return sInstance; - } - - private ReadSettingManager() { - } - - public void setPageStyle(PageStyle pageStyle) { - SpUtil.setIntValue(SHARED_READ_BG, pageStyle.ordinal()); - } - - public void setBrightness(int progress) { - SpUtil.setIntValue(SHARED_READ_BRIGHTNESS, progress); - } - - public void setAutoBrightness(boolean isAuto) { - SpUtil.setBooleanValue(SHARED_READ_IS_BRIGHTNESS_AUTO, isAuto); - } - - public void setDefaultTextSize(boolean isDefault) { - SpUtil.setBooleanValue(SHARED_READ_IS_TEXT_DEFAULT, isDefault); - } - - public void setTextSize(int textSize) { - SpUtil.setIntValue(SHARED_READ_TEXT_SIZE, textSize); - } - - public void setPageMode(PageMode mode) { - SpUtil.setIntValue(SHARED_READ_PAGE_MODE, mode.ordinal()); - } - - public void setNightMode(boolean isNight) { - SpUtil.setBooleanValue(SHARED_READ_NIGHT_MODE, isNight); - } - - public int getBrightness() { - return SpUtil.getIntValue(SHARED_READ_BRIGHTNESS, 40); - } - - public boolean isBrightnessAuto() { - return SpUtil.getBooleanValue(SHARED_READ_IS_BRIGHTNESS_AUTO, false); - } - - public int getTextSize() { - return SpUtil.getIntValue(SHARED_READ_TEXT_SIZE, ScreenUtils.INSTANCE.spToPx(16)); - } - - public boolean isDefaultTextSize() { - return SpUtil.getBooleanValue(SHARED_READ_IS_TEXT_DEFAULT, false); - } - - public PageMode getPageMode() { - int mode = SpUtil.getIntValue(SHARED_READ_PAGE_MODE, PageMode.SIMULATION.ordinal()); - return PageMode.values()[mode]; - } - - public PageStyle getPageStyle() { - int style = SpUtil.getIntValue(SHARED_READ_BG, PageStyle.BG_0.ordinal()); - return PageStyle.values()[style]; - } - - public boolean isNightMode() { - return SpUtil.getBooleanValue(SHARED_READ_NIGHT_MODE, false); - } - - public void setVolumeTurnPage(boolean isTurn) { - SpUtil.setBooleanValue(SHARED_READ_VOLUME_TURN_PAGE, isTurn); - } - - public boolean isVolumeTurnPage() { - return SpUtil.getBooleanValue(SHARED_READ_VOLUME_TURN_PAGE, false); - } - - public void setFullScreen(boolean isFullScreen) { - SpUtil.setBooleanValue(SHARED_READ_FULL_SCREEN, isFullScreen); - } - - public boolean isFullScreen() { - return SpUtil.getBooleanValue(SHARED_READ_FULL_SCREEN, false); - } - - public void setConvertType(int convertType) { - SpUtil.setIntValue(SHARED_READ_CONVERT_TYPE, convertType); - } - - public int getConvertType() { - return SpUtil.getIntValue(SHARED_READ_CONVERT_TYPE, 1); - } -} diff --git a/app/src/main/java/com/novel/read/widget/page/ReadSettingManager.kt b/app/src/main/java/com/novel/read/widget/page/ReadSettingManager.kt new file mode 100644 index 0000000..b4f3adf --- /dev/null +++ b/app/src/main/java/com/novel/read/widget/page/ReadSettingManager.kt @@ -0,0 +1,104 @@ +package com.novel.read.widget.page + + +import com.novel.read.utlis.DialogUtils +import com.novel.read.utlis.ScreenUtils +import com.novel.read.utlis.SpUtil + +class ReadSettingManager private constructor() { + + var brightness: Int + get() = SpUtil.getIntValue(SHARED_READ_BRIGHTNESS, 40) + set(progress) = SpUtil.setIntValue(SHARED_READ_BRIGHTNESS, progress) + + val isBrightnessAuto: Boolean + get() = SpUtil.getBooleanValue(SHARED_READ_IS_BRIGHTNESS_AUTO, false) + + var textSize: Int + get() = SpUtil.getIntValue(SHARED_READ_TEXT_SIZE, ScreenUtils.spToPx(16)) + set(textSize) = SpUtil.setIntValue(SHARED_READ_TEXT_SIZE, textSize) + + var isDefaultTextSize: Boolean + get() = SpUtil.getBooleanValue(SHARED_READ_IS_TEXT_DEFAULT, false) + set(isDefault) = SpUtil.setBooleanValue(SHARED_READ_IS_TEXT_DEFAULT, isDefault) + + var pageMode: PageMode + get() { + val mode = SpUtil.getIntValue(SHARED_READ_PAGE_MODE, PageMode.SIMULATION.ordinal) + return PageMode.values()[mode] + } + set(mode) = SpUtil.setIntValue(SHARED_READ_PAGE_MODE, mode.ordinal) + + var pageStyle: PageStyle + get() { + val style = SpUtil.getIntValue(SHARED_READ_BG, PageStyle.BG_0.ordinal) + return PageStyle.values()[style] + } + set(pageStyle) = SpUtil.setIntValue(SHARED_READ_BG, pageStyle.ordinal) + + var isNightMode: Boolean + get() = SpUtil.getBooleanValue(SHARED_READ_NIGHT_MODE, false) + set(isNight) = SpUtil.setBooleanValue(SHARED_READ_NIGHT_MODE, isNight) + + var isVolumeTurnPage: Boolean + get() = SpUtil.getBooleanValue(SHARED_READ_VOLUME_TURN_PAGE, false) + set(isTurn) = SpUtil.setBooleanValue(SHARED_READ_VOLUME_TURN_PAGE, isTurn) + + var isFullScreen: Boolean + get() = SpUtil.getBooleanValue(SHARED_READ_FULL_SCREEN, false) + set(isFullScreen) = SpUtil.setBooleanValue(SHARED_READ_FULL_SCREEN, isFullScreen) + + var convertType: Int + get() = SpUtil.getIntValue(SHARED_READ_CONVERT_TYPE, 1) + set(convertType) = SpUtil.setIntValue(SHARED_READ_CONVERT_TYPE, convertType) + + fun setAutoBrightness(isAuto: Boolean) { + SpUtil.setBooleanValue(SHARED_READ_IS_BRIGHTNESS_AUTO, isAuto) + } + + companion object { + /*************实在想不出什么好记的命名方式。。 */ + val READ_BG_DEFAULT = 0 + val READ_BG_1 = 1 + val READ_BG_2 = 2 + val READ_BG_3 = 3 + val READ_BG_4 = 4 + val NIGHT_MODE = 5 + + val SHARED_READ_BG = "shared_read_bg" + val SHARED_READ_BRIGHTNESS = "shared_read_brightness" + val SHARED_READ_IS_BRIGHTNESS_AUTO = "shared_read_is_brightness_auto" + val SHARED_READ_TEXT_SIZE = "shared_read_text_size" + val SHARED_READ_IS_TEXT_DEFAULT = "shared_read_text_default" + val SHARED_READ_PAGE_MODE = "shared_read_mode" + val SHARED_READ_NIGHT_MODE = "shared_night_mode" + val SHARED_READ_VOLUME_TURN_PAGE = "shared_read_volume_turn_page" + val SHARED_READ_FULL_SCREEN = "shared_read_full_screen" + val SHARED_READ_CONVERT_TYPE = "shared_read_convert_type" + +// @Volatile +// private var sInstance: ReadSettingManager? = null +// +// val instance: ReadSettingManager? +// get() { +// if (sInstance == null) { +// synchronized(ReadSettingManager::class.java) { +// if (sInstance == null) { +// sInstance = ReadSettingManager() +// } +// } +// } +// return sInstance +// } + @Volatile + private var instance: ReadSettingManager? = null + + @Synchronized + fun getInstance(): ReadSettingManager { + if (instance == null) { + instance = ReadSettingManager() + } + return instance as ReadSettingManager + } + } +}