diff --git a/firebase-messaging-directboot/CHANGELOG.md b/firebase-messaging-directboot/CHANGELOG.md index 459f7ec15b7..7ab2c5f2753 100644 --- a/firebase-messaging-directboot/CHANGELOG.md +++ b/firebase-messaging-directboot/CHANGELOG.md @@ -1,5 +1,8 @@ # Unreleased +# 23.1.1 +* [changed] Removed unused classes. + # 23.1.0 * [changed] Internal changes to ensure functionality alignment with other SDK releases. diff --git a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/ExecutorFactory.java b/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/ExecutorFactory.java deleted file mode 100644 index 6e801ce3227..00000000000 --- a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/ExecutorFactory.java +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright 2020 Google LLC -// -// 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.google.firebase.messaging.directboot.threads; - -import com.google.errorprone.annotations.CompileTimeConstant; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; - -/** - * Factory to work as a drop-in replacement for {@link java.util.concurrent.Executors}. - * - * - * - *

One divergence from Executors is that unused threads are allowed to time out after a period of - * inactivity, so subsequent semi-cold starts may experience a tiny bit of latency. It is not - * expected to be a significant performance concern, but will save significant memory. - */ -public interface ExecutorFactory { - - /** - * Creates a thread pool that creates new threads as needed, but will reuse previously constructed - * threads when they are available. - * - *

Drop-in replacement for {@link java.util.concurrent.Executors#newCachedThreadPool()}. - * - * @param priority see {@link ThreadPriority}. - */ - public ExecutorService newThreadPool(ThreadPriority priority); - - /** - * Creates a thread pool that creates new threads as needed, but will reuse previously constructed - * threads when they are available. - * - *

Drop-in replacement for {@link - * java.util.concurrent.Executors#newCachedThreadPool(ThreadFactory)}. - * - * @param threadFactory the factory to use when creating new threads. - * @param priority see {@link ThreadPriority}. - */ - public ExecutorService newThreadPool(ThreadFactory threadFactory, ThreadPriority priority); - - /** - * Creates a thread pool that creates new threads as needed, but will reuse previously constructed - * threads when they are available. - * - *

Drop-in replacement for {@link java.util.concurrent.Executors#newFixedThreadPool(int)}. - * - * @param maxConcurrency at most this number of tasks will be executed concurrently. Overflow - * tasks will be placed into an unbounded queue. - * @param priority see {@link ThreadPriority}. - */ - public ExecutorService newThreadPool(int maxConcurrency, ThreadPriority priority); - - /** - * Creates a thread pool that creates new threads as needed, but will reuse previously constructed - * threads when they are available. - * - *

Drop-in replacement for {@link java.util.concurrent.Executors#newFixedThreadPool(int, - * ThreadFactory)}. - * - * @param maxConcurrency at most this number of tasks will be executed concurrently. Overflow - * tasks will be placed into an unbounded queue. - * @param threadFactory the factory to use when creating new threads. - * @param priority see {@link ThreadPriority}. - */ - public ExecutorService newThreadPool( - int maxConcurrency, ThreadFactory threadFactory, ThreadPriority priority); - - /** - * Creates an executor that mimics a single-threaded executor, in the sense that operations can - * happen at most one-at-a-time. - * - *

Drop-in replacement for {@link java.util.concurrent.Executors#newSingleThreadExecutor()}. - * - * @param priority see {@link ThreadPriority}. - */ - public ExecutorService newSingleThreadExecutor(ThreadPriority priority); - - /** - * Creates an executor that mimics a single-threaded executor, in the sense that operations can - * happen at most one-at-a-time. - * - *

Drop-in replacement for {@link - * java.util.concurrent.Executors#newSingleThreadExecutor(ThreadFactory)}. - * - * @param threadFactory the factory to use when creating new threads. - * @param priority see {@link ThreadPriority}. - */ - public ExecutorService newSingleThreadExecutor( - ThreadFactory threadFactory, ThreadPriority priority); - - /** - * Creates a ScheduledThreadPool that allows executing tasks in the future. If you don't require - * this functionality, just use {@link #newCachedThreadPool(int)}. - * - *

WARNING: Do not leak these, since these never terminate their threads. - * - *

Drop-in replacement for {@link java.util.concurrent.Executors#newScheduledThreadPool(int)}. - * - * @param maxConcurrency at most this number of tasks will be executed concurrently. Overflow - * tasks will be placed into an unbounded queue. - * @param priority see {@link ThreadPriority}. - */ - public ScheduledExecutorService newScheduledThreadPool( - int maxConcurrency, ThreadPriority priority); - - /** - * Creates a ScheduledThreadPool that allows executing tasks in the future. If you don't require - * this functionality, just use {@link #newCachedThreadPool(int)}. - * - *

WARNING: Do not leak these, since these never terminate their threads. - * - *

Drop-in replacement for {@link java.util.concurrent.Executors#newScheduledThreadPool(int, - * ThreadFactory)}. - * - * @param maxConcurrency at most this number of tasks will be executed concurrently. Overflow - * tasks will be placed into an unbounded queue. - * @param threadFactory the factory to use when creating new threads. - * @param priority see {@link ThreadPriority}. - */ - public ScheduledExecutorService newScheduledThreadPool( - int maxConcurrency, ThreadFactory threadFactory, ThreadPriority priority); - - /** - * Executes a one-off task where previously {@code new Thread()} may have been used. - * - *

Note that this may use a global, unlimited thread pool. Thus, threads should not generally - * try to manipulate the thread priority or name within the Runnable. - * - * @param moduleName name of the module - * @param name name of the thread; only sometimes honored - * @param priority see {@link ThreadPriority}. - * @param runnable the Runnable to run - */ - public void executeOneOff( - @CompileTimeConstant String moduleName, - @CompileTimeConstant String name, - ThreadPriority priority, - Runnable runnable); - - /** - * Executes a one-off task where previously {@code new Thread()} may have been used. - * - *

This returns a Future to allow you to query the state; you can use {@code Future.isDone()} - * instead of {@code !Thread.isAlive())}, and {@code Future.get()} instead of {@code - * Thread.join()}. - * - * @param moduleName name of the module - * @param name name of the thread; only sometimes honored - * @param priority see {@link ThreadPriority}. - * @param runnable the Runnable to run - */ - public Future submitOneOff( - @CompileTimeConstant String moduleName, - @CompileTimeConstant String name, - ThreadPriority priority, - Runnable runnable); -} diff --git a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/PoolableExecutors.java b/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/PoolableExecutors.java deleted file mode 100644 index 431b127976a..00000000000 --- a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/PoolableExecutors.java +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright 2020 Google LLC -// -// 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.google.firebase.messaging.directboot.threads; - -import android.annotation.SuppressLint; -import androidx.annotation.NonNull; -import com.google.errorprone.annotations.CompileTimeConstant; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.FutureTask; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -/** - * Provider of the {@link ExecutorFactory} that should be used to create thread executors. - * - *

