• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Whether the caller holds START_FOREGROUND_SERVICES_FROM_BACKGROUND permission
56     boolean mHasBackgroundForegroundServiceStartsPermission;
57 
58     // As given to us
59     Bundle mArguments;
60 
61     // Any intermediate results that have been collected.
62     Bundle mCurResults;
63 
64     // Copy of instrumentationClass.
65     ComponentName mResultClass;
66 
67     // Contains all running processes that have active instrumentation.
68     final ArrayList<ProcessRecord> mRunningProcesses = new ArrayList<>();
69 
70     // Set to true when we have told the watcher the instrumentation is finished.
71     boolean mFinished;
72 
73     // The uid of the process who started this instrumentation.
74     int mSourceUid;
75 
76     // True if instrumentation should take place without restarting the target process.
77     boolean mNoRestart;
78 
ActiveInstrumentation(ActivityManagerService service)79     ActiveInstrumentation(ActivityManagerService service) {
80         mService = service;
81     }
82 
removeProcess(ProcessRecord proc)83     void removeProcess(ProcessRecord proc) {
84         mFinished = true;
85         mRunningProcesses.remove(proc);
86         if (mRunningProcesses.size() == 0) {
87             mService.mActiveInstrumentation.remove(this);
88         }
89     }
90 
toString()91     public String toString() {
92         StringBuilder sb = new StringBuilder(128);
93         sb.append("ActiveInstrumentation{");
94         sb.append(Integer.toHexString(System.identityHashCode(this)));
95         sb.append(' ');
96         sb.append(mClass.toShortString());
97         if (mFinished) {
98             sb.append(" FINISHED");
99         }
100         sb.append(" ");
101         sb.append(mRunningProcesses.size());
102         sb.append(" procs");
103         sb.append('}');
104         return sb.toString();
105     }
106 
dump(PrintWriter pw, String prefix)107     void dump(PrintWriter pw, String prefix) {
108         pw.print(prefix); pw.print("mClass="); pw.print(mClass);
109         pw.print(" mFinished="); pw.println(mFinished);
110         pw.print(prefix); pw.println("mRunningProcesses:");
111         for (int i=0; i<mRunningProcesses.size(); i++) {
112             pw.print(prefix); pw.print("  #"); pw.print(i); pw.print(": ");
113             pw.println(mRunningProcesses.get(i));
114         }
115         pw.print(prefix); pw.print("mTargetProcesses=");
116         pw.println(Arrays.toString(mTargetProcesses));
117         pw.print(prefix); pw.print("mTargetInfo=");
118         pw.println(mTargetInfo);
119         if (mTargetInfo != null) {
120             mTargetInfo.dump(new PrintWriterPrinter(pw), prefix + "  ", 0);
121         }
122         if (mProfileFile != null) {
123             pw.print(prefix); pw.print("mProfileFile="); pw.println(mProfileFile);
124         }
125         if (mWatcher != null) {
126             pw.print(prefix); pw.print("mWatcher="); pw.println(mWatcher);
127         }
128         if (mUiAutomationConnection != null) {
129             pw.print(prefix); pw.print("mUiAutomationConnection=");
130             pw.println(mUiAutomationConnection);
131         }
132         pw.print("mHasBackgroundActivityStartsPermission=");
133         pw.println(mHasBackgroundActivityStartsPermission);
134         pw.print("mHasBackgroundForegroundServiceStartsPermission=");
135         pw.println(mHasBackgroundForegroundServiceStartsPermission);
136         pw.print(prefix); pw.print("mArguments=");
137         pw.println(mArguments);
138     }
139 
dumpDebug(ProtoOutputStream proto, long fieldId)140     void dumpDebug(ProtoOutputStream proto, long fieldId) {
141         long token = proto.start(fieldId);
142         mClass.dumpDebug(proto, ActiveInstrumentationProto.CLASS);
143         proto.write(ActiveInstrumentationProto.FINISHED, mFinished);
144         for (int i=0; i<mRunningProcesses.size(); i++) {
145             mRunningProcesses.get(i).dumpDebug(proto,
146                     ActiveInstrumentationProto.RUNNING_PROCESSES);
147         }
148         for (String p : mTargetProcesses) {
149             proto.write(ActiveInstrumentationProto.TARGET_PROCESSES, p);
150         }
151         if (mTargetInfo != null) {
152             mTargetInfo.dumpDebug(proto, ActiveInstrumentationProto.TARGET_INFO, 0);
153         }
154         proto.write(ActiveInstrumentationProto.PROFILE_FILE, mProfileFile);
155         proto.write(ActiveInstrumentationProto.WATCHER, mWatcher.toString());
156         proto.write(ActiveInstrumentationProto.UI_AUTOMATION_CONNECTION,
157                 mUiAutomationConnection.toString());
158         if (mArguments != null) {
159             mArguments.dumpDebug(proto, ActiveInstrumentationProto.ARGUMENTS);
160         }
161         proto.end(token);
162     }
163 }
164