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

Commit f6f90dd8 authored by bobyang's avatar bobyang
Browse files

Show the toast if the target activity is empty

Video: https://drive.google.com/file/d/1JgLZv9IDP9qrIuopBfJPe2KDTC4Z2ocU/view?usp=drive_link&resourcekey=0-CvBzAXqv6Bb1SDtqwIagWA
Flag: com.android.wallpaper.enable_pack_theme_entry
Bug: 433180644
Test: manually
Change-Id: Ie9ad502eb05a2877e432ce04edb565839450d188
parent 89a00d59
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 ->
@@ -523,10 +529,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({
@@ -624,6 +650,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,
@@ -779,5 +812,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"
    }
}