Commit 6f355ed1 authored by Mygod's avatar Mygod

LongSparseArray

parent 5b1b2794
......@@ -23,6 +23,7 @@ package com.github.shadowsocks.database
import android.net.Uri
import android.util.Base64
import android.util.Log
import android.util.LongSparseArray
import androidx.core.net.toUri
import androidx.room.*
import com.github.shadowsocks.plugin.PluginConfiguration
......@@ -249,7 +250,7 @@ class Profile : Serializable {
}
override fun toString() = toUri().toString()
fun toJson(profiles: Map<Long, Profile>? = null): JSONObject = JSONObject().apply {
fun toJson(profiles: LongSparseArray<Profile>? = null): JSONObject = JSONObject().apply {
put("server", host)
put("server_port", remotePort)
put("password", password)
......@@ -274,7 +275,7 @@ class Profile : Serializable {
}
})
put("udpdns", udpdns)
val fallback = profiles[udpFallback]
val fallback = profiles.get(udpFallback ?: return@apply)
if (fallback != null && fallback.plugin.isNullOrEmpty()) fallback.toJson().also { put("udp_fallback", it) }
}
......
......@@ -28,6 +28,7 @@ import android.nfc.NdefRecord
import android.nfc.NfcAdapter
import android.os.Bundle
import android.text.format.Formatter
import android.util.LongSparseArray
import android.view.*
import android.widget.ImageView
import android.widget.LinearLayout
......@@ -301,7 +302,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
val profilesAdapter by lazy { ProfilesAdapter() }
private lateinit var undoManager: UndoSnackbarManager<Profile>
private val statsCache = mutableMapOf<Long, TrafficStats>()
private val statsCache = LongSparseArray<TrafficStats>()
private val clipboard by lazy { requireContext().getSystemService<ClipboardManager>()!! }
......@@ -443,7 +444,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
REQUEST_EXPORT_PROFILES -> {
val profiles = ProfileManager.getAllProfiles()
if (profiles != null) try {
val lookup = profiles.associateBy { it.id }
val lookup = LongSparseArray<Profile>(profiles.size).apply { profiles.forEach { put(it.id, it) } }
requireContext().contentResolver.openOutputStream(data?.data!!)!!.bufferedWriter().use {
it.write(JSONArray(profiles.map { it.toJson(lookup) }.toTypedArray()).toString(2))
}
......@@ -458,7 +459,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
override fun onTrafficUpdated(profileId: Long, stats: TrafficStats) {
if (profileId != 0L) { // ignore aggregate stats
statsCache[profileId] = stats
statsCache.put(profileId, stats)
profilesAdapter.refreshId(profileId)
}
}
......
......@@ -30,6 +30,7 @@ import android.os.DeadObjectException
import android.os.Handler
import android.text.format.Formatter
import android.util.Log
import android.util.LongSparseArray
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.leanback.preference.LeanbackPreferenceFragment
......@@ -318,7 +319,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragment(), ShadowsocksConnecti
if (resultCode != Activity.RESULT_OK) return
val profiles = ProfileManager.getAllProfiles()
if (profiles != null) try {
val lookup = profiles.associateBy { it.id }
val lookup = LongSparseArray<Profile>(profiles.size).apply { profiles.forEach { put(it.id, it) } }
activity.contentResolver.openOutputStream(data?.data!!)!!.bufferedWriter().use {
it.write(JSONArray(profiles.map { it.toJson(lookup) }.toTypedArray()).toString(2))
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment