Add a BroadcastReceiver to receive network status change and enable or disable video autoplay.

This commit is contained in:
Alex Ning 2020-04-30 17:33:52 +08:00
parent 4678caa92b
commit ac10441d54
4 changed files with 55 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,8 @@
package ml.docilealligator.infinityforreddit.Event;
public class ChangeWifiStatusEvent {
public boolean isConnectedToWifi;
public ChangeWifiStatusEvent(boolean isConnectedToWifi) {
this.isConnectedToWifi = isConnectedToWifi;
}
}

View File

@ -62,6 +62,7 @@ import ml.docilealligator.infinityforreddit.Event.ChangeShowElapsedTimeEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent; import ml.docilealligator.infinityforreddit.Event.ChangeSpoilerBlurEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeVideoAutoplayEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVideoAutoplayEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeVoteButtonsPositionEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVoteButtonsPositionEvent;
import ml.docilealligator.infinityforreddit.Event.ChangeWifiStatusEvent;
import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList;
import ml.docilealligator.infinityforreddit.Event.ShowDividerInCompactLayoutPreferenceEvent; import ml.docilealligator.infinityforreddit.Event.ShowDividerInCompactLayoutPreferenceEvent;
import ml.docilealligator.infinityforreddit.FragmentCommunicator; 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() { private void refreshAdapter() {
int previousPosition = -1; int previousPosition = -1;
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {

View File

@ -1,6 +1,8 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Application; import android.app.Application;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -10,8 +12,15 @@ import com.evernote.android.state.StateSaver;
import com.livefront.bridge.Bridge; import com.livefront.bridge.Bridge;
import com.livefront.bridge.SavedStateHandler; 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 { public class Infinity extends Application {
private AppComponent mAppComponent; private AppComponent mAppComponent;
private NetworkWifiStatusReceiver mNetworkWifiStatusReceiver;
@Override @Override
public void onCreate() { public void onCreate() {
@ -32,6 +41,10 @@ public class Infinity extends Application {
StateSaver.restoreInstanceState(target, state); 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() { public AppComponent getAppComponent() {