/*
   * (non-Javadoc)
   * This will check if the user is at root dir. If so, if they press back
   * again, it will close the app.
   * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
   */
  @Override
  public boolean onKeyDown(int keycode, KeyEvent event) {
    String current = flmg.getCurrentDir();

    if (keycode == KeyEvent.KEYCODE_SEARCH) {
      showDialog(SEARCH_B);

      return true;

    } else if (keycode == KeyEvent.KEYCODE_BACK && use_back_key && !current.equals("/")) {
      if (handler.isMultiSelected()) {
        table.killMultiSelect();
        Toast.makeText(Main.this, "Multi-select is now off", Toast.LENGTH_SHORT).show();
      }

      handler.updateDirectory(flmg.getPreviousDir());
      path_label.setText(flmg.getCurrentDir());

      return true;

    } else if (keycode == KeyEvent.KEYCODE_BACK && use_back_key && current.equals("/")) {
      Toast.makeText(Main.this, "Press back again to quit.", Toast.LENGTH_SHORT).show();
      use_back_key = false;
      path_label.setText(flmg.getCurrentDir());

      return false;

    } else if (keycode == KeyEvent.KEYCODE_BACK && !use_back_key && current.equals("/")) {
      finish();

      return false;
    }
    return false;
  }
  /**
   * To add more functionality and let the user interact with more file types, this is the function
   * to add the ability.
   *
   * <p>(note): this method can be done more efficiently
   */
  @Override
  public void onListItemClick(ListView parent, View view, int position, long id) {
    final String item = handler.getData(position);
    boolean multiSelect = handler.isMultiSelected();
    File file = new File(flmg.getCurrentDir() + "/" + item);
    String item_ext = null;

    try {
      item_ext = item.substring(item.lastIndexOf("."), item.length());

    } catch (IndexOutOfBoundsException e) {
      item_ext = "";
    }

    /*
     * If the user has multi-select on, we just need to record the file
     * not make an intent for it.
     */
    if (multiSelect) {
      table.addMultiPosition(position, file.getPath());

    } else {
      if (file.isDirectory()) {
        if (file.canRead()) {
          handler.updateDirectory(flmg.getNextDir(item, false));
          path_label.setText(flmg.getCurrentDir());

          /*set back button switch to true (this will be better implemented later)*/
          if (!use_back_key) use_back_key = true;

        } else
          Toast.makeText(this, "Can't read folder due to permissions", Toast.LENGTH_SHORT).show();
      }

      /*music file selected--add more audio formats*/
      else if (item_ext.equalsIgnoreCase(".mp3") || item_ext.equalsIgnoreCase(".m4a")) {

        Intent music_int = new Intent(this, AudioPlayblack.class);
        music_int.putExtra("MUSIC PATH", flmg.getCurrentDir() + "/" + item);
        startActivity(music_int);
      }

      /*photo file selected*/
      else if (item_ext.equalsIgnoreCase(".jpeg")
          || item_ext.equalsIgnoreCase(".jpg")
          || item_ext.equalsIgnoreCase(".png")
          || item_ext.equalsIgnoreCase(".gif")
          || item_ext.equalsIgnoreCase(".tiff")) {

        if (file.exists()) {
          Intent pic_int = new Intent();
          pic_int.setAction(android.content.Intent.ACTION_VIEW);
          pic_int.setDataAndType(Uri.fromFile(file), "image/*");
          startActivity(pic_int);
        }
      }

      /*video file selected--add more video formats*/
      else if (item_ext.equalsIgnoreCase(".m4v")
          || item_ext.equalsIgnoreCase(".3gp")
          || item_ext.equalsIgnoreCase(".wmv")
          || item_ext.equalsIgnoreCase(".mp4")
          || item_ext.equalsIgnoreCase(".ogg")) {

        if (file.exists()) {
          Intent movie_int = new Intent();
          movie_int.setAction(android.content.Intent.ACTION_VIEW);
          movie_int.setDataAndType(Uri.fromFile(file), "video/*");
          startActivity(movie_int);
        }
      }

      /*zip and gzip file selected (gzip will be implemented soon)*/
      else if (item_ext.equalsIgnoreCase(".zip")) { // || item_ext.equalsIgnoreCase(".gzip")) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        AlertDialog alert;
        zipped_target = flmg.getCurrentDir() + "/" + item;
        CharSequence[] option = {"Extract here", "Extract to..."};

        builder.setTitle("Extract");
        builder.setItems(
            option,
            new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                  case 0:
                    String dir = flmg.getCurrentDir();
                    handler.unZipFile(item, dir + "/");
                    break;

                  case 1:
                    detail_label.setText("Holding " + item + " to extract");
                    holding_zip = true;
                    break;
                }
              }
            });

        alert = builder.create();
        alert.show();
      }

      /*pdf file selected*/
      else if (item_ext.equalsIgnoreCase(".pdf")) {

        if (file.exists()) {
          Intent file_int = new Intent();
          file_int.setAction(android.content.Intent.ACTION_VIEW);
          file_int.setDataAndType(Uri.fromFile(file), "application/pdf");
          startActivity(file_int);
        }
      }

      /*Android application file*/
      else if (item_ext.equalsIgnoreCase(".apk")) {

        if (file.exists()) {
          Intent apk_int = new Intent();
          apk_int.setAction(android.content.Intent.ACTION_VIEW);
          apk_int.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
          startActivity(apk_int);
        }
      }

      /* HTML file */
      else if (item_ext.equalsIgnoreCase(".html")) {

        if (file.exists()) {
          Intent html_int = new Intent();
          html_int.setAction(android.content.Intent.ACTION_VIEW);
          html_int.setDataAndType(Uri.fromFile(file), "application/htmlviewer");
          try {
            startActivity(html_int);
          } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "Sorry, couldn't find a HTML view", Toast.LENGTH_SHORT).show();
          }
        }
      }
    }
  }