diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-14 14:27:55 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-09-14 14:40:10 +0200 |
commit | 2fc0bb1a03f624e297d2afdeb95231cf906afc21 (patch) | |
tree | b1f9ed3d59683b5bbe4975821988c0e9ce7f2e84 /ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt | |
parent | ui: remove hacky manual check for keyguard (diff) | |
download | wireguard-android-2fc0bb1a03f624e297d2afdeb95231cf906afc21.tar.xz wireguard-android-2fc0bb1a03f624e297d2afdeb95231cf906afc21.zip |
coroutines: convert low-hanging fruits
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt | 60 |
1 files changed, 34 insertions, 26 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 35bd3ce9..1a29c5e6 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt @@ -22,6 +22,10 @@ import com.wireguard.android.databinding.ObservableKeyedArrayList import com.wireguard.android.model.ApplicationData import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.requireTargetFragment +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class AppListDialogFragment : DialogFragment() { private val appData = ObservableKeyedArrayList<String, ApplicationData>() @@ -33,33 +37,37 @@ class AppListDialogFragment : DialogFragment() { private fun loadData() { val activity = activity ?: return val pm = activity.packageManager - Application.getAsyncWorker().supplyAsync<List<ApplicationData>> { - val launcherIntent = Intent(Intent.ACTION_MAIN, null) - launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER) - val resolveInfos = pm.queryIntentActivities(launcherIntent, 0) - val applicationData: MutableList<ApplicationData> = ArrayList() - resolveInfos.forEach { - val packageName = it.activityInfo.packageName - val appData = ApplicationData(it.loadIcon(pm), it.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() + CoroutineScope(Dispatchers.Default).launch { + try { + val applicationData: MutableList<ApplicationData> = ArrayList() + withContext(Dispatchers.IO) { + val launcherIntent = Intent(Intent.ACTION_MAIN, null) + launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER) + val resolveInfos = pm.queryIntentActivities(launcherIntent, 0) + resolveInfos.forEach { + val packageName = it.activityInfo.packageName + val appData = ApplicationData(it.loadIcon(pm), it.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() + } + }) } - }) - } - applicationData.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name }) - applicationData - }.whenComplete { data, throwable -> - if (data != null) { - appData.clear() - appData.addAll(data) - } else { - val error = ErrorMessages[throwable] - val message = activity.getString(R.string.error_fetching_apps, error) - Toast.makeText(activity, message, Toast.LENGTH_LONG).show() - dismissAllowingStateLoss() + } + applicationData.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name }) + withContext(Dispatchers.Main) { + appData.clear() + appData.addAll(applicationData) + } + } catch (e: Exception) { + withContext(Dispatchers.Main) { + val error = ErrorMessages[e] + val message = activity.getString(R.string.error_fetching_apps, error) + Toast.makeText(activity, message, Toast.LENGTH_LONG).show() + dismissAllowingStateLoss() + } } } } |