From 140bf8caee6b06671262f5d58775ee5ba2a1e433 Mon Sep 17 00:00:00 2001 From: inorichi Date: Fri, 12 Feb 2016 19:36:00 +0100 Subject: [PATCH] Allow to force a rotation --- .../data/preference/PreferencesHelper.java | 4 ++ .../tachiyomi/ui/reader/ReaderActivity.java | 47 +++++++++---------- .../tachiyomi/ui/reader/ReaderMenu.java | 23 +++++++-- app/src/main/res/values/arrays.xml | 14 ++++++ app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 6 +++ 6 files changed, 65 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java index b3c6132fc..512910d6c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.java @@ -60,6 +60,10 @@ public class PreferencesHelper { return rxPrefs.getBoolean(getKey(R.string.pref_lock_orientation_key), true); } + public Preference rotation() { + return rxPrefs.getInteger(getKey(R.string.pref_rotation_type_key), 1); + } + public Preference enableTransitions() { return rxPrefs.getBoolean(getKey(R.string.pref_enable_transitions_key), true); } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java index 4f960e096..7965f1654 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.java @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.reader; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; +import android.content.res.Configuration; import android.graphics.Color; import android.os.Build; import android.os.Bundle; @@ -13,7 +14,6 @@ import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; -import android.view.Surface; import android.view.View; import android.view.WindowManager; import android.widget.TextView; @@ -277,9 +277,9 @@ public class ReaderActivity extends BaseRxActivity { .asObservable() .subscribe(this::setPageNumberVisibility)); - subscriptions.add(preferences.lockOrientation() + subscriptions.add(preferences.rotation() .asObservable() - .subscribe(this::setOrientation)); + .subscribe(this::setRotation)); subscriptions.add(preferences.hideStatusBar() .asObservable() @@ -299,28 +299,25 @@ public class ReaderActivity extends BaseRxActivity { .subscribe(this::applyTheme)); } - private void setOrientation(boolean locked) { - if (locked) { - int orientation; - int rotation = ((WindowManager) getSystemService( - Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); - switch (rotation) { - case Surface.ROTATION_0: - orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; - break; - case Surface.ROTATION_90: - orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; - break; - case Surface.ROTATION_180: - orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; - break; - default: - orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; - break; - } - setRequestedOrientation(orientation); - } else { - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + private void setRotation(int rotation) { + switch (rotation) { + // Rotation free + case 1: + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); + break; + // Lock in current rotation + case 2: + int currentOrientation = getResources().getConfiguration().orientation; + setRotation(currentOrientation == Configuration.ORIENTATION_PORTRAIT ? 3 : 4); + break; + // Lock in portrait + case 3: + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); + break; + // Lock in landscape + case 4: + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); + break; } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java index 386a324a9..d1274dfe6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.reader; import android.app.Dialog; import android.content.Context; +import android.content.res.Configuration; import android.support.v7.widget.Toolbar; import android.view.Gravity; import android.view.Menu; @@ -178,20 +179,31 @@ public class ReaderMenu { if (nextChapterBtn != null) nextChapterBtn.setVisible(nextChapter != null); } + @SuppressWarnings("ConstantConditions") private void initializeMenu() { - // Orientation changes + // Orientation selector add(preferences.lockOrientation().asObservable() .subscribe(locked -> { - int resourceId = !locked ? R.drawable.ic_screen_rotation : - activity.getResources().getConfiguration().orientation == 1 ? + boolean isPortrait = activity.getResources().getConfiguration() + .orientation == Configuration.ORIENTATION_PORTRAIT; + int resourceId = !locked ? R.drawable.ic_screen_rotation : isPortrait ? R.drawable.ic_screen_lock_portrait : R.drawable.ic_screen_lock_landscape; lockOrientation.setImageResource(resourceId); })); - lockOrientation.setOnClickListener(v -> - preferences.lockOrientation().set(!preferences.lockOrientation().get())); + lockOrientation.setOnClickListener(v -> { + showImmersiveDialog(new MaterialDialog.Builder(activity) + .title(R.string.pref_rotation_type) + .items(R.array.rotation_type) + .itemsCallbackSingleChoice(preferences.rotation().get() - 1, + (d, itemView, which, text) -> { + preferences.rotation().set(which + 1); + return true; + }) + .build()); + }); // Zoom selector zoomSelector.setOnClickListener(v -> { @@ -279,6 +291,7 @@ public class ReaderMenu { initializePopupMenu(); } + @SuppressWarnings("ConstantConditions") private void initializePopupMenu() { // Load values from preferences enableTransitions.setChecked(preferences.enableTransitions().get()); diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index b23b6bfc1..031c189eb 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -80,6 +80,20 @@ 4 + + @string/rotation_free + @string/rotation_lock + @string/rotation_force_portrait + @string/rotation_force_landscape + + + + 1 + 2 + 3 + 4 + + @string/update_never @string/update_1hour diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index b827b2bf3..1eb4696b6 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -20,6 +20,7 @@ pref_zoom_start_key pref_hide_status_bar_key pref_lock_orientation_key + pref_rotation_type_key pref_enable_transitions_key pref_show_page_number_key pref_keep_screen_on_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e23ce60fd..c11507f83 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -111,6 +111,12 @@ Left Right Center + Rotation + Free + Lock + Force portrait + Force landscape + Downloads directory