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

Commit 283eb1e1 authored by George Lin's avatar George Lin Committed by Android (Google) Code Review
Browse files

Merge "Fix the grid option drawable" into main

parents 7dbfcf81 679ce6e4
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -161,10 +161,12 @@ constructor(
    }

    override fun getGridOptionDrawable(iconId: Int): Drawable? {
        val launcherPackageName =
            context.getString(com.android.themepicker.R.string.launcher_overlayable_package)
        try {
            val drawable =
                ResourcesCompat.getDrawable(
                    context.packageManager.getResourcesForApplication(APP_RESOURCES_PACKAGE_NAME),
                    context.packageManager.getResourcesForApplication(launcherPackageName),
                    iconId,
                    /* theme = */ null,
                )
@@ -172,7 +174,7 @@ constructor(
        } catch (exception: Resources.NotFoundException) {
            Log.w(
                TAG,
                "Unable to find drawable resource from package $APP_RESOURCES_PACKAGE_NAME with resource ID $iconId",
                "Unable to find drawable resource from package $launcherPackageName with resource ID $iconId",
            )
            return null
        }
@@ -194,7 +196,5 @@ constructor(
        const val COL_IS_DEFAULT: String = "is_default"
        const val COL_PATH: String = "path"
        const val KEY_GRID_ICON_ID: String = "grid_icon_id"
        private const val APP_RESOURCES_PACKAGE_NAME: String =
            "com.google.android.apps.nexuslauncher"
    }
}
+25 −0
Original line number Diff line number Diff line
@@ -16,12 +16,16 @@
package com.android.customization.model.grid;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.res.ResourcesCompat;
import androidx.lifecycle.LiveData;

import com.android.customization.model.CustomizationManager;
@@ -123,4 +127,25 @@ public class GridOptionsManager implements CustomizationManager<GridOption> {
    public LiveData<Object> getOptionChangeObservable(@Nullable Handler handler) {
        return mProvider.getOptionChangeObservable(handler);
    }

    /**
     * Get the grid option drawable.
     */
    @Nullable
    public Drawable getGridOptionDrawable(Context context, int iconId) {
        String launcherPackageName = context
                .getString(com.android.themepicker.R.string.launcher_overlayable_package);
        try {
            return ResourcesCompat.getDrawable(
                    context.getPackageManager().getResourcesForApplication(launcherPackageName),
                            iconId, null
                    );
        } catch (Resources.NotFoundException | PackageManager.NameNotFoundException nameNotFound) {
            Log.w(TAG, "Unable to find drawable resource from package "
                    + launcherPackageName
                    +  " with resource ID "
                    + iconId);
            return null;
        }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -410,6 +410,7 @@ constructor(
                    applicationScope = getApplicationCoroutineScope(),
                    repository =
                        GridRepositoryImpl(
                            appContext = appContext,
                            applicationScope = getApplicationCoroutineScope(),
                            manager = GridOptionsManager.getInstance(context),
                            backgroundDispatcher = bgDispatcher,
+12 −2
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@

package com.android.customization.picker.grid.data.repository

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.lifecycle.asFlow
import com.android.customization.model.CustomizationManager
import com.android.customization.model.CustomizationManager.Callback
@@ -51,9 +53,12 @@ interface GridRepository {
    fun clearSelectedOption()

    fun isSelectedOptionApplied(): Boolean

    fun getGridOptionDrawable(iconId: Int): Drawable?
}

class GridRepositoryImpl(
    private val appContext: Context,
    private val applicationScope: CoroutineScope,
    private val manager: GridOptionsManager,
    private val backgroundDispatcher: CoroutineDispatcher,
@@ -99,7 +104,7 @@ class GridRepositoryImpl(
                            continuation.resume(
                                GridOptionItemsModel.Error(
                                    throwable ?: Exception("Failed to load grid options!")
                                ),
                                )
                            )
                        }
                    },
@@ -114,6 +119,7 @@ class GridRepositoryImpl(
            name = option.title,
            rows = option.rows,
            cols = option.cols,
            iconId = option.gridIconId,
            isSelected =
                selectedOption
                    .map { it.key() }
@@ -170,7 +176,7 @@ class GridRepositoryImpl(
                        callback.onError(throwable)
                    }
                }
            } else callback
            } else callback,
        )
    }

@@ -184,6 +190,10 @@ class GridRepositoryImpl(

    override fun isSelectedOptionApplied() = selectedOption.value?.name == appliedOption?.name

    override fun getGridOptionDrawable(iconId: Int): Drawable? {
        return manager.getGridOptionDrawable(appContext, iconId)
    }

    private fun GridOption?.key(): String? {
        return if (this != null) "${cols}x${rows}" else null
    }
+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package com.android.customization.picker.grid.domain.interactor

import android.graphics.drawable.Drawable
import com.android.customization.model.CustomizationManager
import com.android.customization.picker.grid.data.repository.GridRepository
import com.android.customization.picker.grid.shared.model.GridOptionItemModel
@@ -94,6 +95,7 @@ class GridInteractor(
                            name = option.name,
                            cols = option.cols,
                            rows = option.rows,
                            iconId = option.iconId,
                            isSelected = option.isSelected,
                            onSelected = {
                                option.onSelected()
@@ -106,4 +108,6 @@ class GridInteractor(
            model
        }
    }

    fun getGridOptionDrawable(iconId: Int): Drawable? = repository.getGridOptionDrawable(iconId)
}
Loading