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