1 /* 2 * Copyright (C) 2017 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.server.am; 18 19 import android.app.IInstrumentationWatcher; 20 import android.app.IUiAutomationConnection; 21 import android.content.ComponentName; 22 import android.content.pm.ApplicationInfo; 23 import android.os.Bundle; 24 import android.util.PrintWriterPrinter; 25 import android.util.proto.ProtoOutputStream; 26 27 import java.io.PrintWriter; 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 31 class ActiveInstrumentation { 32 final ActivityManagerService mService; 33 34 // Class installed to instrument app 35 ComponentName mClass; 36 37 // All process names that should be instrumented 38 String[] mTargetProcesses; 39 40 // The application being instrumented 41 ApplicationInfo mTargetInfo; 42 43 // Where to save profiling 44 String mProfileFile; 45 46 // Who is waiting 47 IInstrumentationWatcher mWatcher; 48 49 // Connection to use the UI introspection APIs. 50 IUiAutomationConnection mUiAutomationConnection; 51 52 // Whether the caller holds START_ACTIVITIES_FROM_BACKGROUND permission 53 boolean mHasBackgroundActivityStartsPermission; 54 55 // As given to us 56 Bundle mArguments; 57 58 // Any intermediate results that have been collected. 59 Bundle mCurResults; 60 61 // Copy of instrumentationClass. 62 ComponentName mResultClass; 63 64 // Contains all running processes that have active instrumentation. 65 final ArrayList<ProcessRecord> mRunningProcesses = new ArrayList<>(); 66 67 // Set to true when we have told the watcher the instrumentation is finished. 68 boolean mFinished; 69 ActiveInstrumentation(ActivityManagerService service)70 ActiveInstrumentation(ActivityManagerService service) { 71 mService = service; 72 } 73 removeProcess(ProcessRecord proc)74 void removeProcess(ProcessRecord proc) { 75 mFinished = true; 76 mRunningProcesses.remove(proc); 77 if (mRunningProcesses.size() == 0) { 78 mService.mActiveInstrumentation.remove(this); 79 } 80 } 81 toString()82 public String toString() { 83 StringBuilder sb = new StringBuilder(128); 84 sb.append("ActiveInstrumentation{"); 85 sb.append(Integer.toHexString(System.identityHashCode(this))); 86 sb.append(' '); 87 sb.append(mClass.toShortString()); 88 if (mFinished) { 89 sb.append(" FINISHED"); 90 } 91 sb.append(" "); 92 sb.append(mRunningProcesses.size()); 93 sb.append(" procs"); 94 sb.append('}'); 95 return sb.toString(); 96 } 97 dump(PrintWriter pw, String prefix)98 void dump(PrintWriter pw, String prefix) { 99 pw.print(prefix); pw.print("mClass="); pw.print(mClass); 100 pw.print(" mFinished="); pw.println(mFinished); 101 pw.print(prefix); pw.println("mRunningProcesses:"); 102 for (int i=0; i<mRunningProcesses.size(); i++) { 103 pw.print(prefix); pw.print(" #"); pw.print(i); pw.print(": "); 104 pw.println(mRunningProcesses.get(i)); 105 } 106 pw.print(prefix); pw.print("mTargetProcesses="); 107 pw.println(Arrays.toString(mTargetProcesses)); 108 pw.print(prefix); pw.print("mTargetInfo="); 109 pw.println(mTargetInfo); 110 if (mTargetInfo != null) { 111 mTargetInfo.dump(new PrintWriterPrinter(pw), prefix + " ", 0); 112 } 113 if (mProfileFile != null) { 114 pw.print(prefix); pw.print("mProfileFile="); pw.println(mProfileFile); 115 } 116 if (mWatcher != null) { 117 pw.print(prefix); pw.print("mWatcher="); pw.println(mWatcher); 118 } 119 if (mUiAutomationConnection != null) { 120 pw.print(prefix); pw.print("mUiAutomationConnection="); 121 pw.println(mUiAutomationConnection); 122 } 123 pw.print("mHasBackgroundActivityStartsPermission="); 124 pw.println(mHasBackgroundActivityStartsPermission); 125 pw.print(prefix); pw.print("mArguments="); 126 pw.println(mArguments); 127 } 128 writeToProto(ProtoOutputStream proto, long fieldId)129 void writeToProto(ProtoOutputStream proto, long fieldId) { 130 long token = proto.start(fieldId); 131 mClass.writeToProto(proto, ActiveInstrumentationProto.CLASS); 132 proto.write(ActiveInstrumentationProto.FINISHED, mFinished); 133 for (int i=0; i<mRunningProcesses.size(); i++) { 134 mRunningProcesses.get(i).writeToProto(proto, 135 ActiveInstrumentationProto.RUNNING_PROCESSES); 136 } 137 for (String p : mTargetProcesses) { 138 proto.write(ActiveInstrumentationProto.TARGET_PROCESSES, p); 139 } 140 if (mTargetInfo != null) { 141 mTargetInfo.writeToProto(proto, ActiveInstrumentationProto.TARGET_INFO, 0); 142 } 143 proto.write(ActiveInstrumentationProto.PROFILE_FILE, mProfileFile); 144 proto.write(ActiveInstrumentationProto.WATCHER, mWatcher.toString()); 145 proto.write(ActiveInstrumentationProto.UI_AUTOMATION_CONNECTION, 146 mUiAutomationConnection.toString()); 147 if (mArguments != null) { 148 mArguments.writeToProto(proto, ActiveInstrumentationProto.ARGUMENTS); 149 } 150 proto.end(token); 151 } 152 } 153