Page turning via volume key

This commit is contained in:
seven332 committed 2015-04-10 16:43:58 +08:00
1 parent b18cdd04ce
commit 2027b806df
11 files changed
+221 -36

No files matched your search

@@ -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<Set<Receiver>> 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<Receiver> receiverSet = mReceiverSetMap.get(id);
if (receiverSet != null) {
for (Receiver receiver : receiverSet) {
receiver.onReceive(obj);
}
}
}
});
}
public void register(int id, Receiver receiver) {
Set<Receiver> 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<Receiver> receiverSet = mReceiverSetMap.get(id);
if (receiverSet != null) {
receiverSet.remove(receiver);
if (receiverSet.isEmpty()) {
mReceiverSetMap.remove(id);
}
}
}
public interface Receiver {
void onReceive (final Object obj);
}
}
@@ -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);
@@ -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()) {
@@ -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<DownloadInfo> diList, String filename) {
for (DownloadInfo di : diList)
if (filename.startsWith(String.valueOf(di.galleryInfo.gid)))
@@ -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);
}
@@ -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();
}
}
@@ -238,6 +238,7 @@
<string name="page_scaling_title">页面缩放</string>
<string name="start_position_title">开页位置</string>
<string name="keep_screen_on_title">阅读时屏幕常亮</string>
<string name="volume_page_title">音量键翻页</string>
<string name="gallery_show_clock_title">显示时间</string>
<string name="gallery_show_battery_title">显示电量</string>
<string name="custom_lightness_title">阅读时使用自定义屏幕亮度</string>
+2 -1
View File
@@ -226,6 +226,7 @@
<string name="page_scaling_title">頁面縮放</string>
<string name="start_position_title">開頁位置</string>
<string name="keep_screen_on_title">閲讀時屏幕常亮</string>
<string name="volume_page_title">音量鍵翻頁</string>
<string name="gallery_show_clock_title">顯示時間</string>
<string name="gallery_show_battery_title">顯示電量</string>
<string name="custom_lightness_title">閲讀時使用自定義屏幕亮度</string>
@@ -234,7 +235,7 @@
<string name="custom_codec_summary">自定義解碼可能會更使用更少內存,解碼更快</string>
<string name="custom_codec_summary_not">自定義解碼暫不支持 x86, mips</string>
<string name="clean_redundancy_title">清理閲讀宂餘</string>
<string name="clean_redundancy_mesg">你確定要清理閲讀宂餘嗎?不在下載列表中的畫廊圖片文件文件將被刪除。</string>
<string name="clean_redundancy_mesg">你確定要清理閲讀宂餘嗎?不在下載列表中的畫廊圖片文件將被刪除。</string>
<string name="no_redundancy">未發現宂餘</string>
<string name="clean_redundancy">完成宂餘清理,共清理 %d 項</string>
<string name="decode_format_title">解碼格式</string>
+2 -1
View File
@@ -226,6 +226,7 @@
<string name="page_scaling_title">頁面縮放</string>
<string name="start_position_title">開頁位置</string>
<string name="keep_screen_on_title">閱讀時螢幕常亮</string>
<string name="volume_page_title">音量鍵翻頁</string>
<string name="gallery_show_clock_title">顯示時間</string>
<string name="gallery_show_battery_title">顯示電量</string>
<string name="custom_lightness_title">閱讀時使用自定義螢幕亮度</string>
@@ -234,7 +235,7 @@
<string name="custom_codec_summary">自定義解碼可能會更使用更少記憶體,解碼更快</string>
<string name="custom_codec_summary_not">自定義解碼暫不支援 x86, mips</string>
<string name="clean_redundancy_title">清理閱讀冗餘</string>
<string name="clean_redundancy_mesg">你確定要清理閱讀冗餘嗎?不在下載列表中的畫廊圖片檔案檔案將被刪除。</string>
<string name="clean_redundancy_mesg">你確定要清理閱讀冗餘嗎?不在下載列表中的畫廊圖片檔案將被刪除。</string>
<string name="no_redundancy">未發現冗餘</string>
<string name="clean_redundancy">完成冗餘清理,共清理 %d 項</string>
<string name="decode_format_title">解碼格式</string>
+1
View File
@@ -243,6 +243,7 @@
<string name="page_scaling_title">Page scaling</string>
<string name="start_position_title">Start position</string>
<string name="keep_screen_on_title">Keep screen when reading</string>
<string name="volume_page_title">Page turn via volume key</string>
<string name="gallery_show_clock_title">Show clock</string>
<string name="gallery_show_battery_title">Show battery</string>
<string name="custom_lightness_title">Custom lightness when reading</string>
+6 -1
View File
@@ -43,6 +43,11 @@
android:title="@string/keep_screen_on_title"
android:defaultValue="false" />
<CheckBoxPreference
android:key="volume_page"
android:title="@string/volume_page_title"
android:defaultValue="false" />
<CheckBoxPreference
android:key="gallery_show_clock"
android:title="@string/gallery_show_clock_title"
@@ -82,4 +87,4 @@
android:key="clean_redundancy"
android:title="@string/clean_redundancy_title" />
</PreferenceScreen>
</PreferenceScreen>