mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-21 11:28:56 -04:00
Refactor: Code cleanup + Butterknife
- Code cleanup post shifting of TransferProgressFragment functionality - Use Butterknife for peer-devices list's onItemClickListener()
This commit is contained in:
parent
5ef495d792
commit
17d424803b
@ -28,13 +28,13 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnItemClick;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@ -63,8 +63,10 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.FileItem.Fil
|
||||
* The module is used for transferring ZIM files from one device to another, from within the
|
||||
* app. Two devices are connected to each other using WiFi Direct, followed by file transfer.
|
||||
*
|
||||
* The module uses this activity along with {DeviceListFragment} to manage connection
|
||||
* and file transfer between the devices.
|
||||
* File transfer involves two phases:
|
||||
* 1) Handshake with the selected peer device, using {@link PeerGroupHandshakeAsyncTask}
|
||||
* 2) After handshake, starting the files transfer using {@link SenderDeviceAsyncTask} on the sender
|
||||
* device and {@link ReceiverDeviceAsyncTask} files receiving device
|
||||
*/
|
||||
public class LocalFileTransferActivity extends AppCompatActivity implements WifiP2pManager.PeerListListener, WifiP2pManager.ConnectionInfoListener {
|
||||
|
||||
@ -88,19 +90,18 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
@BindView(R.id.text_view_empty_peer_list) TextView textViewPeerDevices;
|
||||
@BindView(R.id.recycler_view_transfer_files) RecyclerView filesRecyclerView;
|
||||
|
||||
private ArrayList<Uri> fileUriArrayList;
|
||||
// For sender device, stores Uris of files to be transferred
|
||||
public Boolean fileSendingDevice = false;// Whether the device is the file sender or not
|
||||
|
||||
public WifiDirectManager wifiDirectManager;
|
||||
private ArrayList<Uri> fileUriArrayList; // For sender device, stores uris of the files
|
||||
public @NonNull Boolean fileSendingDevice = false;// Whether the device is the file sender or not
|
||||
|
||||
public @NonNull WifiDirectManager wifiDirectManager = new WifiDirectManager(this);
|
||||
|
||||
private int totalFilesForTransfer = -1;
|
||||
private int filesSent = 0; // Count of number of files transferred until now
|
||||
private ArrayList<FileItem> filesToSend = new ArrayList<>();
|
||||
|
||||
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 P2P group formed between the two devices
|
||||
private List<WifiP2pDevice> peerDevices = new ArrayList<WifiP2pDevice>();
|
||||
|
||||
private WifiP2pDevice selectedPeerDevice = null;
|
||||
@ -147,9 +148,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/* Initialisations for using the WiFi P2P API */
|
||||
this.wifiDirectManager = new WifiDirectManager(this);
|
||||
wifiDirectManager.initialiseWifiDirectManager();
|
||||
|
||||
listViewPeerDevices.setAdapter(new WifiPeerListAdapter(this, R.layout.row_peer_device, peerDevices));
|
||||
@ -161,29 +159,28 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
filesToSend.add(new FileItem(getFileName(fileUriArrayList.get(i)), TO_BE_SENT));
|
||||
}
|
||||
|
||||
displayTransferProgressFragment();
|
||||
|
||||
listViewPeerDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
/* Connection can only be initiated by user of the sender device, & only when transfer has not been started */
|
||||
if (!fileSendingDevice || fileTransferStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
selectedPeerDevice = (WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
||||
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(selectedPeerDevice),
|
||||
new Function0<Unit>() {
|
||||
@Override public Unit invoke() {
|
||||
(wifiDirectManager).connect(selectedPeerDevice);
|
||||
showToast(LocalFileTransferActivity.this, R.string.performing_handshake, Toast.LENGTH_LONG);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
displayFileTransferProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@OnItemClick(R.id.list_peer_devices)
|
||||
void onItemClick(int position) {
|
||||
/* Connection can only be initiated by user of the sender device, & only when transfer has not been started */
|
||||
if (!fileSendingDevice || fileTransferStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
selectedPeerDevice = (WifiP2pDevice) listViewPeerDevices.getAdapter().getItem(position);
|
||||
alertDialogShower.show(new KiwixDialog.FileTransferConfirmation(selectedPeerDevice),
|
||||
new Function0<Unit>() {
|
||||
@Override public Unit invoke() {
|
||||
(wifiDirectManager).connect(selectedPeerDevice);
|
||||
showToast(LocalFileTransferActivity.this, R.string.performing_handshake, Toast.LENGTH_LONG);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.wifi_file_share_items, menu);
|
||||
@ -211,8 +208,8 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
}
|
||||
|
||||
onInitiateDiscovery();
|
||||
|
||||
wifiDirectManager.discoverPeerDevices();
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return super.onOptionsItemSelected(item);
|
||||
@ -240,6 +237,20 @@ 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;
|
||||
|
||||
if (userDevice != null) {
|
||||
deviceName.setText(userDevice.deviceName);
|
||||
Log.d(TAG, getDeviceStatus(userDevice.status));
|
||||
}
|
||||
}
|
||||
|
||||
public void clearPeers() {
|
||||
peerDevices.clear();
|
||||
((WifiPeerListAdapter) listViewPeerDevices.getAdapter()).notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public String getErrorMessage(int reason) {
|
||||
switch (reason) {
|
||||
case WifiP2pManager.ERROR:
|
||||
@ -254,23 +265,170 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
}
|
||||
}
|
||||
|
||||
public void resetPeers() {
|
||||
clearPeers();
|
||||
public static String getDeviceStatus(int status) {
|
||||
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
||||
switch (status) {
|
||||
case WifiP2pDevice.AVAILABLE:
|
||||
return "Available";
|
||||
case WifiP2pDevice.INVITED:
|
||||
return "Invited";
|
||||
case WifiP2pDevice.CONNECTED:
|
||||
return "Connected";
|
||||
case WifiP2pDevice.FAILED:
|
||||
return "Failed";
|
||||
case WifiP2pDevice.UNAVAILABLE:
|
||||
return "Unavailable";
|
||||
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
public void resetData() {
|
||||
clearPeers();
|
||||
public static String getFileName(Uri fileUri) {
|
||||
String fileUriString = fileUri.toString();
|
||||
// Returns text after location of last slash in the file path
|
||||
return fileUriString.substring(fileUriString.lastIndexOf('/') + 1);
|
||||
}
|
||||
|
||||
static void showToast(Context context, int stringResource, int duration) {
|
||||
showToast(context, context.getString(stringResource), duration);
|
||||
private void displayFileTransferProgress() {
|
||||
fileListAdapter = new FileListAdapter(filesToSend);
|
||||
filesRecyclerView.setAdapter(fileListAdapter);
|
||||
filesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
}
|
||||
|
||||
static void showToast(Context context, String text, int duration) {
|
||||
Toast.makeText(context, text, duration).show();
|
||||
public void onInitiateDiscovery() { // Setup UI for searching peers
|
||||
searchingPeersProgressBar.setVisibility(View.VISIBLE);
|
||||
listViewPeerDevices.setVisibility(View.INVISIBLE);
|
||||
textViewPeerDevices.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
/* Helper methods used in the activity */
|
||||
public void setClientAddress(InetAddress clientAddress) {
|
||||
if (clientAddress == null) {
|
||||
// null is returned only in case of a failed handshake
|
||||
showToast(this, R.string.device_not_cooperating, Toast.LENGTH_LONG);
|
||||
wifiDirectManager.closeLocalFileTransferActivity();
|
||||
return;
|
||||
}
|
||||
|
||||
// If control reaches here, means handshake was successful
|
||||
selectedPeerDeviceInetAddress = clientAddress;
|
||||
startFileTransfer();
|
||||
}
|
||||
|
||||
private void startFileTransfer() {
|
||||
fileTransferStarted = true;
|
||||
|
||||
if (groupInfo.groupFormed && !fileSendingDevice) {
|
||||
displayFileTransferProgress();
|
||||
|
||||
receiverDeviceAsyncTask = new ReceiverDeviceAsyncTask(this);
|
||||
receiverDeviceAsyncTask.execute();
|
||||
} else if (groupInfo.groupFormed) {
|
||||
{
|
||||
Log.d(LocalFileTransferActivity.TAG, "Starting file transfer");
|
||||
|
||||
fileReceiverDeviceAddress =
|
||||
(groupInfo.isGroupOwner) ? selectedPeerDeviceInetAddress : groupInfo.groupOwnerAddress;
|
||||
|
||||
// Hack for allowing slower receiver devices to setup server before sender device requests to connect
|
||||
showToast(this, R.string.preparing_files, Toast.LENGTH_LONG);
|
||||
for (int i = 0; i < 20000000; i++) ;
|
||||
|
||||
senderDeviceAsyncTaskArray = new SenderDeviceAsyncTask[totalFilesForTransfer];
|
||||
for (int i = 0; i < totalFilesForTransfer; i++) {
|
||||
senderDeviceAsyncTaskArray[i] = new SenderDeviceAsyncTask(this, i);
|
||||
senderDeviceAsyncTaskArray[i].execute(fileUriArrayList.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public WifiP2pDevice getUserDevice() {
|
||||
return userDevice;
|
||||
}
|
||||
|
||||
public int getTotalFilesForTransfer() {
|
||||
return totalFilesForTransfer;
|
||||
}
|
||||
|
||||
public void setTotalFilesForTransfer(int totalFilesForTransfer) {
|
||||
this.totalFilesForTransfer = totalFilesForTransfer;
|
||||
}
|
||||
|
||||
public ArrayList<FileItem> getFileItems() {
|
||||
return filesToSend;
|
||||
}
|
||||
|
||||
public void setFileItems(ArrayList<FileItem> fileItems) {
|
||||
this.filesToSend = fileItems;
|
||||
}
|
||||
|
||||
public void incrementTotalFilesSent() {
|
||||
this.filesSent++;
|
||||
}
|
||||
|
||||
public boolean allFilesSent() {
|
||||
return (filesSent == totalFilesForTransfer);
|
||||
}
|
||||
|
||||
public String getZimStorageRootPath() {
|
||||
return (sharedPreferenceUtil.getPrefStorage() + "/Kiwix/");
|
||||
}
|
||||
|
||||
public InetAddress getFileReceiverDeviceAddress() {
|
||||
return fileReceiverDeviceAddress;
|
||||
}
|
||||
|
||||
public static void copyToOutputStream(InputStream inputStream, OutputStream outputStream)
|
||||
throws IOException {
|
||||
byte[] bufferForBytes = new byte[1024];
|
||||
int bytesRead;
|
||||
|
||||
Log.d(TAG, "Copying to OutputStream...");
|
||||
while ((bytesRead = inputStream.read(bufferForBytes)) != -1) {
|
||||
outputStream.write(bufferForBytes, 0, bytesRead);
|
||||
}
|
||||
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
Log.d(LocalFileTransferActivity.TAG, "Both streams closed");
|
||||
}
|
||||
|
||||
public void changeStatus(int itemIndex, @FileItem.FileStatus int status) {
|
||||
filesToSend.get(itemIndex).setFileStatus(status);
|
||||
fileListAdapter.notifyItemChanged(itemIndex);
|
||||
}
|
||||
|
||||
/* From WifiP2pManager.PeerListListener callback-interface */
|
||||
@Override
|
||||
public void onPeersAvailable(WifiP2pDeviceList peers) {
|
||||
searchingPeersProgressBar.setVisibility(View.GONE);
|
||||
listViewPeerDevices.setVisibility(View.VISIBLE);
|
||||
|
||||
peerDevices.clear();
|
||||
peerDevices.addAll(peers.getDeviceList());
|
||||
((WifiPeerListAdapter) listViewPeerDevices.getAdapter()).notifyDataSetChanged();
|
||||
|
||||
if (peerDevices.size() == 0) {
|
||||
Log.d(LocalFileTransferActivity.TAG, "No devices found");
|
||||
}
|
||||
}
|
||||
|
||||
/* From WifiP2pManager.ConnectionInfoListener callback-interface */
|
||||
@Override
|
||||
public void onConnectionInfoAvailable(WifiP2pInfo info) {
|
||||
/* Devices have successfully connected, and 'info' holds information about the wifi p2p group formed */
|
||||
groupInfo = info;
|
||||
// Start handshake between the devices
|
||||
if(BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "Starting handshake");
|
||||
}
|
||||
peerGroupHandshakeAsyncTask = new PeerGroupHandshakeAsyncTask(this, groupInfo);
|
||||
peerGroupHandshakeAsyncTask.execute();
|
||||
}
|
||||
|
||||
/* Helper methods used for checking permissions and states of services */
|
||||
private boolean checkCoarseLocationAccessPermission() { // Required by Android to detect wifi-p2p peers
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
|
||||
@ -421,6 +579,29 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
}
|
||||
}
|
||||
|
||||
/* Miscellaneous helper methods*/
|
||||
static void showToast(Context context, int stringResource, int duration) {
|
||||
showToast(context, context.getString(stringResource), duration);
|
||||
}
|
||||
|
||||
static void showToast(Context context, String text, int duration) {
|
||||
Toast.makeText(context, text, duration).show();
|
||||
}
|
||||
|
||||
void cancelAsyncTasks() {
|
||||
if (peerGroupHandshakeAsyncTask != null) {
|
||||
peerGroupHandshakeAsyncTask.cancel(true);
|
||||
}
|
||||
|
||||
if (senderDeviceAsyncTaskArray != null) {
|
||||
for (SenderDeviceAsyncTask task : senderDeviceAsyncTaskArray) {
|
||||
task.cancel(true);
|
||||
}
|
||||
} else if (receiverDeviceAsyncTask != null) {
|
||||
receiverDeviceAsyncTask.cancel(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
@ -443,223 +624,4 @@ public class LocalFileTransferActivity extends AppCompatActivity implements Wifi
|
||||
super.onBackPressed();
|
||||
wifiDirectManager.closeLocalFileTransferActivity();
|
||||
}
|
||||
|
||||
public static String getFileName(Uri fileUri) {
|
||||
String fileUriString = fileUri.toString();
|
||||
// Returns text after location of last slash in the file path
|
||||
return fileUriString.substring(fileUriString.lastIndexOf('/') + 1);
|
||||
}
|
||||
|
||||
private void displayTransferProgressFragment() {
|
||||
/*transferProgressFragment = TransferProgressFragment.newInstance(filesToSend);
|
||||
FragmentManager fragmentManager = this.getSupportFragmentManager();
|
||||
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||
fragmentTransaction.add(R.id.container_fragment_transfer_progress, transferProgressFragment)
|
||||
.commit();*/
|
||||
fileListAdapter = new FileListAdapter(filesToSend);
|
||||
filesRecyclerView.setAdapter(fileListAdapter);
|
||||
filesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
}
|
||||
|
||||
public void updateUserDevice(WifiP2pDevice device) { // Update UI with user device's details
|
||||
this.userDevice = device;
|
||||
|
||||
if (userDevice != null) {
|
||||
deviceName.setText(userDevice.deviceName);
|
||||
Log.d(TAG, getDeviceStatus(userDevice.status));
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDeviceStatus(int status) {
|
||||
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Peer Status: " + status);
|
||||
switch (status) {
|
||||
case WifiP2pDevice.AVAILABLE:
|
||||
return "Available";
|
||||
case WifiP2pDevice.INVITED:
|
||||
return "Invited";
|
||||
case WifiP2pDevice.CONNECTED:
|
||||
return "Connected";
|
||||
case WifiP2pDevice.FAILED:
|
||||
return "Failed";
|
||||
case WifiP2pDevice.UNAVAILABLE:
|
||||
return "Unavailable";
|
||||
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/* From WifiP2pManager.PeerListListener callback-interface */
|
||||
@Override
|
||||
public void onPeersAvailable(WifiP2pDeviceList peers) {
|
||||
searchingPeersProgressBar.setVisibility(View.GONE);
|
||||
listViewPeerDevices.setVisibility(View.VISIBLE);
|
||||
|
||||
peerDevices.clear();
|
||||
peerDevices.addAll(peers.getDeviceList());
|
||||
((WifiPeerListAdapter) listViewPeerDevices.getAdapter()).notifyDataSetChanged();
|
||||
|
||||
if (peerDevices.size() == 0) {
|
||||
Log.d(LocalFileTransferActivity.TAG, "No devices found");
|
||||
}
|
||||
}
|
||||
|
||||
public void clearPeers() {
|
||||
peerDevices.clear();
|
||||
((WifiPeerListAdapter) listViewPeerDevices.getAdapter()).notifyDataSetChanged();
|
||||
}
|
||||
|
||||
void cancelAsyncTasks() {
|
||||
if (peerGroupHandshakeAsyncTask != null) {
|
||||
peerGroupHandshakeAsyncTask.cancel(true);
|
||||
}
|
||||
|
||||
if (senderDeviceAsyncTaskArray != null) {
|
||||
for (SenderDeviceAsyncTask task : senderDeviceAsyncTaskArray) {
|
||||
task.cancel(true);
|
||||
}
|
||||
} else if (receiverDeviceAsyncTask != null) {
|
||||
receiverDeviceAsyncTask.cancel(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void onInitiateDiscovery() { // Setup UI for searching peers
|
||||
searchingPeersProgressBar.setVisibility(View.VISIBLE);
|
||||
listViewPeerDevices.setVisibility(View.INVISIBLE);
|
||||
textViewPeerDevices.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
/* From WifiP2pManager.ConnectionInfoListener callback-interface */
|
||||
@Override
|
||||
public void onConnectionInfoAvailable(WifiP2pInfo info) {
|
||||
/* Devices have successfully connected, and 'info' holds information about the wifi p2p group formed */
|
||||
groupInfo = info;
|
||||
// Start handshake between the devices
|
||||
Log.d(TAG, "Starting handshake");
|
||||
peerGroupHandshakeAsyncTask = new PeerGroupHandshakeAsyncTask(this, groupInfo);
|
||||
peerGroupHandshakeAsyncTask.execute();
|
||||
}
|
||||
|
||||
public void setClientAddress(InetAddress clientAddress) {
|
||||
if (clientAddress == null) {
|
||||
// null is returned only in case of a failed handshake
|
||||
showToast(this, R.string.device_not_cooperating, Toast.LENGTH_LONG);
|
||||
wifiDirectManager.closeLocalFileTransferActivity();
|
||||
return;
|
||||
}
|
||||
|
||||
// If control reaches here, means handshake was successful
|
||||
selectedPeerDeviceInetAddress = clientAddress;
|
||||
startFileTransfer();
|
||||
}
|
||||
|
||||
private void startFileTransfer() {
|
||||
fileTransferStarted = true;
|
||||
|
||||
if (groupInfo.groupFormed && !fileSendingDevice) {
|
||||
displayTransferProgressFragment();
|
||||
|
||||
receiverDeviceAsyncTask = new ReceiverDeviceAsyncTask(this);
|
||||
receiverDeviceAsyncTask.execute();
|
||||
} else if (groupInfo.groupFormed) {
|
||||
{
|
||||
Log.d(LocalFileTransferActivity.TAG, "Starting file transfer");
|
||||
|
||||
fileReceiverDeviceAddress =
|
||||
(groupInfo.isGroupOwner) ? selectedPeerDeviceInetAddress : groupInfo.groupOwnerAddress;
|
||||
|
||||
// Hack for allowing slower receiver devices to setup server before sender device requests to connect
|
||||
showToast(this, R.string.preparing_files, Toast.LENGTH_LONG);
|
||||
for (int i = 0; i < 20000000; i++) ;
|
||||
|
||||
senderDeviceAsyncTaskArray = new SenderDeviceAsyncTask[totalFilesForTransfer];
|
||||
for (int i = 0; i < totalFilesForTransfer; i++) {
|
||||
senderDeviceAsyncTaskArray[i] = new SenderDeviceAsyncTask(this, i);
|
||||
senderDeviceAsyncTaskArray[i].execute(fileUriArrayList.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Helper methods */
|
||||
|
||||
public WifiP2pDevice getUserDevice() {
|
||||
return userDevice;
|
||||
}
|
||||
|
||||
public int getTotalFilesForTransfer() {
|
||||
return totalFilesForTransfer;
|
||||
}
|
||||
|
||||
public void setTotalFilesForTransfer(int totalFilesForTransfer) {
|
||||
this.totalFilesForTransfer = totalFilesForTransfer;
|
||||
}
|
||||
|
||||
public ArrayList<FileItem> getFileItems() {
|
||||
return filesToSend;
|
||||
}
|
||||
|
||||
public void setFileItems(ArrayList<FileItem> fileItems) {
|
||||
this.filesToSend = fileItems;
|
||||
}
|
||||
|
||||
/*public ArrayList<Uri> getFileUriList() {
|
||||
return fileUriArrayList;
|
||||
}*/
|
||||
|
||||
public void incrementTotalFilesSent() {
|
||||
this.filesSent++;
|
||||
}
|
||||
|
||||
public boolean allFilesSent() {
|
||||
return (filesSent == totalFilesForTransfer);
|
||||
}
|
||||
|
||||
public String getZimStorageRootPath() {
|
||||
return (sharedPreferenceUtil.getPrefStorage() + "/Kiwix/");
|
||||
}
|
||||
|
||||
public InetAddress getFileReceiverDeviceAddress() {
|
||||
return fileReceiverDeviceAddress;
|
||||
}
|
||||
|
||||
public static void copyToOutputStream(InputStream inputStream, OutputStream outputStream)
|
||||
throws IOException {
|
||||
byte[] bufferForBytes = new byte[1024];
|
||||
int bytesRead;
|
||||
|
||||
Log.d(TAG, "Copying to OutputStream...");
|
||||
while ((bytesRead = inputStream.read(bufferForBytes)) != -1) {
|
||||
outputStream.write(bufferForBytes, 0, bytesRead);
|
||||
}
|
||||
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
Log.d(LocalFileTransferActivity.TAG, "Both streams closed");
|
||||
}
|
||||
|
||||
public void changeStatus(int itemIndex, @FileItem.FileStatus int status) {
|
||||
filesToSend.get(itemIndex).setFileStatus(status);
|
||||
fileListAdapter.notifyItemChanged(itemIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Part of the local file sharing module, this fragment is responsible for displaying details of
|
||||
* the user device and the list of available peer devices, and once the user taps on a particular
|
||||
* peer device item, connecting to that device and initiating file transfer.
|
||||
*
|
||||
* File transfer involves two phases:
|
||||
* 1) Handshake with the selected peer device, using {@link PeerGroupHandshakeAsyncTask}
|
||||
* 2) After handshake, starting the files transfer using {@link SenderDeviceAsyncTask} on the sender
|
||||
* device and {@link ReceiverDeviceAsyncTask} files receiving device
|
||||
*
|
||||
* The starting point for the module is {@link LocalFileTransferActivity}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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
|
||||
* {@link FileItem} class.
|
||||
*/
|
||||
|
@ -22,7 +22,7 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
||||
import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTransferActivity.getFileName;
|
||||
|
||||
/**
|
||||
* Helper class for the local file sharing module, used in {DeviceListFragment}.
|
||||
* Helper class for the local file sharing module.
|
||||
*
|
||||
* Once two peer devices are connected through wifi direct, this task is executed to perform a
|
||||
* handshake between the two devices. The purpose of the handshake is to allow the file sending
|
||||
@ -49,14 +49,15 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
||||
|
||||
@Override
|
||||
protected InetAddress doInBackground(Void... voids) {
|
||||
Log.d(TAG, "Handshake in process");
|
||||
if(BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "Handshake in progress");
|
||||
}
|
||||
|
||||
if (groupInfo.groupFormed && groupInfo.isGroupOwner && !isCancelled()) {
|
||||
try (ServerSocket serverSocket = new ServerSocket(PEER_HANDSHAKE_PORT)) {
|
||||
serverSocket.setReuseAddress(true);
|
||||
Socket server = serverSocket.accept();
|
||||
|
||||
Log.d(TAG, "Group owner accepted connection");
|
||||
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(server.getInputStream());
|
||||
Object object = objectInputStream.readObject();
|
||||
|
||||
@ -81,8 +82,6 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
||||
client.connect((new InetSocketAddress(groupInfo.groupOwnerAddress.getHostAddress(),
|
||||
PEER_HANDSHAKE_PORT)), 15000);
|
||||
|
||||
Log.d(TAG, "Group owner accepted connection");
|
||||
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(client.getOutputStream());
|
||||
objectOutputStream.writeObject(
|
||||
HANDSHAKE_MESSAGE); // Send message for the peer device to verify
|
||||
@ -151,6 +150,5 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
||||
@Override
|
||||
protected void onPostExecute(InetAddress inetAddress) {
|
||||
localFileTransferActivity.setClientAddress(inetAddress);
|
||||
Log.d(TAG, "Handshake over");
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
||||
import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTransferActivity.showToast;
|
||||
|
||||
/**
|
||||
* Helper class for the local file sharing module, used in {DeviceListFragment}.
|
||||
* Helper class for the local file sharing module.
|
||||
*
|
||||
* Once the handshake has successfully taken place, this async-task is used to receive files from
|
||||
* the sender device on the FILE_TRANSFER_PORT port. No. of files to be received (and their names)
|
||||
|
@ -23,14 +23,14 @@ import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTra
|
||||
import static org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTransferActivity.showToast;
|
||||
|
||||
/**
|
||||
* Helper class for the local file sharing module, used in {DeviceListFragment}.
|
||||
* Helper class for the local file sharing module.
|
||||
*
|
||||
* Once the handshake between the two connected devices has taked place, this async-task is used
|
||||
* on the sender device to transfer the file to the receiver device at the FILE_TRANSFER_PORT port.
|
||||
*
|
||||
* It takes in the uri of a single file, and copies all the bytes from input stream of the file to
|
||||
* the output stream of the receiver device. Also changes the status of the corresponding FileItem
|
||||
* on the list of files for transfer in {TransferProgressFragment}.
|
||||
* on the list of files for transfer.
|
||||
*
|
||||
* A new task is created by the sender for every file to be transferred
|
||||
*/
|
||||
|
@ -1,75 +0,0 @@
|
||||
package org.kiwix.kiwixmobile.zim_manager.local_file_transfer;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.Unbinder;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class TransferProgressFragment extends Fragment {
|
||||
|
||||
private static final String FILE_ITEMS = "file_items";
|
||||
|
||||
/*@BindView(R.id.recycler_view_transfer_files) RecyclerView filesRecyclerView;*/
|
||||
private Unbinder unbinder;
|
||||
|
||||
/*private ArrayList<FileItem> fileItems;
|
||||
private FileListAdapter fileListAdapter;*/
|
||||
|
||||
public TransferProgressFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
public static TransferProgressFragment newInstance(ArrayList<FileItem> fileItems) {
|
||||
TransferProgressFragment fragment = new TransferProgressFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelableArrayList(FILE_ITEMS, fileItems);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Bundle bundle = this.getArguments();
|
||||
/*this.fileItems = bundle.getParcelableArrayList(FILE_ITEMS);*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_transfer_progress, container, false);
|
||||
unbinder = ButterKnife.bind(this, view);
|
||||
|
||||
/*fileListAdapter = new FileListAdapter(fileItems);
|
||||
filesRecyclerView.setAdapter(fileListAdapter);
|
||||
|
||||
filesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));*/
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
if (unbinder != null) unbinder.unbind();
|
||||
}
|
||||
|
||||
/*public void changeStatus(int itemIndex, @FileItem.FileStatus int status) {
|
||||
fileItems.get(itemIndex).setFileStatus(status);
|
||||
fileListAdapter.notifyItemChanged(itemIndex);
|
||||
}*/
|
||||
}
|
@ -46,7 +46,7 @@ public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
||||
} else {
|
||||
wifiActivity.setWifiP2pEnabled(false);
|
||||
showToast(wifiActivity, R.string.discovery_needs_wifi, Toast.LENGTH_SHORT);
|
||||
wifiActivity.resetPeers();
|
||||
wifiActivity.clearPeers();
|
||||
}
|
||||
Log.d(LocalFileTransferActivity.TAG, "WiFi P2P state changed - " + wifiP2pState);
|
||||
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
|
||||
@ -69,7 +69,7 @@ public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
|
||||
manager.requestConnectionInfo(channel, wifiActivity);
|
||||
} else {
|
||||
// Not connected after connection change -> Disconnected
|
||||
wifiActivity.resetData();
|
||||
wifiActivity.clearPeers();
|
||||
}
|
||||
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
|
||||
// Update UI with wifi-direct details about the user device
|
||||
|
@ -96,7 +96,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener {
|
||||
// Upon disconnection, retry one more time
|
||||
if (manager != null && !retryChannel) {
|
||||
Log.d(TAG, "Channel lost, trying again");
|
||||
activity.resetData();
|
||||
activity.clearPeers();
|
||||
retryChannel = true;
|
||||
manager.initialize(activity, getMainLooper(), this);
|
||||
} else {
|
||||
|
@ -1,45 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/transparent"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<View
|
||||
android:id="@+id/view_file_list_boundary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="@android:color/black"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/text_view_files_for_transfer"
|
||||
android:layout_marginLeft="5dp"
|
||||
android:layout_marginRight="5dp"
|
||||
android:layout_marginTop="10dp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_view_files_for_transfer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/files_for_transfer"
|
||||
android:textSize="16sp"
|
||||
app:fontFamily="monospace"
|
||||
android:gravity="center"
|
||||
android:paddingTop="10dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/view_file_list_boundary"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@id/recycler_view_transfer_files"/>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycler_view_transfer_files"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/text_view_files_for_transfer"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user