/**
   * Get the history.
   *
   * @return A list of lists of HistoryItem. Each top-level list represents a day of history.
   */
  public Cursor fetchHistory() {

    int historyLimit;
    try {
      historyLimit =
          Integer.parseInt(
              PreferenceManager.getDefaultSharedPreferences(mContext)
                  .getString(Constants.PREFERENCES_BROWSER_HISTORY_SIZE, "90"));
    } catch (Exception e) {
      historyLimit = 90;
    }

    Date limit = DateUtils.getDateAtMidnight(-historyLimit);

    String whereClause =
        "("
            + HISTORY_LAST_VISITED_DATE
            + " <= \""
            + DateUtils.getDateAsUniversalString(mContext, new Date())
            + "\") AND "
            + "("
            + HISTORY_LAST_VISITED_DATE
            + " > \""
            + DateUtils.getDateAsUniversalString(mContext, limit)
            + "\")";

    return mDb.query(
        HISTORY_DATABASE_TABLE,
        new String[] {HISTORY_ROWID, HISTORY_TITLE, HISTORY_URL, HISTORY_LAST_VISITED_DATE},
        whereClause,
        null,
        null,
        null,
        HISTORY_LAST_VISITED_DATE + " DESC");
  }
 /** Delete all records from history witch have a visited date prior to the history max age. */
 public void truncateHistory() {
   mDb.execSQL(
       "DELETE FROM "
           + HISTORY_DATABASE_TABLE
           + " WHERE "
           + HISTORY_LAST_VISITED_DATE
           + " < \""
           + DateUtils.getHistoryLimit(mContext)
           + "\";");
 }
  /**
   * Update the history. If the url is already present, the visited date is updated. If not, a new
   * record is created.
   *
   * @param title The page title.
   * @param url The page url.
   */
  public void updateHistory(String title, String url) {

    long existingId = getHistoryItemIdByUrl(url);

    if (existingId != -1) {
      ContentValues args = new ContentValues();
      args.put(HISTORY_TITLE, title);
      args.put(HISTORY_LAST_VISITED_DATE, DateUtils.getNow(mContext));

      mDb.update(HISTORY_DATABASE_TABLE, args, HISTORY_ROWID + "=" + existingId, null);
    } else {
      ContentValues initialValues = new ContentValues();
      initialValues.put(HISTORY_TITLE, title);
      initialValues.put(HISTORY_URL, url);
      initialValues.put(HISTORY_LAST_VISITED_DATE, DateUtils.getNow(mContext));

      mDb.insert(HISTORY_DATABASE_TABLE, null, initialValues);
    }
  }
  /** Display a confirmation dialog and perform the bookmarks export. */
  private void exportBookmarks() {

    final String fileName = DateUtils.getNowForFileName() + ".xml";

    ApplicationUtils.showOkCancelDialog(
        this,
        android.R.drawable.ic_dialog_info,
        this.getString(R.string.BookmarksListActivity_ExportDialogTitle),
        String.format(
            this.getString(R.string.BookmarksListActivity_ExportDialogMessage),
            IOUtils.getBookmarksExportFolder().getAbsolutePath() + "/" + fileName),
        new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {

            doExportBookmarks(fileName);
          }
        });
  }
    @Override
    public void run() {

      File file = new File(IOUtils.getBookmarksExportFolder(), mFileName);

      if ((file != null) && (file.exists()) && (file.canRead())) {

        try {

          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();

          Document document = builder.parse(file);

          Element root = document.getDocumentElement();

          if ((root != null) && (root.getNodeName().equals("bookmarkslist"))) {

            NodeList bookmarksList = root.getElementsByTagName("bookmark");

            Node bookmark;
            NodeList bookmarkItems;
            String title;
            String url;
            String creationDate;
            Node item;

            for (int i = 0; i < bookmarksList.getLength(); i++) {

              bookmark = bookmarksList.item(i);

              if (bookmark != null) {

                title = null;
                url = null;
                creationDate = null;

                bookmarkItems = bookmark.getChildNodes();

                for (int j = 0; j < bookmarkItems.getLength(); j++) {

                  item = bookmarkItems.item(j);

                  if ((item != null) && (item.getNodeName() != null)) {
                    if (item.getNodeName().equals("title")) {
                      title = getNodeContent(item);
                    } else if (item.getNodeName().equals("url")) {
                      url = URLDecoder.decode(getNodeContent(item));
                    } else if (item.getNodeName().equals("creationdate")) {
                      creationDate = getNodeContent(item);
                    }
                  }
                }

                if ((creationDate == null) || (creationDate.length() == 0)) {
                  creationDate = DateUtils.getNow(mContext);
                }

                mDbAdapter.addBookmark(title, url, creationDate);
              }
            }

          } else {
            Log.i("Bookmark import", "Empty or invalid file.");
          }

        } catch (ParserConfigurationException e) {
          Log.w("Bookmark import failed", e.getMessage());
        } catch (SAXException e) {
          Log.w("Bookmark import failed", e.getMessage());
        } catch (IOException e) {
          Log.w("Bookmark import failed", e.getMessage());
        }
      }

      handler.sendEmptyMessage(0);
    }
 /**
  * Add a bookmark to database.
  *
  * @param title The title.
  * @param url The url.
  * @return The new bookmark id.
  */
 public long addBookmark(String title, String url) {
   return addBookmark(title, url, DateUtils.getNow(mContext), 0);
 }