diff --git a/app/src/main/java/com/hippo/ehviewer/content/Messenger.java b/app/src/main/java/com/hippo/ehviewer/content/Messenger.java new file mode 100644 index 000000000..80963abcc --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/content/Messenger.java @@ -0,0 +1,73 @@ +package com.hippo.ehviewer.content; + +import android.util.SparseArray; + +import com.hippo.ehviewer.AppHandler; +import com.hippo.ehviewer.util.IntIdGenerator; + +import java.util.HashSet; +import java.util.Set; + +public class Messenger { + + private IntIdGenerator mIdGenerator; + + private SparseArray> mReceiverSetMap; + + private static Messenger sInstance; + + public static Messenger getInstance() { + if (sInstance == null) { + sInstance = new Messenger(); + } + return sInstance; + } + + private Messenger() { + mIdGenerator = IntIdGenerator.create(); + mReceiverSetMap = new SparseArray<>(); + } + + public int newId() { + return mIdGenerator.nextId(); + } + + public void notify(final int id, final Object obj) { + // Make sure do it in UI thread + AppHandler.getInstance().post(new Runnable() { + @Override + public void run() { + Set receiverSet = mReceiverSetMap.get(id); + if (receiverSet != null) { + for (Receiver receiver : receiverSet) { + receiver.onReceive(obj); + } + } + } + }); + } + + public void register(int id, Receiver receiver) { + Set receiverSet = mReceiverSetMap.get(id); + if (receiverSet == null) { + receiverSet = new HashSet<>(); + mReceiverSetMap.put(id, receiverSet); + } + + receiverSet.add(receiver); + } + + public void unregister(int id, Receiver receiver) { + Set receiverSet = mReceiverSetMap.get(id); + if (receiverSet != null) { + receiverSet.remove(receiver); + if (receiverSet.isEmpty()) { + mReceiverSetMap.remove(id); + } + } + } + + public interface Receiver { + void onReceive (final Object obj); + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/GalleryView.java b/app/src/main/java/com/hippo/ehviewer/gallery/GalleryView.java index 21d4a4585..b114610f6 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/GalleryView.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/GalleryView.java @@ -16,8 +16,6 @@ package com.hippo.ehviewer.gallery; -import java.util.Arrays; - import android.content.Context; import android.graphics.Bitmap; import android.graphics.Movie; @@ -52,6 +50,8 @@ import com.hippo.ehviewer.util.Utils; import com.hippo.ehviewer.util.ZInterpolator; import com.hippo.ehviewer.widget.MaterialToast; +import java.util.Arrays; + public class GalleryView extends GLView implements ImageSet.ImageListener, Runnable { @SuppressWarnings("unused") @@ -958,6 +958,25 @@ public class GalleryView extends GLView implements ImageSet.ImageListener, return false; } + public void showPreviousPage() { + // TODO goto bottom first the to pre page + resetSizePosition(PRE_TARGET_INDEX); + if (!goToPrePage()) { + // Get to first page + MaterialToast.showToast(mContext.getString(R.string.first_page)); + } + } + + public void showNextPage() { + // TODO goto bottom first the to pre page + resetSizePosition(NEXT_TARGET_INDEX); + if (!goToNextPage()) { + // Get to last page + MaterialToast.showToast(mContext.getString(isEnsureSize ? + R.string.the_last_page : R.string.wait_for_more)); + } + } + private class MyGestureListener implements GestureRecognizer.Listener { private boolean isScale = false; @@ -1184,25 +1203,6 @@ public class GalleryView extends GLView implements ImageSet.ImageListener, mDoubleTapRunner.setInterpolator(new ZInterpolator(0.5f)); } - private void onTapPre() { - // TODO goto bottom first the to pre page - resetSizePosition(PRE_TARGET_INDEX); - if (!goToPrePage()) { - // Get to first page - MaterialToast.showToast(mContext.getString(R.string.first_page)); - } - } - - private void onTapNext() { - // TODO goto bottom first the to pre page - resetSizePosition(NEXT_TARGET_INDEX); - if (!goToNextPage()) { - // Get to last page - MaterialToast.showToast(mContext.getString(isEnsureSize ? - R.string.the_last_page : R.string.wait_for_more)); - } - } - @Override public boolean onSingleTapConfirmed(float x, float y) { if (mScrollState != SCROLL_NONE) { @@ -1224,18 +1224,18 @@ public class GalleryView extends GLView implements ImageSet.ImageListener, if (Utils.isInArea(leftArea, (int) x, (int) y)) { if (isRightToLeft()) - onTapNext(); + showNextPage(); else - onTapPre(); + showPreviousPage(); } else if (Utils.isInArea(topArea, (int) x, (int) y)) { zoom(true); } else if (Utils.isInArea(rightArea, (int) x, (int) y)) { if (isRightToLeft()) - onTapPre(); + showPreviousPage(); else - onTapNext(); + showNextPage(); } else if (Utils.isInArea(bottomArea, (int) x, (int) y)) { zoom(false); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java index 5d2eaf3b1..267d4f146 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java @@ -16,8 +16,6 @@ package com.hippo.ehviewer.ui; -import java.io.File; - import android.annotation.TargetApi; import android.app.Dialog; import android.content.Intent; @@ -25,6 +23,8 @@ import android.content.res.Configuration; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -39,6 +39,7 @@ import com.hippo.ehviewer.AppHandler; import com.hippo.ehviewer.R; import com.hippo.ehviewer.app.DirSelectDialog; import com.hippo.ehviewer.app.MaterialAlertDialog; +import com.hippo.ehviewer.content.Messenger; import com.hippo.ehviewer.data.Data; import com.hippo.ehviewer.data.GalleryInfo; import com.hippo.ehviewer.ehclient.ExDownloader; @@ -53,11 +54,13 @@ import com.hippo.ehviewer.widget.ColorView; import com.hippo.ehviewer.widget.MaterialToast; import com.hippo.ehviewer.widget.SlidingLayout; +import java.io.File; + public class GalleryActivity extends AbsActivity implements GalleryView.GalleryViewListener, SeekBar.OnSeekBarChangeListener, FullScreenHelper.OnFullScreenBrokenListener, CompoundButton.OnCheckedChangeListener, SlidingLayout.OnChildHideListener, AdapterView.OnItemSelectedListener, - View.OnClickListener, Utils.OnCopyOverListener { + View.OnClickListener, Utils.OnCopyOverListener, Messenger.Receiver { @SuppressWarnings("unused") private final static String TAG = GalleryActivity.class.getSimpleName(); @@ -110,6 +113,8 @@ public class GalleryActivity extends AbsActivity private Dialog mSendDialog; + private boolean mVolumePage; + private final Runnable mFullScreenTask = new Runnable() { @Override public void run() { mFullScreenHelper.setFullScreen(true); @@ -126,8 +131,7 @@ public class GalleryActivity extends AbsActivity @Override public void onCopyOver(boolean success, File src, File dst) { - MaterialToast.showToast(success ? String.format(getString(R.string.save_successful), dst.getPath()) - : getString(R.string.save_failed)); + MaterialToast.showToast(success ? String.format(getString(R.string.save_successful), dst.getPath()) : getString(R.string.save_failed)); if (success) Config.setString(KEY_LAST_SAVE_IMAGE_FILE_PATH, dst.getParent()); } @@ -265,7 +269,9 @@ public class GalleryActivity extends AbsActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.gl_root_group); + + mVolumePage = Config.getVolumePage(); + Messenger.getInstance().register(Config.MESSENGER_ID_VOLUME_PAGE, this); mFullScreenHelper = new FullScreenHelper(this); mFullScreenHelper.setOnFullScreenBrokenListener(this); @@ -298,7 +304,7 @@ public class GalleryActivity extends AbsActivity mSendDialog = createSendDialog(); Intent intent = getIntent(); - mGi = (GalleryInfo) intent.getParcelableExtra(KEY_GALLERY_INFO); + mGi = intent.getParcelableExtra(KEY_GALLERY_INFO); if (mGi == null) { // Get error force finish @@ -382,6 +388,8 @@ public class GalleryActivity extends AbsActivity protected void onDestroy() { super.onDestroy(); + Messenger.getInstance().unregister(Config.MESSENGER_ID_VOLUME_PAGE, this); + if (mImageSet != null) mImageSet.free(); if (mGalleryView != null) @@ -416,6 +424,31 @@ public class GalleryActivity extends AbsActivity } } + @Override + public boolean onKeyDown (int keyCode, @NonNull KeyEvent event) { + if (mVolumePage) { + switch (keyCode) { + case KeyEvent.KEYCODE_VOLUME_DOWN: + mGalleryView.showPreviousPage(); + return true; + case KeyEvent.KEYCODE_VOLUME_UP: + mGalleryView.showNextPage(); + return true; + default: + return super.onKeyDown(keyCode, event); + } + } else { + return super.onKeyDown(keyCode, event); + } + } + + @Override + public void onReceive(Object obj) { + if (obj instanceof Boolean) { + mVolumePage = (Boolean) obj; + } + } + @Override public void onTapCenter() { if (!mConfigSliding.isShowing()) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/SettingsActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/SettingsActivity.java index 549d5dfdc..f2fab89c7 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/SettingsActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/SettingsActivity.java @@ -50,6 +50,7 @@ import com.hippo.ehviewer.UpdateHelper; import com.hippo.ehviewer.app.DirSelectDialog; import com.hippo.ehviewer.app.MaterialAlertDialog; import com.hippo.ehviewer.app.MaterialProgressDialog; +import com.hippo.ehviewer.content.Messenger; import com.hippo.ehviewer.data.ApiGalleryInfo; import com.hippo.ehviewer.data.Data; import com.hippo.ehviewer.data.DownloadInfo; @@ -81,6 +82,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.prefs.PreferenceChangeListener; public class SettingsActivity extends AbsPreferenceActivity { @SuppressWarnings("unused") @@ -400,11 +402,13 @@ public class SettingsActivity extends AbsPreferenceActivity { } public static class ReadFragment extends PreferenceFragment implements - Preference.OnPreferenceClickListener { + Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { + private static final String KEY_VOLUME_PAGE = "volume_page"; private static final String KEY_CUSTOM_CODEC = "custom_codec"; private static final String KEY_CLEAN_REDUNDANCY = "clean_redundancy"; + private CheckBoxPreference mVolumePage; private CheckBoxPreference mCustomCodec; private Preference mCleanRedundancy; @@ -415,6 +419,8 @@ public class SettingsActivity extends AbsPreferenceActivity { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.read_settings); + mVolumePage = (CheckBoxPreference) findPreference(KEY_VOLUME_PAGE); + mVolumePage.setOnPreferenceChangeListener(this); mCustomCodec = (CheckBoxPreference) findPreference(KEY_CUSTOM_CODEC); mCleanRedundancy = findPreference(KEY_CLEAN_REDUNDANCY); mCleanRedundancy.setOnPreferenceClickListener(this); @@ -428,6 +434,17 @@ public class SettingsActivity extends AbsPreferenceActivity { } } + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + final String key = preference.getKey(); + switch (key) { + case KEY_VOLUME_PAGE: + Messenger.getInstance().notify(Config.MESSENGER_ID_VOLUME_PAGE, newValue); + break; + } + return true; + } + private boolean isInDownloadList(List diList, String filename) { for (DownloadInfo di : diList) if (filename.startsWith(String.valueOf(di.galleryInfo.gid))) diff --git a/app/src/main/java/com/hippo/ehviewer/util/Config.java b/app/src/main/java/com/hippo/ehviewer/util/Config.java index 048f19139..98e760d57 100644 --- a/app/src/main/java/com/hippo/ehviewer/util/Config.java +++ b/app/src/main/java/com/hippo/ehviewer/util/Config.java @@ -22,6 +22,7 @@ import android.content.pm.ActivityInfo; import android.os.Environment; import android.preference.PreferenceManager; +import com.hippo.ehviewer.content.Messenger; import com.hippo.ehviewer.data.ListUrls; import com.hippo.ehviewer.ehclient.EhClient; @@ -43,6 +44,14 @@ public final class Config { private static SharedPreferences mConfigPre; + public static final int MESSENGER_ID_VOLUME_PAGE; + + static { + Messenger messenger = Messenger.getInstance(); + MESSENGER_ID_VOLUME_PAGE = messenger.newId(); + } + + /** * Init Config * @@ -343,6 +352,9 @@ public final class Config { private static final String KEY_START_POSITION = "start_position"; private static final int DEFAULT_START_POSITION = 1; + private static final String KEY_VOLUME_PAGE = "volume_page"; + private static final boolean DEFAULT_VOLUME_PAGE = false; + private static final String KEY_GALLERY_SHOW_CLOCK = "gallery_show_clock"; private static final boolean DEFAULT_GALLERY_SHOW_CLOCK = true; @@ -395,6 +407,15 @@ public final class Config { setBoolean(KEY_KEEP_SCREEN_ON, value); } + public static void setVolumePage(boolean value) { + setBoolean(KEY_VOLUME_PAGE, value); + } + + public static boolean getVolumePage() { + return getBoolean(KEY_VOLUME_PAGE, DEFAULT_VOLUME_PAGE); + } + + public static boolean getShowClock() { return getBoolean(KEY_GALLERY_SHOW_CLOCK, DEFAULT_GALLERY_SHOW_CLOCK); } diff --git a/app/src/main/java/com/hippo/ehviewer/util/IntIdGenerator.java b/app/src/main/java/com/hippo/ehviewer/util/IntIdGenerator.java new file mode 100644 index 000000000..d3d852862 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/util/IntIdGenerator.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2015 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.hippo.ehviewer.util; + +public final class IntIdGenerator { + + private int mId = 0; + + private IntIdGenerator() { + // Empty + } + + public synchronized int nextId() { + return mId++; + } + + public static IntIdGenerator create() { + return new IntIdGenerator(); + } +} diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index dcf1734ce..9edf090a2 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -238,6 +238,7 @@ 页面缩放 开页位置 阅读时屏幕常亮 + 音量键翻页 显示时间 显示电量 阅读时使用自定义屏幕亮度 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 32c566426..82d920c7f 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -226,6 +226,7 @@ 頁面縮放 開頁位置 閲讀時屏幕常亮 + 音量鍵翻頁 顯示時間 顯示電量 閲讀時使用自定義屏幕亮度 @@ -234,7 +235,7 @@ 自定義解碼可能會更使用更少內存,解碼更快 自定義解碼暫不支持 x86, mips 清理閲讀宂餘 - 你確定要清理閲讀宂餘嗎?不在下載列表中的畫廊圖片文件文件將被刪除。 + 你確定要清理閲讀宂餘嗎?不在下載列表中的畫廊圖片文件將被刪除。 未發現宂餘 完成宂餘清理,共清理 %d 項 解碼格式 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index eec093ba0..1c2caf823 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -226,6 +226,7 @@ 頁面縮放 開頁位置 閱讀時螢幕常亮 + 音量鍵翻頁 顯示時間 顯示電量 閱讀時使用自定義螢幕亮度 @@ -234,7 +235,7 @@ 自定義解碼可能會更使用更少記憶體,解碼更快 自定義解碼暫不支援 x86, mips 清理閱讀冗餘 - 你確定要清理閱讀冗餘嗎?不在下載列表中的畫廊圖片檔案檔案將被刪除。 + 你確定要清理閱讀冗餘嗎?不在下載列表中的畫廊圖片檔案將被刪除。 未發現冗餘 完成冗餘清理,共清理 %d 項 解碼格式 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 40bd01743..15330b9da 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -243,6 +243,7 @@ Page scaling Start position Keep screen when reading + Page turn via volume key Show clock Show battery Custom lightness when reading diff --git a/app/src/main/res/xml/read_settings.xml b/app/src/main/res/xml/read_settings.xml index 41dfbef53..2da82e65d 100644 --- a/app/src/main/res/xml/read_settings.xml +++ b/app/src/main/res/xml/read_settings.xml @@ -43,6 +43,11 @@ android:title="@string/keep_screen_on_title" android:defaultValue="false" /> + + - \ No newline at end of file +