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
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;
25import android.util.proto.ProtoOutputStream;
26
27import 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
52    // As given to us
53    Bundle mArguments;
54
55    // Any intermediate results that have been collected.
56    Bundle mCurResults;
57
58    // Copy of instrumentationClass.
59    ComponentName mResultClass;
60
61    // Contains all running processes that have active instrumentation.
62    final ArrayList<ProcessRecord> mRunningProcesses = new ArrayList<>();
63
64    // Set to true when we have told the watcher the instrumentation is finished.
65    boolean mFinished;
66
67    ActiveInstrumentation(ActivityManagerService service) {
68        mService = service;
69    }
70
71    void removeProcess(ProcessRecord proc) {
72        mFinished = true;
73        mRunningProcesses.remove(proc);
74        if (mRunningProcesses.size() == 0) {
75            mService.mActiveInstrumentation.remove(this);
76        }
77    }
78
79    public String toString() {
80        StringBuilder sb = new StringBuilder(128);
81        sb.append("ActiveInstrumentation{");
82        sb.append(Integer.toHexString(System.identityHashCode(this)));
83        sb.append(' ');
84        sb.append(mClass.toShortString());
85        if (mFinished) {
86            sb.append(" FINISHED");
87        }
88        sb.append(" ");
89        sb.append(mRunningProcesses.size());
90        sb.append(" procs");
91        sb.append('}');
92        return sb.toString();
93    }
94
95    void dump(PrintWriter pw, String prefix) {
96        pw.print(prefix); pw.print("mClass="); pw.print(mClass);
97        pw.print(" mFinished="); pw.println(mFinished);
98        pw.print(prefix); pw.println("mRunningProcesses:");
99        for (int i=0; i<mRunningProcesses.size(); i++) {
100            pw.print(prefix); pw.print("  #"); pw.print(i); pw.print(": ");
101            pw.println(mRunningProcesses.get(i));
102        }
103        pw.print(prefix); pw.print("mTargetProcesses=");
104        pw.println(Arrays.toString(mTargetProcesses));
105        pw.print(prefix); pw.print("mTargetInfo=");
106        pw.println(mTargetInfo);
107        if (mTargetInfo != null) {
108            mTargetInfo.dump(new PrintWriterPrinter(pw), prefix + "  ", 0);
109        }
110        if (mProfileFile != null) {
111            pw.print(prefix); pw.print("mProfileFile="); pw.println(mProfileFile);
112        }
113        if (mWatcher != null) {
114            pw.print(prefix); pw.print("mWatcher="); pw.println(mWatcher);
115        }
116        if (mUiAutomationConnection != null) {
117            pw.print(prefix); pw.print("mUiAutomationConnection=");
118            pw.println(mUiAutomationConnection);
119        }
120        pw.print(prefix); pw.print("mArguments=");
121        pw.println(mArguments);
122    }
123
124    void writeToProto(ProtoOutputStream proto, long fieldId) {
125        long token = proto.start(fieldId);
126        mClass.writeToProto(proto, ActiveInstrumentationProto.CLASS);
127        proto.write(ActiveInstrumentationProto.FINISHED, mFinished);
128        for (int i=0; i<mRunningProcesses.size(); i++) {
129            mRunningProcesses.get(i).writeToProto(proto,
130                    ActiveInstrumentationProto.RUNNING_PROCESSES);
131        }
132        for (String p : mTargetProcesses) {
133            proto.write(ActiveInstrumentationProto.TARGET_PROCESSES, p);
134        }
135        if (mTargetInfo != null) {
136            mTargetInfo.writeToProto(proto, ActiveInstrumentationProto.TARGET_INFO);
137        }
138        proto.write(ActiveInstrumentationProto.PROFILE_FILE, mProfileFile);
139        proto.write(ActiveInstrumentationProto.WATCHER, mWatcher.toString());
140        proto.write(ActiveInstrumentationProto.UI_AUTOMATION_CONNECTION,
141                mUiAutomationConnection.toString());
142        proto.write(ActiveInstrumentationProto.ARGUMENTS, mArguments.toString());
143        proto.end(token);
144    }
145}
146