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 24 package com.drawelements.deqp.testercore; 25 26 import android.app.ActivityManager; 27 import android.content.ComponentName; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.os.Process; 31 import java.util.List; 32 33 public class RemoteAPI { 34 35 private static final String LOG_TAG = "dEQP"; 36 37 private Context m_context; 38 private String m_processName; 39 private String m_logFileName; 40 private boolean m_canBeRunning; 41 RemoteAPI(Context context, String logFileName)42 public RemoteAPI(Context context, String logFileName) { 43 m_context = context; 44 m_processName = m_context.getPackageName() + ":testercore"; 45 m_logFileName = logFileName; 46 m_canBeRunning = false; 47 } 48 getDefaultTesterComponent()49 private ComponentName getDefaultTesterComponent() { 50 return new ComponentName(m_context.getPackageName(), 51 "android.app.NativeActivity"); 52 } 53 getTesterComponent(String testerName)54 private ComponentName getTesterComponent(String testerName) { 55 if (testerName != null && !testerName.equals("")) { 56 ComponentName component = 57 ComponentName.unflattenFromString(testerName); 58 if (component == null) { 59 Log.e(LOG_TAG, "Invalid component name supplied (" + 60 testerName + "), using default"); 61 component = getDefaultTesterComponent(); 62 } 63 return component; 64 } else { 65 return getDefaultTesterComponent(); 66 } 67 } 68 start(String testerName, String cmdLine, String caseList)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 + 80 " --deqp-log-filename=" + m_logFileName; 81 82 if (caseList != null) 83 cmdLine = cmdLine + " --deqp-caselist=" + caseList; 84 85 cmdLine = cmdLine.replaceAll(" ", " "); 86 testIntent.putExtra("cmdLine", cmdLine); 87 88 // Try to resolve intent. 89 boolean isActivity = m_context.getPackageManager().resolveActivity( 90 testIntent, 0) != null; 91 boolean isService = 92 m_context.getPackageManager().resolveService(testIntent, 0) != null; 93 94 if (!isActivity && !isService) { 95 Log.e(LOG_TAG, "Can't resolve component as activity or service (" + 96 component.flattenToString() + 97 "), using default"); 98 component = getDefaultTesterComponent(); 99 } 100 101 Log.d(LOG_TAG, "Starting activity " + component.flattenToString()); 102 103 try { 104 if (isService) 105 m_context.startService(testIntent); 106 else 107 m_context.startActivity(testIntent); 108 } catch (Exception e) { 109 Log.e(LOG_TAG, "Failed to start tester", e); 110 return false; 111 } 112 113 m_canBeRunning = true; 114 return true; 115 } 116 kill()117 public boolean kill() { 118 ActivityManager.RunningAppProcessInfo processInfo = 119 findProcess(m_processName); 120 121 // \note not mutating m_canBeRunning yet since process does not die 122 // immediately 123 124 if (processInfo != null) { 125 Log.d(LOG_TAG, "Killing " + m_processName); 126 Process.killProcess(processInfo.pid); 127 return true; 128 } else { 129 return false; 130 } 131 132 // \todo [2010-11-01 pyry] Block until tester process is not running? 133 } 134 isRunning()135 public boolean isRunning() { 136 if (!m_canBeRunning) { 137 return false; 138 } else if (isProcessRunning(m_processName)) { 139 return true; 140 } else { 141 // Cache result. Safe, because only start() can spawn the process 142 m_canBeRunning = false; 143 return false; 144 } 145 } 146 getLogFileName()147 public String getLogFileName() { return m_logFileName; } 148 findProcess(String name)149 private ActivityManager.RunningAppProcessInfo findProcess(String name) { 150 ActivityManager activityMgr = 151 (ActivityManager)m_context.getSystemService( 152 Context.ACTIVITY_SERVICE); 153 List<ActivityManager.RunningAppProcessInfo> processes = 154 activityMgr.getRunningAppProcesses(); 155 156 for (ActivityManager.RunningAppProcessInfo info : processes) { 157 // Log.v(LOG_TAG, "Found proc : " + info.processName + " " + 158 // info.pid); 159 if (info.processName.equals(name)) 160 return info; 161 } 162 163 return null; 164 } 165 isProcessRunning(String processName)166 private boolean isProcessRunning(String processName) { 167 // Log.d(LOG_TAG, "isProcessRunning(): " + processName); 168 return (findProcess(processName) != null); 169 } 170 } 171