mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Refactor WifiDirectBroadcastReceiver
Shift all calls from WifiDirectBroadcastReceiver to WifiDirectManager through callbacks
This commit is contained in:
parent
083d7768b8
commit
2e41f51fc8
@ -167,17 +167,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
|||||||
WifiP2pDevice senderSelectedPeerDevice =
|
WifiP2pDevice senderSelectedPeerDevice =
|
||||||
(WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
(WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
||||||
wifiDirectManager.sendToDevice(senderSelectedPeerDevice);
|
wifiDirectManager.sendToDevice(senderSelectedPeerDevice);
|
||||||
/*wifiDirectManager.setSenderSelectedPeerDevice(senderSelectedPeerDevice);
|
|
||||||
alertDialogShower.show(
|
|
||||||
new KiwixDialog.FileTransferConfirmation(senderSelectedPeerDevice.deviceName),
|
|
||||||
new Function0<Unit>() {
|
|
||||||
@Override public Unit invoke() {
|
|
||||||
wifiDirectManager.connect();
|
|
||||||
showToast(LocalFileTransferActivity.this, R.string.performing_handshake,
|
|
||||||
Toast.LENGTH_LONG);
|
|
||||||
return Unit.INSTANCE;
|
|
||||||
}
|
|
||||||
});*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -7,12 +7,9 @@ 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 android.util.Log;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import org.kiwix.kiwixmobile.R;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTransferActivity.showToast;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for the local file sharing module.
|
* Helper class for the local file sharing module.
|
||||||
@ -24,14 +21,14 @@ public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
private WifiP2pManager manager;
|
private WifiP2pManager manager;
|
||||||
private WifiP2pManager.Channel channel;
|
private WifiP2pManager.Channel channel;
|
||||||
private LocalFileTransferActivity wifiActivity;
|
private WifiDirectManager wifiDirectManager;
|
||||||
|
|
||||||
public WifiDirectBroadcastReceiver(@NonNull WifiP2pManager manager, @NonNull WifiP2pManager.Channel channel,
|
public WifiDirectBroadcastReceiver(@NonNull WifiP2pManager manager, @NonNull WifiP2pManager.Channel channel,
|
||||||
@NonNull LocalFileTransferActivity activity) {
|
@NonNull WifiDirectManager wifiDirectManager) {
|
||||||
super();
|
super();
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
this.wifiActivity = activity;
|
this.wifiDirectManager = wifiDirectManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -43,23 +40,23 @@ public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
|||||||
int wifiP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
|
int wifiP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
|
||||||
|
|
||||||
if (wifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
|
if (wifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
|
||||||
wifiActivity.wifiDirectManager.setWifiP2pEnabled(true);
|
((BroadcastListener) wifiDirectManager).setWifiP2pEnabled(true);
|
||||||
} else {
|
} else {
|
||||||
wifiActivity.wifiDirectManager.setWifiP2pEnabled(false);
|
((BroadcastListener) wifiDirectManager).setWifiP2pEnabled(false);
|
||||||
showToast(wifiActivity, R.string.discovery_needs_wifi, Toast.LENGTH_SHORT);
|
|
||||||
wifiActivity.clearPeers();
|
|
||||||
}
|
}
|
||||||
Log.d(LocalFileTransferActivity.TAG, "WiFi P2P state changed - " + wifiP2pState);
|
Log.d(LocalFileTransferActivity.TAG, "WiFi P2P state changed - " + wifiP2pState);
|
||||||
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
|
|
||||||
|
|
||||||
|
|
||||||
|
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
|
||||||
if (manager != null) {
|
if (manager != null) {
|
||||||
/* List of available peers has changed, so request & use the new list through
|
/* List of available peers has changed, so request & use the new list through
|
||||||
* PeerListListener.requestPeers() callback */
|
* PeerListListener.requestPeers() callback */
|
||||||
manager.requestPeers(channel, wifiActivity.wifiDirectManager);
|
manager.requestPeers(channel, wifiDirectManager);
|
||||||
}
|
}
|
||||||
Log.d(LocalFileTransferActivity.TAG, "P2P peers changed");
|
Log.d(LocalFileTransferActivity.TAG, "P2P peers changed");
|
||||||
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
|
|
||||||
|
|
||||||
|
|
||||||
|
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
|
||||||
if (manager == null) {
|
if (manager == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -67,15 +64,23 @@ public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
if (networkInfo.isConnected()) {
|
if (networkInfo.isConnected()) {
|
||||||
// Request connection info about the wifi p2p group formed upon connection
|
// Request connection info about the wifi p2p group formed upon connection
|
||||||
manager.requestConnectionInfo(channel, wifiActivity.wifiDirectManager);
|
manager.requestConnectionInfo(channel, wifiDirectManager);
|
||||||
} else {
|
} else {
|
||||||
// Not connected after connection change -> Disconnected
|
// Not connected after connection change -> Disconnected
|
||||||
wifiActivity.clearPeers();
|
((BroadcastListener) wifiDirectManager).onDisconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
|
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
|
||||||
// Update UI with wifi-direct details about the user device
|
((BroadcastListener) wifiDirectManager).onDeviceChanged(intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
|
||||||
wifiActivity.updateUserDevice(
|
|
||||||
intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface BroadcastListener {
|
||||||
|
void setWifiP2pEnabled(boolean wifiP2pEnabled);
|
||||||
|
|
||||||
|
void onDisconnected();
|
||||||
|
|
||||||
|
void onDeviceChanged(@Nullable WifiP2pDevice userDevice);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import android.os.Build;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import kotlin.Unit;
|
import kotlin.Unit;
|
||||||
import kotlin.jvm.functions.Function0;
|
import kotlin.jvm.functions.Function0;
|
||||||
@ -27,7 +28,8 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
|||||||
/**
|
/**
|
||||||
* Manager for the Wifi-P2p API, used in the local file transfer module
|
* Manager for the Wifi-P2p API, used in the local file transfer module
|
||||||
* */
|
* */
|
||||||
public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener {
|
public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener,
|
||||||
|
WifiDirectBroadcastReceiver.BroadcastListener {
|
||||||
|
|
||||||
private static final String TAG = "WifiDirectManager";
|
private static final String TAG = "WifiDirectManager";
|
||||||
|
|
||||||
@ -41,8 +43,6 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
|||||||
private WifiP2pManager.Channel channel;
|
private WifiP2pManager.Channel channel;
|
||||||
// Connects the module to device's underlying Wifi p2p framework
|
// Connects the module to device's underlying Wifi p2p framework
|
||||||
|
|
||||||
/*private final IntentFilter intentFilter = new IntentFilter();
|
|
||||||
// For specifying broadcasts (of the P2P API) that the module needs to respond to*/
|
|
||||||
private BroadcastReceiver receiver = null; // For receiving the broadcasts given by above filter
|
private BroadcastReceiver receiver = null; // For receiving the broadcasts given by above filter
|
||||||
|
|
||||||
private WifiP2pDevice userDevice; // Represents the device on which the app is running
|
private WifiP2pDevice userDevice; // Represents the device on which the app is running
|
||||||
@ -59,18 +59,12 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
|||||||
public void initialiseWifiDirectManager(@NonNull AlertDialogShower alertDialogShower) {
|
public void initialiseWifiDirectManager(@NonNull AlertDialogShower alertDialogShower) {
|
||||||
this.alertDialogShower = alertDialogShower;
|
this.alertDialogShower = alertDialogShower;
|
||||||
|
|
||||||
/*// Intents that the broadcast receiver will be responding to
|
|
||||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
|
|
||||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
|
|
||||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
|
|
||||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);*/
|
|
||||||
|
|
||||||
manager = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
|
manager = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
|
||||||
channel = manager.initialize(activity, getMainLooper(), null);
|
channel = manager.initialize(activity, getMainLooper(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerWifiDirectBroadcastRecevier() {
|
public void registerWifiDirectBroadcastRecevier() {
|
||||||
receiver = new WifiDirectBroadcastReceiver(manager, channel, activity);
|
receiver = new WifiDirectBroadcastReceiver(manager, channel, 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();
|
||||||
@ -105,8 +99,25 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setWifiP2pEnabled(boolean wifiP2pEnabled) {
|
public void setWifiP2pEnabled(boolean wifiP2pEnabled) {
|
||||||
this.wifiP2pEnabled = wifiP2pEnabled;
|
this.wifiP2pEnabled = wifiP2pEnabled;
|
||||||
|
|
||||||
|
if(wifiP2pEnabled == false) {
|
||||||
|
showToast(activity, R.string.discovery_needs_wifi, Toast.LENGTH_SHORT);
|
||||||
|
activity.clearPeers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisconnected() {
|
||||||
|
activity.clearPeers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDeviceChanged(@Nullable WifiP2pDevice userDevice) {
|
||||||
|
// Update UI with wifi-direct details about the user device
|
||||||
|
activity.updateUserDevice(userDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isWifiP2pEnabled() {
|
public boolean isWifiP2pEnabled() {
|
||||||
@ -161,10 +172,6 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
|||||||
return groupInfo.groupOwnerAddress;
|
return groupInfo.groupOwnerAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public void setSenderSelectedPeerDevice(@NonNull WifiP2pDevice senderSelectedPeerDevice) {
|
|
||||||
this.senderSelectedPeerDevice = senderSelectedPeerDevice;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
public void sendToDevice(@NonNull WifiP2pDevice senderSelectedPeerDevice) {
|
public void sendToDevice(@NonNull WifiP2pDevice senderSelectedPeerDevice) {
|
||||||
this.senderSelectedPeerDevice = senderSelectedPeerDevice;
|
this.senderSelectedPeerDevice = senderSelectedPeerDevice;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user