1 /* 2 * Copyright (C) 2021 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.bedstead.nene.activities; 18 19 import static android.Manifest.permission.REAL_GET_TASKS; 20 21 import android.app.ActivityManager; 22 23 import com.android.bedstead.nene.TestApis; 24 import com.android.bedstead.nene.annotations.Experimental; 25 import com.android.bedstead.nene.packages.ComponentReference; 26 import com.android.bedstead.nene.permissions.PermissionContext; 27 28 import java.util.List; 29 30 public final class Activities { 31 32 private final TestApis mTestApis; 33 Activities(TestApis testApis)34 public Activities(TestApis testApis) { 35 mTestApis = testApis; 36 } 37 38 /** 39 * Wrap the given {@link NeneActivity} to use Nene APIs. 40 */ wrap(NeneActivity activity)41 public Activity<NeneActivity> wrap(NeneActivity activity) { 42 return new Activity<>(mTestApis, activity, activity); 43 } 44 45 /** 46 * Wrap the given {@link NeneActivity} subclass to use Nene APIs. 47 */ wrap(Class<E> clazz, E activity)48 public <E extends NeneActivity> Activity<E> wrap(Class<E> clazz, E activity) { 49 return new Activity<>(mTestApis, activity, activity); 50 } 51 52 /** 53 * Wrap the given {@link android.app.Activity} to use Nene APIs. 54 */ wrap(android.app.Activity activity)55 public LocalActivity wrap(android.app.Activity activity) { 56 return new LocalActivity(mTestApis, activity); 57 } 58 59 /** 60 * Get the {@link ComponentReference} of the activity currently in the foreground. 61 */ 62 @Experimental foregroundActivity()63 public ComponentReference foregroundActivity() { 64 try (PermissionContext p = mTestApis.permissions().withPermission(REAL_GET_TASKS)) { 65 ActivityManager activityManager = 66 mTestApis.context().instrumentedContext().getSystemService( 67 ActivityManager.class); 68 List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(1); 69 if (runningTasks.isEmpty()) { 70 return null; 71 } 72 73 return new ComponentReference(mTestApis, runningTasks.get(0).topActivity); 74 } 75 } 76 77 /** 78 * Return the current state of task locking. The three possible outcomes 79 * are {@link ActivityManager#LOCK_TASK_MODE_NONE}, 80 * {@link ActivityManager#LOCK_TASK_MODE_LOCKED} 81 * and {@link ActivityManager#LOCK_TASK_MODE_PINNED}. 82 */ 83 @Experimental getLockTaskModeState()84 public int getLockTaskModeState() { 85 ActivityManager activityManager = 86 mTestApis.context().instrumentedContext().getSystemService( 87 ActivityManager.class); 88 89 return activityManager.getLockTaskModeState(); 90 } 91 } 92