From 908cd0aaab5ebea29428dd2ad82dc77ed90616aa Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 15 Sep 2020 00:22:57 +0800 Subject: Add an option to disable vibration when swipe action is triggered. --- .../ChangeVibrateWhenActionTriggeredEvent.java | 9 +++++++ .../infinityforreddit/Fragment/PostFragment.java | 13 +++++++--- .../Settings/SwipeActionPreferenceFragment.java | 29 ++++++++++++++++++++++ .../Utils/SharedPreferencesUtils.java | 1 + 4 files changed, 49 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 (limited to 'app/src/main/java') 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"; -- cgit v1.2.3