Optimize usage of SparseArray
This commit is contained in:
@@ -8,7 +8,6 @@ import android.os.RemoteException
|
|||||||
import android.system.Os
|
import android.system.Os
|
||||||
import android.system.OsConstants
|
import android.system.OsConstants
|
||||||
import androidx.collection.LongSparseArray
|
import androidx.collection.LongSparseArray
|
||||||
import androidx.collection.set
|
|
||||||
import androidx.collection.valueIterator
|
import androidx.collection.valueIterator
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.channels.*
|
import kotlinx.coroutines.channels.*
|
||||||
@@ -248,7 +247,7 @@ class RootServer {
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
val callback = Callback.Ordinary(this, counter, classLoader, future as CompletableDeferred<Parcelable?>)
|
val callback = Callback.Ordinary(this, counter, classLoader, future as CompletableDeferred<Parcelable?>)
|
||||||
if (active) {
|
if (active) {
|
||||||
callbackLookup[counter] = callback
|
callbackLookup.append(counter, callback)
|
||||||
sendLocked(command)
|
sendLocked(command)
|
||||||
} else future.cancel()
|
} else future.cancel()
|
||||||
callback
|
callback
|
||||||
@@ -279,7 +278,7 @@ class RootServer {
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
val callback = Callback.Channel(this@RootServer, counter, classLoader, this as SendChannel<Parcelable?>)
|
val callback = Callback.Channel(this@RootServer, counter, classLoader, this as SendChannel<Parcelable?>)
|
||||||
if (active) {
|
if (active) {
|
||||||
callbackLookup[counter] = callback
|
callbackLookup.append(counter, callback)
|
||||||
sendLocked(command)
|
sendLocked(command)
|
||||||
} else callback.finish.cancel()
|
} else callback.finish.cancel()
|
||||||
callback
|
callback
|
||||||
@@ -434,7 +433,7 @@ class RootServer {
|
|||||||
}
|
}
|
||||||
is RootCommand<*> -> {
|
is RootCommand<*> -> {
|
||||||
val commandJob = Job()
|
val commandJob = Job()
|
||||||
cancellables[callback] = { commandJob.cancel() }
|
cancellables.append(callback) { commandJob.cancel() }
|
||||||
defaultWorker.launch(commandJob) {
|
defaultWorker.launch(commandJob) {
|
||||||
val result = try {
|
val result = try {
|
||||||
val result = command.execute();
|
val result = command.execute();
|
||||||
@@ -452,7 +451,7 @@ class RootServer {
|
|||||||
val result = try {
|
val result = try {
|
||||||
coroutineScope {
|
coroutineScope {
|
||||||
command.create(this).also {
|
command.create(this).also {
|
||||||
cancellables[callback] = { it.cancel() }
|
cancellables.append(callback) { it.cancel() }
|
||||||
}.consumeEach { result ->
|
}.consumeEach { result ->
|
||||||
withContext(callbackWorker) { output.pushResult(callback, result) }
|
withContext(callbackWorker) { output.pushResult(callback, result) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ data class SoftApConfigurationCompat(
|
|||||||
* Otherwise, use [optimizeChannels] or [setChannel].
|
* Otherwise, use [optimizeChannels] or [setChannel].
|
||||||
*/
|
*/
|
||||||
@TargetApi(23)
|
@TargetApi(23)
|
||||||
var channels: SparseIntArray = SparseIntArray(1).apply { put(BAND_2GHZ, 0) },
|
var channels: SparseIntArray = SparseIntArray(1).apply { append(BAND_2GHZ, 0) },
|
||||||
var securityType: Int = SoftApConfiguration.SECURITY_TYPE_OPEN,
|
var securityType: Int = SoftApConfiguration.SECURITY_TYPE_OPEN,
|
||||||
@TargetApi(30)
|
@TargetApi(30)
|
||||||
var maxNumberOfClients: Int = 0,
|
var maxNumberOfClients: Int = 0,
|
||||||
@@ -281,12 +281,12 @@ data class SoftApConfigurationCompat(
|
|||||||
hiddenSSID,
|
hiddenSSID,
|
||||||
// https://cs.android.com/android/platform/superproject/+/master:frameworks/base/wifi/java/android/net/wifi/SoftApConfToXmlMigrationUtil.java;l=87;drc=aa6527cf41671d1ed417b8ebdb6b3aa614f62344
|
// https://cs.android.com/android/platform/superproject/+/master:frameworks/base/wifi/java/android/net/wifi/SoftApConfToXmlMigrationUtil.java;l=87;drc=aa6527cf41671d1ed417b8ebdb6b3aa614f62344
|
||||||
SparseIntArray(1).apply {
|
SparseIntArray(1).apply {
|
||||||
if (Build.VERSION.SDK_INT < 23) put(BAND_LEGACY, 0) else put(when (val band = apBand.getInt(this)) {
|
if (Build.VERSION.SDK_INT >= 23) append(when (val band = apBand.getInt(this)) {
|
||||||
0 -> BAND_2GHZ
|
0 -> BAND_2GHZ
|
||||||
1 -> BAND_5GHZ
|
1 -> BAND_5GHZ
|
||||||
-1 -> BAND_LEGACY
|
-1 -> BAND_LEGACY
|
||||||
else -> throw IllegalArgumentException("Unexpected band $band")
|
else -> throw IllegalArgumentException("Unexpected band $band")
|
||||||
}, apChannel.getInt(this))
|
}, apChannel.getInt(this)) else append(BAND_LEGACY, 0)
|
||||||
},
|
},
|
||||||
allowedKeyManagement.nextSetBit(0).let { selected ->
|
allowedKeyManagement.nextSetBit(0).let { selected ->
|
||||||
require(allowedKeyManagement.nextSetBit(selected + 1) < 0) {
|
require(allowedKeyManagement.nextSetBit(selected + 1) < 0) {
|
||||||
@@ -319,7 +319,7 @@ data class SoftApConfigurationCompat(
|
|||||||
passphrase,
|
passphrase,
|
||||||
isHiddenSsid,
|
isHiddenSsid,
|
||||||
if (BuildCompat.isAtLeastS()) getChannels(this) as SparseIntArray else SparseIntArray(1).apply {
|
if (BuildCompat.isAtLeastS()) getChannels(this) as SparseIntArray else SparseIntArray(1).apply {
|
||||||
put(getBand(this) as Int, getChannel(this) as Int)
|
append(getBand(this) as Int, getChannel(this) as Int)
|
||||||
},
|
},
|
||||||
securityType,
|
securityType,
|
||||||
getMaxNumberOfClients(this) as Int,
|
getMaxNumberOfClients(this) as Int,
|
||||||
@@ -359,13 +359,13 @@ data class SoftApConfigurationCompat(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
fun setChannel(channel: Int, band: Int = BAND_LEGACY) {
|
fun setChannel(channel: Int, band: Int = BAND_LEGACY) {
|
||||||
channels = SparseIntArray(1).apply { put(band, channel) }
|
channels = SparseIntArray(1).apply { append(band, channel) }
|
||||||
}
|
}
|
||||||
fun optimizeChannels(channels: SparseIntArray = this.channels) {
|
fun optimizeChannels(channels: SparseIntArray = this.channels) {
|
||||||
this.channels = SparseIntArray(channels.size()).apply {
|
this.channels = SparseIntArray(channels.size()).apply {
|
||||||
var setBand = 0
|
var setBand = 0
|
||||||
repeat(channels.size()) { i -> if (channels.valueAt(i) == 0) setBand = setBand or channels.keyAt(i) }
|
repeat(channels.size()) { i -> if (channels.valueAt(i) == 0) setBand = setBand or channels.keyAt(i) }
|
||||||
if (setBand != 0) put(setBand, 0) // merge all bands into one
|
if (setBand != 0) append(setBand, 0) // merge all bands into one
|
||||||
repeat(channels.size()) { i ->
|
repeat(channels.size()) { i ->
|
||||||
val band = channels.keyAt(i)
|
val band = channels.keyAt(i)
|
||||||
if (band and setBand == 0) put(band, channels.valueAt(i))
|
if (band and setBand == 0) put(band, channels.valueAt(i))
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ class WifiApDialogFragment : AlertDialogFragment<WifiApDialogFragment.Arg, WifiA
|
|||||||
SoftApConfigurationCompat.BAND_6GHZ to dialogView.band6G,
|
SoftApConfigurationCompat.BAND_6GHZ to dialogView.band6G,
|
||||||
SoftApConfigurationCompat.BAND_60GHZ to dialogView.band60G)) {
|
SoftApConfigurationCompat.BAND_60GHZ to dialogView.band60G)) {
|
||||||
val channel = (spinner.selectedItem as ChannelOption?)?.channel
|
val channel = (spinner.selectedItem as ChannelOption?)?.channel
|
||||||
if (channel != null && channel >= 0) channels.put(band, channel)
|
if (channel != null && channel >= 0) channels.append(band, channel)
|
||||||
}
|
}
|
||||||
if (!arg.p2pMode && BuildCompat.isAtLeastS() && dialogView.bridgedMode.isChecked) {
|
if (!arg.p2pMode && BuildCompat.isAtLeastS() && dialogView.bridgedMode.isChecked) {
|
||||||
this.channels = channels
|
this.channels = channels
|
||||||
|
|||||||
Reference in New Issue
Block a user