Browse Source

Add more methods to BookmarkManager

master
Anthony Restaino 10 years ago
parent
commit
7a01732a1e
  1. 120
      src/acr/browser/lightning/BookmarkManager.java
  2. 14
      src/acr/browser/lightning/HistoryItem.java

120
src/acr/browser/lightning/BookmarkManager.java

@ -32,16 +32,14 @@ public class BookmarkManager {
} }
/** /**
* This method adds the the HistoryItem @param item to permanent bookmark * This method adds the the HistoryItem item to permanent bookmark storage
* storage
* *
* @param item * @param item
*/ */
public void addBookmark(HistoryItem item) { public void addBookmark(HistoryItem item) {
File bookmarksExport = new File(mContext.getFilesDir(), FILE_BOOKMARKS); File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try { try {
BufferedWriter bookmarkWriter = new BufferedWriter( BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
new FileWriter(bookmarksExport, true));
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
object.put(TITLE, item.getTitle()); object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl()); object.put(URL, item.getUrl());
@ -58,16 +56,14 @@ public class BookmarkManager {
} }
/** /**
* This method adds the @param list of HistoryItems to permanent bookmark * This method adds the list of HistoryItems to permanent bookmark storage
* storage
* *
* @param list * @param list
*/ */
public void addBookmarkList(List<HistoryItem> list) { public void addBookmarkList(List<HistoryItem> list) {
File bookmarksExport = new File(mContext.getFilesDir(), FILE_BOOKMARKS); File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try { try {
BufferedWriter bookmarkWriter = new BufferedWriter( BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
new FileWriter(bookmarksExport, true));
for (HistoryItem item : list) { for (HistoryItem item : list) {
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
object.put(TITLE, item.getTitle()); object.put(TITLE, item.getTitle());
@ -121,12 +117,45 @@ public class BookmarkManager {
*/ */
public List<HistoryItem> getBookmarks() { public List<HistoryItem> getBookmarks() {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>(); List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookmarksImport = new File(mContext.getFilesDir(), FILE_BOOKMARKS); File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try { try {
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksImport)); BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile));
String line;
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
HistoryItem item = new HistoryItem();
item.setTitle(object.getString(TITLE));
item.setUrl(object.getString(URL));
item.setFolder(object.getString(FOLDER));
item.setOrder(object.getInt(ORDER));
bookmarks.add(item);
}
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 bookmarks located in the specified folder
*
* @param folder
* @return
*/
public List<HistoryItem> getBookmarksFromFolder(String folder) {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksFile));
String line; String line;
while ((line = bookmarksReader.readLine()) != null) { while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line); JSONObject object = new JSONObject(line);
if (object.getString(FOLDER).equals(folder)) {
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));
@ -134,6 +163,7 @@ public class BookmarkManager {
item.setOrder(object.getInt(ORDER)); item.setOrder(object.getInt(ORDER));
bookmarks.add(item); bookmarks.add(item);
} }
}
bookmarksReader.close(); bookmarksReader.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
@ -145,6 +175,40 @@ public class BookmarkManager {
return bookmarks; return bookmarks;
} }
/**
* This method returns a list of all folders
*
* @return
*/
public List<HistoryItem> getFolders() {
List<HistoryItem> folders = new ArrayList<HistoryItem>();
List<String> folderNameList = 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);
String folderName = object.getString(FOLDER);
if (!folderName.isEmpty() && !folderNameList.contains(folderName)) {
HistoryItem item = new HistoryItem();
item.setTitle(folderName);
item.setUrl(Constants.FOLDER + folderName);
folderNameList.add(folderName);
folders.add(item);
}
}
bookmarksReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return folders;
}
/** /**
* This method imports all bookmarks that are included in the device's * This method imports all bookmarks that are included in the device's
* permanent bookmark storage * permanent bookmark storage
@ -188,8 +252,8 @@ public class BookmarkManager {
} }
/** /**
* This method imports the bookmarks from a backup @param file that is * This method imports the bookmarks from a backup file that is located on
* located on external storage * external storage
* *
* @param dir * @param dir
* @param file * @param file
@ -217,4 +281,32 @@ public class BookmarkManager {
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* This method overwrites the entire bookmark file with the list of
* bookmarks. This is useful when an edit has been made to one or more
* bookmarks in the list
*
* @param list
*/
public void overwriteBookmarks(List<HistoryItem> list) {
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, false));
for (HistoryItem item : list) {
JSONObject object = new JSONObject();
object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl());
object.put(FOLDER, item.getFolder());
object.put(ORDER, item.getOrder());
bookmarkWriter.write(object.toString());
bookmarkWriter.newLine();
}
bookmarkWriter.close();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
} }

14
src/acr/browser/lightning/HistoryItem.java

@ -8,13 +8,13 @@ import android.graphics.Bitmap;
public class HistoryItem implements Comparable<HistoryItem> { public class HistoryItem implements Comparable<HistoryItem> {
// private variables // private variables
private int mId; private int mId = 0;
private String mUrl; private String mUrl = "";
private String mTitle; private String mTitle = "";
private Bitmap mBitmap; private String mFolder = "";
private String mFolder; private Bitmap mBitmap = null;
private int mImageId; private int mImageId = 0;
private int mOrder; private int mOrder = 0;
// Empty constructor // Empty constructor
public HistoryItem() { public HistoryItem() {

Loading…
Cancel
Save