mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-16 02:48:08 -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
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
@ -45,7 +45,7 @@ import java.net.Socket
|
||||
private const val TIME_OUT = 15000
|
||||
|
||||
internal class SenderDevice(
|
||||
private val activity: Activity,
|
||||
private val context: Context,
|
||||
private val wifiDirectManager: WifiDirectManager,
|
||||
private val fileReceiverDeviceAddress: InetAddress
|
||||
) {
|
||||
@ -59,7 +59,7 @@ internal class SenderDevice(
|
||||
.forEachIndexed { fileIndex, fileItem ->
|
||||
try {
|
||||
Socket().use { socket ->
|
||||
activity.contentResolver.openInputStream(fileItem?.fileUri!!).use { fileInputStream ->
|
||||
context.contentResolver.openInputStream(fileItem?.fileUri!!).use { fileInputStream ->
|
||||
socket.bind(null)
|
||||
socket.connect(
|
||||
InetSocketAddress(hostAddress, WifiDirectManager.FILE_TRANSFER_PORT),
|
||||
|
@ -17,7 +17,6 @@
|
||||
*/
|
||||
package org.kiwix.kiwixmobile.localFileTransfer
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.IntentFilter
|
||||
@ -59,7 +58,7 @@ import javax.inject.Inject
|
||||
*/
|
||||
@SuppressWarnings("MissingPermission", "ProtectedMemberInFinalClass")
|
||||
class WifiDirectManager @Inject constructor(
|
||||
private val activity: Activity,
|
||||
private val context: Context,
|
||||
private val sharedPreferenceUtil: SharedPreferenceUtil,
|
||||
private val alertDialogShower: AlertDialogShower
|
||||
) : ChannelListener, PeerListListener, ConnectionInfoListener, P2pEventListener {
|
||||
@ -94,8 +93,8 @@ class WifiDirectManager @Inject constructor(
|
||||
fun startWifiDirectManager(filesForTransfer: List<FileItem>) {
|
||||
this.filesForTransfer = filesForTransfer
|
||||
isFileSender = filesForTransfer.isNotEmpty()
|
||||
manager = activity.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
|
||||
channel = manager.initialize(activity, Looper.getMainLooper(), null)
|
||||
manager = context.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
|
||||
channel = manager.initialize(context, Looper.getMainLooper(), null)
|
||||
registerWifiDirectBroadcastReceiver()
|
||||
}
|
||||
|
||||
@ -110,20 +109,20 @@ class WifiDirectManager @Inject constructor(
|
||||
addAction(WifiP2pManager.WIFI_P2P_CONNECTION_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() {
|
||||
manager.discoverPeers(channel, object : ActionListener {
|
||||
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) {
|
||||
Log.d(TAG, "${activity.getString(R.string.discovery_failed)}: ${getErrorMessage(reason)}")
|
||||
activity.toast(R.string.discovery_failed, Toast.LENGTH_SHORT)
|
||||
Log.d(TAG, "${context.getString(R.string.discovery_failed)}: ${getErrorMessage(reason)}")
|
||||
context.toast(R.string.discovery_failed, Toast.LENGTH_SHORT)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -132,7 +131,7 @@ class WifiDirectManager @Inject constructor(
|
||||
override fun onWifiP2pStateChanged(isEnabled: Boolean) {
|
||||
isWifiP2pEnabled = isEnabled
|
||||
if (!isWifiP2pEnabled) {
|
||||
activity.toast(R.string.discovery_needs_wifi, Toast.LENGTH_SHORT)
|
||||
context.toast(R.string.discovery_needs_wifi, Toast.LENGTH_SHORT)
|
||||
callbacks?.onConnectionToPeersLost()
|
||||
}
|
||||
Log.d(TAG, "WiFi P2P state changed - $isWifiP2pEnabled")
|
||||
@ -167,9 +166,9 @@ class WifiDirectManager @Inject constructor(
|
||||
Log.d(TAG, "Channel lost, trying again")
|
||||
callbacks?.onConnectionToPeersLost()
|
||||
shouldRetry = false
|
||||
manager.initialize(activity, Looper.getMainLooper(), this)
|
||||
manager.initialize(context, Looper.getMainLooper(), this)
|
||||
} 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), {
|
||||
hasSenderStartedConnection = true
|
||||
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) {
|
||||
val errorMessage = getErrorMessage(reason)
|
||||
Log.d(TAG, activity.getString(R.string.connection_failed) + ": " + errorMessage)
|
||||
activity.toast(R.string.connection_failed, Toast.LENGTH_LONG)
|
||||
Log.d(TAG, context.getString(R.string.connection_failed) + ": " + errorMessage)
|
||||
context.toast(R.string.connection_failed, Toast.LENGTH_LONG)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -232,7 +231,7 @@ class WifiDirectManager @Inject constructor(
|
||||
Log.d(TAG, "InetAddress is null")
|
||||
}
|
||||
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")
|
||||
val fileReceiverDeviceAddress =
|
||||
if (groupInfo.isGroupOwner) inetAddress else groupInfo.groupOwnerAddress
|
||||
activity.toast(R.string.preparing_files, Toast.LENGTH_LONG)
|
||||
val senderDevice = SenderDevice(activity, this, fileReceiverDeviceAddress)
|
||||
context.toast(R.string.preparing_files, Toast.LENGTH_LONG)
|
||||
val senderDevice = SenderDevice(context, this, fileReceiverDeviceAddress)
|
||||
val isFileSendSuccessfully = senderDevice.send(filesForTransfer)
|
||||
onFileTransferAsyncTaskComplete(isFileSendSuccessfully)
|
||||
if (BuildConfig.DEBUG) {
|
||||
@ -277,8 +276,8 @@ class WifiDirectManager @Inject constructor(
|
||||
filesForTransfer[itemIndex].fileStatus = status
|
||||
callbacks?.onFileStatusChanged(itemIndex)
|
||||
if (status == FileStatus.ERROR) {
|
||||
activity.toast(
|
||||
activity.getString(
|
||||
context.toast(
|
||||
context.getString(
|
||||
R.string.error_transferring, filesForTransfer[itemIndex].fileName
|
||||
)
|
||||
)
|
||||
@ -325,9 +324,9 @@ class WifiDirectManager @Inject constructor(
|
||||
|
||||
private fun onFileTransferAsyncTaskComplete(wereAllFilesTransferred: Boolean) {
|
||||
if (wereAllFilesTransferred) {
|
||||
activity.toast(R.string.file_transfer_complete, Toast.LENGTH_LONG)
|
||||
context.toast(R.string.file_transfer_complete, Toast.LENGTH_LONG)
|
||||
} else {
|
||||
activity.toast(R.string.error_during_transfer, Toast.LENGTH_LONG)
|
||||
context.toast(R.string.error_during_transfer, Toast.LENGTH_LONG)
|
||||
}
|
||||
callbacks?.onFileTransferComplete()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user