1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.animation; 18 19 import android.annotation.FloatRange; 20 import android.graphics.Rect; 21 import android.util.ArrayMap; 22 import android.view.SurfaceControl; 23 24 import java.util.Map; 25 import java.util.concurrent.Executor; 26 27 /** 28 * A composite {@link UIComponent.Transaction} that combines multiple other transactions for each ui 29 * type. 30 * @hide 31 */ 32 public class Transactions implements UIComponent.Transaction<UIComponent> { 33 private final Map<Class, UIComponent.Transaction> mTransactions = new ArrayMap<>(); 34 35 /** Register a transaction object for updating a certain {@link UIComponent} type. */ registerTransactionForClass( Class<T> clazz, UIComponent.Transaction transaction)36 public <T extends UIComponent> Transactions registerTransactionForClass( 37 Class<T> clazz, UIComponent.Transaction transaction) { 38 mTransactions.put(clazz, transaction); 39 return this; 40 } 41 getTransactionFor(UIComponent ui)42 private UIComponent.Transaction getTransactionFor(UIComponent ui) { 43 UIComponent.Transaction transaction = mTransactions.get(ui.getClass()); 44 if (transaction == null) { 45 transaction = ui.newTransaction(); 46 mTransactions.put(ui.getClass(), transaction); 47 } 48 return transaction; 49 } 50 51 @Override setAlpha(UIComponent ui, @FloatRange(from = 0.0, to = 1.0) float alpha)52 public Transactions setAlpha(UIComponent ui, @FloatRange(from = 0.0, to = 1.0) float alpha) { 53 getTransactionFor(ui).setAlpha(ui, alpha); 54 return this; 55 } 56 57 @Override setVisible(UIComponent ui, boolean visible)58 public Transactions setVisible(UIComponent ui, boolean visible) { 59 getTransactionFor(ui).setVisible(ui, visible); 60 return this; 61 } 62 63 @Override setBounds(UIComponent ui, Rect bounds)64 public Transactions setBounds(UIComponent ui, Rect bounds) { 65 getTransactionFor(ui).setBounds(ui, bounds); 66 return this; 67 } 68 69 @Override attachToTransitionLeash( UIComponent ui, SurfaceControl transitionLeash, int w, int h)70 public Transactions attachToTransitionLeash( 71 UIComponent ui, SurfaceControl transitionLeash, int w, int h) { 72 getTransactionFor(ui).attachToTransitionLeash(ui, transitionLeash, w, h); 73 return this; 74 } 75 76 @Override detachFromTransitionLeash( UIComponent ui, Executor executor, Runnable onDone)77 public Transactions detachFromTransitionLeash( 78 UIComponent ui, Executor executor, Runnable onDone) { 79 getTransactionFor(ui).detachFromTransitionLeash(ui, executor, onDone); 80 return this; 81 } 82 83 @Override commit()84 public void commit() { 85 mTransactions.values().forEach(UIComponent.Transaction::commit); 86 } 87 } 88