1 /* 2 * Copyright (C) 2019 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.car.ui.paintbooth.currentactivity; 18 19 import android.app.ActivityManager; 20 import android.app.ActivityManager.RunningTaskInfo; 21 import android.content.ComponentName; 22 import android.os.RemoteException; 23 import android.util.Log; 24 25 import androidx.annotation.NonNull; 26 27 import java.lang.reflect.Constructor; 28 import java.lang.reflect.InvocationTargetException; 29 import java.util.List; 30 31 /** 32 * Interface for wrappers around {@link android.app.ActivityTaskManager} so that we can exclude them 33 * from non-system builds like gradle and google3. 34 */ 35 interface ActivityTaskManager { 36 37 interface TaskStackListener { onTaskCreated(int taskId, ComponentName componentName)38 void onTaskCreated(int taskId, ComponentName componentName); 39 onTaskRemoved(int taskId)40 void onTaskRemoved(int taskId); 41 onTaskDescriptionChanged(ActivityManager.RunningTaskInfo taskInfo)42 void onTaskDescriptionChanged(ActivityManager.RunningTaskInfo taskInfo); 43 onTaskMovedToFront(ActivityManager.RunningTaskInfo taskInfo)44 void onTaskMovedToFront(ActivityManager.RunningTaskInfo taskInfo); 45 } 46 getTasks(int maxNum)47 List<RunningTaskInfo> getTasks(int maxNum) throws RemoteException; 48 registerTaskStackListener(TaskStackListener listener)49 void registerTaskStackListener(TaskStackListener listener) throws RemoteException; 50 unregisterTaskStackListener(TaskStackListener listener)51 void unregisterTaskStackListener(TaskStackListener listener) throws RemoteException; 52 53 final class ActivityTaskManagerStub implements ActivityTaskManager { 54 @Override getTasks(int num)55 public List<RunningTaskInfo> getTasks(int num) throws RemoteException { 56 throw new RemoteException("ActivityTaskManager is not available"); 57 } 58 59 @Override registerTaskStackListener(TaskStackListener listener)60 public void registerTaskStackListener(TaskStackListener listener) throws RemoteException { 61 throw new RemoteException("ActivityTaskManager is not available"); 62 } 63 64 @Override unregisterTaskStackListener(TaskStackListener listener)65 public void unregisterTaskStackListener(TaskStackListener listener) throws RemoteException { 66 throw new RemoteException("ActivityTaskManager is not available"); 67 } 68 } 69 70 @NonNull getService()71 static ActivityTaskManager getService() { 72 try { 73 Class<?> clazz = Class.forName( 74 "com.android.car.ui.paintbooth.currentactivity.ActivityTaskManagerImpl"); 75 Constructor<?> constructor = clazz.getDeclaredConstructor(); 76 constructor.setAccessible(true); 77 return (ActivityTaskManager) constructor.newInstance(); 78 } catch (ClassNotFoundException | IllegalAccessException | InstantiationException 79 | NoSuchMethodException | InvocationTargetException e) { 80 Log.e("paintbooth", "ActivityTaskManager is not available", e); 81 return new ActivityTaskManagerStub(); 82 } 83 } 84 } 85