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