If a factory is not provided during initialization, then it provides a default implementation. - */ -// TODO(b/144941134): Remove nullness suppression. -@SuppressWarnings("nullness") -public class PoolableExecutors { - - private static final ExecutorFactory DEFAULT_INSTANCE = new DefaultExecutorFactory(); - private static volatile ExecutorFactory instance = DEFAULT_INSTANCE; - - private PoolableExecutors() {} - - public static ExecutorFactory factory() { - return instance; - } - - /** A {@link ExecutorFactory} that creates the default thread executors. */ - private static class DefaultExecutorFactory implements ExecutorFactory { - - private static final long CORE_THREAD_TIMEOUT_SECS = 60L; - - /** {@inheritDoc} */ - @NonNull - @Override - // TODO(b/258424598): Migrate to go/firebase-android-executors - @SuppressLint("ThreadPoolCreation") - public ExecutorService newThreadPool(ThreadPriority priority) { - // NOTE: Cached threadpools automatically time out all threads. They have no concept of core - // threads; the queue blocks until a thread is started. - return Executors.unconfigurableExecutorService(Executors.newCachedThreadPool()); - } - - /** {@inheritDoc} */ - @NonNull - @Override - // TODO(b/258424598): Migrate to go/firebase-android-executors - @SuppressLint("ThreadPoolCreation") - public ExecutorService newThreadPool(ThreadFactory threadFactory, ThreadPriority priority) { - return Executors.unconfigurableExecutorService(Executors.newCachedThreadPool(threadFactory)); - } - - /** {@inheritDoc} */ - @NonNull - @Override - public ExecutorService newThreadPool(int maxConcurrency, ThreadPriority priority) { - // TODO(gboyer): Honor the thread priority even when no factory is provided, by creating - // a factory that actually sets priority. - // TODO(gboyer): Add a @CompileTimeConstant String name argument; this will replace almost - // all uses of the ThreadFactory version, and could later help with tracing. - return newThreadPool(maxConcurrency, Executors.defaultThreadFactory(), priority); - } - - /** {@inheritDoc} */ - @NonNull - @Override - // TODO(b/258424598): Migrate to go/firebase-android-executors - @SuppressLint("ThreadPoolCreation") - public ExecutorService newThreadPool( - int maxConcurrency, ThreadFactory threadFactory, ThreadPriority priority) { - ThreadPoolExecutor executor = - new ThreadPoolExecutor( - maxConcurrency, - maxConcurrency, - CORE_THREAD_TIMEOUT_SECS, - TimeUnit.SECONDS, - new LinkedBlockingQueue(), - threadFactory); - // This allows core threads to be terminated if they are not used; otherwise, these threads - // will forever suck up memory until the executor is shutdown or finalized. Generally, it is - // fairly fast in Android to start and stop threads, especially if this is limited to only - // happening once per minute. - executor.allowCoreThreadTimeOut(true); - return Executors.unconfigurableExecutorService(executor); - } - - /** {@inheritDoc} */ - @NonNull - @Override - public ExecutorService newSingleThreadExecutor(ThreadPriority priority) { - return newThreadPool(1, priority); - } - - /** {@inheritDoc} */ - @NonNull - @Override - public ExecutorService newSingleThreadExecutor( - ThreadFactory threadFactory, ThreadPriority priority) { - return newThreadPool(1, threadFactory, priority); - } - - /** {@inheritDoc} */ - @NonNull - @Override - // TODO(b/258424598): Migrate to go/firebase-android-executors - @SuppressLint("ThreadPoolCreation") - public ScheduledExecutorService newScheduledThreadPool( - int maxConcurrency, ThreadPriority priority) { - // NOTE: There's no way to make a scheduled executor stop threads automatically, because - // at least one thread is needed to waiting for future tasks. - // TODO(b/63802200): Consider wrapping this in a finalizable decorator that prevents runaway - // memory leaks from non-shutdown pools. - return Executors.unconfigurableScheduledExecutorService( - Executors.newScheduledThreadPool(maxConcurrency)); - } - - /** {@inheritDoc} */ - @NonNull - @Override - // TODO(b/258424598): Migrate to go/firebase-android-executors - @SuppressLint("ThreadPoolCreation") - public ScheduledExecutorService newScheduledThreadPool( - int maxConcurrency, ThreadFactory threadFactory, ThreadPriority priority) { - return Executors.unconfigurableScheduledExecutorService( - Executors.newScheduledThreadPool(maxConcurrency, threadFactory)); - } - - @Override - @NonNull - // TODO(b/258424598): Migrate to go/firebase-android-executors - @SuppressLint("ThreadPoolCreation") - public void executeOneOff( - @CompileTimeConstant final String moduleName, - @CompileTimeConstant final String name, - ThreadPriority priority, - Runnable runnable) { - new Thread(runnable, name).start(); - } - - @Override - @NonNull - // TODO(b/258424598): Migrate to go/firebase-android-executors - @SuppressLint("ThreadPoolCreation") - public Future submitOneOff( - @CompileTimeConstant final String moduleName, - @CompileTimeConstant final String name, - ThreadPriority priority, - Runnable runnable) { - FutureTask task = new FutureTask<>(runnable, null); - new Thread(task, name).start(); - return task; - } - } - - /** - * Installs the {@link ExecutorFactory} implementation; INTERNAL USE ONLY. - * - *

