mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Increment: LocalFileTransferActivity now supports sending multiple files
All selected files (not just the first one) are trasferred to the receiving device
This commit is contained in:
parent
72d1694150
commit
459a087728
@ -268,7 +268,10 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
if(groupInfo.isGroupOwner) fileReceiverAddress = selectedPeerDeviceInetAddress;
|
if(groupInfo.isGroupOwner) fileReceiverAddress = selectedPeerDeviceInetAddress;
|
||||||
else fileReceiverAddress = groupInfo.groupOwnerAddress;
|
else fileReceiverAddress = groupInfo.groupOwnerAddress;
|
||||||
for(int i = 0; i < 10000000; i++);
|
for(int i = 0; i < 10000000; i++);
|
||||||
new FileSenderAsyncTask(getContext(), this, groupInfo).execute(fileUriList);
|
|
||||||
|
for(int i = 0; i < totalFiles; i++) {
|
||||||
|
new FileSenderAsyncTask(getContext(), this, groupInfo).execute(fileUriList.get(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -494,7 +497,7 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
return fileReceiverAddress;
|
return fileReceiverAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class FileSenderAsyncTask extends AsyncTask<ArrayList<Uri>, Void, String> {
|
public static class FileSenderAsyncTask extends AsyncTask<Uri, Void, String> {
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
private DeviceListFragment deviceListFragment;
|
private DeviceListFragment deviceListFragment;
|
||||||
@ -507,9 +510,9 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(ArrayList<Uri>... fileUriList) {
|
protected String doInBackground(Uri... fileUris) {
|
||||||
|
|
||||||
ArrayList<Uri> uriList = fileUriList[0];
|
Uri fileUri = fileUris[0];
|
||||||
|
|
||||||
Socket socket = new Socket();
|
Socket socket = new Socket();
|
||||||
try {
|
try {
|
||||||
@ -526,7 +529,7 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
Log.d(LocalFileTransferActivity.TAG, "ContentResolver obtained");
|
Log.d(LocalFileTransferActivity.TAG, "ContentResolver obtained");
|
||||||
InputStream is = null;
|
InputStream is = null;
|
||||||
try {
|
try {
|
||||||
is = cr.openInputStream(uriList.get(0));
|
is = cr.openInputStream(fileUri);
|
||||||
Log.d(LocalFileTransferActivity.TAG, "Opened InputStream");
|
Log.d(LocalFileTransferActivity.TAG, "Opened InputStream");
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
Log.d(LocalFileTransferActivity.TAG, e.toString());
|
Log.d(LocalFileTransferActivity.TAG, e.toString());
|
||||||
@ -568,14 +571,18 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
try {
|
try {
|
||||||
ServerSocket serverSocket = new ServerSocket(FILE_TRANSFER_PORT);
|
ServerSocket serverSocket = new ServerSocket(FILE_TRANSFER_PORT);
|
||||||
Log.d(LocalFileTransferActivity.TAG, "Server: Socket opened at " + FILE_TRANSFER_PORT);
|
Log.d(LocalFileTransferActivity.TAG, "Server: Socket opened at " + FILE_TRANSFER_PORT);
|
||||||
|
|
||||||
|
int totalFileCount = deviceListFragment.getTotalFiles();
|
||||||
|
for(int currentFile = 1; currentFile <= totalFileCount; currentFile++) {
|
||||||
Socket client = serverSocket.accept();
|
Socket client = serverSocket.accept();
|
||||||
Log.d(LocalFileTransferActivity.TAG, "Server: Client connected");
|
Log.d(LocalFileTransferActivity.TAG, "Server: Client connected");
|
||||||
|
|
||||||
ArrayList<String> fileNames = deviceListFragment.getFileNames();
|
ArrayList<String> fileNames = deviceListFragment.getFileNames();
|
||||||
String incomingFileName = fileNames.get(0);
|
String incomingFileName = fileNames.get(currentFile-1);
|
||||||
|
|
||||||
// File selector, file not exists,
|
// File selector, file not exists,
|
||||||
//TODO: Change to appropriate file-path
|
//TODO: Change to appropriate file-path
|
||||||
|
// Save in the Kiwix app folder
|
||||||
final File clientNoteFileLocation = new File(Environment.getExternalStorageDirectory() + "/KiwixWifi/"+incomingFileName);
|
final File clientNoteFileLocation = new File(Environment.getExternalStorageDirectory() + "/KiwixWifi/"+incomingFileName);
|
||||||
File dirs = new File(clientNoteFileLocation.getParent());
|
File dirs = new File(clientNoteFileLocation.getParent());
|
||||||
if(!dirs.exists()) {
|
if(!dirs.exists()) {
|
||||||
@ -589,9 +596,10 @@ public class DeviceListFragment extends ListFragment implements WifiP2pManager.P
|
|||||||
Log.d(LocalFileTransferActivity.TAG, "Copying files");
|
Log.d(LocalFileTransferActivity.TAG, "Copying files");
|
||||||
InputStream inputStream = client.getInputStream();
|
InputStream inputStream = client.getInputStream();
|
||||||
copyFile(inputStream, new FileOutputStream(clientNoteFileLocation));
|
copyFile(inputStream, new FileOutputStream(clientNoteFileLocation));
|
||||||
|
}
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
return clientNoteFileLocation.getAbsolutePath();
|
|
||||||
|
return "";
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(LocalFileTransferActivity.TAG, e.getMessage());
|
Log.e(LocalFileTransferActivity.TAG, e.getMessage());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user