From 908cd0aaab5ebea29428dd2ad82dc77ed90616aa Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 15 Sep 2020 00:22:57 +0800 Subject: [PATCH] Add an option to disable vibration when swipe action is triggered. --- ...ChangeVibrateWhenActionTriggeredEvent.java | 9 ++++++ .../Fragment/PostFragment.java | 13 +++++++-- .../SwipeActionPreferenceFragment.java | 29 +++++++++++++++++++ .../Utils/SharedPreferencesUtils.java | 1 + app/src/main/res/values/strings.xml | 4 +++ .../xml/gestures_and_buttons_preference.xml | 4 +++ .../main/res/xml/swipe_action_preferences.xml | 9 ++++++ 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeVibrateWhenActionTriggeredEvent.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java create mode 100644 app/src/main/res/xml/swipe_action_preferences.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeVibrateWhenActionTriggeredEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeVibrateWhenActionTriggeredEvent.java new file mode 100644 index 00000000..bef491d6 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeVibrateWhenActionTriggeredEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeVibrateWhenActionTriggeredEvent { + public boolean vibrateWhenActionTriggered; + + public ChangeVibrateWhenActionTriggeredEvent(boolean vibrateWhenActionTriggered) { + this.vibrateWhenActionTriggered = vibrateWhenActionTriggered; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index f334b7fa..0d8c99ad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -79,6 +79,7 @@ import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeStartAutoplayVisibleAreaOffsetEvent; import ml.docilealligator.infinityforreddit.Event.ChangeTimeFormatEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeVibrateWhenActionTriggeredEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVideoAutoplayEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVoteButtonsPositionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeWifiStatusEvent; @@ -171,6 +172,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private boolean hasPost = false; private boolean isShown = false; private boolean savePostFeedScrolledPosition; + private boolean vibrateWhenActionTriggered; private PostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; private Window window; @@ -396,6 +398,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { boolean nsfw = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, false); + vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); Locale locale = getResources().getConfiguration().locale; if (postType == PostDataSource.TYPE_SEARCH) { @@ -640,7 +643,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { backgroundRight = new ColorDrawable(customThemeWrapper.getUpvoted()); drawableLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null); drawableRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null); - int screenBottom = window.getDecorView().getHeight(); touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { boolean exceedThreshold = false; @@ -681,7 +683,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (!exceedThreshold) { exceedThreshold = true; - if (v != null) { + if (vibrateWhenActionTriggered && v != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { v.vibrate(VibrationEffect.createOneShot(10, 175)); } else { @@ -706,7 +708,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (!exceedThreshold) { exceedThreshold = true; - if (v != null) { + if (vibrateWhenActionTriggered && v != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { v.vibrate(VibrationEffect.createOneShot(10, 175)); } else { @@ -1169,6 +1171,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { savePostFeedScrolledPosition = changeSavePostFeedScrolledPositionEvent.savePostFeedScrolledPosition; } + @Subscribe + public void onChangeVibrateWhenActionTriggeredEvent(ChangeVibrateWhenActionTriggeredEvent changeVibrateWhenActionTriggeredEvent) { + vibrateWhenActionTriggered = changeVibrateWhenActionTriggeredEvent.vibrateWhenActionTriggered; + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java new file mode 100644 index 00000000..80358559 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java @@ -0,0 +1,29 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.os.Bundle; + +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; + +import org.greenrobot.eventbus.EventBus; + +import ml.docilealligator.infinityforreddit.Event.ChangeVibrateWhenActionTriggeredEvent; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class SwipeActionPreferenceFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.swipe_action_preferences, rootKey); + + SwitchPreference vibrateWhenActionTriggeredSwitch = findPreference(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED); + + if (vibrateWhenActionTriggeredSwitch != null) { + vibrateWhenActionTriggeredSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeVibrateWhenActionTriggeredEvent((Boolean) newValue)); + return true; + }); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index dccf3e1b..50ab5e5a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -121,6 +121,7 @@ public class SharedPreferencesUtils { public static final String GIF_DOWNLOAD_LOCATION = "gif_download_location"; public static final String VIDEO_DOWNLOAD_LOCATION = "video_download_location"; public static final String SEPARATE_FOLDER_FOR_EACH_SUBREDDIT = "separate_folder_for_each_subreddit"; + public static final String VIBRATE_WHEN_ACTION_TRIGGERED = "vibrate_when_action_triggered"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0cb693f3..882a9dc4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -478,6 +478,8 @@ Gif Download Location Video Download Location Separate Folder for Each Subreddit + Swipe Action + Vibrate the phone when the action is triggered Cannot get the link @@ -877,5 +879,7 @@ Failed to block user View Full Markdown + + Hello blank fragment diff --git a/app/src/main/res/xml/gestures_and_buttons_preference.xml b/app/src/main/res/xml/gestures_and_buttons_preference.xml index 7a279247..7b883826 100644 --- a/app/src/main/res/xml/gestures_and_buttons_preference.xml +++ b/app/src/main/res/xml/gestures_and_buttons_preference.xml @@ -39,4 +39,8 @@ app:key="swap_tap_and_long_in_comments" app:title="@string/settings_swap_tap_and_long_title" /> + + \ No newline at end of file diff --git a/app/src/main/res/xml/swipe_action_preferences.xml b/app/src/main/res/xml/swipe_action_preferences.xml new file mode 100644 index 00000000..daba347b --- /dev/null +++ b/app/src/main/res/xml/swipe_action_preferences.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file