diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-11-16 16:41:33 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-11-16 16:41:33 +0100 |
commit | ffe937768f9a6c9d24ee9d383c2d7a6b2dd4acc4 (patch) | |
tree | 396f39a30285d45e2550c8c0d6514f404a7902d0 | |
parent | 74e553368cafd37b99864d6aaa0b9702c4b6139b (diff) |
ui: fix slow app listfix/app-list
-rw-r--r-- | ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt | 65 |
1 files changed, 53 insertions, 12 deletions
diff --git a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt index 1a40a1cd..50b2c098 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt @@ -6,7 +6,11 @@ package com.wireguard.android.fragment import android.Manifest import android.app.Dialog +import android.content.Context +import android.content.pm.PackageInfo import android.os.Bundle +import android.graphics.drawable.Drawable +import android.util.Log import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AlertDialog @@ -33,33 +37,60 @@ class AppListDialogFragment : DialogFragment() { private var initiallyExcluded = false private var button: Button? = null private var tabs: TabLayout? = null + private var context: Context? = null private fun loadData() { val activity = activity ?: return val pm = activity.packageManager + val dummyIcon: Drawable = context!!.getDrawable(17301551)!! + val onPropertyChangedCb = object : Observable.OnPropertyChangedCallback() { + override fun onPropertyChanged(sender: Observable?, propertyId: Int) { + if (propertyId == BR.selected) + setButtonText() + } + } lifecycleScope.launch(Dispatchers.Default) { try { - val applicationData: MutableList<ApplicationData> = ArrayList() + Log.e(TAG, "lifecycleScope.launch") + + val packageInfoData: MutableList<Pair<String, PackageInfo>> = ArrayList() withContext(Dispatchers.IO) { val packageInfos = pm.getPackagesHoldingPermissions(arrayOf(Manifest.permission.INTERNET), 0) packageInfos.forEach { val packageName = it.packageName - val appInfo = it.applicationInfo - val appData = ApplicationData(appInfo.loadIcon(pm), appInfo.loadLabel(pm).toString(), packageName, currentlySelectedApps.contains(packageName)) - applicationData.add(appData) - appData.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() { - override fun onPropertyChanged(sender: Observable?, propertyId: Int) { - if (propertyId == BR.selected) - setButtonText() - } - }) + packageInfoData.add(Pair(packageName, it)) } } - applicationData.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name }) + packageInfoData.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.first }) + withContext(Dispatchers.Main.immediate) { appData.clear() - appData.addAll(applicationData) } + + launch(Dispatchers.IO) { + packageInfoData.forEach { + val packageName = it.first + val appInfo = it.second.applicationInfo + val icon = appInfo.loadIcon(pm) + val appDataItem = ApplicationData(icon, appInfo.loadLabel(pm).toString(), packageName, currentlySelectedApps.contains(packageName)) + withContext(Dispatchers.Main.immediate) { + appData.add(appDataItem) + } + } + + withContext(Dispatchers.Main.immediate) { + appData.forEach() { + it.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() { + override fun onPropertyChanged(sender: Observable?, propertyId: Int) { + if (propertyId == BR.selected) + setButtonText() + } + }) + } + } + } + + Log.e(TAG, "lifecycleScope.launch done") } catch (e: Throwable) { withContext(Dispatchers.Main.immediate) { val error = ErrorMessages[e] @@ -69,6 +100,12 @@ class AppListDialogFragment : DialogFragment() { } } } + Log.e(TAG, "lifecycleScope.launch after") + } + + override fun onAttach(context: Context) { + super.onAttach(context) + this.context = context } override fun onCreate(savedInstanceState: Bundle?) { @@ -89,6 +126,7 @@ class AppListDialogFragment : DialogFragment() { } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + Log.e(TAG, "onCreateDialog begin") val alertDialogBuilder = MaterialAlertDialogBuilder(requireActivity()) val binding = AppListDialogFragmentBinding.inflate(requireActivity().layoutInflater, null, false) binding.executePendingBindings() @@ -119,6 +157,7 @@ class AppListDialogFragment : DialogFragment() { } } } + Log.e(TAG, "onCreateDialog done") return dialog } @@ -140,6 +179,8 @@ class AppListDialogFragment : DialogFragment() { const val KEY_SELECTED_APPS = "selected_apps" const val KEY_IS_EXCLUDED = "is_excluded" const val REQUEST_SELECTION = "request_selection" + private const val TAG = "WireGuard/AppListDialogFragment" + fun newInstance(selectedApps: ArrayList<String?>?, isExcluded: Boolean): AppListDialogFragment { val extras = Bundle() extras.putStringArrayList(KEY_SELECTED_APPS, selectedApps) |