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 android.server.wm; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.IBinder; 22 import android.util.Log; 23 import java.util.HashMap; 24 import java.util.Map; 25 import java.util.concurrent.CountDownLatch; 26 import java.util.concurrent.TimeUnit; 27 28 /** 29 * A service collecting data from other apps used by a test. 30 * 31 * Use {@link TestLogClient} to send data to this service. 32 */ 33 public class TestLogService extends Service { 34 private static final String TAG = "TestLogService"; 35 36 private static final Object mLock = new Object(); 37 38 static class ClientChannel { 39 final String mStopKey; 40 final CountDownLatch mLatch = new CountDownLatch(1); 41 final Map<String, String> mResults = new HashMap<>(); 42 43 ClientChannel(String stopKey) { 44 mStopKey = stopKey; 45 } 46 } 47 48 private static Map<String, ClientChannel> mChannels = new HashMap<>(); 49 50 @Override 51 public IBinder onBind(Intent intent) { 52 return null; 53 } 54 55 @Override 56 public int onStartCommand(Intent intent, int flags, int startId) { 57 record(intent.getStringExtra(TestLogClient.EXTRA_LOG_TAG), 58 intent.getStringExtra(TestLogClient.EXTRA_KEY), 59 intent.getStringExtra(TestLogClient.EXTRA_VALUE)); 60 return START_NOT_STICKY; 61 } 62 63 /** 64 * Prepare to receive results from a client with a specified tag. 65 * 66 * @param logtag Unique tag for the client. 67 * @param stopKey The key that signals that the client has completed all required actions. 68 */ 69 public static void registerClient(String logtag, String stopKey) { 70 synchronized (mLock) { 71 if (mChannels.containsKey(logtag)) { 72 throw new IllegalArgumentException(logtag); 73 } 74 mChannels.put(logtag, new ClientChannel(stopKey)); 75 } 76 } 77 78 /** 79 * Wait for the client to complete all required actions and return the results. 80 * 81 * @param logtag Unique tag for the client. 82 * @param timeoutMs Latch timeout in ms. 83 * @return The map of results from the client 84 */ 85 public static Map<String, String> getResultsForClient(String logtag, int timeoutMs) { 86 Map<String, String> result = new HashMap<>(); 87 CountDownLatch latch; 88 synchronized (mLock) { 89 if (!mChannels.containsKey(logtag)) { 90 return result; 91 } 92 latch = mChannels.get(logtag).mLatch; 93 } 94 try { 95 latch.await(timeoutMs, TimeUnit.MILLISECONDS); 96 } catch (InterruptedException ignore) { 97 } 98 synchronized (mLock) { 99 for (Map.Entry<String, String> e : mChannels.get(logtag).mResults.entrySet()) { 100 result.put(e.getKey(), e.getValue()); 101 } 102 } 103 return result; 104 } 105 106 private static void record(String logtag, String key, String value) { 107 synchronized (mLock) { 108 if (!mChannels.containsKey(logtag)) { 109 Log.e(TAG, "Unexpected logtag: " + logtag); 110 return; 111 } 112 ClientChannel channel = mChannels.get(logtag); 113 channel.mResults.put(key, value); 114 if (key.equals(channel.mStopKey)) { 115 channel.mLatch.countDown(); 116 } 117 } 118 } 119 } 120