1 /* 2 * Copyright (C) 2020 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 android.window; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.RequiresPermission; 22 import android.annotation.SuppressLint; 23 import android.annotation.TestApi; 24 import android.app.ActivityTaskManager; 25 import android.os.IBinder; 26 import android.os.RemoteException; 27 import android.util.Singleton; 28 import android.view.RemoteAnimationAdapter; 29 import android.view.SurfaceControl; 30 31 /** 32 * Base class for organizing specific types of windows like Tasks and DisplayAreas 33 * 34 * @hide 35 */ 36 @TestApi 37 public class WindowOrganizer { 38 39 /** 40 * Apply multiple WindowContainer operations at once. 41 * 42 * Note that using this API requires the caller to hold 43 * {@link android.Manifest.permission#MANAGE_ACTIVITY_TASKS}. 44 * 45 * @param t The transaction to apply. 46 */ 47 @RequiresPermission(value = android.Manifest.permission.MANAGE_ACTIVITY_TASKS) applyTransaction(@onNull WindowContainerTransaction t)48 public void applyTransaction(@NonNull WindowContainerTransaction t) { 49 try { 50 if (!t.isEmpty()) { 51 getWindowOrganizerController().applyTransaction(t); 52 } 53 } catch (RemoteException e) { 54 throw e.rethrowFromSystemServer(); 55 } 56 } 57 58 /** 59 * Apply multiple WindowContainer operations at once. 60 * 61 * Note that using this API requires the caller to hold 62 * {@link android.Manifest.permission#MANAGE_ACTIVITY_TASKS}. 63 * 64 * @param t The transaction to apply. 65 * @param callback This transaction will use the synchronization scheme described in 66 * BLASTSyncEngine.java. The SurfaceControl transaction containing the effects of this 67 * WindowContainer transaction will be passed to this callback when ready. 68 * @return An ID for the sync operation which will later be passed to transactionReady callback. 69 * This lets the caller differentiate overlapping sync operations. 70 */ 71 @RequiresPermission(value = android.Manifest.permission.MANAGE_ACTIVITY_TASKS) applySyncTransaction(@onNull WindowContainerTransaction t, @NonNull WindowContainerTransactionCallback callback)72 public int applySyncTransaction(@NonNull WindowContainerTransaction t, 73 @NonNull WindowContainerTransactionCallback callback) { 74 try { 75 return getWindowOrganizerController().applySyncTransaction(t, callback.mInterface); 76 } catch (RemoteException e) { 77 throw e.rethrowFromSystemServer(); 78 } 79 } 80 81 /** 82 * Starts a new transition, don't use this to start an already created one. 83 * @param type The type of the transition. This is ignored if a transitionToken is provided. 84 * @param t The set of window operations that are part of this transition. 85 * @return A token identifying the transition. This will be the same as transitionToken if it 86 * was provided. 87 * @hide 88 */ 89 @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) 90 @NonNull startNewTransition(int type, @Nullable WindowContainerTransaction t)91 public IBinder startNewTransition(int type, @Nullable WindowContainerTransaction t) { 92 try { 93 return getWindowOrganizerController().startNewTransition(type, t); 94 } catch (RemoteException e) { 95 throw e.rethrowFromSystemServer(); 96 } 97 } 98 99 /** 100 * Starts an already created transition. 101 * @param transitionToken An existing transition to start. 102 * @hide 103 */ 104 @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) startTransition(@onNull IBinder transitionToken, @Nullable WindowContainerTransaction t)105 public void startTransition(@NonNull IBinder transitionToken, 106 @Nullable WindowContainerTransaction t) { 107 try { 108 getWindowOrganizerController().startTransition(transitionToken, t); 109 } catch (RemoteException e) { 110 throw e.rethrowFromSystemServer(); 111 } 112 } 113 114 /** 115 * Finishes a running transition. 116 * @param transitionToken The transition to finish. Can't be null. 117 * @param t A set of window operations to apply before finishing. 118 * 119 * @hide 120 */ 121 @SuppressLint("ExecutorRegistration") 122 @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) finishTransition(@onNull IBinder transitionToken, @Nullable WindowContainerTransaction t)123 public void finishTransition(@NonNull IBinder transitionToken, 124 @Nullable WindowContainerTransaction t) { 125 try { 126 getWindowOrganizerController().finishTransition(transitionToken, t); 127 } catch (RemoteException e) { 128 throw e.rethrowFromSystemServer(); 129 } 130 } 131 132 /** 133 * Register an ITransitionPlayer to handle transition animations. 134 * @hide 135 */ 136 @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) registerTransitionPlayer(@onNull ITransitionPlayer player)137 public void registerTransitionPlayer(@NonNull ITransitionPlayer player) { 138 try { 139 getWindowOrganizerController().registerTransitionPlayer(player); 140 } catch (RemoteException e) { 141 throw e.rethrowFromSystemServer(); 142 } 143 } 144 145 /** 146 * Unregister a previously-registered ITransitionPlayer. 147 * @hide 148 */ 149 @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) unregisterTransitionPlayer(@onNull ITransitionPlayer player)150 public void unregisterTransitionPlayer(@NonNull ITransitionPlayer player) { 151 try { 152 getWindowOrganizerController().unregisterTransitionPlayer(player); 153 } catch (RemoteException e) { 154 throw e.rethrowFromSystemServer(); 155 } 156 } 157 158 /** 159 * @see TransitionMetrics 160 * @hide 161 */ getTransitionMetricsReporter()162 public static ITransitionMetricsReporter getTransitionMetricsReporter() { 163 try { 164 return getWindowOrganizerController().getTransitionMetricsReporter(); 165 } catch (RemoteException e) { 166 throw e.rethrowFromSystemServer(); 167 } 168 } 169 170 /** 171 * Use WM's transaction-queue instead of Shell's independent one. This is necessary 172 * if WM and Shell need to coordinate transactions (eg. for shell transitions). 173 * @return true if successful, false otherwise. 174 * @hide 175 */ shareTransactionQueue()176 public boolean shareTransactionQueue() { 177 final IBinder wmApplyToken; 178 try { 179 wmApplyToken = getWindowOrganizerController().getApplyToken(); 180 } catch (RemoteException e) { 181 throw e.rethrowFromSystemServer(); 182 } 183 if (wmApplyToken == null) { 184 return false; 185 } 186 SurfaceControl.Transaction.setDefaultApplyToken(wmApplyToken); 187 return true; 188 } 189 getWindowOrganizerController()190 static IWindowOrganizerController getWindowOrganizerController() { 191 return IWindowOrganizerControllerSingleton.get(); 192 } 193 194 private static final Singleton<IWindowOrganizerController> IWindowOrganizerControllerSingleton = 195 new Singleton<IWindowOrganizerController>() { 196 @Override 197 protected IWindowOrganizerController create() { 198 try { 199 return ActivityTaskManager.getService().getWindowOrganizerController(); 200 } catch (RemoteException e) { 201 return null; 202 } 203 } 204 }; 205 } 206