/**
   * Waits for file to be fully written to the file system before retrieving its file properties.
   *
   * @param filePath Full path of the image file
   * @throws IOException
   */
  private File getMediaFile(String filePath) {
    File file = new File(FileUtils.stripSeparator(filePath));

    // time begin waiting for file write
    long start = (new Date()).getTime();

    // wait for the file to be fully written, then grab its properties
    FileConnection fconn = null;
    try {
      fconn = (FileConnection) Connector.open(filePath, Connector.READ);
      if (fconn.exists()) {
        // wait for file to be fully written
        long fileSize = fconn.fileSize();
        long size = 0;
        Thread thisThread = Thread.currentThread();
        while (myThread == thisThread) {
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            break;
          }
          size = fconn.fileSize();
          if (fileSize != 0 && size == fileSize) {
            break;
          }
          fileSize = size;
        }
        Logger.log(
            this.getClass().getName()
                + ": "
                + filePath
                + " size="
                + Long.toString(fileSize)
                + " bytes");

        // retrieve file properties
        file.setLastModifiedDate(fconn.lastModified());
        file.setName(FileUtils.stripSeparator(fconn.getName()));
        file.setSize(fileSize);
        file.setType(MIMETypeAssociations.getMIMEType(filePath));
      }
    } catch (IOException e) {
      Logger.log(this.getClass().getName() + ": " + e);
    } finally {
      try {
        if (fconn != null) fconn.close();
      } catch (IOException ignored) {
      }
    }

    // log time it took to write the file
    long end = (new Date()).getTime();
    Logger.log(this.getClass().getName() + ": wait time=" + Long.toString(end - start) + " ms");

    return file;
  }
  /** Cleanup the plugin resources and delete temporary directory that may have been created. */
  public void destroy() {
    // allow plugins to clean up
    pluginManagerFunction.onDestroy();

    // delete temporary application directory
    // NOTE: doing this on a background thread doesn't work because the app
    // is closing and the thread is killed before it completes.
    try {
      FileUtils.deleteApplicationTempDirectory();
    } catch (Exception e) {
      Logger.log(
          this.getClass().getName()
              + ": error deleting application temp directory: "
              + e.getMessage());
    }
  }