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 com.android.wm.shell.pip;
18 
19 import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
20 import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
21 
22 import android.app.ActivityTaskManager;
23 import android.app.ActivityTaskManager.RootTaskInfo;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.os.RemoteException;
27 import android.util.Log;
28 import android.util.Pair;
29 
30 /** A class that includes convenience methods. */
31 public class PipUtils {
32     private static final String TAG = "PipUtils";
33 
34     /**
35      * @return the ComponentName and user id of the top non-SystemUI activity in the pinned stack.
36      * The component name may be null if no such activity exists.
37      */
getTopPipActivity(Context context)38     public static Pair<ComponentName, Integer> getTopPipActivity(Context context) {
39         try {
40             final String sysUiPackageName = context.getPackageName();
41             final RootTaskInfo pinnedTaskInfo = ActivityTaskManager.getService().getRootTaskInfo(
42                     WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
43             if (pinnedTaskInfo != null && pinnedTaskInfo.childTaskIds != null
44                     && pinnedTaskInfo.childTaskIds.length > 0) {
45                 for (int i = pinnedTaskInfo.childTaskNames.length - 1; i >= 0; i--) {
46                     ComponentName cn = ComponentName.unflattenFromString(
47                             pinnedTaskInfo.childTaskNames[i]);
48                     if (cn != null && !cn.getPackageName().equals(sysUiPackageName)) {
49                         return new Pair<>(cn, pinnedTaskInfo.childTaskUserIds[i]);
50                     }
51                 }
52             }
53         } catch (RemoteException e) {
54             Log.w(TAG, "Unable to get pinned stack.");
55         }
56         return new Pair<>(null, 0);
57     }
58 }
59