May only be called once. - * - *

Call this only via build-visibility-restricted PunchClockThreadsImplementationApi. - */ - static void installExecutorFactory(ExecutorFactory instance) { - if (PoolableExecutors.instance != DEFAULT_INSTANCE) { - throw new IllegalStateException("Trying to install an ExecutorFactory twice!"); - } - PoolableExecutors.instance = instance; - } -} diff --git a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/ThreadPriority.java b/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/ThreadPriority.java deleted file mode 100644 index dcb8eae39b6..00000000000 --- a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/ThreadPriority.java +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2020 Google LLC -// -// 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.google.firebase.messaging.directboot.threads; - -/** - * Commonly used thread priorities. - * - *

A true, but simple, enum is used for type safety to prevent mistakes like thread pool - * factories frequently take in integers that could be mistaken for an {@code IntDef} if the caller - * is not using Android Lint. The {@code @SimpleEnum} annotation is not used because of complex - * downstream usage, so minification by ProGuard depends on the app's usage. - */ -public enum ThreadPriority { - - /** - * Reduced performance but at greater power efficiency. If the user isn't staring at a spinner - * while you're doing your work, or if more CPU power won't help, or if you're just not sure what - * to choose - then this is for you! - * - *

Example use cases: - Doing auto-backup. - Cleaning up a database. - Computing contextual - * signals in the background. - Pretty much anything that's I/O bound (tasks that are mostly disk - * and network access). - A sane default. - * - *

Such tasks will be yield to more important work, but won't be starved, and will still be - * allowed to make forward progress. On some devices with big.LITTLE CPU architectures running - * Marshmallow, your tasks may be locked to the little cores, executing slower but drawing 3x less - * power for the same amount of work! (This is primarily true for Nexus 5x/6p devices, and any - * other OEM with specialized kernel logic; it is not true for Pixels.) - * - *

See b/25246923 for more. - */ - LOW_POWER, - - /** - * Better performance at the expense of power and execution of other tasks. If the user will be - * waiting for your work to complete (i.e. staring at a spinner), then this is a good choice. - * Often this is the case for handling client app requests. Otherwise, it's best to use {@link - * #LOW_POWER}. - * - *

Tasks with this priority will be allowed to contend with other urgent things across the - * system. On devices with big.LITTLE CPU architectures running Marshmallow, your tasks will be - * allowed to schedule on the big cores, executing faster but drawing 3x more power for the same - * amount of work. - * - *

Some tasks require even higher priority, such as some UI work or real-time audio. If needed, - * create your own thread(s) with an elevated priority. Please be careful! - * - *

See b/25246923 for more. - */ - HIGH_SPEED -} diff --git a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/package-info.java b/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/package-info.java deleted file mode 100644 index 23e006c76d4..00000000000 --- a/firebase-messaging-directboot/src/main/java/com/google/firebase/messaging/directboot/threads/package-info.java +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2022 Google LLC -// -// 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. - -/** @hide */ -package com.google.firebase.messaging.directboot.threads;