Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8e25c7fb authored by Bob Yang's avatar Bob Yang Committed by Android (Google) Code Review
Browse files

Merge "Show the toast if the target activity is empty" into main

parents b0ade4e3 f6f90dd8
Loading
Loading
Loading
Loading
+36 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.wallpaper.customization.ui.binder
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.drawable.AdaptiveIconDrawable
import android.provider.Settings
import android.view.View
@@ -70,8 +71,11 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.google.android.material.snackbar.Snackbar
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.DisposableHandle
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
@@ -273,6 +277,8 @@ constructor(private val defaultCustomizationOptionsBinder: DefaultCustomizationO
                .first { it.first == ThemePickerHomeCustomizationOption.COLOR_CONTRAST }
                .second
        optionColorContrast.setOnClickListener { navigateToColorContrastSettingsActivity.invoke() }
        val backgroundScope =
            CoroutineScope(Dispatchers.IO + Job() + CoroutineName(BACKGROUND_CONTEXT))

        ColorUpdateBinder.bind(
            setColor = { color ->
@@ -530,10 +536,30 @@ constructor(private val defaultCustomizationOptionsBinder: DefaultCustomizationO
                            intent ->
                            if (intent != null) {
                                optionPackThemeHome?.setOnClickListener {
                                    backgroundScope.launch {
                                        if (isActivityAvailable(view.context, intent)) {
                                            navigateToPackThemeActivity.invoke(intent)
                                        } else {
                                            showNoPackThemeIntentErrorMessage(
                                                lifecycleOwner,
                                                view,
                                                optionsViewModel,
                                            )
                                        }
                                    }
                                }
                                optionPackThemeLock?.setOnClickListener {
                                    backgroundScope.launch {
                                        if (isActivityAvailable(view.context, intent)) {
                                            navigateToPackThemeActivity.invoke(intent)
                                        } else {
                                            showNoPackThemeIntentErrorMessage(
                                                lifecycleOwner,
                                                view,
                                                optionsViewModel,
                                            )
                                        }
                                    }
                                }
                            } else {
                                optionPackThemeHome?.setOnClickListener({
@@ -631,6 +657,13 @@ constructor(private val defaultCustomizationOptionsBinder: DefaultCustomizationO
    // Track the current show clock flag. If it turns from false to true, animate fade-in.
    private var isClockCurrentlyShown: Boolean? = null

    private suspend fun isActivityAvailable(context: Context, intent: Intent): Boolean {
        val packageManager: PackageManager = context.packageManager
        val activities =
            packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
        return activities.isNotEmpty()
    }

    private fun showNoPackThemeIntentErrorMessage(
        lifecycleOwner: LifecycleOwner,
        view: View,
@@ -786,5 +819,6 @@ constructor(private val defaultCustomizationOptionsBinder: DefaultCustomizationO

    companion object {
        private const val THUMBNAIL_CORNER_RADIUS = 18
        private const val BACKGROUND_CONTEXT = "backgroundContext"
    }
}
+49 −1
Original line number Diff line number Diff line
@@ -16,8 +16,11 @@

package com.android.wallpaper.customization.ui.binder

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.view.View
import android.widget.Toast
import androidx.core.graphics.drawable.DrawableCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
@@ -31,6 +34,10 @@ import com.android.wallpaper.picker.customization.ui.viewmodel.ColorUpdateViewMo
import com.android.wallpaper.picker.customization.ui.viewmodel.CustomizationPickerViewModel2
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

@Singleton
@@ -49,6 +56,8 @@ class ThemePickerSuggestedEntryBinder @Inject constructor() : PackThemeSuggested
        val optionsViewModel =
            viewModel.customizationOptionsViewModel as ThemePickerCustomizationOptionsViewModel

        val backgroundScope =
            CoroutineScope(Dispatchers.IO + Job() + CoroutineName(BACKGROUND_CONTEXT))
        lifecycleOwner.lifecycleScope.launch {
            lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
@@ -56,7 +65,17 @@ class ThemePickerSuggestedEntryBinder @Inject constructor() : PackThemeSuggested
                        .collect { intent ->
                            if (intent != null) {
                                view.suggestedChip.setOnClickListener {
                                    backgroundScope.launch {
                                        if (isActivityAvailable(view.context, intent)) {
                                            navigateToPackThemeActivity.invoke(intent)
                                        } else {
                                            showNoPackThemeIntentErrorMessage(
                                                lifecycleOwner,
                                                view,
                                                optionsViewModel,
                                            )
                                        }
                                    }
                                }
                            } else {
                                view.suggestedChip.setOnClickListener { null }
@@ -68,6 +87,9 @@ class ThemePickerSuggestedEntryBinder @Inject constructor() : PackThemeSuggested
                        if (packThemeData.suggestedChipThemePackInfo.title.isNotEmpty()) {
                            view.visibility = View.VISIBLE
                            view.hideSuggestedChip = false
                        } else {
                            view.visibility = View.GONE
                            view.hideSuggestedChip = true
                        }
                        view.suggestedChipText.text = packThemeData.suggestedChipThemePackInfo.title
                    }
@@ -94,4 +116,30 @@ class ThemePickerSuggestedEntryBinder @Inject constructor() : PackThemeSuggested
            lifecycleOwner = lifecycleOwner,
        )
    }

    private suspend fun isActivityAvailable(context: Context, intent: Intent): Boolean {
        val packageManager: PackageManager = context.packageManager
        val activities =
            packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
        return activities.isNotEmpty()
    }

    private fun showNoPackThemeIntentErrorMessage(
        lifecycleOwner: LifecycleOwner,
        view: View,
        optionsViewModel: ThemePickerCustomizationOptionsViewModel,
    ) {
        lifecycleOwner.lifecycleScope.launch {
            Toast.makeText(
                    view.context,
                    optionsViewModel.packThemeViewModel.noAppErrorMessage,
                    Toast.LENGTH_SHORT,
                )
                .show()
        }
    }

    companion object {
        private const val BACKGROUND_CONTEXT = "backgroundContext"
    }
}