/* * Copyright (C) 2021 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.systemui.util.concurrency; /** * Allows triggering methods based on a passed in id or message, generally on another thread. * * Messages sent on to this router must be processed in order. That is to say, if three * messages are sent with no delay, they must be processed in the order they were sent. Moreover, * if messages are sent with various delays, they must be processed in order of their delay. * * Messages can be passed by either a simple integer or an instance of a class. Unique integers are * considered unique messages. Unique message classes (not instances) are considered unique * messages. You can use message classes to pass extra data for processing to subscribers. * *
* // Three messages with three unique integer messages. * // They can be subscribed to independently. * router.sendMessage(0); * router.sendMessage(1); * router.sendMessage(2); * * // Three messages with two unique message classes. * // The first and third messages will be delivered to the same subscribers. * router.sendMessage(new Foo(0)); * router.sendMessage(new Bar(1)); * router.sendMessage(new Foo(2)); ** * The number of unique ids and message types used should be relatively constrained. Construct * a custom message-class and put unique, per-message data inside of it. */ public interface MessageRouter { /** * Alerts any listeners subscribed to the passed in id. * * The number of unique ids used should be relatively constrained - used to identify the type * of message being sent. If unique information needs to be passed with each call, use * {@link #sendMessage(Object)}. * * @param id An identifier for the message */ default void sendMessage(int id) { sendMessageDelayed(id, 0); } /** * Alerts any listeners subscribed to the passed in message. * * The number of message types used should be relatively constrained. If no unique information * needs to be passed in, you can simply use {@link #sendMessage(int)}} which takes an integer * instead of a unique class type. * * The class of the passed in object will be used to router the message. * * @param data A message containing extra data for processing. */ default void sendMessage(Object data) { sendMessageDelayed(data, 0); } /** * Alerts any listeners subscribed to the passed in id in the future. * * The number of unique ids used should be relatively constrained - used to identify the type * of message being sent. If unique information needs to be passed with each call, use * {@link #sendMessageDelayed(Object, long)}. * * @param id An identifier for the message * @param delayMs Number of milliseconds to wait before alerting. */ void sendMessageDelayed(int id, long delayMs); /** * Alerts any listeners subscribed to the passed in message in the future. * * The number of message types used should be relatively constrained. If no unique information * needs to be passed in, you can simply use {@link #sendMessageDelayed(int, long)} which takes * an integer instead of a unique class type. * * @param data A message containing extra data for processing. * @param delayMs Number of milliseconds to wait before alerting. */ void sendMessageDelayed(Object data, long delayMs); /** * Cancel all unprocessed messages for a given id. * * If a message has multiple listeners and one of those listeners has been alerted, the other * listeners that follow it may also be alerted. This is only guaranteed to cancel messages * that are still queued. * * @param id The message id to cancel. */ void cancelMessages(int id); /** * Cancel all unprocessed messages for a given message type. * * If a message has multiple listeners and one of those listeners has been alerted, the other * listeners that follow it may also be alerted. This is only guaranteed to cancel messages * that are still queued. * * @param messageType The class of the message to cancel */