Optimize usage of SparseArray

This commit is contained in:
Mygod
2021-05-31 02:07:04 -04:00
parent f7e7011992
commit 0a2e13556a
3 changed files with 11 additions and 12 deletions

View File

@@ -8,7 +8,6 @@ import android.os.RemoteException
import android.system.Os
import android.system.OsConstants
import androidx.collection.LongSparseArray
import androidx.collection.set
import androidx.collection.valueIterator
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
@@ -248,7 +247,7 @@ class RootServer {
@Suppress("UNCHECKED_CAST")
val callback = Callback.Ordinary(this, counter, classLoader, future as CompletableDeferred<Parcelable?>)
if (active) {
callbackLookup[counter] = callback
callbackLookup.append(counter, callback)
sendLocked(command)
} else future.cancel()
callback
@@ -279,7 +278,7 @@ class RootServer {
@Suppress("UNCHECKED_CAST")
val callback = Callback.Channel(this@RootServer, counter, classLoader, this as SendChannel<Parcelable?>)
if (active) {
callbackLookup[counter] = callback
callbackLookup.append(counter, callback)
sendLocked(command)
} else callback.finish.cancel()
callback
@@ -434,7 +433,7 @@ class RootServer {
}
is RootCommand<*> -> {
val commandJob = Job()
cancellables[callback] = { commandJob.cancel() }
cancellables.append(callback) { commandJob.cancel() }
defaultWorker.launch(commandJob) {
val result = try {
val result = command.execute();
@@ -452,7 +451,7 @@ class RootServer {
val result = try {
coroutineScope {
command.create(this).also {
cancellables[callback] = { it.cancel() }
cancellables.append(callback) { it.cancel() }
}.consumeEach { result ->
withContext(callbackWorker) { output.pushResult(callback, result) }
}

View File

@@ -32,7 +32,7 @@ data class SoftApConfigurationCompat(
* Otherwise, use [optimizeChannels] or [setChannel].
*/
@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,
@TargetApi(30)
var maxNumberOfClients: Int = 0,
@@ -281,12 +281,12 @@ data class SoftApConfigurationCompat(
hiddenSSID,
// https://cs.android.com/android/platform/superproject/+/master:frameworks/base/wifi/java/android/net/wifi/SoftApConfToXmlMigrationUtil.java;l=87;drc=aa6527cf41671d1ed417b8ebdb6b3aa614f62344
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
1 -> BAND_5GHZ
-1 -> BAND_LEGACY
else -> throw IllegalArgumentException("Unexpected band $band")
}, apChannel.getInt(this))
}, apChannel.getInt(this)) else append(BAND_LEGACY, 0)
},
allowedKeyManagement.nextSetBit(0).let { selected ->
require(allowedKeyManagement.nextSetBit(selected + 1) < 0) {
@@ -319,7 +319,7 @@ data class SoftApConfigurationCompat(
passphrase,
isHiddenSsid,
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,
getMaxNumberOfClients(this) as Int,
@@ -359,13 +359,13 @@ data class SoftApConfigurationCompat(
return result
}
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) {
this.channels = SparseIntArray(channels.size()).apply {
var setBand = 0
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 ->
val band = channels.keyAt(i)
if (band and setBand == 0) put(band, channels.valueAt(i))

View File

@@ -115,7 +115,7 @@ class WifiApDialogFragment : AlertDialogFragment<WifiApDialogFragment.Arg, WifiA
SoftApConfigurationCompat.BAND_6GHZ to dialogView.band6G,
SoftApConfigurationCompat.BAND_60GHZ to dialogView.band60G)) {
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) {
this.channels = channels