Add more methods to BookmarkManager
This commit is contained in:
parent
09b3f40d1c
commit
7a01732a1e
@ -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,9 +117,9 @@ 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;
|
String line;
|
||||||
while ((line = bookmarksReader.readLine()) != null) {
|
while ((line = bookmarksReader.readLine()) != null) {
|
||||||
JSONObject object = new JSONObject(line);
|
JSONObject object = new JSONObject(line);
|
||||||
@ -145,6 +141,74 @@ public class BookmarkManager {
|
|||||||
return bookmarks;
|
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
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user