• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.content.ComponentName;
21 import android.os.Bundle;
22 import android.os.Process;
23 import android.os.RemoteException;
24 import android.util.Slog;
25 
26 import java.util.ArrayList;
27 
28 public class InstrumentationReporter {
29     static final boolean DEBUG = false;
30     static final String TAG = ActivityManagerDebugConfig.TAG_AM;
31 
32     static final int REPORT_TYPE_STATUS = 0;
33     static final int REPORT_TYPE_FINISHED = 1;
34 
35     final Object mLock = new Object();
36     ArrayList<Report> mPendingReports;
37     Thread mThread;
38 
39     final class MyThread extends Thread {
MyThread()40         public MyThread() {
41             super("InstrumentationReporter");
42             if (DEBUG) Slog.d(TAG, "Starting InstrumentationReporter: " + this);
43         }
44 
45         @Override
run()46         public void run() {
47             Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
48             boolean waited = false;
49             while (true) {
50                 ArrayList<Report> reports;
51                 synchronized (mLock) {
52                     reports = mPendingReports;
53                     mPendingReports = null;
54                     if (reports == null || reports.isEmpty()) {
55                         if (!waited) {
56                             // Sleep for a little bit, to avoid thrashing through threads.
57                             try {
58                                 mLock.wait(10000); // 10 seconds
59                             } catch (InterruptedException e) {
60                             }
61                             waited = true;
62                             continue;
63                         } else {
64                             mThread = null;
65                             if (DEBUG) Slog.d(TAG, "Exiting InstrumentationReporter: " + this);
66                             return;
67                         }
68                     }
69                 }
70 
71                 waited = false;
72 
73                 for (int i=0; i<reports.size(); i++) {
74                     final Report rep = reports.get(i);
75                     try {
76                         if (rep.mType == REPORT_TYPE_STATUS) {
77                             if (DEBUG) Slog.d(TAG, "Dispatch status to " + rep.mWatcher
78                                     + ": " + rep.mName.flattenToShortString()
79                                     + " code=" + rep.mResultCode + " result=" + rep.mResults);
80                             rep.mWatcher.instrumentationStatus(rep.mName, rep.mResultCode,
81                                     rep.mResults);
82                         } else {
83                             if (DEBUG) Slog.d(TAG, "Dispatch finished to " + rep.mWatcher
84                                     + ": " + rep.mName.flattenToShortString()
85                                     + " code=" + rep.mResultCode + " result=" + rep.mResults);
86                             rep.mWatcher.instrumentationFinished(rep.mName, rep.mResultCode,
87                                     rep.mResults);
88                         }
89                     } catch (RemoteException e) {
90                         Slog.i(TAG, "Failure reporting to instrumentation watcher: comp="
91                                 + rep.mName + " results=" + rep.mResults);
92                     }
93                 }
94             }
95         }
96     }
97 
98     final class Report {
99         final int mType;
100         final IInstrumentationWatcher mWatcher;
101         final ComponentName mName;
102         final int mResultCode;
103         final Bundle mResults;
104 
Report(int type, IInstrumentationWatcher watcher, ComponentName name, int resultCode, Bundle results)105         Report(int type, IInstrumentationWatcher watcher, ComponentName name, int resultCode,
106                 Bundle results) {
107             mType = type;
108             mWatcher = watcher;
109             mName = name;
110             mResultCode = resultCode;
111             mResults = results;
112         }
113     }
114 
reportStatus(IInstrumentationWatcher watcher, ComponentName name, int resultCode, Bundle results)115     public void reportStatus(IInstrumentationWatcher watcher, ComponentName name, int resultCode,
116             Bundle results) {
117         if (DEBUG) Slog.d(TAG, "Report status to " + watcher
118                 + ": " + name.flattenToShortString()
119                 + " code=" + resultCode + " result=" + results);
120         report(new Report(REPORT_TYPE_STATUS, watcher, name, resultCode, results));
121     }
122 
reportFinished(IInstrumentationWatcher watcher, ComponentName name, int resultCode, Bundle results)123     public void reportFinished(IInstrumentationWatcher watcher, ComponentName name, int resultCode,
124             Bundle results) {
125         if (DEBUG) Slog.d(TAG, "Report finished to " + watcher
126                 + ": " + name.flattenToShortString()
127                 + " code=" + resultCode + " result=" + results);
128         report(new Report(REPORT_TYPE_FINISHED, watcher, name, resultCode, results));
129     }
130 
report(Report report)131     private void report(Report report) {
132         synchronized (mLock) {
133             if (mThread == null) {
134                 mThread = new MyThread();
135                 mThread.start();
136             }
137             if (mPendingReports == null) {
138                 mPendingReports = new ArrayList<>();
139             }
140             mPendingReports.add(report);
141             mLock.notifyAll();
142         }
143     }
144 }
145