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

Commit c2d2de08 authored by Matías Hernández's avatar Matías Hernández
Browse files

Changes to icon picker for reusability in "add mode" flow

* Don't finish the fragment from the controller (ugh!) instead just report the selected icon via a listener.
* Highlight the selected icon in the list.
* Cache the icon drawables (since we're using selectors for the colors, we don't need to swap them, one per icon resource id is enough).
* Improved the tests a bit too.

Bug: 333901673
Bug: 326442408
Test: ates
Flag: android.app.modes_ui
Change-Id: Ib2ec7a7e3ed99b13f9264aa6f7c209ee3f6967a0
parent 67d977b7
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
<!--
  ~ Copyright (C) 2024 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<!-- Color list for the background in each item in the icon picker list. -->
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android">

    <item android:state_pressed="true" android:color="?androidprv:attr/materialColorPrimary" />
    <item android:state_selected="true" android:color="?androidprv:attr/materialColorPrimary" />
    <item android:color="?androidprv:attr/materialColorSecondaryContainer" />
</selector>
+25 −0
Original line number Diff line number Diff line
<!--
  ~ Copyright (C) 2024 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<!-- Color list for the icon in each item in the icon picker list. -->
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android">

    <item android:state_pressed="true" android:color="?androidprv:attr/materialColorOnPrimary" />
    <item android:state_selected="true" android:color="?androidprv:attr/materialColorOnPrimary" />
    <item android:color="?androidprv:attr/materialColorOnSecondaryContainer" />
</selector>
+7 −5
Original line number Diff line number Diff line
@@ -50,14 +50,16 @@ class IconUtil {

    /**
     * Returns a variant of the supplied {@code icon} to be used in the icon picker. The inner icon
     * is 36x36dp and it's contained into a circle of diameter 54dp.
     * is 36x36dp and it's contained into a circle of diameter 54dp. It's also set up so that
     * selection and pressed states are represented in the color.
     */
    static Drawable makeIconCircle(@NonNull Context context, @NonNull Drawable icon) {
        ShapeDrawable background = new ShapeDrawable(new OvalShape());
        background.getPaint().setColor(Utils.getColorAttrDefaultColor(context,
                com.android.internal.R.attr.materialColorSecondaryContainer));
        icon.setTint(Utils.getColorAttrDefaultColor(context,
                com.android.internal.R.attr.materialColorOnSecondaryContainer));
        background.setTintList(
                context.getColorStateList(R.color.modes_icon_picker_item_background));
        icon = icon.mutate();
        icon.setTintList(
                context.getColorStateList(R.color.modes_icon_picker_item_icon));

        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { background, icon });

+5 −6
Original line number Diff line number Diff line
@@ -18,13 +18,14 @@ package com.android.settings.notification.modes;

import android.app.AlertDialog;
import android.app.Application;
import android.app.AutomaticZenRule;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;

import com.android.settings.R;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -72,11 +73,9 @@ public class ZenModeFragment extends ZenModeFragmentBase {

        // Set title for the entire screen
        ZenMode mode = getMode();
        AutomaticZenRule azr = getAZR();
        if (mode == null || azr == null) {
            return;
        if (mode != null) {
            requireActivity().setTitle(mode.getName());
        }
        getActivity().setTitle(azr.getName());
    }

    @Override
@@ -92,7 +91,7 @@ public class ZenModeFragment extends ZenModeFragmentBase {
    }

    @Override
    protected boolean onOptionsItemSelected(MenuItem item, ZenMode zenMode) {
    protected boolean onOptionsItemSelected(MenuItem item, @NonNull ZenMode zenMode) {
        switch (item.getItemId()) {
            case DELETE_MODE:
                new AlertDialog.Builder(mContext)
+12 −9
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.settings.notification.modes;

import static android.provider.Settings.EXTRA_AUTOMATIC_ZEN_RULE_ID;

import android.app.AutomaticZenRule;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
@@ -34,7 +33,10 @@ import com.android.settings.R;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.notification.modes.ZenMode;

import com.google.common.base.Preconditions;

import java.util.List;
import java.util.function.Consumer;

/**
 * Base class for Settings pages used to configure individual modes.
@@ -175,14 +177,15 @@ abstract class ZenModeFragmentBase extends ZenModesFragmentBase {
        return mZenMode;
    }

    /**
     * Get AutomaticZenRule associated with current mode data, or null if it doesn't exist.
     */
    @Nullable
    public AutomaticZenRule getAZR() {
        if (mZenMode == null) {
            return null;
    protected final boolean saveMode(Consumer<ZenMode> updater) {
        Preconditions.checkState(mBackend != null);
        ZenMode mode = mZenMode;
        if (mode == null) {
            Log.wtf(TAG, "Cannot save mode, it hasn't been loaded (" + getClass() + ")");
            return false;
        }
        return mZenMode.getRule();
        updater.accept(mode);
        mBackend.updateMode(mode);
        return true;
    }
}
Loading