mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 12:42:56 -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
|
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
|
null, R.string.transfer_to, R.string.yes, android.R.string.cancel
|
||||||
), HasBodyFormatArgs {
|
), HasBodyFormatArgs {
|
||||||
constructor(selectedPeerDevice: WifiP2pDevice) : this(arrayOf(selectedPeerDevice.deviceName))
|
constructor(selectedPeerDeviceName: String) : this(arrayOf(selectedPeerDeviceName))
|
||||||
}
|
}
|
||||||
|
|
||||||
open class YesNoDialog(
|
open class YesNoDialog(
|
||||||
|
@ -16,7 +16,6 @@ import android.util.Log;
|
|||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.AdapterView;
|
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
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 {
|
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";
|
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_WIFI_P2P = 1;
|
||||||
public static final int REQUEST_ENABLE_LOCATION_SERVICES = 2;
|
public static final int REQUEST_ENABLE_LOCATION_SERVICES = 2;
|
||||||
private static final int PERMISSION_REQUEST_CODE_COARSE_LOCATION = 1;
|
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 totalFilesForTransfer = -1;
|
||||||
private int filesSent = 0; // Count of number of files transferred until now
|
private int filesSent = 0; // Count of number of files transferred until now
|
||||||
private ArrayList<FileItem> filesToSend = new ArrayList<>();
|
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 List<WifiP2pDevice> peerDevices = new ArrayList<WifiP2pDevice>();
|
||||||
|
|
||||||
private WifiP2pDevice selectedPeerDevice = null;
|
|
||||||
private InetAddress selectedPeerDeviceInetAddress;
|
private InetAddress selectedPeerDeviceInetAddress;
|
||||||
|
|
||||||
private InetAddress fileReceiverDeviceAddress; // IP address of the file receiving device
|
private InetAddress fileReceiverDeviceAddress; // IP address of the file receiving device
|
||||||
private boolean fileTransferStarted = false;
|
private boolean fileTransferStarted = false;
|
||||||
|
|
||||||
@ -114,8 +109,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
private SenderDeviceAsyncTask senderDeviceAsyncTaskArray[];
|
private SenderDeviceAsyncTask senderDeviceAsyncTaskArray[];
|
||||||
private ReceiverDeviceAsyncTask receiverDeviceAsyncTask;
|
private ReceiverDeviceAsyncTask receiverDeviceAsyncTask;
|
||||||
|
|
||||||
private FileListAdapter fileListAdapter;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -170,11 +163,12 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
selectedPeerDevice = (WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
WifiP2pDevice senderSelectedPeerDevice = (WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
||||||
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(selectedPeerDevice),
|
wifiDirectManager.setSenderSelectedPeerDevice(senderSelectedPeerDevice);
|
||||||
|
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(senderSelectedPeerDevice.deviceName),
|
||||||
new Function0<Unit>() {
|
new Function0<Unit>() {
|
||||||
@Override public Unit invoke() {
|
@Override public Unit invoke() {
|
||||||
(wifiDirectManager).connect(selectedPeerDevice);
|
wifiDirectManager.connect();
|
||||||
showToast(LocalFileTransferActivity.this, R.string.performing_handshake, Toast.LENGTH_LONG);
|
showToast(LocalFileTransferActivity.this, R.string.performing_handshake, Toast.LENGTH_LONG);
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
}
|
}
|
||||||
@ -196,7 +190,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
|
|
||||||
if (!checkExternalStorageWritePermission()) return true;
|
if (!checkExternalStorageWritePermission()) return true;
|
||||||
|
|
||||||
// Initiate discovery
|
/* Initiate discovery */
|
||||||
if (!isWifiP2pEnabled()) {
|
if (!isWifiP2pEnabled()) {
|
||||||
requestEnableWifiP2pServices();
|
requestEnableWifiP2pServices();
|
||||||
return true;
|
return true;
|
||||||
@ -237,8 +231,8 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
return this.wifiDirectManager.isWifiP2pEnabled();
|
return this.wifiDirectManager.isWifiP2pEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateUserDevice(WifiP2pDevice device) { // Update UI with user device's details
|
public void updateUserDevice(WifiP2pDevice userDevice) { // Update UI with user device's details
|
||||||
this.userDevice = device;
|
wifiDirectManager.setUserDevice(userDevice);
|
||||||
|
|
||||||
if (userDevice != null) {
|
if (userDevice != null) {
|
||||||
deviceName.setText(userDevice.deviceName);
|
deviceName.setText(userDevice.deviceName);
|
||||||
@ -251,20 +245,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
((WifiPeerListAdapter) listViewPeerDevices.getAdapter()).notifyDataSetChanged();
|
((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) {
|
public static String getDeviceStatus(int status) {
|
||||||
|
|
||||||
if (BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
if (BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
||||||
@ -319,17 +299,17 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
private void startFileTransfer() {
|
private void startFileTransfer() {
|
||||||
fileTransferStarted = true;
|
fileTransferStarted = true;
|
||||||
|
|
||||||
if (groupInfo.groupFormed && !fileSendingDevice) {
|
if (wifiDirectManager.isGroupFormed() && !fileSendingDevice) {
|
||||||
displayFileTransferProgress();
|
displayFileTransferProgress();
|
||||||
|
|
||||||
receiverDeviceAsyncTask = new ReceiverDeviceAsyncTask(this);
|
receiverDeviceAsyncTask = new ReceiverDeviceAsyncTask(this);
|
||||||
receiverDeviceAsyncTask.execute();
|
receiverDeviceAsyncTask.execute();
|
||||||
} else if (groupInfo.groupFormed) {
|
} else if (wifiDirectManager.isGroupFormed()) { // && fileSendingDevice
|
||||||
{
|
{
|
||||||
Log.d(LocalFileTransferActivity.TAG, "Starting file transfer");
|
Log.d(LocalFileTransferActivity.TAG, "Starting file transfer");
|
||||||
|
|
||||||
fileReceiverDeviceAddress =
|
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
|
// Hack for allowing slower receiver devices to setup server before sender device requests to connect
|
||||||
showToast(this, R.string.preparing_files, Toast.LENGTH_LONG);
|
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() {
|
public int getTotalFilesForTransfer() {
|
||||||
return totalFilesForTransfer;
|
return totalFilesForTransfer;
|
||||||
}
|
}
|
||||||
@ -417,9 +393,10 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
|
|
||||||
/* From WifiP2pManager.ConnectionInfoListener callback-interface */
|
/* From WifiP2pManager.ConnectionInfoListener callback-interface */
|
||||||
@Override
|
@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 */
|
/* Devices have successfully connected, and 'info' holds information about the wifi p2p group formed */
|
||||||
groupInfo = info;
|
wifiDirectManager.setGroupInfo(groupInfo);
|
||||||
|
|
||||||
// Start handshake between the devices
|
// Start handshake between the devices
|
||||||
if(BuildConfig.DEBUG) {
|
if(BuildConfig.DEBUG) {
|
||||||
Log.d(TAG, "Starting handshake");
|
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) {
|
static void showToast(Context context, int stringResource, int duration) {
|
||||||
showToast(context, context.getString(stringResource), duration);
|
showToast(context, context.getString(stringResource), duration);
|
||||||
}
|
}
|
||||||
|
@ -6,11 +6,13 @@ import android.content.IntentFilter;
|
|||||||
import android.net.wifi.WpsInfo;
|
import android.net.wifi.WpsInfo;
|
||||||
import android.net.wifi.p2p.WifiP2pConfig;
|
import android.net.wifi.p2p.WifiP2pConfig;
|
||||||
import android.net.wifi.p2p.WifiP2pDevice;
|
import android.net.wifi.p2p.WifiP2pDevice;
|
||||||
|
import android.net.wifi.p2p.WifiP2pInfo;
|
||||||
import android.net.wifi.p2p.WifiP2pManager;
|
import android.net.wifi.p2p.WifiP2pManager;
|
||||||
import android.os.Build;
|
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 java.net.InetAddress;
|
||||||
import org.kiwix.kiwixmobile.R;
|
import org.kiwix.kiwixmobile.R;
|
||||||
|
|
||||||
import static android.os.Looper.getMainLooper;
|
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
|
// 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 WifiP2pInfo groupInfo; // Corresponds to P2P group formed between the two devices
|
||||||
|
|
||||||
|
private WifiP2pDevice senderSelectedPeerDevice = null;
|
||||||
|
|
||||||
public WifiDirectManager(@NonNull LocalFileTransferActivity activity) {
|
public WifiDirectManager(@NonNull LocalFileTransferActivity activity) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
}
|
}
|
||||||
@ -73,7 +80,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(int reason) {
|
public void onFailure(int reason) {
|
||||||
String errorMessage = activity.getErrorMessage(reason);
|
String errorMessage = getErrorMessage(reason);
|
||||||
Log.d(TAG, activity.getString(R.string.discovery_failed) + ": " + errorMessage);
|
Log.d(TAG, activity.getString(R.string.discovery_failed) + ": " + errorMessage);
|
||||||
showToast(activity,
|
showToast(activity,
|
||||||
activity.getString(R.string.discovery_failed),
|
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();
|
WifiP2pConfig config = new WifiP2pConfig();
|
||||||
config.deviceAddress = peerDevice.deviceAddress;
|
config.deviceAddress = senderSelectedPeerDevice.deviceAddress;
|
||||||
config.wps.setup = WpsInfo.PBC;
|
config.wps.setup = WpsInfo.PBC;
|
||||||
|
|
||||||
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
|
manager.connect(channel, config, new WifiP2pManager.ActionListener() {
|
||||||
@ -117,7 +152,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(int reason) {
|
public void onFailure(int reason) {
|
||||||
String errorMessage = activity.getErrorMessage(reason);
|
String errorMessage = getErrorMessage(reason);
|
||||||
Log.d(TAG, activity.getString(R.string.connection_failed) + ": " + errorMessage);
|
Log.d(TAG, activity.getString(R.string.connection_failed) + ": " + errorMessage);
|
||||||
showToast(activity, activity.getString(R.string.connection_failed),
|
showToast(activity, activity.getString(R.string.connection_failed),
|
||||||
Toast.LENGTH_LONG);
|
Toast.LENGTH_LONG);
|
||||||
@ -155,4 +190,19 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
|||||||
public void closeActivity() {
|
public void closeActivity() {
|
||||||
activity.finish();
|
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