mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 03:54:18 -04:00
Refactor WifiDirectManager
- Consolidate the wifi direct transfer cleanup code in WDM - Cleanup now performed by WDM, initiated by the destroyWifiDirectManager() call in onDestroy()
This commit is contained in:
parent
314ff811fd
commit
19b316644e
@ -133,11 +133,11 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
||||
actionBar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
wifiDirectManager.closeLocalFileTransferActivity();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
wifiDirectManager.initialiseWifiDirectManager(alertDialogShower);
|
||||
wifiDirectManager.createWifiDirectManager(alertDialogShower);
|
||||
|
||||
listViewPeerDevices.setAdapter(
|
||||
new WifiPeerListAdapter(this, R.layout.row_peer_device, peerDevices));
|
||||
@ -269,7 +269,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
||||
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();
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -440,7 +440,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
||||
Log.e(TAG, "Location permission not granted");
|
||||
|
||||
showToast(this, R.string.permission_refused_location, Toast.LENGTH_LONG);
|
||||
wifiDirectManager.closeLocalFileTransferActivity();
|
||||
finish();
|
||||
break;
|
||||
}
|
||||
|
||||
@ -448,7 +448,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
||||
Log.e(TAG, "Storage write permission not granted");
|
||||
|
||||
showToast(this, R.string.permission_refused_storage, Toast.LENGTH_LONG);
|
||||
wifiDirectManager.closeLocalFileTransferActivity();
|
||||
finish();
|
||||
break;
|
||||
}
|
||||
|
||||
@ -574,6 +574,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
|
||||
|
||||
@Override protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
cancelAsyncTasks();
|
||||
wifiDirectManager.destroyWifiDirectManager();
|
||||
}
|
||||
}
|
||||
|
@ -119,6 +119,6 @@ class ReceiverDeviceAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
Toast.LENGTH_LONG);
|
||||
}
|
||||
|
||||
(localFileTransferActivity).wifiDirectManager.closeLocalFileTransferActivity();
|
||||
localFileTransferActivity.finish();
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
||||
}
|
||||
|
||||
/* Initialisations for using the WiFi P2P API */
|
||||
public void initialiseWifiDirectManager(@NonNull AlertDialogShower alertDialogShower) {
|
||||
public void createWifiDirectManager(@NonNull AlertDialogShower alertDialogShower) {
|
||||
this.alertDialogShower = alertDialogShower;
|
||||
|
||||
manager = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
|
||||
@ -213,14 +213,14 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
||||
}
|
||||
|
||||
// TODO: Shift async tasks to WDM and handle cleanup from here itself
|
||||
public void closeLocalFileTransferActivity() {
|
||||
public void destroyWifiDirectManager() {
|
||||
activity.cancelAsyncTasks();
|
||||
|
||||
activity.isFileSender = false;
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
|
||||
channel.close();
|
||||
if (!activity.isFileSender) {
|
||||
disconnect();
|
||||
} else {
|
||||
closeChannel();
|
||||
}
|
||||
disconnect();
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
@ -229,19 +229,21 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
|
||||
@Override
|
||||
public void onFailure(int reasonCode) {
|
||||
Log.d(TAG, "Disconnect failed. Reason: " + reasonCode);
|
||||
closeActivity();
|
||||
closeChannel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Log.d(TAG, "Disconnect successful");
|
||||
closeActivity();
|
||||
closeChannel();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void closeActivity() {
|
||||
activity.finish();
|
||||
public void closeChannel() {
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user