1 /*
2  * Copyright (C) 2014 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 package com.android.tradefed.device;
17 
18 import com.android.tradefed.log.LogUtil.CLog;
19 
20 import java.util.LinkedList;
21 import java.util.List;
22 
23 /**
24  * A proxy class to propagate requests to multiple {@link IDeviceMonitor}s.
25  */
26 public class DeviceMonitorMultiplexer implements IDeviceMonitor {
27 
28     private final List<IDeviceMonitor> mDeviceMonitors;
29 
DeviceMonitorMultiplexer()30     public DeviceMonitorMultiplexer() {
31         mDeviceMonitors = new LinkedList<>();
32     }
33 
34     /**
35      * {@inheritDoc}
36      */
37     @Override
run()38     public synchronized void run() {
39         for (IDeviceMonitor monitor : mDeviceMonitors) {
40             monitor.run();
41         }
42     }
43 
44     /**
45      * {@inheritDoc}
46      */
47     @Override
setDeviceLister(DeviceLister lister)48     public synchronized void setDeviceLister(DeviceLister lister) {
49         for (IDeviceMonitor monitor : mDeviceMonitors) {
50             monitor.setDeviceLister(lister);
51         }
52     }
53 
54     /**
55      * {@inheritDoc}
56      */
57     @Override
notifyDeviceStateChange(String serial, DeviceAllocationState oldState, DeviceAllocationState newState)58     public synchronized void notifyDeviceStateChange(String serial, DeviceAllocationState oldState,
59             DeviceAllocationState newState) {
60         for (IDeviceMonitor monitor : mDeviceMonitors) {
61             monitor.notifyDeviceStateChange(serial, oldState, newState);
62         }
63     }
64 
addMonitors(List<IDeviceMonitor> globalDeviceMonitors)65     public synchronized void addMonitors(List<IDeviceMonitor> globalDeviceMonitors) {
66         mDeviceMonitors.addAll(globalDeviceMonitors);
67     }
68 
addMonitor(IDeviceMonitor globalDeviceMonitor)69     public synchronized void addMonitor(IDeviceMonitor globalDeviceMonitor) {
70         mDeviceMonitors.add(globalDeviceMonitor);
71     }
72 
removeMonitor(IDeviceMonitor mon)73     public synchronized void removeMonitor(IDeviceMonitor mon) {
74         mDeviceMonitors.remove(mon);
75     }
76 
77     @Override
stop()78     public synchronized void stop() {
79         for (IDeviceMonitor monitor : mDeviceMonitors) {
80             try {
81                 monitor.stop();
82             } catch (RuntimeException e) {
83                 CLog.e(e);
84             }
85         }
86     }
87 }
88