aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2020-04-30 09:33:52 +0000
committerAlex Ning <chineseperson5@gmail.com>2020-04-30 09:33:52 +0000
commitac10441d5494db48fd3bb05f4a37e34a5ed38261 (patch)
treecdf181ca4df886e0b652e7d93a6bb38ca1899bcc /app/src/main
parent4678caa92bad919201b53000e8d4100bf253c497 (diff)
downloadinfinity-for-reddit-ac10441d5494db48fd3bb05f4a37e34a5ed38261.tar
infinity-for-reddit-ac10441d5494db48fd3bb05f4a37e34a5ed38261.tar.gz
infinity-for-reddit-ac10441d5494db48fd3bb05f4a37e34a5ed38261.tar.bz2
infinity-for-reddit-ac10441d5494db48fd3bb05f4a37e34a5ed38261.tar.lz
infinity-for-reddit-ac10441d5494db48fd3bb05f4a37e34a5ed38261.tar.xz
infinity-for-reddit-ac10441d5494db48fd3bb05f4a37e34a5ed38261.tar.zst
infinity-for-reddit-ac10441d5494db48fd3bb05f4a37e34a5ed38261.zip
Add a BroadcastReceiver to receive network status change and enable or disable video autoplay.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/BroadcastReceiver/NetworkWifiStatusReceiver.java22
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java12
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java13
4 files changed, 55 insertions, 0 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BroadcastReceiver/NetworkWifiStatusReceiver.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BroadcastReceiver/NetworkWifiStatusReceiver.java
new file mode 100644
index 00000000..fb8d3ae4
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BroadcastReceiver/NetworkWifiStatusReceiver.java
@@ -0,0 +1,22 @@
+package ml.docilealligator.infinityforreddit.BroadcastReceiver;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+public class NetworkWifiStatusReceiver extends BroadcastReceiver {
+ private NetworkWifiStatusReceiverListener networkWifiStatusReceiverListener;
+
+ public interface NetworkWifiStatusReceiverListener {
+ void networkStatusChange();
+ }
+
+ public NetworkWifiStatusReceiver(NetworkWifiStatusReceiverListener listener) {
+ networkWifiStatusReceiverListener = listener;
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ networkWifiStatusReceiverListener.networkStatusChange();
+ }
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java
new file mode 100644
index 00000000..2c25b8c9
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeWifiStatusEvent.java
@@ -0,0 +1,8 @@
+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 6ce0ac92..5b76d340 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java
@@ -62,6 +62,7 @@ import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent;
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.FragmentCommunicator;
@@ -854,6 +855,17 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}
}
+ @Subscribe
+ public void onChangeWifiStatusEvent(ChangeWifiStatusEvent changeWifiStatusEvent) {
+ if (mAdapter != null) {
+ String autoplay = mSharedPreferences.getString(SharedPreferencesUtils.VIDEO_AUTOPLAY, SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_NEVER);
+ if (autoplay.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ON_WIFI)) {
+ mAdapter.setAutoplay(changeWifiStatusEvent.isConnectedToWifi);
+ 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 df5a1100..90478a08 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java
@@ -1,6 +1,8 @@
package ml.docilealligator.infinityforreddit;
import android.app.Application;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
import android.os.Bundle;
import androidx.annotation.NonNull;
@@ -10,8 +12,15 @@ import com.evernote.android.state.StateSaver;
import com.livefront.bridge.Bridge;
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.Utils.Utils;
+
public class Infinity extends Application {
private AppComponent mAppComponent;
+ private NetworkWifiStatusReceiver mNetworkWifiStatusReceiver;
@Override
public void onCreate() {
@@ -32,6 +41,10 @@ public class Infinity extends Application {
StateSaver.restoreInstanceState(target, state);
}
});
+
+ mNetworkWifiStatusReceiver =
+ new NetworkWifiStatusReceiver(() -> EventBus.getDefault().post(new ChangeWifiStatusEvent(Utils.isConnectedToWifi(getApplicationContext()))));
+ registerReceiver(mNetworkWifiStatusReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public AppComponent getAppComponent() {