1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.launcher3.tapl; 18 19 import static androidx.test.InstrumentationRegistry.getInstrumentation; 20 import static androidx.test.InstrumentationRegistry.getTargetContext; 21 22 import android.app.Instrumentation; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.pm.ActivityInfo; 27 import android.content.pm.ResolveInfo; 28 import android.content.res.Resources; 29 30 import java.util.List; 31 32 public class TestHelpers { 33 34 private static Boolean sIsInLauncherProcess; 35 isInLauncherProcess()36 public static boolean isInLauncherProcess() { 37 if (sIsInLauncherProcess == null) { 38 sIsInLauncherProcess = initIsInLauncherProcess(); 39 } 40 return sIsInLauncherProcess; 41 } 42 initIsInLauncherProcess()43 private static boolean initIsInLauncherProcess() { 44 ActivityInfo info = getLauncherInMyProcess(); 45 46 // If we are in the same process, we can instantiate the class name. 47 try { 48 Class launcherClazz = Class.forName("com.android.launcher3.Launcher"); 49 return launcherClazz.isAssignableFrom(Class.forName(info.name)); 50 } catch (Exception e) { 51 return false; 52 } 53 } 54 getHomeIntentInPackage(Context context)55 public static Intent getHomeIntentInPackage(Context context) { 56 return new Intent(Intent.ACTION_MAIN) 57 .addCategory(Intent.CATEGORY_HOME) 58 .setPackage(context.getPackageName()) 59 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 60 } 61 getLauncherInMyProcess()62 public static ActivityInfo getLauncherInMyProcess() { 63 Instrumentation instrumentation = getInstrumentation(); 64 if (instrumentation.getTargetContext() == null) { 65 return null; 66 } 67 68 List<ResolveInfo> launchers = getTargetContext().getPackageManager() 69 .queryIntentActivities(getHomeIntentInPackage(getTargetContext()), 0); 70 if (launchers.size() != 1) { 71 return null; 72 } 73 return launchers.get(0).activityInfo; 74 } 75 getOverviewPackageName()76 public static String getOverviewPackageName() { 77 Resources res = Resources.getSystem(); 78 int id = res.getIdentifier("config_recentsComponentName", "string", "android"); 79 if (id != 0) { 80 return ComponentName.unflattenFromString(res.getString(id)).getPackageName(); 81 } 82 return "com.android.systemui"; 83 } 84 } 85