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 70 // The uid of the process who started this instrumentation. 71 int mSourceUid; 72 ActiveInstrumentation(ActivityManagerService service)73 ActiveInstrumentation(ActivityManagerService service) { 74 mService = service; 75 } 76 removeProcess(ProcessRecord proc)77 void removeProcess(ProcessRecord proc) { 78 mFinished = true; 79 mRunningProcesses.remove(proc); 80 if (mRunningProcesses.size() == 0) { 81 mService.mActiveInstrumentation.remove(this); 82 } 83 } 84 toString()85 public String toString() { 86 StringBuilder sb = new StringBuilder(128); 87 sb.append("ActiveInstrumentation{"); 88 sb.append(Integer.toHexString(System.identityHashCode(this))); 89 sb.append(' '); 90 sb.append(mClass.toShortString()); 91 if (mFinished) { 92 sb.append(" FINISHED"); 93 } 94 sb.append(" "); 95 sb.append(mRunningProcesses.size()); 96 sb.append(" procs"); 97 sb.append('}'); 98 return sb.toString(); 99 } 100 dump(PrintWriter pw, String prefix)101 void dump(PrintWriter pw, String prefix) { 102 pw.print(prefix); pw.print("mClass="); pw.print(mClass); 103 pw.print(" mFinished="); pw.println(mFinished); 104 pw.print(prefix); pw.println("mRunningProcesses:"); 105 for (int i=0; i<mRunningProcesses.size(); i++) { 106 pw.print(prefix); pw.print(" #"); pw.print(i); pw.print(": "); 107 pw.println(mRunningProcesses.get(i)); 108 } 109 pw.print(prefix); pw.print("mTargetProcesses="); 110 pw.println(Arrays.toString(mTargetProcesses)); 111 pw.print(prefix); pw.print("mTargetInfo="); 112 pw.println(mTargetInfo); 113 if (mTargetInfo != null) { 114 mTargetInfo.dump(new PrintWriterPrinter(pw), prefix + " ", 0); 115 } 116 if (mProfileFile != null) { 117 pw.print(prefix); pw.print("mProfileFile="); pw.println(mProfileFile); 118 } 119 if (mWatcher != null) { 120 pw.print(prefix); pw.print("mWatcher="); pw.println(mWatcher); 121 } 122 if (mUiAutomationConnection != null) { 123 pw.print(prefix); pw.print("mUiAutomationConnection="); 124 pw.println(mUiAutomationConnection); 125 } 126 pw.print("mHasBackgroundActivityStartsPermission="); 127 pw.println(mHasBackgroundActivityStartsPermission); 128 pw.print(prefix); pw.print("mArguments="); 129 pw.println(mArguments); 130 } 131 dumpDebug(ProtoOutputStream proto, long fieldId)132 void dumpDebug(ProtoOutputStream proto, long fieldId) { 133 long token = proto.start(fieldId); 134 mClass.dumpDebug(proto, ActiveInstrumentationProto.CLASS); 135 proto.write(ActiveInstrumentationProto.FINISHED, mFinished); 136 for (int i=0; i<mRunningProcesses.size(); i++) { 137 mRunningProcesses.get(i).dumpDebug(proto, 138 ActiveInstrumentationProto.RUNNING_PROCESSES); 139 } 140 for (String p : mTargetProcesses) { 141 proto.write(ActiveInstrumentationProto.TARGET_PROCESSES, p); 142 } 143 if (mTargetInfo != null) { 144 mTargetInfo.dumpDebug(proto, ActiveInstrumentationProto.TARGET_INFO, 0); 145 } 146 proto.write(ActiveInstrumentationProto.PROFILE_FILE, mProfileFile); 147 proto.write(ActiveInstrumentationProto.WATCHER, mWatcher.toString()); 148 proto.write(ActiveInstrumentationProto.UI_AUTOMATION_CONNECTION, 149 mUiAutomationConnection.toString()); 150 if (mArguments != null) { 151 mArguments.dumpDebug(proto, ActiveInstrumentationProto.ARGUMENTS); 152 } 153 proto.end(token); 154 } 155 } 156