blob: 9510f594785a0e94fa227d7a44956dfa0cf7c711 [file] [log] [blame]
Dianne Hackborn34041732017-01-31 15:27:13 -08001/*
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
17package com.android.server.am;
18
19import android.app.IInstrumentationWatcher;
20import android.app.IUiAutomationConnection;
21import android.content.ComponentName;
22import android.content.pm.ApplicationInfo;
23import android.os.Bundle;
24import android.util.PrintWriterPrinter;
Yi Jin148d7f42017-11-28 14:23:56 -080025import android.util.proto.ProtoOutputStream;
26
Dianne Hackborn34041732017-01-31 15:27:13 -080027import java.io.PrintWriter;
28import java.util.ArrayList;
29import java.util.Arrays;
30
31class 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
Michal Karpinskidaef80f2019-01-29 16:50:51 +000052 // Whether the caller holds START_ACTIVITIES_FROM_BACKGROUND permission
53 boolean mHasBackgroundActivityStartsPermission;
54
Dianne Hackborn34041732017-01-31 15:27:13 -080055 // 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 ActiveInstrumentation(ActivityManagerService service) {
71 mService = service;
72 }
73
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
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
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 }
Michal Karpinskidaef80f2019-01-29 16:50:51 +0000123 pw.print("mHasBackgroundActivityStartsPermission=");
124 pw.println(mHasBackgroundActivityStartsPermission);
Dianne Hackborn34041732017-01-31 15:27:13 -0800125 pw.print(prefix); pw.print("mArguments=");
126 pw.println(mArguments);
127 }
Yi Jin148d7f42017-11-28 14:23:56 -0800128
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) {
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800141 mTargetInfo.writeToProto(proto, ActiveInstrumentationProto.TARGET_INFO, 0);
Yi Jin148d7f42017-11-28 14:23:56 -0800142 }
143 proto.write(ActiveInstrumentationProto.PROFILE_FILE, mProfileFile);
144 proto.write(ActiveInstrumentationProto.WATCHER, mWatcher.toString());
145 proto.write(ActiveInstrumentationProto.UI_AUTOMATION_CONNECTION,
146 mUiAutomationConnection.toString());
Kweku Adams87c60a02018-06-13 12:13:52 -0700147 if (mArguments != null) {
148 mArguments.writeToProto(proto, ActiveInstrumentationProto.ARGUMENTS);
149 }
Yi Jin148d7f42017-11-28 14:23:56 -0800150 proto.end(token);
151 }
Dianne Hackborn34041732017-01-31 15:27:13 -0800152}