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.IActivityManager;
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 public class PipUtils {
31 
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      */
getTopPinnedActivity(Context context, IActivityManager activityManager)38     public static Pair<ComponentName, Integer> getTopPinnedActivity(Context context,
39             IActivityManager activityManager) {
40         try {
41             final String sysUiPackageName = context.getPackageName();
42             final StackInfo pinnedStackInfo =
43                     activityManager.getStackInfo(WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED);
44             if (pinnedStackInfo != null && pinnedStackInfo.taskIds != null &&
45                     pinnedStackInfo.taskIds.length > 0) {
46                 for (int i = pinnedStackInfo.taskNames.length - 1; i >= 0; i--) {
47                     ComponentName cn = ComponentName.unflattenFromString(
48                             pinnedStackInfo.taskNames[i]);
49                     if (cn != null && !cn.getPackageName().equals(sysUiPackageName)) {
50                         return new Pair<>(cn, pinnedStackInfo.taskUserIds[i]);
51                     }
52                 }
53             }
54         } catch (RemoteException e) {
55             Log.w(TAG, "Unable to get pinned stack.");
56         }
57         return new Pair<>(null, 0);
58     }
59 }
60