4

For my parents I am writing a simple program to copy files from their digital photocamera to their 'My Documents' folder. They always need my help (they are not so technically advanced) to get their pictures off their camera so I decided to help them out. I called it The Copying Machine. Since I couldn't find a suitable USB-Listener in Java, I wrote one myself:

private void sync()
{
    // All devices in an ArrayList
    File[] roots = File.listRoots();
    ArrayList<File> newList = new ArrayList<File>();
    for(File f : roots)
    {
        newList.add(f);
    }

    // Delete unavailable devices
    ArrayList<File> removeThese = new ArrayList<File>();
    for(File f : devices)
    {
        if(!newList.contains(f))
        {
            removeThese.add(f);
        }
    }
    devices.removeAll(removeThese);

    // Add unknown devices
    for(File f : newList)
    {
        if(!devices.contains(f) && f.canRead() && f.canWrite())
        {
            alarm(f); // Called when new device inserted
            devices.add(f);
        }
    }
}

This method is called every 1000ms in a seperate Thread and I guess that will do. Admitted, this is a dirty method but it works. I tested this function often and I always had the result I wanted. When I continued building my programm, I found that the thread would stop detecting new devices when I hide my programm to the SystemTray. When I open it again, the detection thread still won't work. Could anyone tell me what causes this and how this is to be solved?

Rick Slinkman
  • 643
  • 10
  • 23
  • instead of calling it every 1000ms just write a .bat file that calls it and place it on their desktop. I believe that they can do `DOUBLE-CLICK` even if they're no so technological ;) – Nir Alfasi Jul 03 '12 at 18:28
  • It seems like you would have less trouble writing a computer driver and installing it on your parents. :D – Wug Jul 03 '12 at 18:29
  • Have a look at what happens at the point where the polling thread is started. Is is interrupted somehow? – Wolfgang Jul 03 '12 at 18:33
  • 1
    @alfasin Unfortunately, they sometimes even struggle doing a double-click.. – Rick Slinkman Jul 03 '12 at 18:34
  • @RickSlinkman are you saying that it's easier to plug-in the camera to the USB port than double-click the mouse ??? :))) – Nir Alfasi Jul 03 '12 at 18:36
  • @Wolfgang Thanks for your tip. When I call my hideToTray() method, it saves some info and then it interrupts the polling thread. The polling is then stopped and will not be activated when the JFrame unhides. Thanks, it works fine now! :D – Rick Slinkman Jul 03 '12 at 18:39
  • @alfasin Yes, my parents click twice, but very slow. That means the PC detects 2 single clicks. They are not fast enough to double click, they are scared of what is about to happen when they double click :P The camera switch only needs 1 push, so that works fine :) – Rick Slinkman Jul 03 '12 at 18:46
  • 1
    tell them to click once and then press enter. this will work too ;) – Nir Alfasi Jul 03 '12 at 18:48

1 Answers1

2

Upon saving the data inserted by the user, I stopped detection of new devices. This was foolish of me so I thank you for making me aware of this.

public boolean saveSettings() 
{
    File f = new File(fsv.getHomeDirectory() + File.separator + "settings.cms");
    ObjectOutputStream objOut;
    try 
    {
        // Here was my problem. 
        detector.stopDetection();

        if(gui.saveSettings())
        {
            // Settings-file wegschrijven
            objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
            objOut.writeObject(settings);
            objOut.flush();
            objOut.close();
            return true;
        }
        else
        {
            return false;
        }
    } 
    catch (IOException e) 
    {
        handleExceptions(e);
        return false;
    }
}
Rick Slinkman
  • 643
  • 10
  • 23