mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Changed the root package from net.sf.briar to org.briarproject.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package org.briarproject.plugins.file;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(PollingRemovableDriveMonitor.class.getName());
|
||||
|
||||
private final Executor pluginExecutor;
|
||||
private final RemovableDriveFinder finder;
|
||||
private final long pollingInterval;
|
||||
private final Object pollingLock = new Object();
|
||||
|
||||
private volatile boolean running = false;
|
||||
private volatile Callback callback = null;
|
||||
|
||||
public PollingRemovableDriveMonitor(Executor pluginExecutor,
|
||||
RemovableDriveFinder finder, long pollingInterval) {
|
||||
this.pluginExecutor = pluginExecutor;
|
||||
this.finder = finder;
|
||||
this.pollingInterval = pollingInterval;
|
||||
}
|
||||
|
||||
public void start(Callback callback) throws IOException {
|
||||
this.callback = callback;
|
||||
running = true;
|
||||
pluginExecutor.execute(this);
|
||||
}
|
||||
|
||||
public void stop() throws IOException {
|
||||
running = false;
|
||||
synchronized(pollingLock) {
|
||||
pollingLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Collection<File> drives = finder.findRemovableDrives();
|
||||
while(running) {
|
||||
synchronized(pollingLock) {
|
||||
pollingLock.wait(pollingInterval);
|
||||
}
|
||||
if(!running) return;
|
||||
Collection<File> newDrives = finder.findRemovableDrives();
|
||||
for(File f : newDrives) {
|
||||
if(!drives.contains(f)) callback.driveInserted(f);
|
||||
}
|
||||
drives = newDrives;
|
||||
}
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Interrupted while waiting to poll");
|
||||
Thread.currentThread().interrupt();
|
||||
} catch(IOException e) {
|
||||
callback.exceptionThrown(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user