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