1 2 package com.android.testing.uiautomation; 3 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.pm.IPackageManager; 7 import android.content.pm.ResolveInfo; 8 import android.os.RemoteException; 9 import android.os.ServiceManager; 10 import android.os.SystemClock; 11 import android.util.Log; 12 13 import java.util.List; 14 import java.util.Map; 15 import java.util.TreeMap; 16 17 public class UiTestHelper { 18 19 private static final String LOGTAG = "UiTestHelper"; 20 21 private static final long DEFAULT_WAIT_TIMEOUT = 5000; 22 23 private static final long POLL_INTERVAL = 250; 24 25 private Context mContext; 26 private AutomationProvider mProvider; 27 private Map<String, Intent> mLauncherAppList; 28 UiTestHelper(Context context, AutomationProvider provider)29 public UiTestHelper(Context context, AutomationProvider provider) { 30 mContext = context; 31 mProvider = provider; 32 reloadLauncherAppList(); 33 } 34 waitForWindow(String title)35 public boolean waitForWindow(String title) { 36 return waitForWindow(title, DEFAULT_WAIT_TIMEOUT); 37 } 38 waitForWindow(String title, long timeout)39 public boolean waitForWindow(String title, long timeout) { 40 long startMills = SystemClock.uptimeMillis(); 41 boolean titleMatch = false; 42 while (SystemClock.uptimeMillis() - startMills < timeout) { 43 try { 44 titleMatch = title.equals(mProvider.getCurrentActivityName()); 45 } catch (RemoteException e) { 46 Log.e(LOGTAG, "failed to get current activity name", e); 47 break; 48 } 49 if (titleMatch) 50 break; 51 try { 52 Thread.sleep(POLL_INTERVAL); 53 } catch (InterruptedException e) { 54 } 55 } 56 return titleMatch; 57 } 58 reloadLauncherAppList()59 public void reloadLauncherAppList() { 60 mLauncherAppList = getLauncherAppList(); 61 } 62 launchApplication(String appName)63 public boolean launchApplication(String appName) { 64 Intent intent = mLauncherAppList.get(appName); 65 if (intent == null) 66 return false; 67 mContext.startActivity(intent); 68 return true; 69 } 70 getLauncherAppList()71 private Map<String, Intent> getLauncherAppList() { 72 final Intent queryIntent = new Intent(); 73 final Map<String, Intent> launchIntents = new TreeMap<String, Intent>(); 74 // get package manager and query pm for intents declared by apps as 75 // launcher and main 76 // basically those shown as icons in all apps screen 77 IPackageManager mPm = IPackageManager.Stub 78 .asInterface(ServiceManager.getService("package")); 79 queryIntent.addCategory(Intent.CATEGORY_LAUNCHER); 80 queryIntent.setAction(Intent.ACTION_MAIN); 81 final List<ResolveInfo> results; 82 try { 83 results = mPm.queryIntentActivities(queryIntent, null, 0); 84 } catch (RemoteException e) { 85 e.printStackTrace(); 86 return null; 87 } 88 for (ResolveInfo info : results) { 89 Intent tmpIntent = new Intent(queryIntent); 90 tmpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 91 tmpIntent.setClassName(info.activityInfo.applicationInfo.packageName, 92 info.activityInfo.name); 93 String appName = info.activityInfo.loadLabel(mContext.getPackageManager()).toString(); 94 launchIntents.put(appName, tmpIntent); 95 } 96 return launchIntents; 97 } 98 } 99