diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-09-27 08:02:10 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-09-27 08:02:10 +0000 |
commit | 62a457a868a5441483cbce8de7ec43db106d880d (patch) | |
tree | 0e731315219554bbad3fd5483a71a3ab256416d6 /app | |
parent | c4f8c91944fc1a0a4e919f8f2f0870ecbb2a09fb (diff) | |
download | infinity-for-reddit-62a457a868a5441483cbce8de7ec43db106d880d.tar infinity-for-reddit-62a457a868a5441483cbce8de7ec43db106d880d.tar.gz infinity-for-reddit-62a457a868a5441483cbce8de7ec43db106d880d.tar.bz2 infinity-for-reddit-62a457a868a5441483cbce8de7ec43db106d880d.tar.lz infinity-for-reddit-62a457a868a5441483cbce8de7ec43db106d880d.tar.xz infinity-for-reddit-62a457a868a5441483cbce8de7ec43db106d880d.tar.zst infinity-for-reddit-62a457a868a5441483cbce8de7ec43db106d880d.zip |
Data Saving Mode is available.
Diffstat (limited to 'app')
16 files changed, 264 insertions, 29 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index cc047de9..e6184b6a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -82,7 +82,7 @@ import ml.docilealligator.infinityforreddit.CustomView.CustomToroContainer; import ml.docilealligator.infinityforreddit.DeleteThing; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; -import ml.docilealligator.infinityforreddit.Event.ChangeWifiStatusEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeNetworkStatusEvent; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivity; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; @@ -1373,11 +1373,21 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } @Subscribe - public void onChangeWifiStatusEvent(ChangeWifiStatusEvent changeWifiStatusEvent) { + public void onChangeNetworkStatusEvent(ChangeNetworkStatusEvent changeNetworkStatusEvent) { if (mAdapter != null) { String autoplay = mSharedPreferences.getString(SharedPreferencesUtils.VIDEO_AUTOPLAY, SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_NEVER); + String dataSavingMode = mSharedPreferences.getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF); + boolean stateChanged = false; if (autoplay.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ON_WIFI)) { - mAdapter.setAutoplay(changeWifiStatusEvent.isConnectedToWifi); + mAdapter.setAutoplay(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_WIFI); + stateChanged = true; + } + if (dataSavingMode.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) { + mAdapter.setDataSavingMode(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_CELLULAR); + stateChanged = true; + } + + if (stateChanged) { refreshAdapter(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index d1c417f5..5c5f9a04 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -177,6 +177,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy private double mStartAutoplayVisibleAreaOffset; private boolean mMuteNSFWVideo; private boolean mAutomaticallyTryRedgifs; + private boolean mDataSavingMode; private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback; private boolean isInitiallyLoading; private boolean isInitiallyLoadingFailed; @@ -463,10 +464,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy mShowAbsoluteNumberOfVotes = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES, true); String autoplayString = sharedPreferences.getString(SharedPreferencesUtils.VIDEO_AUTOPLAY, SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_NEVER); + int networkType = Utils.getConnectedNetwork(activity); if (autoplayString.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ALWAYS_ON)) { mAutoplay = true; } else if (autoplayString.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ON_WIFI)) { - mAutoplay = Utils.isConnectedToWifi(activity); + mAutoplay = networkType == Utils.NETWORK_TYPE_WIFI; } mAutoplayNsfwVideos = sharedPreferences.getBoolean(SharedPreferencesUtils.AUTOPLAY_NSFW_VIDEOS, true); mMuteAutoplayingVideos = sharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_AUTOPLAYING_VIDEOS, true); @@ -480,6 +482,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy mMuteNSFWVideo = sharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_NSFW_VIDEO, false); mAutomaticallyTryRedgifs = sharedPreferences.getBoolean(SharedPreferencesUtils.AUTOMATICALLY_TRY_REDGIFS, true); + String dataSavingModeString = sharedPreferences.getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF); + if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ALWAYS)) { + mDataSavingMode = true; + } else if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) { + mDataSavingMode = networkType == Utils.NETWORK_TYPE_CELLULAR; + } + mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback; isInitiallyLoading = true; isInitiallyLoadingFailed = false; @@ -1401,7 +1410,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy private Post.Preview getSuitablePreview(ArrayList<Post.Preview> previews) { Post.Preview preview; if (!previews.isEmpty()) { - preview = previews.get(0); + int previewIndex; + if (mDataSavingMode && previews.size() > 2) { + previewIndex = previews.size() / 2; + } else { + previewIndex = 0; + } + preview = previews.get(previewIndex); if (preview.getPreviewWidth() * preview.getPreviewHeight() >= 65 * 1000 * 1000) { for (int i = previews.size() - 1; i >= 1; i--) { preview = previews.get(i); @@ -1835,6 +1850,10 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy mAutoplay = autoplay; } + public void setDataSavingMode(boolean dataSavingMode) { + mDataSavingMode = dataSavingMode; + } + public void onItemSwipe(RecyclerView.ViewHolder viewHolder, int direction) { if (viewHolder instanceof PostDetailBaseViewHolder) { if (direction == ItemTouchHelper.LEFT || direction == ItemTouchHelper.START) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 86ce0e1c..db43a4c5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -186,7 +186,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView private boolean mAutomaticallyTryRedgifs; private boolean mLongPressToHideToolbarInCompactLayout; private boolean mCompactLayoutToolbarHiddenByDefault; - private boolean mDataSavingMode; + private boolean mDataSavingMode = false; private Drawable mCommentIcon; private NetworkState networkState; private ExoCreator mExoCreator; @@ -219,10 +219,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView mShowDividerInCompactLayout = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_DIVIDER_IN_COMPACT_LAYOUT, true); mShowAbsoluteNumberOfVotes = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ABSOLUTE_NUMBER_OF_VOTES, true); String autoplayString = sharedPreferences.getString(SharedPreferencesUtils.VIDEO_AUTOPLAY, SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_NEVER); + int networkType = Utils.getConnectedNetwork(activity); if (autoplayString.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ALWAYS_ON)) { mAutoplay = true; } else if (autoplayString.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ON_WIFI)) { - mAutoplay = Utils.isConnectedToWifi(activity); + mAutoplay = networkType == Utils.NETWORK_TYPE_WIFI; } mAutoplayNsfwVideos = sharedPreferences.getBoolean(SharedPreferencesUtils.AUTOPLAY_NSFW_VIDEOS, true); mMuteAutoplayingVideos = sharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_AUTOPLAYING_VIDEOS, true); @@ -240,6 +241,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView mLongPressToHideToolbarInCompactLayout = sharedPreferences.getBoolean(SharedPreferencesUtils.LONG_PRESS_TO_HIDE_TOOLBAR_IN_COMPACT_LAYOUT, false); mCompactLayoutToolbarHiddenByDefault = sharedPreferences.getBoolean(SharedPreferencesUtils.POST_COMPACT_LAYOUT_TOOLBAR_HIDDEN_BY_DEFAULT, false); + String dataSavingModeString = sharedPreferences.getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF); + if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ALWAYS)) { + mDataSavingMode = true; + } else if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) { + mDataSavingMode = networkType == Utils.NETWORK_TYPE_CELLULAR; + } + mPostLayout = postLayout; mColorPrimaryLightTheme = customThemeWrapper.getColorPrimaryLightTheme(); @@ -945,17 +953,23 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView private Post.Preview getSuitablePreview(ArrayList<Post.Preview> previews) { Post.Preview preview; if (!previews.isEmpty()) { - preview = previews.get(0); - if (preview.getPreviewWidth() * preview.getPreviewHeight() >= 65 * 1000 * 1000) { + int previewIndex; + if (mDataSavingMode && previews.size() > 2) { + previewIndex = previews.size() / 2; + } else { + previewIndex = 0; + } + preview = previews.get(previewIndex); + if (preview.getPreviewWidth() * preview.getPreviewHeight() >= 75_000_000) { for (int i = previews.size() - 1; i >= 1; i--) { preview = previews.get(i); if (mImageViewWidth >= preview.getPreviewWidth()) { - if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 75 * 1000 * 1000) { + if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 75_000_000) { return preview; } } else { int height = mImageViewWidth / preview.getPreviewWidth() * preview.getPreviewHeight(); - if (mImageViewWidth * height <= 75 * 1000 * 1000) { + if (mImageViewWidth * height <= 75_000_000) { return preview; } } @@ -963,7 +977,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } int divisor = 2; - while (preview.getPreviewWidth() * preview.getPreviewHeight() / divisor / divisor > 75 * 1000 * 1000) { + while (preview.getPreviewWidth() * preview.getPreviewHeight() / divisor / divisor > 75_000_000) { preview.setPreviewWidth(preview.getPreviewWidth() / divisor); preview.setPreviewHeight(preview.getPreviewHeight() / divisor); divisor *= 2; @@ -1275,6 +1289,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView mCompactLayoutToolbarHiddenByDefault = compactLayoutToolbarHiddenByDefault; } + public void setDataSavingMode(boolean dataSavingMode) { + mDataSavingMode = dataSavingMode; + } + @Override public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { super.onViewRecycled(holder); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDataSavingModeEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDataSavingModeEvent.java new file mode 100644 index 00000000..2be45caf --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDataSavingModeEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeDataSavingModeEvent { + public String dataSavingMode; + + public ChangeDataSavingModeEvent(String dataSavingMode) { + this.dataSavingMode = dataSavingMode; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeNetworkStatusEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeNetworkStatusEvent.java new file mode 100644 index 00000000..7fe86dee --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeNetworkStatusEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeNetworkStatusEvent { + public int connectedNetwork; + + public ChangeNetworkStatusEvent(int connectedNetwork) { + this.connectedNetwork = connectedNetwork; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java deleted file mode 100644 index 2c25b8c9..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java +++ /dev/null @@ -1,8 +0,0 @@ -package ml.docilealligator.infinityforreddit.Event; - -public class ChangeWifiStatusEvent { - public boolean isConnectedToWifi; - public ChangeWifiStatusEvent(boolean isConnectedToWifi) { - this.isConnectedToWifi = isConnectedToWifi; - } -} 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 d5b638b3..0316defa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -71,12 +71,14 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomView.CustomToroContainer; import ml.docilealligator.infinityforreddit.Event.ChangeAutoplayNsfwVideosEvent; import ml.docilealligator.infinityforreddit.Event.ChangeCompactLayoutToolbarHiddenByDefaultEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeDataSavingModeEvent; import ml.docilealligator.infinityforreddit.Event.ChangeDefaultPostLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangeEnableSwipeActionSwitchEvent; import ml.docilealligator.infinityforreddit.Event.ChangeLongPressToHideToolbarInCompactLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangeMuteAutoplayingVideosEvent; import ml.docilealligator.infinityforreddit.Event.ChangeMuteNSFWVideoEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWBlurEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeNetworkStatusEvent; import ml.docilealligator.infinityforreddit.Event.ChangePostLayoutEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSavePostFeedScrolledPositionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeShowAbsoluteNumberOfVotesEvent; @@ -88,7 +90,6 @@ 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; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.ShowDividerInCompactLayoutPreferenceEvent; import ml.docilealligator.infinityforreddit.Event.ShowThumbnailOnTheRightInCompactLayoutEvent; @@ -1180,11 +1181,21 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } @Subscribe - public void onChangeWifiStatusEvent(ChangeWifiStatusEvent changeWifiStatusEvent) { + public void onChangeNetworkStatusEvent(ChangeNetworkStatusEvent changeNetworkStatusEvent) { if (mAdapter != null) { String autoplay = mSharedPreferences.getString(SharedPreferencesUtils.VIDEO_AUTOPLAY, SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_NEVER); + String dataSavingMode = mSharedPreferences.getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF); + boolean stateChanged = false; if (autoplay.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ON_WIFI)) { - mAdapter.setAutoplay(changeWifiStatusEvent.isConnectedToWifi); + mAdapter.setAutoplay(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_WIFI); + stateChanged = true; + } + if (dataSavingMode.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) { + mAdapter.setDataSavingMode(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_CELLULAR); + stateChanged = true; + } + + if (stateChanged) { refreshAdapter(); } } @@ -1261,6 +1272,20 @@ public class PostFragment extends Fragment implements FragmentCommunicator { swipeActionThreshold = changeSwipeActionThresholdEvent.swipeActionThreshold; } + @Subscribe + public void onChangeDataSavingModeEvent(ChangeDataSavingModeEvent changeDataSavingModeEvent) { + if (mAdapter != null) { + boolean dataSavingMode = false; + if (changeDataSavingModeEvent.dataSavingMode.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) { + dataSavingMode = Utils.isConnectedToCellularData(activity); + } else if (changeDataSavingModeEvent.dataSavingMode.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ALWAYS)) { + dataSavingMode = true; + } + mAdapter.setDataSavingMode(dataSavingMode); + refreshAdapter(); + } + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java index 04edd825..d9ee278c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java @@ -15,7 +15,7 @@ import com.livefront.bridge.SavedStateHandler; import org.greenrobot.eventbus.EventBus; import ml.docilealligator.infinityforreddit.BroadcastReceiver.NetworkWifiStatusReceiver; -import ml.docilealligator.infinityforreddit.Event.ChangeWifiStatusEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeNetworkStatusEvent; import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.inifinityforreddit.EventBusIndex; @@ -46,7 +46,9 @@ public class Infinity extends Application { EventBus.builder().addIndex(new EventBusIndex()).installDefaultEventBus(); mNetworkWifiStatusReceiver = - new NetworkWifiStatusReceiver(() -> EventBus.getDefault().post(new ChangeWifiStatusEvent(Utils.isConnectedToWifi(getApplicationContext())))); + new NetworkWifiStatusReceiver(() -> { + EventBus.getDefault().post(new ChangeNetworkStatusEvent(Utils.getConnectedNetwork(getApplicationContext()))); + }); registerReceiver(mNetworkWifiStatusReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java new file mode 100644 index 00000000..b057ad3a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java @@ -0,0 +1,29 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.os.Bundle; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceFragmentCompat; + +import org.greenrobot.eventbus.EventBus; + +import ml.docilealligator.infinityforreddit.Event.ChangeDataSavingModeEvent; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class DataSavingModePreferenceFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.data_saving_mode_preferences, rootKey); + + ListPreference dataSavingModeListPreference = findPreference(SharedPreferencesUtils.DATA_SAVING_MODE); + + if (dataSavingModeListPreference != null) { + dataSavingModeListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeDataSavingModeEvent((String) 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 99f90024..141cceaa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -130,6 +130,10 @@ public class SharedPreferencesUtils { public static final String MUTE_NSFW_VIDEO = "mute_nsfw_video"; public static final String VIDEO_PLAYER_IGNORE_NAV_BAR = "video_player_ignore_nav_bar"; public static final String SAVE_FRONT_PAGE_SCROLLED_POSITION = "save_front_page_scrolled_position"; + public static final String DATA_SAVING_MODE = "data_saving_mode"; + public static final String DATA_SAVING_MODE_OFF = "0"; + public static final String DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA = "1"; + public static final String DATA_SAVING_MODE_ALWAYS = "2"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_COUNT = "_main_page_tab_count"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java index f07363d9..f3d32e5d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java @@ -5,7 +5,9 @@ import android.content.Context; import android.graphics.drawable.Drawable; import android.net.ConnectivityManager; import android.net.Network; +import android.net.NetworkCapabilities; import android.net.NetworkInfo; +import android.os.Build; import android.text.Html; import android.text.Spannable; import android.util.DisplayMetrics; @@ -24,6 +26,9 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SortType; public class Utils { + public static final int NETWORK_TYPE_OTHER = -1; + public static final int NETWORK_TYPE_WIFI = 0; + public static final int NETWORK_TYPE_CELLULAR = 1; private static final long SECOND_MILLIS = 1000; private static final long MINUTE_MILLIS = 60 * SECOND_MILLIS; private static final long HOUR_MILLIS = 60 * MINUTE_MILLIS; @@ -140,13 +145,85 @@ public class Utils { textView.setText(html); } + public static int getConnectedNetwork(Context context) { + ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connMgr != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Network nw = connMgr.getActiveNetwork(); + if (nw == null) return NETWORK_TYPE_OTHER; + NetworkCapabilities actNw = connMgr.getNetworkCapabilities(nw); + if (actNw != null) { + if (actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return NETWORK_TYPE_WIFI; + } + if (actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + return NETWORK_TYPE_CELLULAR; + } + } + return NETWORK_TYPE_OTHER; + } else { + boolean isWifi = false; + boolean isCellular = false; + for (Network network : connMgr.getAllNetworks()) { + NetworkInfo networkInfo = connMgr.getNetworkInfo(network); + if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { + isWifi = true; + } + if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { + isCellular = true; + } + } + + if (isWifi) { + return NETWORK_TYPE_WIFI; + } + + if (isCellular) { + return NETWORK_TYPE_CELLULAR; + } + + return NETWORK_TYPE_OTHER; + } + } + + return NETWORK_TYPE_OTHER; + } + public static boolean isConnectedToWifi(Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connMgr != null) { - for (Network network : connMgr.getAllNetworks()) { - NetworkInfo networkInfo = connMgr.getNetworkInfo(network); - if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { - return networkInfo.isConnected(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Network nw = connMgr.getActiveNetwork(); + if (nw == null) return false; + NetworkCapabilities actNw = connMgr.getNetworkCapabilities(nw); + return actNw != null && actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI); + } else { + for (Network network : connMgr.getAllNetworks()) { + NetworkInfo networkInfo = connMgr.getNetworkInfo(network); + if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { + return networkInfo.isConnected(); + } + } + } + } + + return false; + } + + public static boolean isConnectedToCellularData(Context context) { + ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connMgr != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Network nw = connMgr.getActiveNetwork(); + if (nw == null) return false; + NetworkCapabilities actNw = connMgr.getNetworkCapabilities(nw); + return actNw != null && actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR); + } else { + for (Network network : connMgr.getAllNetworks()) { + NetworkInfo networkInfo = connMgr.getNetworkInfo(network); + if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { + return networkInfo.isConnected(); + } } } } diff --git a/app/src/main/res/drawable/ic_data_saving_mode_black_24dp.xml b/app/src/main/res/drawable/ic_data_saving_mode_black_24dp.xml new file mode 100644 index 00000000..89f69eca --- /dev/null +++ b/app/src/main/res/drawable/ic_data_saving_mode_black_24dp.xml @@ -0,0 +1,9 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24" + android:viewportHeight="24"> + <path + android:pathData="M11.8,10.9c-2.27,-0.59 -3,-1.2 -3,-2.15 0,-1.09 1.01,-1.85 2.7,-1.85 1.78,0 2.44,0.85 2.5,2.1h2.21c-0.07,-1.72 -1.12,-3.3 -3.21,-3.81V3h-3v2.16c-1.94,0.42 -3.5,1.68 -3.5,3.61 0,2.31 1.91,3.46 4.7,4.13 2.5,0.6 3,1.48 3,2.41 0,0.69 -0.49,1.79 -2.7,1.79 -2.06,0 -2.87,-0.92 -2.98,-2.1h-2.2c0.12,2.19 1.76,3.42 3.68,3.83V21h3v-2.15c1.95,-0.37 3.5,-1.5 3.5,-3.55 0,-2.84 -2.43,-3.81 -4.7,-4.4z" + android:fillColor="#000000"/> +</vector> diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 41eedb8e..51f5f3ee 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -270,4 +270,16 @@ <item>0.8</item> </string-array> + <string-array name="settings_data_saving_mode"> + <item>Off</item> + <item>Only on Cellular Data</item> + <item>Always on</item> + </string-array> + + <string-array name="settings_data_saving_mode_values"> + <item>0</item> + <item>1</item> + <item>2</item> + </string-array> + </resources>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0e83c1d8..6d8d37cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -486,6 +486,7 @@ <string name="settings_bottom_app_bar_option_3">Option 3</string> <string name="settings_bottom_app_bar_option_4">Option 4</string> <string name="settings_bottom_app_bar_fab">Floating Action Button</string> + <string name="settings_data_saving_mode">Data Saving Mode</string> <string name="no_link_available">Cannot get the link</string> diff --git a/app/src/main/res/xml/data_saving_mode_preferences.xml b/app/src/main/res/xml/data_saving_mode_preferences.xml new file mode 100644 index 00000000..1f66892f --- /dev/null +++ b/app/src/main/res/xml/data_saving_mode_preferences.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + <ListPreference + app:defaultValue="0" + app:key="data_saving_mode" + android:title="@string/settings_data_saving_mode" + app:entries="@array/settings_data_saving_mode" + app:entryValues="@array/settings_data_saving_mode_values" + app:useSimpleSummaryProvider="true" /> + +</PreferenceScreen>
\ No newline at end of file diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 68c407ae..88756af0 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -47,6 +47,12 @@ android:icon="@drawable/ic_security_24dp" app:fragment="ml.docilealligator.infinityforreddit.Settings.SecurityPreferenceFragment" /> + <Preference + app:key="security" + app:title="@string/settings_data_saving_mode" + app:icon="@drawable/ic_data_saving_mode_black_24dp" + app:fragment="ml.docilealligator.infinityforreddit.Settings.DataSavingModePreferenceFragment" /> + <SwitchPreference app:defaultValue="false" app:key="save_front_page_scrolled_position" |