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

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

Follow-up improvements to zen mode icon picker

* Propose icons from com.android.internal.R instead of android.R (as a proof of concept) since this is what we'll be doing with the final icons.
* Provide the icon options as a parameter to the controller (can also be used for testing).
* Fix some Lint warnings.

Test: atest ZenModeIconPickerListPreferenceControllerTest
Bug: 333901673
Flag: android.app.modes_ui
Change-Id: I023eed0fd5719c5c4540f8d145335f60d088e7fb
parent 98a5dbfb
Loading
Loading
Loading
Loading
+28 −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.
 */

package com.android.settings.notification.modes;

import androidx.annotation.DrawableRes;

import com.google.common.collect.ImmutableList;

interface IconOptionsProvider {

    ImmutableList<IconInfo> getIcons();

    record IconInfo(@DrawableRes int resId, String description) { }
}
+48 −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.
 */

package com.android.settings.notification.modes;

import com.google.common.collect.ImmutableList;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;

/** Temporary implementation of {@link IconOptionsProvider} until we have the real list. */
class TempIconOptionsProvider implements IconOptionsProvider {
    @Override
    public ImmutableList<IconInfo> getIcons() {
        return ImmutableList.copyOf(
                Arrays.stream(com.android.internal.R.drawable.class.getFields())
                        .filter(
                                f -> Modifier.isStatic(f.getModifiers())
                                        && Modifier.isFinal(f.getModifiers())
                                        && f.getName().startsWith("ic_"))
                        .limit(20)
                        .map(f -> {
                            try {
                                return new IconInfo(f.getInt(null), f.getName());
                            } catch (IllegalAccessException e) {
                                return null;
                            }
                        })
                        .filter(Objects::nonNull)
                        .sorted(Comparator.comparing(IconInfo::resId).reversed())
                        .toList());
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -31,8 +31,6 @@ import com.android.settingslib.widget.ActionButtonsPreference;

class ZenModeActionsPreferenceController extends AbstractZenModePreferenceController {

    private ActionButtonsPreference mPreference;

    ZenModeActionsPreferenceController(@NonNull Context context, @NonNull String key,
            @Nullable ZenModesBackend backend) {
        super(context, key, backend);
+2 −1
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ public class ZenModeIconPickerFragment extends ZenModeFragmentBase {
                new ZenModeIconPickerIconPreferenceController(context, "current_icon", this,
                        mBackend),
                new ZenModeIconPickerListPreferenceController(context, "icon_list", this,
                        mBackend));
                        // TODO: b/333901673 - Replace with correct icon list.
                        new TempIconOptionsProvider(), mBackend));
    }
}
+10 −33
Original line number Diff line number Diff line
@@ -37,22 +37,18 @@ import com.android.settingslib.widget.LayoutPreference;

import com.google.common.collect.ImmutableList;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

class ZenModeIconPickerListPreferenceController extends AbstractZenModePreferenceController {

    private final DashboardFragment mFragment;
    private IconAdapter mAdapter;
    private final IconOptionsProvider mIconOptionsProvider;
    @Nullable private IconAdapter mAdapter;

    ZenModeIconPickerListPreferenceController(@NonNull Context context, @NonNull String key,
            @NonNull DashboardFragment fragment, @Nullable ZenModesBackend backend) {
            @NonNull DashboardFragment fragment, @NonNull IconOptionsProvider iconOptionsProvider,
            @Nullable ZenModesBackend backend) {
        super(context, key, backend);
        mFragment = fragment;
        mIconOptionsProvider = iconOptionsProvider;
    }

    @Override
@@ -64,24 +60,7 @@ class ZenModeIconPickerListPreferenceController extends AbstractZenModePreferenc
        }

        if (mAdapter == null) {
            // TODO: b/333901673 - This is just an example; replace with correct list.
            List<IconInfo> exampleIcons =
                    Arrays.stream(android.R.drawable.class.getFields())
                            .filter(
                                    f -> Modifier.isStatic(f.getModifiers())
                                            && f.getName().startsWith("ic_"))
                            .sorted(Comparator.comparing(Field::getName))
                            .limit(20)
                            .map(f -> {
                                try {
                                    return new IconInfo(f.getInt(null), f.getName());
                                } catch (IllegalAccessException e) {
                                    return null;
                                }
                            })
                            .filter(Objects::nonNull)
                            .toList();
            mAdapter = new IconAdapter(exampleIcons);
            mAdapter = new IconAdapter(mIconOptionsProvider);
        }
        RecyclerView recyclerView = pref.findViewById(R.id.icon_list);
        recyclerView.setLayoutManager(new AutoFitGridLayoutManager(mContext));
@@ -103,8 +82,6 @@ class ZenModeIconPickerListPreferenceController extends AbstractZenModePreferenc
        // Nothing to do, the current icon is shown in a different preference.
    }

    private record IconInfo(@DrawableRes int resId, String description) { }

    private class IconHolder extends RecyclerView.ViewHolder {

        private final ImageView mImageView;
@@ -114,7 +91,7 @@ class ZenModeIconPickerListPreferenceController extends AbstractZenModePreferenc
            mImageView = itemView.findViewById(R.id.icon_image_view);
        }

        void bindIcon(IconInfo icon) {
        void bindIcon(IconOptionsProvider.IconInfo icon) {
            mImageView.setImageDrawable(
                    IconUtil.makeIconCircle(itemView.getContext(), icon.resId()));
            itemView.setContentDescription(icon.description());
@@ -124,10 +101,10 @@ class ZenModeIconPickerListPreferenceController extends AbstractZenModePreferenc

    private class IconAdapter extends RecyclerView.Adapter<IconHolder> {

        private final ImmutableList<IconInfo> mIconResources;
        private final ImmutableList<IconOptionsProvider.IconInfo> mIconResources;

        private IconAdapter(List<IconInfo> iconOptions) {
            mIconResources = ImmutableList.copyOf(iconOptions);
        private IconAdapter(IconOptionsProvider iconOptionsProvider) {
            mIconResources = iconOptionsProvider.getIcons();
        }

        @NonNull
Loading