Add methods to BookmarkManager
This commit is contained in:
parent
7a01732a1e
commit
bdb3fe286d
@ -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);
|
||||||
}
|
}
|
||||||
bookmarkList.add(new HistoryItem(url, title));
|
if (!bookmarkUrls.contains(url)) {
|
||||||
|
number++;
|
||||||
|
bookmarkList.add(new HistoryItem(url, title));
|
||||||
|
}
|
||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,14 +327,17 @@ 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);
|
||||||
HistoryItem item = new HistoryItem();
|
if (!bookmarkUrls.contains(object.getString(URL))) {
|
||||||
item.setTitle(object.getString(TITLE));
|
HistoryItem item = new HistoryItem();
|
||||||
item.setUrl(object.getString(URL));
|
item.setTitle(object.getString(TITLE));
|
||||||
item.setFolder(object.getString(FOLDER));
|
item.setUrl(object.getString(URL));
|
||||||
item.setOrder(object.getInt(ORDER));
|
item.setFolder(object.getString(FOLDER));
|
||||||
addBookmark(item);
|
item.setOrder(object.getInt(ORDER));
|
||||||
|
addBookmark(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bookmarksReader.close();
|
bookmarksReader.close();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user