mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 12:03:09 -04:00
Refactor LocalFileTransferActivity
This commit is contained in:
parent
de956fc663
commit
925c44b2c1
@ -28,7 +28,7 @@ sealed class KiwixDialog(
|
||||
data class FileTransferConfirmation(override val args: Array<out Any>) : KiwixDialog( // For the local file transfer module
|
||||
null, R.string.transfer_to, R.string.yes, android.R.string.cancel
|
||||
), HasBodyFormatArgs {
|
||||
constructor(selectedPeerDevice: WifiP2pDevice) : this(arrayOf(selectedPeerDevice.deviceName))
|
||||
constructor(selectedPeerDeviceName: String) : this(arrayOf(selectedPeerDeviceName))
|
||||
}
|
||||
|
||||
open class YesNoDialog(
|
||||
|
@ -16,7 +16,6 @@ import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
@ -70,8 +69,8 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.FileItem.Fil
|
||||
*/
|
||||
public class LocalFileTransferActivity extends AppCompatActivity implements WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener {
|
||||
|
||||
// Not a typo, 'Log' tags have a length upper limit of 25 characters
|
||||
public static final String TAG = "LocalFileTransferActvty";
|
||||
// Not a typo, 'Log' tags have a length upper limit of 25 characters
|
||||
public static final int REQUEST_ENABLE_WIFI_P2P = 1;
|
||||
public static final int REQUEST_ENABLE_LOCATION_SERVICES = 2;
|
||||
private static final int PERMISSION_REQUEST_CODE_COARSE_LOCATION = 1;
|
||||
@ -99,14 +98,10 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
private int totalFilesForTransfer = -1;
|
||||
private int filesSent = 0; // Count of number of files transferred until now
|
||||
private ArrayList<FileItem> filesToSend = new ArrayList<>();
|
||||
private FileListAdapter fileListAdapter;
|
||||
|
||||
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 List<WifiP2pDevice> peerDevices = new ArrayList<WifiP2pDevice>();
|
||||
|
||||
private WifiP2pDevice selectedPeerDevice = null;
|
||||
private InetAddress selectedPeerDeviceInetAddress;
|
||||
|
||||
private InetAddress fileReceiverDeviceAddress; // IP address of the file receiving device
|
||||
private boolean fileTransferStarted = false;
|
||||
|
||||
@ -114,8 +109,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
private SenderDeviceAsyncTask senderDeviceAsyncTaskArray[];
|
||||
private ReceiverDeviceAsyncTask receiverDeviceAsyncTask;
|
||||
|
||||
private FileListAdapter fileListAdapter;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -170,11 +163,12 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
return;
|
||||
}
|
||||
|
||||
selectedPeerDevice = (WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
||||
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(selectedPeerDevice),
|
||||
WifiP2pDevice senderSelectedPeerDevice = (WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
||||
wifiDirectManager.setSenderSelectedPeerDevice(senderSelectedPeerDevice);
|
||||
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(senderSelectedPeerDevice.deviceName),
|
||||
new Function0<Unit>() {
|
||||
@Override public Unit invoke() {
|
||||
(wifiDirectManager).connect(selectedPeerDevice);
|
||||
wifiDirectManager.connect();
|
||||
showToast(LocalFileTransferActivity.this, R.string.performing_handshake, Toast.LENGTH_LONG);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
@ -196,7 +190,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
|
||||
if (!checkExternalStorageWritePermission()) return true;
|
||||
|
||||
// Initiate discovery
|
||||
/* Initiate discovery */
|
||||
if (!isWifiP2pEnabled()) {
|
||||
requestEnableWifiP2pServices();
|
||||
return true;
|
||||
@ -237,8 +231,8 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
return this.wifiDirectManager.isWifiP2pEnabled();
|
||||
}
|
||||
|
||||
public void updateUserDevice(WifiP2pDevice device) { // Update UI with user device's details
|
||||
this.userDevice = device;
|
||||
public void updateUserDevice(WifiP2pDevice userDevice) { // Update UI with user device's details
|
||||
wifiDirectManager.setUserDevice(userDevice);
|
||||
|
||||
if (userDevice != null) {
|
||||
deviceName.setText(userDevice.deviceName);
|
||||
@ -251,20 +245,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
((WifiPeerListAdapter) listViewPeerDevices.getAdapter()).notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public String getErrorMessage(int reason) {
|
||||
switch (reason) {
|
||||
case WifiP2pManager.ERROR:
|
||||
return "Internal error";
|
||||
case WifiP2pManager.BUSY:
|
||||
return "Framework busy, unable to service request";
|
||||
case WifiP2pManager.P2P_UNSUPPORTED:
|
||||
return "P2P unsupported on this device";
|
||||
|
||||
default:
|
||||
return "Unknown error code - " + reason;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDeviceStatus(int status) {
|
||||
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
||||
@ -319,17 +299,17 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
private void startFileTransfer() {
|
||||
fileTransferStarted = true;
|
||||
|
||||
if (groupInfo.groupFormed && !fileSendingDevice) {
|
||||
if (wifiDirectManager.isGroupFormed() && !fileSendingDevice) {
|
||||
displayFileTransferProgress();
|
||||
|
||||
receiverDeviceAsyncTask = new ReceiverDeviceAsyncTask(this);
|
||||
receiverDeviceAsyncTask.execute();
|
||||
} else if (groupInfo.groupFormed) {
|
||||
} else if (wifiDirectManager.isGroupFormed()) { // && fileSendingDevice
|
||||
{
|
||||
Log.d(LocalFileTransferActivity.TAG, "Starting file transfer");
|
||||
|
||||
fileReceiverDeviceAddress =
|
||||
(groupInfo.isGroupOwner) ? selectedPeerDeviceInetAddress : groupInfo.groupOwnerAddress;
|
||||
(wifiDirectManager.isGroupOwner()) ? selectedPeerDeviceInetAddress : wifiDirectManager.getGroupOwnerAddress();
|
||||
|
||||
// Hack for allowing slower receiver devices to setup server before sender device requests to connect
|
||||
showToast(this, R.string.preparing_files, Toast.LENGTH_LONG);
|
||||
@ -344,10 +324,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
}
|
||||
}
|
||||
|
||||
public WifiP2pDevice getUserDevice() {
|
||||
return userDevice;
|
||||
}
|
||||
|
||||
public int getTotalFilesForTransfer() {
|
||||
return totalFilesForTransfer;
|
||||
}
|
||||
@ -417,9 +393,10 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
|
||||
/* From WifiP2pManager.ConnectionInfoListener callback-interface */
|
||||
@Override
|
||||
public void onConnectionInfoAvailable(WifiP2pInfo info) {
|
||||
public void onConnectionInfoAvailable(WifiP2pInfo groupInfo) {
|
||||
/* Devices have successfully connected, and 'info' holds information about the wifi p2p group formed */
|
||||
groupInfo = info;
|
||||
wifiDirectManager.setGroupInfo(groupInfo);
|
||||
|
||||
// Start handshake between the devices
|
||||
if(BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "Starting handshake");
|
||||
@ -579,7 +556,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
}
|
||||
}
|
||||
|
||||
/* Miscellaneous helper methods*/
|
||||
/* Miscellaneous helper methods */
|
||||
static void showToast(Context context, int stringResource, int duration) {
|
||||
showToast(context, context.getString(stringResource), duration);
|
||||
}
|
||||
|
@ -6,11 +6,13 @@ import android.content.IntentFilter;
|
||||
import android.net.wifi.WpsInfo;
|
||||
import android.net.wifi.p2p.WifiP2pConfig;
|
||||
import android.net.wifi.p2p.WifiP2pDevice;
|
||||
import android.net.wifi.p2p.WifiP2pInfo;
|
||||
import android.net.wifi.p2p.WifiP2pManager;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
import java.net.InetAddress;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
|
||||
import static android.os.Looper.getMainLooper;
|
||||
@ -38,6 +40,11 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
||||
// 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 WifiP2pDevice userDevice; // Represents the device on which the app is running
|
||||
private WifiP2pInfo groupInfo; // Corresponds to P2P group formed between the two devices
|
||||
|
||||
private WifiP2pDevice senderSelectedPeerDevice = null;
|
||||
|
||||
public WifiDirectManager(@NonNull LocalFileTransferActivity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
@ -73,7 +80,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
||||
|
||||
@Override
|
||||
public void onFailure(int reason) {
|
||||
String errorMessage = activity.getErrorMessage(reason);
|
||||
String errorMessage = getErrorMessage(reason);
|
||||
Log.d(TAG, activity.getString(R.string.discovery_failed) + ": " + errorMessage);
|
||||
showToast(activity,
|
||||
activity.getString(R.string.discovery_failed),
|
||||
@ -104,9 +111,37 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
||||
}
|
||||
}
|
||||
|
||||
public void connect(@NonNull final WifiP2pDevice peerDevice) {
|
||||
public void setUserDevice(WifiP2pDevice userDevice) {
|
||||
this.userDevice = userDevice;
|
||||
}
|
||||
|
||||
public void setGroupInfo(WifiP2pInfo groupInfo) {
|
||||
this.groupInfo = groupInfo;
|
||||
}
|
||||
|
||||
public boolean isGroupFormed() {
|
||||
return groupInfo.groupFormed;
|
||||
}
|
||||
|
||||
public boolean isGroupOwner() {
|
||||
return groupInfo.isGroupOwner;
|
||||
}
|
||||
|
||||
public InetAddress getGroupOwnerAddress() {
|
||||
return groupInfo.groupOwnerAddress;
|
||||
}
|
||||
|
||||
public void setSenderSelectedPeerDevice(WifiP2pDevice senderSelectedPeerDevice) {
|
||||
this.senderSelectedPeerDevice = senderSelectedPeerDevice;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
if(senderSelectedPeerDevice == null) {
|
||||
Log.d(TAG, "No device set as selected");
|
||||
}
|
||||
|
||||
WifiP2pConfig config = new WifiP2pConfig();
|
||||
config.deviceAddress = peerDevice.deviceAddress;
|
||||
config.deviceAddress = senderSelectedPeerDevice.deviceAddress;
|
||||
config.wps.setup = WpsInfo.PBC;
|
||||
|
||||
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
|
||||
@ -117,7 +152,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
||||
|
||||
@Override
|
||||
public void onFailure(int reason) {
|
||||
String errorMessage = activity.getErrorMessage(reason);
|
||||
String errorMessage = getErrorMessage(reason);
|
||||
Log.d(TAG, activity.getString(R.string.connection_failed) + ": " + errorMessage);
|
||||
showToast(activity, activity.getString(R.string.connection_failed),
|
||||
Toast.LENGTH_LONG);
|
||||
@ -155,4 +190,19 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
||||
public void closeActivity() {
|
||||
activity.finish();
|
||||
}
|
||||
|
||||
|
||||
public String getErrorMessage(int reason) {
|
||||
switch (reason) {
|
||||
case WifiP2pManager.ERROR:
|
||||
return "Internal error";
|
||||
case WifiP2pManager.BUSY:
|
||||
return "Framework busy, unable to service request";
|
||||
case WifiP2pManager.P2P_UNSUPPORTED:
|
||||
return "P2P unsupported on this device";
|
||||
|
||||
default:
|
||||
return "Unknown error code - " + reason;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user