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