mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 20:24:03 -04:00
Refactor WifiDirectManager
This commit is contained in:
parent
9ca2ce00ca
commit
083d7768b8
@ -131,9 +131,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
|||||||
Intent filesIntent = getIntent();
|
Intent filesIntent = getIntent();
|
||||||
fileUriArrayList = filesIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
fileUriArrayList = filesIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
||||||
isFileSender = (fileUriArrayList != null && fileUriArrayList.size() > 0);
|
isFileSender = (fileUriArrayList != null && fileUriArrayList.size() > 0);
|
||||||
/*if (fileUriArrayList != null && fileUriArrayList.size() > 0) {
|
|
||||||
setDeviceAsFileSender();
|
|
||||||
}*/
|
|
||||||
|
|
||||||
setSupportActionBar(actionBar);
|
setSupportActionBar(actionBar);
|
||||||
actionBar.setNavigationIcon(R.drawable.ic_close_white_24dp);
|
actionBar.setNavigationIcon(R.drawable.ic_close_white_24dp);
|
||||||
@ -144,7 +141,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
wifiDirectManager.initialiseWifiDirectManager();
|
wifiDirectManager.initialiseWifiDirectManager(alertDialogShower);
|
||||||
|
|
||||||
listViewPeerDevices.setAdapter(
|
listViewPeerDevices.setAdapter(
|
||||||
new WifiPeerListAdapter(this, R.layout.row_peer_device, peerDevices));
|
new WifiPeerListAdapter(this, R.layout.row_peer_device, peerDevices));
|
||||||
@ -169,7 +166,8 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
|||||||
|
|
||||||
WifiP2pDevice senderSelectedPeerDevice =
|
WifiP2pDevice senderSelectedPeerDevice =
|
||||||
(WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
(WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
||||||
wifiDirectManager.setSenderSelectedPeerDevice(senderSelectedPeerDevice);
|
wifiDirectManager.sendToDevice(senderSelectedPeerDevice);
|
||||||
|
/*wifiDirectManager.setSenderSelectedPeerDevice(senderSelectedPeerDevice);
|
||||||
alertDialogShower.show(
|
alertDialogShower.show(
|
||||||
new KiwixDialog.FileTransferConfirmation(senderSelectedPeerDevice.deviceName),
|
new KiwixDialog.FileTransferConfirmation(senderSelectedPeerDevice.deviceName),
|
||||||
new Function0<Unit>() {
|
new Function0<Unit>() {
|
||||||
@ -179,7 +177,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
|||||||
Toast.LENGTH_LONG);
|
Toast.LENGTH_LONG);
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
}
|
}
|
||||||
});
|
});*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -218,10 +216,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Helper methods used in the activity */
|
/* Helper methods used in the activity */
|
||||||
public void setDeviceAsFileSender() {
|
|
||||||
isFileSender = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isFileSender() {
|
public boolean isFileSender() {
|
||||||
return isFileSender;
|
return isFileSender;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,11 @@ import android.util.Log;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import kotlin.Unit;
|
||||||
|
import kotlin.jvm.functions.Function0;
|
||||||
import org.kiwix.kiwixmobile.R;
|
import org.kiwix.kiwixmobile.R;
|
||||||
|
import org.kiwix.kiwixmobile.utils.AlertDialogShower;
|
||||||
|
import org.kiwix.kiwixmobile.utils.KiwixDialog;
|
||||||
|
|
||||||
import static android.os.Looper.getMainLooper;
|
import static android.os.Looper.getMainLooper;
|
||||||
import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTransferActivity.showToast;
|
import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTransferActivity.showToast;
|
||||||
@ -37,26 +41,29 @@ 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();
|
/*private final IntentFilter intentFilter = new IntentFilter();
|
||||||
// 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*/
|
||||||
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
|
||||||
private WifiP2pInfo groupInfo; // Corresponds to P2P group formed between the two devices
|
private WifiP2pInfo groupInfo; // Corresponds to P2P group formed between the two devices
|
||||||
|
|
||||||
private WifiP2pDevice senderSelectedPeerDevice = null;
|
private WifiP2pDevice senderSelectedPeerDevice = null;
|
||||||
|
private AlertDialogShower alertDialogShower;
|
||||||
|
|
||||||
public WifiDirectManager(@NonNull LocalFileTransferActivity activity) {
|
public WifiDirectManager(@NonNull LocalFileTransferActivity activity) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialisations for using the WiFi P2P API */
|
/* Initialisations for using the WiFi P2P API */
|
||||||
public void initialiseWifiDirectManager() {
|
public void initialiseWifiDirectManager(@NonNull AlertDialogShower alertDialogShower) {
|
||||||
// Intents that the broadcast receiver will be responding to
|
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_STATE_CHANGED_ACTION);
|
||||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
|
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
|
||||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
|
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
|
||||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_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);
|
||||||
@ -64,6 +71,14 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
|||||||
|
|
||||||
public void registerWifiDirectBroadcastRecevier() {
|
public void registerWifiDirectBroadcastRecevier() {
|
||||||
receiver = new WifiDirectBroadcastReceiver(manager, channel, activity);
|
receiver = new WifiDirectBroadcastReceiver(manager, channel, activity);
|
||||||
|
|
||||||
|
// For specifying broadcasts (of the P2P API) that the module needs to respond to
|
||||||
|
IntentFilter intentFilter = new IntentFilter();
|
||||||
|
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);
|
||||||
|
|
||||||
activity.registerReceiver(receiver, intentFilter);
|
activity.registerReceiver(receiver, intentFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,8 +161,22 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
|||||||
return groupInfo.groupOwnerAddress;
|
return groupInfo.groupOwnerAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSenderSelectedPeerDevice(@NonNull WifiP2pDevice senderSelectedPeerDevice) {
|
/*public void setSenderSelectedPeerDevice(@NonNull WifiP2pDevice senderSelectedPeerDevice) {
|
||||||
this.senderSelectedPeerDevice = senderSelectedPeerDevice;
|
this.senderSelectedPeerDevice = senderSelectedPeerDevice;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public void sendToDevice(@NonNull WifiP2pDevice senderSelectedPeerDevice) {
|
||||||
|
this.senderSelectedPeerDevice = senderSelectedPeerDevice;
|
||||||
|
|
||||||
|
alertDialogShower.show(
|
||||||
|
new KiwixDialog.FileTransferConfirmation(senderSelectedPeerDevice.deviceName),
|
||||||
|
new Function0<Unit>() {
|
||||||
|
@Override public Unit invoke() {
|
||||||
|
connect();
|
||||||
|
showToast(activity, R.string.performing_handshake, Toast.LENGTH_LONG);
|
||||||
|
return Unit.INSTANCE;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect() {
|
public void connect() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user