Add methods to BookmarkManager

This commit is contained in:
Anthony Restaino 2014-08-21 23:08:22 -04:00
parent 7a01732a1e
commit bdb3fe286d

View File

@ -38,6 +38,12 @@ public class BookmarkManager {
*/ */
public void addBookmark(HistoryItem item) { public void addBookmark(HistoryItem item) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS); File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
List<String> bookmarkUrls = getBookmarkUrls();
if (bookmarkUrls.contains(item.getUrl())) {
return;
}
try { try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true)); BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
@ -81,12 +87,42 @@ public class BookmarkManager {
} }
} }
/**
* This method deletes the bookmark with the given url
*
* @param url
*/
public void deleteBookmark(String url) {
List<HistoryItem> list = new ArrayList<HistoryItem>();
list = getBookmarks();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(bookmarksFile, false));
for (HistoryItem item : list) {
if (!item.getUrl().equalsIgnoreCase(url)) {
JSONObject object = new JSONObject();
object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder());
fileWriter.write(object.toString());
fileWriter.newLine();
}
}
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
/** /**
* This method exports the stored bookmarks to a text file in the device's * This method exports the stored bookmarks to a text file in the device's
* external download directory * external download directory
*/ */
public void exportBookmarks() { public void exportBookmarks() {
List<HistoryItem> bookmarkList = Utils.getBookmarks(mContext); List<HistoryItem> bookmarkList = getBookmarks();
File bookmarksExport = new File( File bookmarksExport = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"BookmarksExport.txt"); "BookmarksExport.txt");
@ -175,6 +211,32 @@ public class BookmarkManager {
return bookmarks; return bookmarks;
} }
/**
* Method is used internally for searching the bookmarks
*
* @return
*/
private List<String> getBookmarkUrls() {
List<String> bookmarks = new ArrayList<String>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile));
String line;
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
bookmarks.add(object.getString(URL));
}
bookmarksReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return bookmarks;
}
/** /**
* This method returns a list of all folders * This method returns a list of all folders
* *
@ -226,16 +288,18 @@ public class BookmarkManager {
String title, url; String title, url;
int number = 0; int number = 0;
List<String> bookmarkUrls = getBookmarkUrls();
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
number++;
title = cursor.getString(0); title = cursor.getString(0);
url = cursor.getString(1); url = cursor.getString(1);
if (title.isEmpty()) { if (title.isEmpty()) {
title = Utils.getDomainName(url); title = Utils.getDomainName(url);
} }
if (!bookmarkUrls.contains(url)) {
number++;
bookmarkList.add(new HistoryItem(url, title)); bookmarkList.add(new HistoryItem(url, title));
}
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
@ -263,8 +327,10 @@ public class BookmarkManager {
try { try {
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksImport)); BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksImport));
String line; String line;
List<String> bookmarkUrls = getBookmarkUrls();
while ((line = bookmarksReader.readLine()) != null) { while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line); JSONObject object = new JSONObject(line);
if (!bookmarkUrls.contains(object.getString(URL))) {
HistoryItem item = new HistoryItem(); HistoryItem item = new HistoryItem();
item.setTitle(object.getString(TITLE)); item.setTitle(object.getString(TITLE));
item.setUrl(object.getString(URL)); item.setUrl(object.getString(URL));
@ -272,6 +338,7 @@ public class BookmarkManager {
item.setOrder(object.getInt(ORDER)); item.setOrder(object.getInt(ORDER));
addBookmark(item); addBookmark(item);
} }
}
bookmarksReader.close(); bookmarksReader.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();