1/*------------------------------------------------------------------------- 2 * drawElements Quality Program Tester Core 3 * ---------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief TesterCore remote interface. 22 *//*--------------------------------------------------------------------*/ 23 24package com.drawelements.deqp.testercore; 25 26import android.app.ActivityManager; 27import android.content.Context; 28import android.content.ComponentName; 29import android.content.Intent; 30import android.content.pm.PackageManager; 31import android.os.Process; 32import android.os.Build; 33import android.text.TextUtils; 34import java.lang.System; 35 36import java.util.List; 37 38public class RemoteAPI { 39 40 private static final String LOG_TAG = "dEQP/RemoteAPI"; 41 42 private Context m_context; 43 private String m_processName; 44 private String m_logFileName; 45 46 public RemoteAPI (Context context, String logFileName) { 47 m_context = context; 48 m_processName = m_context.getPackageName() + ":testercore"; 49 m_logFileName = logFileName; 50 } 51 52 private ComponentName getDefaultTesterComponent () { 53 return new ComponentName(m_context.getPackageName(), "android.app.NativeActivity"); 54 } 55 56 private ComponentName getTesterComponent (String testerName) { 57 if (testerName != null && !testerName.equals("")) { 58 ComponentName component = ComponentName.unflattenFromString(testerName); 59 if (component == null) { 60 Log.e(LOG_TAG, "Invalid component name supplied (" + testerName + "), using default"); 61 component = getDefaultTesterComponent(); 62 } 63 return component; 64 } else { 65 return getDefaultTesterComponent(); 66 } 67 } 68 69 public boolean start (String testerName, String cmdLine, String caseList) { 70 71 // Choose component 72 ComponentName component = getTesterComponent(testerName); 73 74 Intent testIntent = new Intent(); 75 testIntent.setComponent(component); 76 testIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 77 78 // Add all data to cmdLine 79 cmdLine = testerName + " " + cmdLine + " --deqp-log-filename=" + m_logFileName; 80 81 if (caseList != null) 82 cmdLine = cmdLine + " --deqp-caselist=" + caseList; 83 84 cmdLine = cmdLine.replaceAll(" ", " "); 85 testIntent.putExtra("cmdLine", cmdLine); 86 87 // Try to resolve intent. 88 boolean isActivity = m_context.getPackageManager().resolveActivity(testIntent, 0) != null; 89 boolean isService = m_context.getPackageManager().resolveService(testIntent, 0) != null; 90 91 if (!isActivity && !isService) { 92 Log.e(LOG_TAG, "Can't resolve component as activity or service (" + component.flattenToString() + "), using default"); 93 component = getDefaultTesterComponent(); 94 } 95 96 Log.d(LOG_TAG, "Starting activity " + component.flattenToString()); 97 98 try { 99 if (isService) 100 m_context.startService(testIntent); 101 else 102 m_context.startActivity(testIntent); 103 } catch (Exception e) { 104 Log.e(LOG_TAG, "Failed to start tester", e); 105 return false; 106 } 107 108 return true; 109 } 110 111 public boolean kill () { 112 ActivityManager.RunningAppProcessInfo processInfo = findProcess(m_processName); 113 114 if (processInfo != null) { 115 Log.d(LOG_TAG, "Killing " + m_processName); 116 Process.killProcess(processInfo.pid); 117 return true; 118 } else { 119 return false; 120 } 121 122 // \todo [2010-11-01 pyry] Block until tester process is not running? 123 } 124 125 public boolean isRunning () { 126 return isProcessRunning(m_processName); 127 } 128 129 public String getLogFileName () { 130 return m_logFileName; 131 } 132 133 private ActivityManager.RunningAppProcessInfo findProcess (String name) { 134 ActivityManager activityMgr = (ActivityManager)m_context.getSystemService(Context.ACTIVITY_SERVICE); 135 List<ActivityManager.RunningAppProcessInfo> processes = activityMgr.getRunningAppProcesses(); 136 137 for (ActivityManager.RunningAppProcessInfo info : processes) { 138 // Log.v(LOG_TAG, "Found proc : " + info.processName + " " + info.pid); 139 if (info.processName.equals(name)) 140 return info; 141 } 142 143 return null; 144 } 145 146 private boolean isProcessRunning (String processName) { 147 // Log.d(LOG_TAG, "isProcessRunning(): " + processName); 148 return (findProcess(processName) != null); 149 } 150} 151