mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-16 19:05:27 -04:00
Merge pull request #2499 from gouri-panda/fix_2435_KiwixMainActivity_is_leaking_caused_by_wifip2pmanager
Feature/gouri-panda/2435_KiwixMainActivity_is_leaking_by_WifiP2pManager
This commit is contained in:
commit
2bbf2d7b4f
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.localFileTransfer
|
package org.kiwix.kiwixmobile.localFileTransfer
|
||||||
|
|
||||||
import android.app.Activity
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
@ -45,7 +45,7 @@ import java.net.Socket
|
|||||||
private const val TIME_OUT = 15000
|
private const val TIME_OUT = 15000
|
||||||
|
|
||||||
internal class SenderDevice(
|
internal class SenderDevice(
|
||||||
private val activity: Activity,
|
private val context: Context,
|
||||||
private val wifiDirectManager: WifiDirectManager,
|
private val wifiDirectManager: WifiDirectManager,
|
||||||
private val fileReceiverDeviceAddress: InetAddress
|
private val fileReceiverDeviceAddress: InetAddress
|
||||||
) {
|
) {
|
||||||
@ -59,7 +59,7 @@ internal class SenderDevice(
|
|||||||
.forEachIndexed { fileIndex, fileItem ->
|
.forEachIndexed { fileIndex, fileItem ->
|
||||||
try {
|
try {
|
||||||
Socket().use { socket ->
|
Socket().use { socket ->
|
||||||
activity.contentResolver.openInputStream(fileItem?.fileUri!!).use { fileInputStream ->
|
context.contentResolver.openInputStream(fileItem?.fileUri!!).use { fileInputStream ->
|
||||||
socket.bind(null)
|
socket.bind(null)
|
||||||
socket.connect(
|
socket.connect(
|
||||||
InetSocketAddress(hostAddress, WifiDirectManager.FILE_TRANSFER_PORT),
|
InetSocketAddress(hostAddress, WifiDirectManager.FILE_TRANSFER_PORT),
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.localFileTransfer
|
package org.kiwix.kiwixmobile.localFileTransfer
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
@ -59,7 +58,7 @@ import javax.inject.Inject
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("MissingPermission", "ProtectedMemberInFinalClass")
|
@SuppressWarnings("MissingPermission", "ProtectedMemberInFinalClass")
|
||||||
class WifiDirectManager @Inject constructor(
|
class WifiDirectManager @Inject constructor(
|
||||||
private val activity: Activity,
|
private val context: Context,
|
||||||
private val sharedPreferenceUtil: SharedPreferenceUtil,
|
private val sharedPreferenceUtil: SharedPreferenceUtil,
|
||||||
private val alertDialogShower: AlertDialogShower
|
private val alertDialogShower: AlertDialogShower
|
||||||
) : ChannelListener, PeerListListener, ConnectionInfoListener, P2pEventListener {
|
) : ChannelListener, PeerListListener, ConnectionInfoListener, P2pEventListener {
|
||||||
@ -94,8 +93,8 @@ class WifiDirectManager @Inject constructor(
|
|||||||
fun startWifiDirectManager(filesForTransfer: List<FileItem>) {
|
fun startWifiDirectManager(filesForTransfer: List<FileItem>) {
|
||||||
this.filesForTransfer = filesForTransfer
|
this.filesForTransfer = filesForTransfer
|
||||||
isFileSender = filesForTransfer.isNotEmpty()
|
isFileSender = filesForTransfer.isNotEmpty()
|
||||||
manager = activity.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
|
manager = context.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
|
||||||
channel = manager.initialize(activity, Looper.getMainLooper(), null)
|
channel = manager.initialize(context, Looper.getMainLooper(), null)
|
||||||
registerWifiDirectBroadcastReceiver()
|
registerWifiDirectBroadcastReceiver()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,20 +109,20 @@ class WifiDirectManager @Inject constructor(
|
|||||||
addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
|
addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
|
||||||
addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
|
addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
|
||||||
}
|
}
|
||||||
activity.registerReceiver(receiver, intentFilter)
|
context.registerReceiver(receiver, intentFilter)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun unregisterWifiDirectBroadcastReceiver() = activity.unregisterReceiver(receiver)
|
private fun unregisterWifiDirectBroadcastReceiver() = context.unregisterReceiver(receiver)
|
||||||
|
|
||||||
fun discoverPeerDevices() {
|
fun discoverPeerDevices() {
|
||||||
manager.discoverPeers(channel, object : ActionListener {
|
manager.discoverPeers(channel, object : ActionListener {
|
||||||
override fun onSuccess() {
|
override fun onSuccess() {
|
||||||
activity.toast(R.string.discovery_initiated, Toast.LENGTH_SHORT)
|
context.toast(R.string.discovery_initiated, Toast.LENGTH_SHORT)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFailure(reason: Int) {
|
override fun onFailure(reason: Int) {
|
||||||
Log.d(TAG, "${activity.getString(R.string.discovery_failed)}: ${getErrorMessage(reason)}")
|
Log.d(TAG, "${context.getString(R.string.discovery_failed)}: ${getErrorMessage(reason)}")
|
||||||
activity.toast(R.string.discovery_failed, Toast.LENGTH_SHORT)
|
context.toast(R.string.discovery_failed, Toast.LENGTH_SHORT)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -132,7 +131,7 @@ class WifiDirectManager @Inject constructor(
|
|||||||
override fun onWifiP2pStateChanged(isEnabled: Boolean) {
|
override fun onWifiP2pStateChanged(isEnabled: Boolean) {
|
||||||
isWifiP2pEnabled = isEnabled
|
isWifiP2pEnabled = isEnabled
|
||||||
if (!isWifiP2pEnabled) {
|
if (!isWifiP2pEnabled) {
|
||||||
activity.toast(R.string.discovery_needs_wifi, Toast.LENGTH_SHORT)
|
context.toast(R.string.discovery_needs_wifi, Toast.LENGTH_SHORT)
|
||||||
callbacks?.onConnectionToPeersLost()
|
callbacks?.onConnectionToPeersLost()
|
||||||
}
|
}
|
||||||
Log.d(TAG, "WiFi P2P state changed - $isWifiP2pEnabled")
|
Log.d(TAG, "WiFi P2P state changed - $isWifiP2pEnabled")
|
||||||
@ -167,9 +166,9 @@ class WifiDirectManager @Inject constructor(
|
|||||||
Log.d(TAG, "Channel lost, trying again")
|
Log.d(TAG, "Channel lost, trying again")
|
||||||
callbacks?.onConnectionToPeersLost()
|
callbacks?.onConnectionToPeersLost()
|
||||||
shouldRetry = false
|
shouldRetry = false
|
||||||
manager.initialize(activity, Looper.getMainLooper(), this)
|
manager.initialize(context, Looper.getMainLooper(), this)
|
||||||
} else {
|
} else {
|
||||||
activity.toast(R.string.severe_loss_error, Toast.LENGTH_LONG)
|
context.toast(R.string.severe_loss_error, Toast.LENGTH_LONG)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +190,7 @@ class WifiDirectManager @Inject constructor(
|
|||||||
FileTransferConfirmation(senderSelectedPeerDevice.deviceName), {
|
FileTransferConfirmation(senderSelectedPeerDevice.deviceName), {
|
||||||
hasSenderStartedConnection = true
|
hasSenderStartedConnection = true
|
||||||
connect(senderSelectedPeerDevice)
|
connect(senderSelectedPeerDevice)
|
||||||
activity.toast(R.string.performing_handshake, Toast.LENGTH_LONG)
|
context.toast(R.string.performing_handshake, Toast.LENGTH_LONG)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -208,8 +207,8 @@ class WifiDirectManager @Inject constructor(
|
|||||||
|
|
||||||
override fun onFailure(reason: Int) {
|
override fun onFailure(reason: Int) {
|
||||||
val errorMessage = getErrorMessage(reason)
|
val errorMessage = getErrorMessage(reason)
|
||||||
Log.d(TAG, activity.getString(R.string.connection_failed) + ": " + errorMessage)
|
Log.d(TAG, context.getString(R.string.connection_failed) + ": " + errorMessage)
|
||||||
activity.toast(R.string.connection_failed, Toast.LENGTH_LONG)
|
context.toast(R.string.connection_failed, Toast.LENGTH_LONG)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -232,7 +231,7 @@ class WifiDirectManager @Inject constructor(
|
|||||||
Log.d(TAG, "InetAddress is null")
|
Log.d(TAG, "InetAddress is null")
|
||||||
}
|
}
|
||||||
onFileTransferAsyncTaskComplete(false)
|
onFileTransferAsyncTaskComplete(false)
|
||||||
activity.toast(R.string.connection_refused)
|
context.toast(R.string.connection_refused)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -254,8 +253,8 @@ class WifiDirectManager @Inject constructor(
|
|||||||
Log.d(LocalFileTransferFragment.TAG, "Starting file transfer")
|
Log.d(LocalFileTransferFragment.TAG, "Starting file transfer")
|
||||||
val fileReceiverDeviceAddress =
|
val fileReceiverDeviceAddress =
|
||||||
if (groupInfo.isGroupOwner) inetAddress else groupInfo.groupOwnerAddress
|
if (groupInfo.isGroupOwner) inetAddress else groupInfo.groupOwnerAddress
|
||||||
activity.toast(R.string.preparing_files, Toast.LENGTH_LONG)
|
context.toast(R.string.preparing_files, Toast.LENGTH_LONG)
|
||||||
val senderDevice = SenderDevice(activity, this, fileReceiverDeviceAddress)
|
val senderDevice = SenderDevice(context, this, fileReceiverDeviceAddress)
|
||||||
val isFileSendSuccessfully = senderDevice.send(filesForTransfer)
|
val isFileSendSuccessfully = senderDevice.send(filesForTransfer)
|
||||||
onFileTransferAsyncTaskComplete(isFileSendSuccessfully)
|
onFileTransferAsyncTaskComplete(isFileSendSuccessfully)
|
||||||
if (BuildConfig.DEBUG) {
|
if (BuildConfig.DEBUG) {
|
||||||
@ -277,8 +276,8 @@ class WifiDirectManager @Inject constructor(
|
|||||||
filesForTransfer[itemIndex].fileStatus = status
|
filesForTransfer[itemIndex].fileStatus = status
|
||||||
callbacks?.onFileStatusChanged(itemIndex)
|
callbacks?.onFileStatusChanged(itemIndex)
|
||||||
if (status == FileStatus.ERROR) {
|
if (status == FileStatus.ERROR) {
|
||||||
activity.toast(
|
context.toast(
|
||||||
activity.getString(
|
context.getString(
|
||||||
R.string.error_transferring, filesForTransfer[itemIndex].fileName
|
R.string.error_transferring, filesForTransfer[itemIndex].fileName
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -325,9 +324,9 @@ class WifiDirectManager @Inject constructor(
|
|||||||
|
|
||||||
private fun onFileTransferAsyncTaskComplete(wereAllFilesTransferred: Boolean) {
|
private fun onFileTransferAsyncTaskComplete(wereAllFilesTransferred: Boolean) {
|
||||||
if (wereAllFilesTransferred) {
|
if (wereAllFilesTransferred) {
|
||||||
activity.toast(R.string.file_transfer_complete, Toast.LENGTH_LONG)
|
context.toast(R.string.file_transfer_complete, Toast.LENGTH_LONG)
|
||||||
} else {
|
} else {
|
||||||
activity.toast(R.string.error_during_transfer, Toast.LENGTH_LONG)
|
context.toast(R.string.error_during_transfer, Toast.LENGTH_LONG)
|
||||||
}
|
}
|
||||||
callbacks?.onFileTransferComplete()
|
callbacks?.onFileTransferComplete()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user