Refactor KiwixWifiP2pBroadcastReceiver

This commit is contained in:
Aditya-Sood 2019-08-08 10:11:24 +05:30
parent f3505a646b
commit ba00ece2ca
3 changed files with 64 additions and 60 deletions

View File

@ -6,7 +6,6 @@ import android.content.Intent;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice; import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager; import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -19,67 +18,49 @@ import androidx.annotation.Nullable;
*/ */
public class KiwixWifiP2pBroadcastReceiver extends BroadcastReceiver { public class KiwixWifiP2pBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager; private P2pEventListener p2pEventListener;
private WifiP2pManager.Channel channel;
private WifiDirectManager wifiDirectManager;
public KiwixWifiP2pBroadcastReceiver(@NonNull WifiP2pManager manager, @NonNull WifiP2pManager.Channel channel, public KiwixWifiP2pBroadcastReceiver(@NonNull P2pEventListener p2pEventListener) {
@NonNull WifiDirectManager wifiDirectManager) { this.p2pEventListener = p2pEventListener;
super();
this.manager = manager;
this.channel = channel;
this.wifiDirectManager = wifiDirectManager;
} }
@Override @Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) { public void onReceive(@NonNull Context context, @NonNull Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { switch(intent.getAction()) {
// Update wifi p2p state case WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION: {
int wifiP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); int wifiP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
p2pEventListener.onWifiP2pStateChanged(wifiP2pState);
if (wifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { break;
((P2pEventListener) wifiDirectManager).setWifiP2pEnabled(true);
} else {
((P2pEventListener) wifiDirectManager).setWifiP2pEnabled(false);
} }
Log.d(LocalFileTransferActivity.TAG, "WiFi P2P state changed - " + wifiP2pState);
case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION: {
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { p2pEventListener.onPeersChanged();
if (manager != null) { break;
/* List of available peers has changed, so request & use the new list through
* PeerListListener.requestPeers() callback */
manager.requestPeers(channel, wifiDirectManager);
} }
Log.d(LocalFileTransferActivity.TAG, "P2P peers changed");
case WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION: {
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
p2pEventListener.onConnectionChanged(networkInfo);
if (networkInfo.isConnected()) { break;
// Request connection info about the wifi p2p group formed upon connection
manager.requestConnectionInfo(channel, wifiDirectManager);
} else {
// Not connected after connection change -> Disconnected
((P2pEventListener) wifiDirectManager).onDisconnected();
} }
case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION: {
WifiP2pDevice userDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
p2pEventListener.onDeviceChanged(userDevice);
break;
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { default: break;
((P2pEventListener) wifiDirectManager).onDeviceChanged(intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
} }
} }
public interface P2pEventListener { public interface P2pEventListener {
void setWifiP2pEnabled(boolean wifiP2pEnabled); void onWifiP2pStateChanged(int wifiP2pState);
void onDisconnected(); void onPeersChanged();
void onConnectionChanged(@NonNull NetworkInfo networkInfo);
void onDeviceChanged(@Nullable WifiP2pDevice userDevice); void onDeviceChanged(@Nullable WifiP2pDevice userDevice);
} }

View File

@ -421,13 +421,13 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
wifiDirectManager.registerWifiDirectBroadcastRecevier(); wifiDirectManager.registerWifiDirectBroadcastReceiver();
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
wifiDirectManager.unregisterWifiDirectBroadcastRecevier(); wifiDirectManager.unregisterWifiDirectBroadcastReceiver();
} }
@Override protected void onDestroy() { @Override protected void onDestroy() {

View File

@ -3,6 +3,7 @@ package org.kiwix.kiwixmobile.zim_manager.local_file_transfer;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.net.wifi.WpsInfo; import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig; import android.net.wifi.p2p.WifiP2pConfig;
@ -47,7 +48,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
private AlertDialogShower alertDialogShower; private AlertDialogShower alertDialogShower;
/* Variables related to the WiFi P2P API */ /* Variables related to the WiFi P2P API */
private boolean wifiP2pEnabled = false; // Whether WiFi has been enabled or not private boolean isWifiP2pEnabled = false; // Whether WiFi has been enabled or not
private boolean retryChannel = false; // Whether channel has retried connecting previously private boolean retryChannel = false; // Whether channel has retried connecting previously
private WifiP2pManager manager; // Overall manager of Wifi p2p connections for the module private WifiP2pManager manager; // Overall manager of Wifi p2p connections for the module
@ -97,8 +98,8 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
channel = manager.initialize(activity, getMainLooper(), null); channel = manager.initialize(activity, getMainLooper(), null);
} }
public void registerWifiDirectBroadcastRecevier() { public void registerWifiDirectBroadcastReceiver() {
receiver = new KiwixWifiP2pBroadcastReceiver(manager, channel, this); receiver = new KiwixWifiP2pBroadcastReceiver(this);
// For specifying broadcasts (of the P2P API) that the module needs to respond to // For specifying broadcasts (of the P2P API) that the module needs to respond to
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
@ -110,7 +111,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
activity.registerReceiver(receiver, intentFilter); activity.registerReceiver(receiver, intentFilter);
} }
public void unregisterWifiDirectBroadcastRecevier() { public void unregisterWifiDirectBroadcastReceiver() {
activity.unregisterReceiver(receiver); activity.unregisterReceiver(receiver);
} }
@ -131,19 +132,41 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
} }
@Override @Override
public void setWifiP2pEnabled(boolean wifiP2pEnabled) { public void onWifiP2pStateChanged(int wifiP2pState) {
this.wifiP2pEnabled = wifiP2pEnabled; this.isWifiP2pEnabled = (wifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED);
if(wifiP2pEnabled == false) { if(!isWifiP2pEnabled) {
displayToast(R.string.discovery_needs_wifi, Toast.LENGTH_SHORT); displayToast(R.string.discovery_needs_wifi, Toast.LENGTH_SHORT);
((Callbacks) activity).clearListOfAvailablePeers(); ((Callbacks) activity).clearListOfAvailablePeers();
} }
Log.d(TAG, "WiFi P2P state changed - " + isWifiP2pEnabled);
} }
@Override @Override
public void onDisconnected() { public void onPeersChanged() {
if (manager != null) {
/* List of available peers has changed, so request & use the new list through
* PeerListListener.requestPeers() callback */
manager.requestPeers(channel, this);
}
Log.d(TAG, "P2P peers changed");
}
@Override
public void onConnectionChanged(@NonNull NetworkInfo networkInfo) {
if (manager == null) {
return;
}
if (networkInfo.isConnected()) {
// Request connection info about the wifi p2p group formed upon connection
manager.requestConnectionInfo(channel, this);
} else {
// Not connected after connection change -> Disconnected
((Callbacks) activity).clearListOfAvailablePeers(); ((Callbacks) activity).clearListOfAvailablePeers();
} }
}
@Override @Override
public void onDeviceChanged(@Nullable WifiP2pDevice userDevice) { public void onDeviceChanged(@Nullable WifiP2pDevice userDevice) {
@ -155,7 +178,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
} }
public boolean isWifiP2pEnabled() { public boolean isWifiP2pEnabled() {
return wifiP2pEnabled; return isWifiP2pEnabled;
} }
/* From WifiP2pManager.ChannelListener interface */ /* From WifiP2pManager.ChannelListener interface */