mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 12:03:09 -04:00
Refactor PeerGroupHandshakeAsyncTask
This commit is contained in:
parent
59bb020e91
commit
ab9f9368f3
@ -60,14 +60,16 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
||||
Object object = objectInputStream.readObject();
|
||||
|
||||
// Verify that the peer trying to communicate is a kiwix app intending to transfer files
|
||||
if (object.getClass().equals(String.class) && object.equals(HANDSHAKE_MESSAGE)) {
|
||||
if(BuildConfig.DEBUG) Log.d(TAG, "Client IP address: "+ server.getInetAddress());
|
||||
|
||||
if (isKiwixHandshake(object)) {
|
||||
if(BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "Client IP address: "+ server.getInetAddress());
|
||||
}
|
||||
exchangeFileTransferMetadata(server.getOutputStream(), server.getInputStream());
|
||||
|
||||
server.close();
|
||||
|
||||
return server.getInetAddress();
|
||||
|
||||
} else { // Selected device is not accepting wifi direct connections through the kiwix app
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else if(groupInfo.groupFormed) { //&& !groupInfo.isGroupOwner
|
||||
@ -84,32 +86,43 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
||||
|
||||
return groupInfo.groupOwnerAddress;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null; // So a null is only returned in case of an exception
|
||||
private boolean isKiwixHandshake(Object object) {
|
||||
return (object.getClass().equals(String.class) && object.equals(HANDSHAKE_MESSAGE));
|
||||
}
|
||||
|
||||
private void exchangeFileTransferMetadata(OutputStream outputStream, InputStream inputStream) {
|
||||
try {
|
||||
if(deviceListFragment.isFileSender()) {
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
|
||||
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) {
|
||||
// Send total number of files which will be transferred
|
||||
objectOutputStream.writeObject(""+deviceListFragment.getTotalFilesForTransfer());
|
||||
|
||||
ArrayList<Uri> fileUriList = deviceListFragment.getFileUriList();
|
||||
for (Uri fileUri : fileUriList) { // Send the names of each of those files, in order
|
||||
for(
|
||||
Uri fileUri :fileUriList)
|
||||
|
||||
{ // Send the names of each of those files, in order
|
||||
objectOutputStream.writeObject(getFileName(fileUri));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
else { // Device is not the file sender
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
|
||||
try (ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
|
||||
// Read the number of files
|
||||
Object totalFilesObject = objectInputStream.readObject();
|
||||
|
||||
if(totalFilesObject.getClass().equals(String.class)) {
|
||||
if (totalFilesObject.getClass().equals(String.class)) {
|
||||
int total = Integer.parseInt((String) totalFilesObject);
|
||||
deviceListFragment.setTotalFilesForTransfer(total);
|
||||
|
||||
@ -117,19 +130,18 @@ class PeerGroupHandshakeAsyncTask extends AsyncTask<Void, Void, InetAddress> {
|
||||
for (int i = 0; i < total; i++) { // Read names of each of those files, in order
|
||||
Object fileNameObject = objectInputStream.readObject();
|
||||
|
||||
if(fileNameObject.getClass().equals(String.class)) {
|
||||
if (fileNameObject.getClass().equals(String.class)) {
|
||||
fileItems.add(new FileItem((String) fileNameObject, TO_BE_SENT));
|
||||
}
|
||||
}
|
||||
|
||||
deviceListFragment.setFileItems(fileItems);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(InetAddress inetAddress) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user