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:
Aditya-Sood 2019-08-02 11:47:14 +05:30
parent 314ff811fd
commit 19b316644e
3 changed files with 19 additions and 17 deletions

View File

@ -133,11 +133,11 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
actionBar.setNavigationOnClickListener(new View.OnClickListener() { actionBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
wifiDirectManager.closeLocalFileTransferActivity(); finish();
} }
}); });
wifiDirectManager.initialiseWifiDirectManager(alertDialogShower); wifiDirectManager.createWifiDirectManager(alertDialogShower);
listViewPeerDevices.setAdapter( listViewPeerDevices.setAdapter(
new WifiPeerListAdapter(this, R.layout.row_peer_device, peerDevices)); new WifiPeerListAdapter(this, R.layout.row_peer_device, peerDevices));
@ -269,7 +269,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
if (clientAddress == null) { if (clientAddress == null) {
// null is returned only in case of a failed handshake // null is returned only in case of a failed handshake
showToast(this, R.string.device_not_cooperating, Toast.LENGTH_LONG); showToast(this, R.string.device_not_cooperating, Toast.LENGTH_LONG);
wifiDirectManager.closeLocalFileTransferActivity(); finish();
return; return;
} }
@ -440,7 +440,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
Log.e(TAG, "Location permission not granted"); Log.e(TAG, "Location permission not granted");
showToast(this, R.string.permission_refused_location, Toast.LENGTH_LONG); showToast(this, R.string.permission_refused_location, Toast.LENGTH_LONG);
wifiDirectManager.closeLocalFileTransferActivity(); finish();
break; break;
} }
@ -448,7 +448,7 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
Log.e(TAG, "Storage write permission not granted"); Log.e(TAG, "Storage write permission not granted");
showToast(this, R.string.permission_refused_storage, Toast.LENGTH_LONG); showToast(this, R.string.permission_refused_storage, Toast.LENGTH_LONG);
wifiDirectManager.closeLocalFileTransferActivity(); finish();
break; break;
} }
@ -574,6 +574,6 @@ public class LocalFileTransferActivity extends AppCompatActivity implements
@Override protected void onDestroy() { @Override protected void onDestroy() {
super.onDestroy(); super.onDestroy();
cancelAsyncTasks(); wifiDirectManager.destroyWifiDirectManager();
} }
} }

View File

@ -119,6 +119,6 @@ class ReceiverDeviceAsyncTask extends AsyncTask<Void, Integer, Boolean> {
Toast.LENGTH_LONG); Toast.LENGTH_LONG);
} }
(localFileTransferActivity).wifiDirectManager.closeLocalFileTransferActivity(); localFileTransferActivity.finish();
} }
} }

View File

@ -57,7 +57,7 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
} }
/* Initialisations for using the WiFi P2P API */ /* Initialisations for using the WiFi P2P API */
public void initialiseWifiDirectManager(@NonNull AlertDialogShower alertDialogShower) { public void createWifiDirectManager(@NonNull AlertDialogShower alertDialogShower) {
this.alertDialogShower = alertDialogShower; this.alertDialogShower = alertDialogShower;
manager = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE); 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 // TODO: Shift async tasks to WDM and handle cleanup from here itself
public void closeLocalFileTransferActivity() { public void destroyWifiDirectManager() {
activity.cancelAsyncTasks(); activity.cancelAsyncTasks();
activity.isFileSender = false; if (!activity.isFileSender) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { disconnect();
channel.close(); } else {
closeChannel();
} }
disconnect();
} }
public void disconnect() { public void disconnect() {
@ -229,19 +229,21 @@ public class WifiDirectManager implements WifiP2pManager.ChannelListener, WifiP2
@Override @Override
public void onFailure(int reasonCode) { public void onFailure(int reasonCode) {
Log.d(TAG, "Disconnect failed. Reason: " + reasonCode); Log.d(TAG, "Disconnect failed. Reason: " + reasonCode);
closeActivity(); closeChannel();
} }
@Override @Override
public void onSuccess() { public void onSuccess() {
Log.d(TAG, "Disconnect successful"); Log.d(TAG, "Disconnect successful");
closeActivity(); closeChannel();
} }
}); });
} }
public void closeActivity() { public void closeChannel() {
activity.finish(); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
channel.close();
}
} }