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.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -19,67 +18,49 @@ import androidx.annotation.Nullable;
*/
public class KiwixWifiP2pBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
private WifiDirectManager wifiDirectManager;
private P2pEventListener p2pEventListener;
public KiwixWifiP2pBroadcastReceiver(@NonNull WifiP2pManager manager, @NonNull WifiP2pManager.Channel channel,
@NonNull WifiDirectManager wifiDirectManager) {
super();
this.manager = manager;
this.channel = channel;
this.wifiDirectManager = wifiDirectManager;
public KiwixWifiP2pBroadcastReceiver(@NonNull P2pEventListener p2pEventListener) {
this.p2pEventListener = p2pEventListener;
}
@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Update wifi p2p state
int wifiP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (wifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
((P2pEventListener) wifiDirectManager).setWifiP2pEnabled(true);
} else {
((P2pEventListener) wifiDirectManager).setWifiP2pEnabled(false);
}
Log.d(LocalFileTransferActivity.TAG, "WiFi P2P state changed - " + wifiP2pState);
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
if (manager != null) {
/* 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");
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// 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();
switch(intent.getAction()) {
case WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION: {
int wifiP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
p2pEventListener.onWifiP2pStateChanged(wifiP2pState);
break;
}
case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION: {
p2pEventListener.onPeersChanged();
break;
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
((P2pEventListener) wifiDirectManager).onDeviceChanged(intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
case WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION: {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
p2pEventListener.onConnectionChanged(networkInfo);
break;
}
case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION: {
WifiP2pDevice userDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
p2pEventListener.onDeviceChanged(userDevice);
break;
}
default: break;
}
}
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);
}

View File

@ -421,13 +421,13 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
@Override
public void onResume() {
super.onResume();
wifiDirectManager.registerWifiDirectBroadcastRecevier();
wifiDirectManager.registerWifiDirectBroadcastReceiver();
}
@Override
public void onPause() {
super.onPause();
wifiDirectManager.unregisterWifiDirectBroadcastRecevier();
wifiDirectManager.unregisterWifiDirectBroadcastReceiver();
}
@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.Context;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
@ -47,7 +48,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
private AlertDialogShower alertDialogShower;
/* 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 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);
}
public void registerWifiDirectBroadcastRecevier() {
receiver = new KiwixWifiP2pBroadcastReceiver(manager, channel, this);
public void registerWifiDirectBroadcastReceiver() {
receiver = new KiwixWifiP2pBroadcastReceiver(this);
// For specifying broadcasts (of the P2P API) that the module needs to respond to
IntentFilter intentFilter = new IntentFilter();
@ -110,7 +111,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
activity.registerReceiver(receiver, intentFilter);
}
public void unregisterWifiDirectBroadcastRecevier() {
public void unregisterWifiDirectBroadcastReceiver() {
activity.unregisterReceiver(receiver);
}
@ -131,18 +132,40 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
}
@Override
public void setWifiP2pEnabled(boolean wifiP2pEnabled) {
this.wifiP2pEnabled = wifiP2pEnabled;
public void onWifiP2pStateChanged(int wifiP2pState) {
this.isWifiP2pEnabled = (wifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED);
if(wifiP2pEnabled == false) {
if(!isWifiP2pEnabled) {
displayToast(R.string.discovery_needs_wifi, Toast.LENGTH_SHORT);
((Callbacks) activity).clearListOfAvailablePeers();
}
Log.d(TAG, "WiFi P2P state changed - " + isWifiP2pEnabled);
}
@Override
public void onDisconnected() {
((Callbacks) activity).clearListOfAvailablePeers();
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();
}
}
@Override
@ -155,7 +178,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
}
public boolean isWifiP2pEnabled() {
return wifiP2pEnabled;
return isWifiP2pEnabled;
}
/* From WifiP2pManager.ChannelListener interface */