1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.modules.repository;
22 
23 import java.io.File;
24 import java.util.concurrent.ScheduledFuture;
25 
26 import net.sourceforge.atunes.kernel.DeviceListeners;
27 import net.sourceforge.atunes.model.IBeanFactory;
28 import net.sourceforge.atunes.model.IDeviceHandler;
29 import net.sourceforge.atunes.model.IStateDevice;
30 import net.sourceforge.atunes.model.ITaskService;
31 import net.sourceforge.atunes.utils.Logger;
32 import net.sourceforge.atunes.utils.StringUtils;
33 
34 /**
35  * Monitors when device is connected
36  *
37  * @author alex
38  *
39  */
40 public final class DeviceMonitor implements Runnable {
41 
42 	private int delayInSeconds;
43 
44 	private ScheduledFuture<?> future;
45 
46 	private IDeviceHandler deviceHandler;
47 
48 	private ITaskService taskService;
49 
50 	private IStateDevice stateDevice;
51 
52 	private IBeanFactory beanFactory;
53 
54 	/**
55 	 * @param beanFactory
56 	 */
setBeanFactory(final IBeanFactory beanFactory)57 	public void setBeanFactory(final IBeanFactory beanFactory) {
58 		this.beanFactory = beanFactory;
59 	}
60 
61 	/**
62 	 * @param stateDevice
63 	 */
setStateDevice(final IStateDevice stateDevice)64 	public void setStateDevice(final IStateDevice stateDevice) {
65 		this.stateDevice = stateDevice;
66 	}
67 
68 	/**
69 	 * @param taskService
70 	 */
setTaskService(final ITaskService taskService)71 	public void setTaskService(final ITaskService taskService) {
72 		this.taskService = taskService;
73 	}
74 
75 	/**
76 	 * @param deviceHandler
77 	 */
setDeviceHandler(final IDeviceHandler deviceHandler)78 	public void setDeviceHandler(final IDeviceHandler deviceHandler) {
79 		this.deviceHandler = deviceHandler;
80 	}
81 
82 	/**
83 	 * @param delay
84 	 */
setDelayInSeconds(final int delay)85 	public void setDelayInSeconds(final int delay) {
86 		this.delayInSeconds = delay;
87 	}
88 
89 	/**
90 	 * Start monitor.
91 	 */
startMonitor()92 	void startMonitor() {
93 		this.future = this.taskService.submitPeriodically("Device Monitor",
94 				this.delayInSeconds, this.delayInSeconds, this);
95 	}
96 
97 	/**
98 	 * Stops monitor
99 	 */
stopMonitor()100 	void stopMonitor() {
101 		this.future.cancel(true);
102 	}
103 
104 	/**
105 	 * Returns if monitor is running
106 	 *
107 	 * @return
108 	 */
isMonitorRunning()109 	boolean isMonitorRunning() {
110 		return this.future != null;
111 	}
112 
113 	@Override
run()114 	public void run() {
115 		checkConnection();
116 		checkDisconnection();
117 	}
118 
119 	/**
120 	 * Checks if device has been disconnected, returning true if so, false
121 	 * otherwise
122 	 *
123 	 * @return
124 	 */
checkDisconnection()125 	private boolean checkDisconnection() {
126 		if (!this.deviceHandler.isDeviceConnected()) {
127 			return false;
128 		}
129 
130 		File deviceLocationFile = new File(
131 				this.deviceHandler.getDeviceLocation());
132 		if (!deviceLocationFile.exists()) {
133 			Logger.info("Device disconnected");
134 			this.beanFactory.getBean(DeviceListeners.class).deviceDisconnected(
135 					net.sourceforge.atunes.utils.FileUtils
136 							.getPath(deviceLocationFile));
137 			return true;
138 		}
139 		return false;
140 	}
141 
142 	/**
143 	 * Checks if there is a device connected, returning true if so, false
144 	 * otherwise
145 	 *
146 	 * @param deviceLocation
147 	 * @return
148 	 */
checkConnection()149 	private boolean checkConnection() {
150 		String deviceLocation = this.stateDevice.getDefaultDeviceLocation();
151 		if (!StringUtils.isEmpty(deviceLocation)) {
152 			File deviceLocationFile = new File(deviceLocation);
153 			if (!this.deviceHandler.isDeviceConnected()
154 					&& deviceLocationFile.exists()) {
155 				Logger.info("Device connected");
156 				this.beanFactory.getBean(DeviceListeners.class)
157 						.deviceConnected(
158 								net.sourceforge.atunes.utils.FileUtils
159 										.getPath(deviceLocationFile));
160 				return true;
161 			}
162 		}
163 		return false;
164 	}
165 }
166