From 62a457a868a5441483cbce8de7ec43db106d880d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 27 Sep 2020 16:02:10 +0800 Subject: Data Saving Mode is available. --- .../Activity/ViewPostDetailActivity.java | 16 +++- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 23 +++++- .../Adapter/PostRecyclerViewAdapter.java | 32 ++++++-- .../Event/ChangeDataSavingModeEvent.java | 9 +++ .../Event/ChangeNetworkStatusEvent.java | 9 +++ .../Event/ChangeWifiStatusEvent.java | 8 -- .../infinityforreddit/Fragment/PostFragment.java | 31 +++++++- .../infinityforreddit/Infinity.java | 6 +- .../Settings/DataSavingModePreferenceFragment.java | 29 ++++++++ .../Utils/SharedPreferencesUtils.java | 4 + .../infinityforreddit/Utils/Utils.java | 85 +++++++++++++++++++++- 11 files changed, 223 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDataSavingModeEvent.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeNetworkStatusEvent.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/DataSavingModePreferenceFragment.java (limited to 'app/src/main/java') 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 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 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 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 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(); + } } } } -- cgit v1.2.3