mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Restyle: Applied project code style to entire local_file_transfer sub-package
This commit is contained in:
parent
d4ffe18ad8
commit
57e3aa0182
@ -54,8 +54,9 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
|||||||
* device and {@link ReceiverDeviceAsyncTask} files receiving device
|
* device and {@link ReceiverDeviceAsyncTask} files receiving device
|
||||||
*
|
*
|
||||||
* The starting point for the module is {@link LocalFileTransferActivity}
|
* The starting point for the module is {@link LocalFileTransferActivity}
|
||||||
* */
|
*/
|
||||||
public class DeviceListFragment extends ListFragment implements WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener {
|
public class DeviceListFragment extends ListFragment
|
||||||
|
implements WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener {
|
||||||
|
|
||||||
public static final String TAG = "DeviceListFragment";
|
public static final String TAG = "DeviceListFragment";
|
||||||
public static int PEER_HANDSHAKE_PORT = 8009;
|
public static int PEER_HANDSHAKE_PORT = 8009;
|
||||||
@ -64,8 +65,10 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
private SharedPreferenceUtil sharedPreferenceUtil;
|
private SharedPreferenceUtil sharedPreferenceUtil;
|
||||||
private AlertDialogShower alertDialogShower;
|
private AlertDialogShower alertDialogShower;
|
||||||
|
|
||||||
private LocalFileTransferActivity localFileTransferActivity; // Parent activity, starting point of the module
|
private LocalFileTransferActivity localFileTransferActivity;
|
||||||
private TransferProgressFragment transferProgressFragment; // Sibling fragment, for displaying transfer progress
|
// Parent activity, starting point of the module
|
||||||
|
private TransferProgressFragment transferProgressFragment;
|
||||||
|
// Sibling fragment, for displaying transfer progress
|
||||||
|
|
||||||
// Views part of the DeviceListFragment
|
// Views part of the DeviceListFragment
|
||||||
@BindView(R.id.text_view_device_name) TextView deviceName;
|
@BindView(R.id.text_view_device_name) TextView deviceName;
|
||||||
@ -75,13 +78,15 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
private Unbinder unbinder;
|
private Unbinder unbinder;
|
||||||
|
|
||||||
private boolean fileSender = false; // Whether file sending device or not
|
private boolean fileSender = false; // Whether file sending device or not
|
||||||
private ArrayList<Uri> fileUriList; // Uris of files to be shared, available only for the sender device
|
private ArrayList<Uri> fileUriList;
|
||||||
|
// Uris of files to be shared, available only for the sender device
|
||||||
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 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 the WiFi P2P group formed between the two devices
|
private WifiP2pInfo groupInfo;
|
||||||
|
// Corresponds to the WiFi 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 WifiP2pDevice selectedPeerDevice = null;
|
||||||
@ -100,7 +105,8 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
localFileTransferActivity = (LocalFileTransferActivity) getActivity();
|
localFileTransferActivity = (LocalFileTransferActivity) getActivity();
|
||||||
|
|
||||||
// As DeviceListFragment extends ListFragment for the purpose of displaying list of peers
|
// As DeviceListFragment extends ListFragment for the purpose of displaying list of peers
|
||||||
this.setListAdapter(new WifiPeerListAdapter(localFileTransferActivity, R.layout.row_peer_device, peerDevices));
|
this.setListAdapter(
|
||||||
|
new WifiPeerListAdapter(localFileTransferActivity, R.layout.row_peer_device, peerDevices));
|
||||||
|
|
||||||
if (localFileTransferActivity.isFileSender()) {
|
if (localFileTransferActivity.isFileSender()) {
|
||||||
fileSender = true;
|
fileSender = true;
|
||||||
@ -113,11 +119,11 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
|
|
||||||
displayTransferProgressFragment();
|
displayTransferProgressFragment();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.fragment_device_list, null);
|
View view = inflater.inflate(R.layout.fragment_device_list, null);
|
||||||
unbinder = ButterKnife.bind(this, view);
|
unbinder = ButterKnife.bind(this, view);
|
||||||
|
|
||||||
@ -133,11 +139,13 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
@Override
|
@Override
|
||||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||||
/* Connection can only be initiated by user of the sender device, & only when transfer has not been started */
|
/* Connection can only be initiated by user of the sender device, & only when transfer has not been started */
|
||||||
if(!isFileSender() || fileTransferStarted)
|
if (!isFileSender() || fileTransferStarted) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
selectedPeerDevice = (WifiP2pDevice) getListAdapter().getItem(position);
|
selectedPeerDevice = (WifiP2pDevice) getListAdapter().getItem(position);
|
||||||
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(selectedPeerDevice), new Function0<Unit>() {
|
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(selectedPeerDevice),
|
||||||
|
new Function0<Unit>() {
|
||||||
@Override public Unit invoke() {
|
@Override public Unit invoke() {
|
||||||
((DeviceActionListener) localFileTransferActivity).connect(selectedPeerDevice);
|
((DeviceActionListener) localFileTransferActivity).connect(selectedPeerDevice);
|
||||||
showToast(localFileTransferActivity, R.string.performing_handshake, Toast.LENGTH_LONG);
|
showToast(localFileTransferActivity, R.string.performing_handshake, Toast.LENGTH_LONG);
|
||||||
@ -167,13 +175,19 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
|
|
||||||
if (BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
if (BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case WifiP2pDevice.AVAILABLE : return "Available";
|
case WifiP2pDevice.AVAILABLE:
|
||||||
case WifiP2pDevice.INVITED : return "Invited";
|
return "Available";
|
||||||
case WifiP2pDevice.CONNECTED : return "Connected";
|
case WifiP2pDevice.INVITED:
|
||||||
case WifiP2pDevice.FAILED : return "Failed";
|
return "Invited";
|
||||||
case WifiP2pDevice.UNAVAILABLE:return "Unavailable";
|
case WifiP2pDevice.CONNECTED:
|
||||||
|
return "Connected";
|
||||||
|
case WifiP2pDevice.FAILED:
|
||||||
|
return "Failed";
|
||||||
|
case WifiP2pDevice.UNAVAILABLE:
|
||||||
|
return "Unavailable";
|
||||||
|
|
||||||
default: return "Unknown";
|
default:
|
||||||
|
return "Unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,12 +261,12 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
|
|
||||||
receiverDeviceAsyncTask = new ReceiverDeviceAsyncTask(this, transferProgressFragment);
|
receiverDeviceAsyncTask = new ReceiverDeviceAsyncTask(this, transferProgressFragment);
|
||||||
receiverDeviceAsyncTask.execute();
|
receiverDeviceAsyncTask.execute();
|
||||||
|
|
||||||
} else if (groupInfo.groupFormed) {
|
} else if (groupInfo.groupFormed) {
|
||||||
{
|
{
|
||||||
Log.d(LocalFileTransferActivity.TAG, "Starting file transfer");
|
Log.d(LocalFileTransferActivity.TAG, "Starting file transfer");
|
||||||
|
|
||||||
fileReceiverDeviceAddress = (groupInfo.isGroupOwner) ? selectedPeerDeviceInetAddress : groupInfo.groupOwnerAddress;
|
fileReceiverDeviceAddress =
|
||||||
|
(groupInfo.isGroupOwner) ? selectedPeerDeviceInetAddress : groupInfo.groupOwnerAddress;
|
||||||
|
|
||||||
// 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(localFileTransferActivity, R.string.preparing_files, Toast.LENGTH_LONG);
|
showToast(localFileTransferActivity, R.string.preparing_files, Toast.LENGTH_LONG);
|
||||||
@ -260,7 +274,8 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
|
|
||||||
senderDeviceAsyncTaskArray = new SenderDeviceAsyncTask[totalFilesForTransfer];
|
senderDeviceAsyncTaskArray = new SenderDeviceAsyncTask[totalFilesForTransfer];
|
||||||
for (int i = 0; i < totalFilesForTransfer; i++) {
|
for (int i = 0; i < totalFilesForTransfer; i++) {
|
||||||
senderDeviceAsyncTaskArray[i] = new SenderDeviceAsyncTask(this, transferProgressFragment, i);
|
senderDeviceAsyncTaskArray[i] =
|
||||||
|
new SenderDeviceAsyncTask(this, transferProgressFragment, i);
|
||||||
senderDeviceAsyncTaskArray[i].execute(fileUriList.get(i));
|
senderDeviceAsyncTaskArray[i].execute(fileUriList.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -305,7 +320,8 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
return (filesSent == totalFilesForTransfer);
|
return (filesSent == totalFilesForTransfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void performFieldInjection(SharedPreferenceUtil sharedPreferenceUtil, AlertDialogShower alertDialogShower) {
|
public void performFieldInjection(SharedPreferenceUtil sharedPreferenceUtil,
|
||||||
|
AlertDialogShower alertDialogShower) {
|
||||||
this.sharedPreferenceUtil = sharedPreferenceUtil;
|
this.sharedPreferenceUtil = sharedPreferenceUtil;
|
||||||
this.alertDialogShower = alertDialogShower;
|
this.alertDialogShower = alertDialogShower;
|
||||||
}
|
}
|
||||||
@ -318,14 +334,14 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
return fileReceiverDeviceAddress;
|
return fileReceiverDeviceAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getFileName(Uri fileUri) {
|
public static String getFileName(Uri fileUri) {
|
||||||
String fileUriString = fileUri.toString();
|
String fileUriString = fileUri.toString();
|
||||||
// Returns text after location of last slash in the file path
|
// Returns text after location of last slash in the file path
|
||||||
return fileUriString.substring(fileUriString.lastIndexOf('/') + 1);
|
return fileUriString.substring(fileUriString.lastIndexOf('/') + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copyToOutputStream(InputStream inputStream, OutputStream outputStream) throws IOException {
|
public static void copyToOutputStream(InputStream inputStream, OutputStream outputStream)
|
||||||
|
throws IOException {
|
||||||
byte[] bufferForBytes = new byte[1024];
|
byte[] bufferForBytes = new byte[1024];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.FileItem.Fil
|
|||||||
* Helper class, part of the local file sharing module.
|
* Helper class, part of the local file sharing module.
|
||||||
*
|
*
|
||||||
* Defines a file-item to represent the files being transferred.
|
* Defines a file-item to represent the files being transferred.
|
||||||
* */
|
*/
|
||||||
public class FileItem implements Parcelable {
|
public class FileItem implements Parcelable {
|
||||||
|
|
||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
|
@ -20,7 +20,7 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.FileItem.Fil
|
|||||||
* Helper class, part of the local file sharing module.
|
* Helper class, part of the local file sharing module.
|
||||||
*
|
*
|
||||||
* Defines the Adapter for the list of file-items displayed in {@link TransferProgressFragment}
|
* Defines the Adapter for the list of file-items displayed in {@link TransferProgressFragment}
|
||||||
* */
|
*/
|
||||||
public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.FileViewHolder> {
|
public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.FileViewHolder> {
|
||||||
private final ArrayList<FileItem> fileItems;
|
private final ArrayList<FileItem> fileItems;
|
||||||
|
|
||||||
@ -30,8 +30,10 @@ public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.FileVi
|
|||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public FileListAdapter.FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
public FileListAdapter.FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
|
||||||
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_transfer_list, parent, false);
|
int viewType) {
|
||||||
|
View itemView = LayoutInflater.from(parent.getContext())
|
||||||
|
.inflate(R.layout.item_transfer_list, parent, false);
|
||||||
return new FileViewHolder(itemView, this);
|
return new FileViewHolder(itemView, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,14 +47,16 @@ public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.FileVi
|
|||||||
if (fileItem.getFileStatus() == SENDING) {
|
if (fileItem.getFileStatus() == SENDING) {
|
||||||
holder.statusImage.setVisibility(View.GONE);
|
holder.statusImage.setVisibility(View.GONE);
|
||||||
holder.progressBar.setVisibility(View.VISIBLE);
|
holder.progressBar.setVisibility(View.VISIBLE);
|
||||||
|
} else if (fileItem.getFileStatus()
|
||||||
} else if(fileItem.getFileStatus() != TO_BE_SENT){ // Icon for TO_BE_SENT is assigned by default in the item layout
|
!= TO_BE_SENT) { // Icon for TO_BE_SENT is assigned by default in the item layout
|
||||||
holder.progressBar.setVisibility(View.GONE);
|
holder.progressBar.setVisibility(View.GONE);
|
||||||
|
|
||||||
switch (fileItem.getFileStatus()) {
|
switch (fileItem.getFileStatus()) {
|
||||||
case SENT : holder.statusImage.setImageResource(R.drawable.ic_baseline_check_24px);
|
case SENT:
|
||||||
|
holder.statusImage.setImageResource(R.drawable.ic_baseline_check_24px);
|
||||||
break;
|
break;
|
||||||
case ERROR: holder.statusImage.setImageResource(R.drawable.ic_baseline_error_24px);
|
case ERROR:
|
||||||
|
holder.statusImage.setImageResource(R.drawable.ic_baseline_error_24px);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,10 +52,12 @@ import javax.inject.Inject;
|
|||||||
*
|
*
|
||||||
* The module uses this activity along with {@link DeviceListFragment} to manage connection
|
* The module uses this activity along with {@link DeviceListFragment} to manage connection
|
||||||
* and file transfer between the devices.
|
* and file transfer between the devices.
|
||||||
* */
|
*/
|
||||||
public class LocalFileTransferActivity extends AppCompatActivity implements WifiP2pManager.ChannelListener, DeviceListFragment.DeviceActionListener {
|
public class LocalFileTransferActivity extends AppCompatActivity
|
||||||
|
implements WifiP2pManager.ChannelListener, DeviceListFragment.DeviceActionListener {
|
||||||
|
|
||||||
public static final String TAG = "LocalFileTransferActvty"; // 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_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;
|
||||||
@ -66,18 +68,20 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
|
|
||||||
@BindView(R.id.toolbar_local_file_transfer) Toolbar actionBar;
|
@BindView(R.id.toolbar_local_file_transfer) Toolbar actionBar;
|
||||||
|
|
||||||
private ArrayList<Uri> fileUriArrayList; // For sender device, stores Uris of files to be transferred
|
private ArrayList<Uri> fileUriArrayList;
|
||||||
|
// For sender device, stores Uris of files to be transferred
|
||||||
private Boolean fileSendingDevice = false;// Whether the device is the file sender or not
|
private Boolean fileSendingDevice = false;// Whether the device is the file sender or not
|
||||||
|
|
||||||
|
|
||||||
/* Variables related to the WiFi P2P API */
|
/* Variables related to the WiFi P2P API */
|
||||||
private boolean wifiP2pEnabled = false; // Whether WiFi has been enabled or not
|
private boolean wifiP2pEnabled = false; // Whether WiFi has been enabled or not
|
||||||
private boolean retryChannel = false; // Whether channel has retried connecting previously
|
private boolean retryChannel = false; // Whether channel has retried connecting previously
|
||||||
|
|
||||||
private WifiP2pManager manager; // Overall manager of Wifi p2p connections for the module
|
private WifiP2pManager manager; // Overall manager of Wifi p2p connections for the module
|
||||||
private WifiP2pManager.Channel channel; // Connects the module to device's underlying Wifi p2p framework
|
private WifiP2pManager.Channel channel;
|
||||||
|
// 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 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
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -151,30 +155,33 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final DeviceListFragment deviceListFragment = (DeviceListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_device_list);
|
final DeviceListFragment deviceListFragment =
|
||||||
|
(DeviceListFragment) getSupportFragmentManager().findFragmentById(
|
||||||
|
R.id.fragment_device_list);
|
||||||
deviceListFragment.onInitiateDiscovery();
|
deviceListFragment.onInitiateDiscovery();
|
||||||
deviceListFragment.performFieldInjection(sharedPreferenceUtil, alertDialogShower);
|
deviceListFragment.performFieldInjection(sharedPreferenceUtil, alertDialogShower);
|
||||||
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
showToast(LocalFileTransferActivity.this, R.string.discovery_initiated, Toast.LENGTH_SHORT);
|
showToast(LocalFileTransferActivity.this, R.string.discovery_initiated,
|
||||||
|
Toast.LENGTH_SHORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(int reason) {
|
public void onFailure(int reason) {
|
||||||
String errorMessage = getErrorMessage(reason);
|
String errorMessage = getErrorMessage(reason);
|
||||||
Log.d(TAG, getString(R.string.discovery_failed) + ": " + errorMessage);
|
Log.d(TAG, getString(R.string.discovery_failed) + ": " + errorMessage);
|
||||||
showToast(LocalFileTransferActivity.this, LocalFileTransferActivity.this.getString(R.string.discovery_failed), Toast.LENGTH_SHORT);
|
showToast(LocalFileTransferActivity.this,
|
||||||
|
LocalFileTransferActivity.this.getString(R.string.discovery_failed),
|
||||||
|
Toast.LENGTH_SHORT);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Helper methods used in the activity */
|
/* Helper methods used in the activity */
|
||||||
public void setDeviceAsFileSender() {
|
public void setDeviceAsFileSender() {
|
||||||
fileSendingDevice = true;
|
fileSendingDevice = true;
|
||||||
@ -198,23 +205,31 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
|
|
||||||
private String getErrorMessage(int reason) {
|
private String getErrorMessage(int reason) {
|
||||||
switch (reason) {
|
switch (reason) {
|
||||||
case WifiP2pManager.ERROR: return "Internal error";
|
case WifiP2pManager.ERROR:
|
||||||
case WifiP2pManager.BUSY: return "Framework busy, unable to service request";
|
return "Internal error";
|
||||||
case WifiP2pManager.P2P_UNSUPPORTED: return "P2P unsupported on this device";
|
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;
|
default:
|
||||||
|
return "Unknown error code - " + reason;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetPeers() {
|
public void resetPeers() {
|
||||||
DeviceListFragment deviceListFragment = (DeviceListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_device_list);
|
DeviceListFragment deviceListFragment =
|
||||||
|
(DeviceListFragment) getSupportFragmentManager().findFragmentById(
|
||||||
|
R.id.fragment_device_list);
|
||||||
if (deviceListFragment != null) {
|
if (deviceListFragment != null) {
|
||||||
deviceListFragment.clearPeers();
|
deviceListFragment.clearPeers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetData() {
|
public void resetData() {
|
||||||
DeviceListFragment deviceListFragment = (DeviceListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_device_list);
|
DeviceListFragment deviceListFragment =
|
||||||
|
(DeviceListFragment) getSupportFragmentManager().findFragmentById(
|
||||||
|
R.id.fragment_device_list);
|
||||||
if (deviceListFragment != null) {
|
if (deviceListFragment != null) {
|
||||||
deviceListFragment.clearPeers();
|
deviceListFragment.clearPeers();
|
||||||
}
|
}
|
||||||
@ -228,7 +243,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
Toast.makeText(context, text, duration).show();
|
Toast.makeText(context, text, duration).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* From WifiP2pManager.ChannelListener interface */
|
/* From WifiP2pManager.ChannelListener interface */
|
||||||
@Override
|
@Override
|
||||||
public void onChannelDisconnected() {
|
public void onChannelDisconnected() {
|
||||||
@ -238,13 +252,11 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
resetData();
|
resetData();
|
||||||
retryChannel = true;
|
retryChannel = true;
|
||||||
manager.initialize(this, getMainLooper(), this);
|
manager.initialize(this, getMainLooper(), this);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
showToast(this, R.string.severe_loss_error, Toast.LENGTH_LONG);
|
showToast(this, R.string.severe_loss_error, Toast.LENGTH_LONG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* From DeviceListFragment.DeviceActionListener interface */
|
/* From DeviceListFragment.DeviceActionListener interface */
|
||||||
@Override
|
@Override
|
||||||
public void connect(@NonNull final WifiP2pDevice peerDevice) {
|
public void connect(@NonNull final WifiP2pDevice peerDevice) {
|
||||||
@ -262,14 +274,17 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
public void onFailure(int reason) {
|
public void onFailure(int reason) {
|
||||||
String errorMessage = getErrorMessage(reason);
|
String errorMessage = getErrorMessage(reason);
|
||||||
Log.d(TAG, getString(R.string.connection_failed) + ": " + errorMessage);
|
Log.d(TAG, getString(R.string.connection_failed) + ": " + errorMessage);
|
||||||
showToast(LocalFileTransferActivity.this, getString(R.string.connection_failed), Toast.LENGTH_LONG);
|
showToast(LocalFileTransferActivity.this, getString(R.string.connection_failed),
|
||||||
|
Toast.LENGTH_LONG);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void closeLocalFileTransferActivity() {
|
public void closeLocalFileTransferActivity() {
|
||||||
final DeviceListFragment deviceListFragment = (DeviceListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_device_list);
|
final DeviceListFragment deviceListFragment =
|
||||||
|
(DeviceListFragment) getSupportFragmentManager().findFragmentById(
|
||||||
|
R.id.fragment_device_list);
|
||||||
if (deviceListFragment != null) {
|
if (deviceListFragment != null) {
|
||||||
deviceListFragment.cancelAsyncTasks();
|
deviceListFragment.cancelAsyncTasks();
|
||||||
}
|
}
|
||||||
@ -291,7 +306,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
Log.d(TAG, "Disconnect successful");
|
Log.d(TAG, "Disconnect successful");
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,18 +313,21 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
private boolean checkCoarseLocationAccessPermission() { // Required by Android to detect wifi-p2p peers
|
private boolean checkCoarseLocationAccessPermission() { // Required by Android to detect wifi-p2p peers
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
|
||||||
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) {
|
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) {
|
||||||
alertDialogShower.show(KiwixDialog.LocationPermissionRationale.INSTANCE, new Function0<Unit>() {
|
alertDialogShower.show(KiwixDialog.LocationPermissionRationale.INSTANCE,
|
||||||
|
new Function0<Unit>() {
|
||||||
@Override public Unit invoke() {
|
@Override public Unit invoke() {
|
||||||
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_CODE_COARSE_LOCATION);
|
requestPermissions(new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
|
||||||
|
PERMISSION_REQUEST_CODE_COARSE_LOCATION);
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_CODE_COARSE_LOCATION);
|
requestPermissions(new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
|
||||||
|
PERMISSION_REQUEST_CODE_COARSE_LOCATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -323,18 +340,21 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
private boolean checkExternalStorageWritePermission() { // To access and store the zims
|
private boolean checkExternalStorageWritePermission() { // To access and store the zims
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
|
||||||
if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||||
alertDialogShower.show(KiwixDialog.StoragePermissionRationale.INSTANCE, new Function0<Unit>() {
|
alertDialogShower.show(KiwixDialog.StoragePermissionRationale.INSTANCE,
|
||||||
|
new Function0<Unit>() {
|
||||||
@Override public Unit invoke() {
|
@Override public Unit invoke() {
|
||||||
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE_STORAGE_WRITE_ACCESS);
|
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
|
||||||
|
PERMISSION_REQUEST_CODE_STORAGE_WRITE_ACCESS);
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE_STORAGE_WRITE_ACCESS);
|
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
|
||||||
|
PERMISSION_REQUEST_CODE_STORAGE_WRITE_ACCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -371,34 +391,42 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isLocationServicesEnabled() {
|
private boolean isLocationServicesEnabled() {
|
||||||
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
|
LocationManager locationManager =
|
||||||
|
(LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
|
||||||
boolean gps_enabled = false;
|
boolean gps_enabled = false;
|
||||||
boolean network_enabled = false;
|
boolean network_enabled = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
||||||
} catch(Exception ex) {ex.printStackTrace();}
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
||||||
} catch(Exception ex) {ex.printStackTrace();}
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
return (gps_enabled || network_enabled);
|
return (gps_enabled || network_enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requestEnableLocationServices() {
|
private void requestEnableLocationServices() {
|
||||||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
||||||
Fragment prev = getSupportFragmentManager().findFragmentByTag(RequestEnableLocationServicesDialog.TAG);
|
Fragment prev =
|
||||||
|
getSupportFragmentManager().findFragmentByTag(RequestEnableLocationServicesDialog.TAG);
|
||||||
|
|
||||||
if (prev == null) {
|
if (prev == null) {
|
||||||
RequestEnableLocationServicesDialog dialogFragment = new RequestEnableLocationServicesDialog();
|
RequestEnableLocationServicesDialog dialogFragment =
|
||||||
|
new RequestEnableLocationServicesDialog();
|
||||||
dialogFragment.show(fragmentTransaction, RequestEnableLocationServicesDialog.TAG);
|
dialogFragment.show(fragmentTransaction, RequestEnableLocationServicesDialog.TAG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requestEnableWifiP2pServices() {
|
private void requestEnableWifiP2pServices() {
|
||||||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
||||||
Fragment prev = getSupportFragmentManager().findFragmentByTag(RequestEnableWifiP2pServicesDialog.TAG);
|
Fragment prev =
|
||||||
|
getSupportFragmentManager().findFragmentByTag(RequestEnableWifiP2pServicesDialog.TAG);
|
||||||
|
|
||||||
if (prev == null) {
|
if (prev == null) {
|
||||||
RequestEnableWifiP2pServicesDialog dialogFragment = new RequestEnableWifiP2pServicesDialog();
|
RequestEnableWifiP2pServicesDialog dialogFragment = new RequestEnableWifiP2pServicesDialog();
|
||||||
@ -412,9 +440,11 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
|
|
||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
case REQUEST_ENABLE_LOCATION_SERVICES: {
|
case REQUEST_ENABLE_LOCATION_SERVICES: {
|
||||||
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
|
LocationManager locationManager =
|
||||||
|
(LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
|
||||||
|
|
||||||
if(!(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))) {
|
if (!(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|
||||||
|
|| locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))) {
|
||||||
// If neither provider is enabled
|
// If neither provider is enabled
|
||||||
showToast(this, R.string.permission_refused_location, Toast.LENGTH_LONG);
|
showToast(this, R.string.permission_refused_location, Toast.LENGTH_LONG);
|
||||||
}
|
}
|
||||||
@ -445,7 +475,9 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
|||||||
|
|
||||||
@Override protected void onDestroy() {
|
@Override protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
final DeviceListFragment deviceListFragment = (DeviceListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_device_list);
|
final DeviceListFragment deviceListFragment =
|
||||||
|
(DeviceListFragment) getSupportFragmentManager().findFragmentById(
|
||||||
|
R.id.fragment_device_list);
|
||||||
if (deviceListFragment != null) {
|
if (deviceListFragment != null) {
|
||||||
deviceListFragment.cancelAsyncTasks();
|
deviceListFragment.cancelAsyncTasks();
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.FileItem.Fil
|
|||||||
* (no. of files & their names) with the receiver. Finally, the onPostExecute() of the sender
|
* (no. of files & their names) with the receiver. Finally, the onPostExecute() of the sender
|
||||||
* initiates the file transfer through {@link SenderDeviceAsyncTask} on the sender and using
|
* initiates the file transfer through {@link SenderDeviceAsyncTask} on the sender and using
|
||||||
* {@link ReceiverDeviceAsyncTask} on the receiver.
|
* {@link ReceiverDeviceAsyncTask} on the receiver.
|
||||||
* */
|
*/
|
||||||
class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
||||||
|
|
||||||
private static final String TAG = "PeerGrpHndshakeAsyncTsk";
|
private static final String TAG = "PeerGrpHndshakeAsyncTsk";
|
||||||
@ -66,7 +66,6 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
|||||||
exchangeFileTransferMetadata(server.getOutputStream(), server.getInputStream());
|
exchangeFileTransferMetadata(server.getOutputStream(), server.getInputStream());
|
||||||
|
|
||||||
return server.getInetAddress();
|
return server.getInetAddress();
|
||||||
|
|
||||||
} else { // Selected device is not accepting wifi direct connections through the kiwix app
|
} else { // Selected device is not accepting wifi direct connections through the kiwix app
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -74,25 +73,24 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
} else if (groupInfo.groupFormed && !isCancelled()) { //&& !groupInfo.isGroupOwner
|
||||||
else if(groupInfo.groupFormed && !isCancelled()) { //&& !groupInfo.isGroupOwner
|
|
||||||
try (Socket client = new Socket()) {
|
try (Socket client = new Socket()) {
|
||||||
client.setReuseAddress(true);
|
client.setReuseAddress(true);
|
||||||
client.connect((new InetSocketAddress(groupInfo.groupOwnerAddress.getHostAddress(), PEER_HANDSHAKE_PORT)), 15000);
|
client.connect((new InetSocketAddress(groupInfo.groupOwnerAddress.getHostAddress(),
|
||||||
|
PEER_HANDSHAKE_PORT)), 15000);
|
||||||
|
|
||||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(client.getOutputStream());
|
ObjectOutputStream objectOutputStream = new ObjectOutputStream(client.getOutputStream());
|
||||||
objectOutputStream.writeObject(HANDSHAKE_MESSAGE); // Send message for the peer device to verify
|
objectOutputStream.writeObject(
|
||||||
|
HANDSHAKE_MESSAGE); // Send message for the peer device to verify
|
||||||
|
|
||||||
exchangeFileTransferMetadata(client.getOutputStream(), client.getInputStream());
|
exchangeFileTransferMetadata(client.getOutputStream(), client.getInputStream());
|
||||||
|
|
||||||
return groupInfo.groupOwnerAddress;
|
return groupInfo.groupOwnerAddress;
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,16 +107,13 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
|||||||
|
|
||||||
ArrayList<Uri> fileUriList = deviceListFragment.getFileUriList();
|
ArrayList<Uri> fileUriList = deviceListFragment.getFileUriList();
|
||||||
for (
|
for (
|
||||||
Uri fileUri :fileUriList)
|
Uri fileUri : fileUriList) { // Send the names of each of those files, in order
|
||||||
|
|
||||||
{ // Send the names of each of those files, in order
|
|
||||||
objectOutputStream.writeObject(getFileName(fileUri));
|
objectOutputStream.writeObject(getFileName(fileUri));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
} else { // Device is not the file sender
|
||||||
else { // Device is not the file sender
|
|
||||||
try (ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
|
try (ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
|
||||||
// Read the number of files
|
// Read the number of files
|
||||||
Object totalFilesObject = objectInputStream.readObject();
|
Object totalFilesObject = objectInputStream.readObject();
|
||||||
|
@ -29,7 +29,7 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
|||||||
*
|
*
|
||||||
* A single Task is used for the entire file transfer (the server socket accepts connections as
|
* A single Task is used for the entire file transfer (the server socket accepts connections as
|
||||||
* many times as the no. of files).
|
* many times as the no. of files).
|
||||||
* */
|
*/
|
||||||
class ReceiverDeviceAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
class ReceiverDeviceAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||||
|
|
||||||
private static final String TAG = "ReceiverDeviceAsyncTask";
|
private static final String TAG = "ReceiverDeviceAsyncTask";
|
||||||
@ -38,7 +38,8 @@ class ReceiverDeviceAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
private TransferProgressFragment transferProgressFragment;
|
private TransferProgressFragment transferProgressFragment;
|
||||||
private int fileItemIndex;
|
private int fileItemIndex;
|
||||||
|
|
||||||
public ReceiverDeviceAsyncTask(DeviceListFragment deviceListFragment, TransferProgressFragment transferProgressFragment) {
|
public ReceiverDeviceAsyncTask(DeviceListFragment deviceListFragment,
|
||||||
|
TransferProgressFragment transferProgressFragment) {
|
||||||
this.deviceListFragment = deviceListFragment;
|
this.deviceListFragment = deviceListFragment;
|
||||||
this.transferProgressFragment = transferProgressFragment;
|
this.transferProgressFragment = transferProgressFragment;
|
||||||
}
|
}
|
||||||
@ -78,11 +79,11 @@ class ReceiverDeviceAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
deviceListFragment.incrementTotalFilesSent();
|
deviceListFragment.incrementTotalFilesSent();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isCancelled())
|
if (isCancelled()) {
|
||||||
return false; // Returned in case the task was cancelled
|
return false; // Returned in case the task was cancelled
|
||||||
else
|
} else {
|
||||||
return true; // Returned in case of a successful file transfer
|
return true; // Returned in case of a successful file transfer
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, e.getMessage());
|
Log.e(TAG, e.getMessage());
|
||||||
return false; // Returned when an error was encountered during transfer
|
return false; // Returned when an error was encountered during transfer
|
||||||
@ -104,9 +105,11 @@ class ReceiverDeviceAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
if (BuildConfig.DEBUG) Log.d(TAG, "File transfer complete");
|
if (BuildConfig.DEBUG) Log.d(TAG, "File transfer complete");
|
||||||
|
|
||||||
if (allFilesReceived) {
|
if (allFilesReceived) {
|
||||||
showToast(deviceListFragment.getActivity(), R.string.file_transfer_complete, Toast.LENGTH_LONG);
|
showToast(deviceListFragment.getActivity(), R.string.file_transfer_complete,
|
||||||
|
Toast.LENGTH_LONG);
|
||||||
} else {
|
} else {
|
||||||
showToast(deviceListFragment.getActivity(), R.string.error_during_transfer, Toast.LENGTH_LONG);
|
showToast(deviceListFragment.getActivity(), R.string.error_during_transfer,
|
||||||
|
Toast.LENGTH_LONG);
|
||||||
}
|
}
|
||||||
|
|
||||||
((LocalFileTransferActivity) deviceListFragment.getActivity()).closeLocalFileTransferActivity();
|
((LocalFileTransferActivity) deviceListFragment.getActivity()).closeLocalFileTransferActivity();
|
||||||
|
@ -27,7 +27,8 @@ public class RequestEnableLocationServicesDialog extends DialogFragment {
|
|||||||
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
|
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
|
||||||
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), LocalFileTransferActivity.REQUEST_ENABLE_LOCATION_SERVICES);
|
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),
|
||||||
|
LocalFileTransferActivity.REQUEST_ENABLE_LOCATION_SERVICES);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
|
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
|
||||||
|
@ -32,7 +32,7 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
|||||||
* on the list of files for transfer in {@link TransferProgressFragment}.
|
* on the list of files for transfer in {@link TransferProgressFragment}.
|
||||||
*
|
*
|
||||||
* A new task is created by the sender for every file to be transferred
|
* A new task is created by the sender for every file to be transferred
|
||||||
* */
|
*/
|
||||||
class SenderDeviceAsyncTask extends AsyncTask<Uri, Void, Boolean> {
|
class SenderDeviceAsyncTask extends AsyncTask<Uri, Void, Boolean> {
|
||||||
|
|
||||||
private static final String TAG = "SenderDeviceAsyncTask";
|
private static final String TAG = "SenderDeviceAsyncTask";
|
||||||
@ -41,7 +41,8 @@ class SenderDeviceAsyncTask extends AsyncTask<Uri, Void, Boolean> {
|
|||||||
private TransferProgressFragment transferProgressFragment;
|
private TransferProgressFragment transferProgressFragment;
|
||||||
private int fileItemIndex;
|
private int fileItemIndex;
|
||||||
|
|
||||||
public SenderDeviceAsyncTask(DeviceListFragment deviceListFragment, TransferProgressFragment transferProgressFragment, int fileItemIndex) {
|
public SenderDeviceAsyncTask(DeviceListFragment deviceListFragment,
|
||||||
|
TransferProgressFragment transferProgressFragment, int fileItemIndex) {
|
||||||
this.deviceListFragment = deviceListFragment;
|
this.deviceListFragment = deviceListFragment;
|
||||||
this.transferProgressFragment = transferProgressFragment;
|
this.transferProgressFragment = transferProgressFragment;
|
||||||
this.fileItemIndex = fileItemIndex;
|
this.fileItemIndex = fileItemIndex;
|
||||||
@ -57,7 +58,9 @@ class SenderDeviceAsyncTask extends AsyncTask<Uri, Void, Boolean> {
|
|||||||
Uri fileUri = fileUris[0]; // Uri of file to be transferred
|
Uri fileUri = fileUris[0]; // Uri of file to be transferred
|
||||||
ContentResolver contentResolver = deviceListFragment.getActivity().getContentResolver();
|
ContentResolver contentResolver = deviceListFragment.getActivity().getContentResolver();
|
||||||
|
|
||||||
try (Socket socket = new Socket(); InputStream fileInputStream = contentResolver.openInputStream(fileUri)) { // Represents the sender device
|
try (Socket socket = new Socket();
|
||||||
|
InputStream fileInputStream = contentResolver.openInputStream(
|
||||||
|
fileUri)) { // Represents the sender device
|
||||||
|
|
||||||
if (isCancelled()) {
|
if (isCancelled()) {
|
||||||
return false;
|
return false;
|
||||||
@ -75,7 +78,6 @@ class SenderDeviceAsyncTask extends AsyncTask<Uri, Void, Boolean> {
|
|||||||
if (BuildConfig.DEBUG) Log.d(TAG, "Sender: Data written");
|
if (BuildConfig.DEBUG) Log.d(TAG, "Sender: Data written");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, e.getMessage());
|
Log.e(TAG, e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
@ -94,12 +96,14 @@ class SenderDeviceAsyncTask extends AsyncTask<Uri, Void, Boolean> {
|
|||||||
transferProgressFragment.changeStatus(fileItemIndex, SENT);
|
transferProgressFragment.changeStatus(fileItemIndex, SENT);
|
||||||
} else {
|
} else {
|
||||||
Activity activity = deviceListFragment.getActivity();
|
Activity activity = deviceListFragment.getActivity();
|
||||||
showToast(activity, activity.getString(R.string.error_sending, getFileName(deviceListFragment.getFileUriList().get(fileItemIndex))), Toast.LENGTH_SHORT);
|
showToast(activity, activity.getString(R.string.error_sending,
|
||||||
|
getFileName(deviceListFragment.getFileUriList().get(fileItemIndex))), Toast.LENGTH_SHORT);
|
||||||
transferProgressFragment.changeStatus(fileItemIndex, ERROR);
|
transferProgressFragment.changeStatus(fileItemIndex, ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deviceListFragment.allFilesSent()) {
|
if (deviceListFragment.allFilesSent()) {
|
||||||
showToast(deviceListFragment.getActivity(), R.string.file_transfer_complete, Toast.LENGTH_SHORT);
|
showToast(deviceListFragment.getActivity(), R.string.file_transfer_complete,
|
||||||
|
Toast.LENGTH_SHORT);
|
||||||
deviceListFragment.getActivity().finish();
|
deviceListFragment.getActivity().finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ import java.util.ArrayList;
|
|||||||
* Part of the local file sharing module, this fragment is used to display the progress of the
|
* Part of the local file sharing module, this fragment is used to display the progress of the
|
||||||
* file transfer. It displays a list of files along with their current status (as defined in the
|
* file transfer. It displays a list of files along with their current status (as defined in the
|
||||||
* {@link FileItem} class.
|
* {@link FileItem} class.
|
||||||
* */
|
*/
|
||||||
public class TransferProgressFragment extends Fragment {
|
public class TransferProgressFragment extends Fragment {
|
||||||
|
|
||||||
private static final String FILE_ITEMS = "file_items";
|
private static final String FILE_ITEMS = "file_items";
|
||||||
@ -53,7 +53,8 @@ public class TransferProgressFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.fragment_transfer_progress, container, false);
|
View view = inflater.inflate(R.layout.fragment_transfer_progress, container, false);
|
||||||
unbinder = ButterKnife.bind(this, view);
|
unbinder = ButterKnife.bind(this, view);
|
||||||
|
|
||||||
|
@ -18,14 +18,15 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
|||||||
*
|
*
|
||||||
* Handles the broadcasts pertaining to the wifi p2p group formed in WiFi Direct. Works along with
|
* Handles the broadcasts pertaining to the wifi p2p group formed in WiFi Direct. Works along with
|
||||||
* the wifi p2p manager in {@link LocalFileTransferActivity}.
|
* the wifi p2p manager in {@link LocalFileTransferActivity}.
|
||||||
* */
|
*/
|
||||||
public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
private WifiP2pManager manager;
|
private WifiP2pManager manager;
|
||||||
private WifiP2pManager.Channel channel;
|
private WifiP2pManager.Channel channel;
|
||||||
private LocalFileTransferActivity wifiActivity;
|
private LocalFileTransferActivity wifiActivity;
|
||||||
|
|
||||||
public WifiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, LocalFileTransferActivity activity) {
|
public WifiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
|
||||||
|
LocalFileTransferActivity activity) {
|
||||||
super();
|
super();
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
@ -48,16 +49,16 @@ public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
|||||||
wifiActivity.resetPeers();
|
wifiActivity.resetPeers();
|
||||||
}
|
}
|
||||||
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, (WifiP2pManager.PeerListListener) wifiActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_device_list));
|
manager.requestPeers(channel,
|
||||||
|
(WifiP2pManager.PeerListListener) wifiActivity.getSupportFragmentManager()
|
||||||
|
.findFragmentById(R.id.fragment_device_list));
|
||||||
}
|
}
|
||||||
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) {
|
||||||
@ -67,16 +68,20 @@ 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, (DeviceListFragment) wifiActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_device_list));
|
manager.requestConnectionInfo(channel,
|
||||||
|
(DeviceListFragment) wifiActivity.getSupportFragmentManager()
|
||||||
|
.findFragmentById(R.id.fragment_device_list));
|
||||||
} else {
|
} else {
|
||||||
// Not connected after connection change -> Disconnected
|
// Not connected after connection change -> Disconnected
|
||||||
wifiActivity.resetData();
|
wifiActivity.resetData();
|
||||||
}
|
}
|
||||||
|
|
||||||
} 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
|
// Update UI with wifi-direct details about the user device
|
||||||
DeviceListFragment deviceListFragment = (DeviceListFragment) wifiActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_device_list);
|
DeviceListFragment deviceListFragment =
|
||||||
deviceListFragment.updateUserDevice(intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
|
(DeviceListFragment) wifiActivity.getSupportFragmentManager()
|
||||||
|
.findFragmentById(R.id.fragment_device_list);
|
||||||
|
deviceListFragment.updateUserDevice(
|
||||||
|
intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,14 +23,16 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.DeviceListFr
|
|||||||
/**
|
/**
|
||||||
* Helper class, part of the local file sharing module.
|
* Helper class, part of the local file sharing module.
|
||||||
*
|
*
|
||||||
* Defines the Adapter for the list of wifi peer-device items displayed in {@link DeviceListFragment}
|
* Defines the Adapter for the list of wifi peer-device items displayed in {@link
|
||||||
* */
|
* DeviceListFragment}
|
||||||
|
*/
|
||||||
public class WifiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
|
public class WifiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private List<WifiP2pDevice> listItems;
|
private List<WifiP2pDevice> listItems;
|
||||||
|
|
||||||
public WifiPeerListAdapter(@NonNull Context context, int resource, @NonNull List<WifiP2pDevice> objects) {
|
public WifiPeerListAdapter(@NonNull Context context, int resource,
|
||||||
|
@NonNull List<WifiP2pDevice> objects) {
|
||||||
super(context, resource, objects);
|
super(context, resource, objects);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.listItems = objects;
|
this.listItems = objects;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user