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 { @@ -32,16 +32,14 @@ public class BookmarkManager {
}
/**
* This method adds the the HistoryItem @param item to permanent bookmark
* storage
* This method adds the the HistoryItem item to permanent bookmark storage
*
* @param item
*/
public void addBookmark(HistoryItem item) {
File bookmarksExport = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter bookmarkWriter = new BufferedWriter(
new FileWriter(bookmarksExport, true));
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
JSONObject object = new JSONObject();
object.put(TITLE, item.getTitle());
object.put(URL, item.getUrl());
@ -58,16 +56,14 @@ public class BookmarkManager { @@ -58,16 +56,14 @@ public class BookmarkManager {
}
/**
* This method adds the @param list of HistoryItems to permanent bookmark
* storage
* This method adds the list of HistoryItems to permanent bookmark storage
*
* @param list
*/
public void addBookmarkList(List<HistoryItem> list) {
File bookmarksExport = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
try {
BufferedWriter bookmarkWriter = new BufferedWriter(
new FileWriter(bookmarksExport, true));
BufferedWriter bookmarkWriter = new BufferedWriter(new FileWriter(bookmarksFile, true));
for (HistoryItem item : list) {
JSONObject object = new JSONObject();
object.put(TITLE, item.getTitle());
@ -121,9 +117,9 @@ public class BookmarkManager { @@ -121,9 +117,9 @@ public class BookmarkManager {
*/
public List<HistoryItem> getBookmarks() {
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
File bookmarksImport = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
File bookmarksFile = new File(mContext.getFilesDir(), FILE_BOOKMARKS);
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);
@ -145,6 +141,74 @@ public class BookmarkManager { @@ -145,6 +141,74 @@ public class BookmarkManager {
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;
while ((line = bookmarksReader.readLine()) != null) {
JSONObject object = new JSONObject(line);
if (object.getString(FOLDER).equals(folder)) {
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 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
* permanent bookmark storage
@ -188,8 +252,8 @@ public class BookmarkManager { @@ -188,8 +252,8 @@ public class BookmarkManager {
}
/**
* This method imports the bookmarks from a backup @param file that is
* located on external storage
* This method imports the bookmarks from a backup file that is located on
* external storage
*
* @param dir
* @param file
@ -217,4 +281,32 @@ public class BookmarkManager { @@ -217,4 +281,32 @@ public class BookmarkManager {
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; @@ -8,13 +8,13 @@ import android.graphics.Bitmap;
public class HistoryItem implements Comparable<HistoryItem> {
// private variables
private int mId;
private String mUrl;
private String mTitle;
private Bitmap mBitmap;
private String mFolder;
private int mImageId;
private int mOrder;
private int mId = 0;
private String mUrl = "";
private String mTitle = "";
private String mFolder = "";
private Bitmap mBitmap = null;
private int mImageId = 0;
private int mOrder = 0;
// Empty constructor
public HistoryItem() {

Loading…
Cancel
Save