mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 20:54:13 -04:00
Refactor WifiDirectBroadcastReceiver
This commit is contained in:
parent
91b26a9f08
commit
dd2b4dfa76
@ -24,6 +24,7 @@ import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.fragment.app.ListFragment;
|
||||
|
||||
import org.kiwix.kiwixmobile.BuildConfig;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.utils.SharedPreferenceUtil;
|
||||
|
||||
@ -123,7 +124,7 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
||||
.commit();
|
||||
}
|
||||
|
||||
public void updateUserDevice(WifiP2pDevice device) {
|
||||
public void updateUserDevice(WifiP2pDevice device) { // Update UI with user device's details
|
||||
this.userDevice = device;
|
||||
|
||||
if(userDevice != null) {
|
||||
@ -137,7 +138,7 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
||||
|
||||
public static String getDeviceStatus(int status) {
|
||||
|
||||
Log.d(LocalFileTransferActivity.TAG, "Peer Status: " + status);
|
||||
if(BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
||||
switch (status) {
|
||||
case WifiP2pDevice.AVAILABLE : return "Available";
|
||||
case WifiP2pDevice.INVITED : return "Invited";
|
||||
@ -149,6 +150,7 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
||||
}
|
||||
}
|
||||
|
||||
/* From WifiP2pManager.PeerListListener callback-interface */
|
||||
@Override
|
||||
public void onPeersAvailable(WifiP2pDeviceList peers) {
|
||||
|
||||
@ -171,25 +173,26 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
||||
((WifiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void onInitiateDiscovery() {
|
||||
public void onInitiateDiscovery() { // Setup UI for searching peers
|
||||
ProgressBar searchingPeersProgressBar = deviceListFragmentRootView.findViewById(R.id.progress_bar_searching_peers);
|
||||
searchingPeersProgressBar.setVisibility(View.VISIBLE);
|
||||
FrameLayout frameLayoutPeerDevices = deviceListFragmentRootView.findViewById(R.id.frame_layout_peer_devices);
|
||||
frameLayoutPeerDevices.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
/* From WifiP2pManager.ConnectionInfoListener callback-interface */
|
||||
@Override
|
||||
public void onConnectionInfoAvailable(WifiP2pInfo info) {
|
||||
/* Devices have successfully connected, and 'info' holds information about the wifi p2p group formed */
|
||||
groupInfo = info;
|
||||
|
||||
// Start handshake between the devices
|
||||
new PeerGroupHandshakeAsyncTask(this, groupInfo).execute();
|
||||
// TODO: Disable onclick listener (of list) for connecting to devices - DONE using boolean startedFileTransfer
|
||||
}
|
||||
|
||||
public void setClientAddress(InetAddress clientAddress) {
|
||||
if(clientAddress == null) {
|
||||
// null is returned only in case of a failed handshake
|
||||
Toast.makeText(localFileTransferActivity, "Peer device not cooperating for transfer", Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(localFileTransferActivity, "Selected device not cooperating for transfer", Toast.LENGTH_LONG).show();
|
||||
localFileTransferActivity.closeLocalFileTransferActivity();
|
||||
return;
|
||||
}
|
||||
|
@ -11,6 +11,12 @@ import android.widget.Toast;
|
||||
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
|
||||
/**
|
||||
* Helper class for the local file sharing module.
|
||||
*
|
||||
* Handles the broadcasts pertaining to the wifi p2p group formed in WiFi Direct. Works along with
|
||||
* the wifi p2p manager in {@link LocalFileTransferActivity}.
|
||||
* */
|
||||
public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
private WifiP2pManager manager;
|
||||
@ -29,47 +35,46 @@ public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
||||
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) {
|
||||
wifiActivity.setWifiP2pEnabled(true);
|
||||
} else {
|
||||
wifiActivity.setWifiP2pEnabled(false);
|
||||
Toast.makeText(wifiActivity, "Cannot discover peers without WiFi", Toast.LENGTH_SHORT).show();
|
||||
//TODO
|
||||
wifiActivity.resetPeers();
|
||||
}
|
||||
Log.d(wifiActivity.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)) {
|
||||
|
||||
if(manager != null) {
|
||||
//TODO
|
||||
/* List of available peers has changed, so request & use the new list through
|
||||
* PeerListListener.requestPeers() callback */
|
||||
manager.requestPeers(channel, (WifiP2pManager.PeerListListener) wifiActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_device_list));
|
||||
}
|
||||
Log.d(wifiActivity.TAG, "P2P peers changed");
|
||||
Log.d(LocalFileTransferActivity.TAG, "P2P peers changed");
|
||||
|
||||
} else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
|
||||
|
||||
if(manager == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
|
||||
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
|
||||
|
||||
if(networkInfo.isConnected()) {
|
||||
//TODO
|
||||
//
|
||||
|
||||
// Request connection info about the wifi p2p group formed upon connection
|
||||
manager.requestConnectionInfo(channel, (DeviceListFragment) wifiActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_device_list));
|
||||
} else {
|
||||
// Not connected after connection change -> Disconnected
|
||||
wifiActivity.resetData();
|
||||
}
|
||||
|
||||
} else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
|
||||
//TODO
|
||||
// Update UI with wifi-direct details about the user device
|
||||
DeviceListFragment deviceListFragment = (DeviceListFragment) wifiActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_device_list);
|
||||
deviceListFragment.updateUserDevice((WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
|
||||
deviceListFragment.updateUserDevice(intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user