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