Show icons for apps bypassing DND

(Several things pending, such as the +n icon and the correct pending icons, but it's a start).

Test: atest com.android.settings.notification.modes
Bug: 346551087
Flag: android.app.modes_ui
Change-Id: Ifd2ab6a8bb447739dc8ffe400c3960779d477fd6
This commit is contained in:
Matías Hernández
2024-07-15 17:47:21 +02:00
parent fe712d3ea3
commit 0ebc865c5e
15 changed files with 832 additions and 59 deletions

View File

@@ -0,0 +1,98 @@
/*
* 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 android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import androidx.annotation.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
/**
* A set of icons to be displayed in a {@link CircularIconsPreference}
*
* @param <T> The type of the items in the set. Can be an arbitrary type, the only requirement
* being that the {@code drawableLoader} supplied to the constructor is able to produce
* a {@link Drawable} from it (for example a resource id, a Content Uri, etc).
*/
class CircularIconSet<T> {
@VisibleForTesting // Can be set by tests, before creating instances.
static ExecutorService sExecutorService = Executors.newCachedThreadPool();
static final CircularIconSet<?> EMPTY = new CircularIconSet<>(ImmutableList.of(),
unused -> new ColorDrawable(Color.BLACK));
private final ImmutableList<T> mItems;
private final Function<T, Drawable> mDrawableLoader;
private final ListeningExecutorService mBackgroundExecutor;
private final ConcurrentHashMap<T, Drawable> mCachedIcons;
CircularIconSet(List<T> items, Function<T, Drawable> drawableLoader) {
mItems = ImmutableList.copyOf(items);
mDrawableLoader = drawableLoader;
mBackgroundExecutor = MoreExecutors.listeningDecorator(sExecutorService);
mCachedIcons = new ConcurrentHashMap<>();
}
int size() {
return mItems.size();
}
/**
* Loads all icons from the set, using the supplied {@code drawableLoader}, in a background
* thread.
*/
List<ListenableFuture<Drawable>> getIcons() {
return getIcons(Integer.MAX_VALUE);
}
/**
* Loads up to {@code maxSize} icons from the set, using the supplied {@code drawableLoader}, in
* a background thread.
*/
List<ListenableFuture<Drawable>> getIcons(int maxNumber) {
return mItems.stream().limit(maxNumber)
.map(this::loadIcon)
.toList();
}
private ListenableFuture<Drawable> loadIcon(T item) {
return mBackgroundExecutor.submit(() -> {
if (mCachedIcons.containsKey(item)) {
return mCachedIcons.get(item);
}
Drawable drawable = mDrawableLoader.apply(item);
if (drawable != null) {
mCachedIcons.put(item, drawable);
}
return drawable;
});
}
}