@ -30,398 +30,422 @@ import acr.browser.lightning.utils.Utils;
public class BookmarkManager {
public class BookmarkManager {
private final Context mContext ;
private final Context mContext ;
private static final String TITLE = "title" ;
private static final String TITLE = "title" ;
private static final String URL = "url" ;
private static final String URL = "url" ;
private static final String FOLDER = "folder" ;
private static final String FOLDER = "folder" ;
private static final String ORDER = "order" ;
private static final String ORDER = "order" ;
private static final String FILE_BOOKMARKS = "bookmarks.dat" ;
private static final String FILE_BOOKMARKS = "bookmarks.dat" ;
private static SortedMap < String , Integer > mBookmarkMap = new TreeMap < > (
private static SortedMap < String , Integer > mBookmarkMap = new TreeMap < > (
String . CASE_INSENSITIVE_ORDER ) ;
String . CASE_INSENSITIVE_ORDER ) ;
private static BookmarkManager mInstance ;
private static BookmarkManager mInstance ;
public static BookmarkManager getInstance ( Context context ) {
public static BookmarkManager getInstance ( Context context ) {
if ( mInstance = = null ) {
if ( mInstance = = null ) {
mInstance = new BookmarkManager ( context ) ;
mInstance = new BookmarkManager ( context ) ;
}
}
return mInstance ;
return mInstance ;
}
}
private BookmarkManager ( Context context ) {
private BookmarkManager ( Context context ) {
mContext = context ;
mContext = context ;
mBookmarkMap = getBookmarkUrls ( ) ;
mBookmarkMap = getBookmarkUrls ( ) ;
}
}
/ * *
/ * *
* This method adds the the HistoryItem item to permanent bookmark storage
* This method adds the the HistoryItem item to permanent bookmark storage
*
*
* @param item
* @param item the item to add
* /
* /
public synchronized boolean addBookmark ( HistoryItem item ) {
public synchronized boolean addBookmark ( HistoryItem item ) {
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
if ( item . getUrl ( ) = = null | | mBookmarkMap . containsKey ( item . getUrl ( ) ) ) {
if ( item . getUrl ( ) = = null | | mBookmarkMap . containsKey ( item . getUrl ( ) ) ) {
return false ;
return false ;
}
}
try {
BufferedWriter bookmarkWriter = null ;
BufferedWriter bookmarkWriter = new BufferedWriter ( new FileWriter ( bookmarksFile , true ) ) ;
try {
JSONObject object = new JSONObject ( ) ;
bookmarkWriter = new BufferedWriter ( new FileWriter ( bookmarksFile , true ) ) ;
object . put ( TITLE , item . getTitle ( ) ) ;
JSONObject object = new JSONObject ( ) ;
object . put ( URL , item . getUrl ( ) ) ;
object . put ( TITLE , item . getTitle ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
object . put ( URL , item . getUrl ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
bookmarkWriter . write ( object . toString ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
bookmarkWriter . newLine ( ) ;
bookmarkWriter . write ( object . toString ( ) ) ;
bookmarkWriter . close ( ) ;
bookmarkWriter . newLine ( ) ;
mBookmarkMap . put ( item . getUrl ( ) , 1 ) ;
bookmarkWriter . close ( ) ;
} catch ( IOException | JSONException e ) {
mBookmarkMap . put ( item . getUrl ( ) , 1 ) ;
e . printStackTrace ( ) ;
} catch ( IOException | JSONException e ) {
}
e . printStackTrace ( ) ;
return true ;
} finally {
}
Utils . close ( bookmarkWriter ) ;
}
return true ;
}
/ * *
/ * *
* This method adds the list of HistoryItems to permanent bookmark storage
* This method adds the list of HistoryItems to permanent bookmark storage
*
*
* @param list
* @param list the list of HistoryItems to add to bookmarks
* /
* /
public synchronized void addBookmarkList ( List < HistoryItem > list ) {
private synchronized void addBookmarkList ( List < HistoryItem > list ) {
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
try {
BufferedWriter bookmarkWriter = null ;
BufferedWriter bookmarkWriter = new BufferedWriter ( new FileWriter ( bookmarksFile , true ) ) ;
try {
for ( HistoryItem item : list ) {
bookmarkWriter = new BufferedWriter ( new FileWriter ( bookmarksFile , true ) ) ;
if ( item . getUrl ( ) ! = null & & ! mBookmarkMap . containsKey ( item . getUrl ( ) ) ) {
JSONObject object = new JSONObject ( ) ;
JSONObject object = new JSONObject ( ) ;
for ( HistoryItem item : list ) {
object . put ( TITLE , item . getTitle ( ) ) ;
if ( item . getUrl ( ) ! = null & & ! mBookmarkMap . containsKey ( item . getUrl ( ) ) ) {
object . put ( URL , item . getUrl ( ) ) ;
object . put ( TITLE , item . getTitle ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
object . put ( URL , item . getUrl ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
bookmarkWriter . write ( object . toString ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
bookmarkWriter . newLine ( ) ;
bookmarkWriter . write ( object . toString ( ) ) ;
mBookmarkMap . put ( item . getUrl ( ) , 1 ) ;
bookmarkWriter . newLine ( ) ;
}
mBookmarkMap . put ( item . getUrl ( ) , 1 ) ;
}
}
bookmarkWriter . close ( ) ;
}
} catch ( IOException | JSONException e ) {
} catch ( IOException | JSONException e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
}
} finally {
}
Utils . close ( bookmarkWriter ) ;
}
}
/ * *
/ * *
* This method deletes the bookmark with the given url
* This method deletes the bookmark with the given url
*
*
* @param url
* @param url the url of the bookmark to delete
* /
* /
public synchronized boolean deleteBookmark ( String url ) {
public synchronized boolean deleteBookmark ( String url ) {
List < HistoryItem > list ;
List < HistoryItem > list ;
if ( url = = null ) {
if ( url = = null ) {
return false ;
return false ;
}
}
mBookmarkMap . remove ( url ) ;
mBookmarkMap . remove ( url ) ;
list = getBookmarks ( false ) ;
list = getBookmarks ( false ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
boolean bookmarkDeleted = false ;
boolean bookmarkDeleted = false ;
try {
BufferedWriter fileWriter = null ;
BufferedWriter fileWriter = new BufferedWriter ( new FileWriter ( bookmarksFile , false ) ) ;
try {
for ( HistoryItem item : list ) {
fileWriter = new BufferedWriter ( new FileWriter ( bookmarksFile , false ) ) ;
if ( ! item . getUrl ( ) . equalsIgnoreCase ( url ) ) {
JSONObject object = new JSONObject ( ) ;
JSONObject object = new JSONObject ( ) ;
for ( HistoryItem item : list ) {
object . put ( TITLE , item . getTitle ( ) ) ;
if ( ! item . getUrl ( ) . equalsIgnoreCase ( url ) ) {
object . put ( URL , item . getUrl ( ) ) ;
object . put ( TITLE , item . getTitle ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
object . put ( URL , item . getUrl ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
fileWriter . write ( object . toString ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
fileWriter . newLine ( ) ;
fileWriter . write ( object . toString ( ) ) ;
} else {
fileWriter . newLine ( ) ;
bookmarkDeleted = true ;
} else {
}
bookmarkDeleted = true ;
}
}
fileWriter . close ( ) ;
}
} catch ( IOException | JSONException e ) {
} catch ( IOException | JSONException e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
}
} finally {
return bookmarkDeleted ;
Utils . close ( fileWriter ) ;
}
}
return bookmarkDeleted ;
}
/ * *
/ * *
* 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 synchronized void exportBookmarks ( ) {
public synchronized void exportBookmarks ( ) {
List < HistoryItem > bookmarkList = getBookmarks ( true ) ;
List < HistoryItem > bookmarkList = getBookmarks ( true ) ;
File bookmarksExport = new File (
File bookmarksExport = new File (
Environment . getExternalStoragePublicDirectory ( Environment . DIRECTORY_DOWNLOADS ) ,
Environment . getExternalStoragePublicDirectory ( Environment . DIRECTORY_DOWNLOADS ) ,
"BookmarksExport.txt" ) ;
"BookmarksExport.txt" ) ;
int counter = 0 ;
int counter = 0 ;
while ( bookmarksExport . exists ( ) ) {
while ( bookmarksExport . exists ( ) ) {
counter + + ;
counter + + ;
bookmarksExport = new File (
bookmarksExport = new File (
Environment . getExternalStoragePublicDirectory ( Environment . DIRECTORY_DOWNLOADS ) ,
Environment . getExternalStoragePublicDirectory ( Environment . DIRECTORY_DOWNLOADS ) ,
"BookmarksExport-" + counter + ".txt" ) ;
"BookmarksExport-" + counter + ".txt" ) ;
}
}
try {
BufferedWriter bookmarkWriter = null ;
BufferedWriter bookmarkWriter = new BufferedWriter ( new FileWriter ( bookmarksExport ,
try {
false ) ) ;
bookmarkWriter = new BufferedWriter ( new FileWriter ( bookmarksExport ,
for ( HistoryItem item : bookmarkList ) {
false ) ) ;
JSONObject object = new JSONObject ( ) ;
JSONObject object = new JSONObject ( ) ;
object . put ( TITLE , item . getTitle ( ) ) ;
for ( HistoryItem item : bookmarkList ) {
object . put ( URL , item . getUrl ( ) ) ;
object . put ( TITLE , item . getTitle ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
object . put ( URL , item . getUrl ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
object . put ( FOLDER , item . getFolder ( ) ) ;
bookmarkWriter . write ( object . toString ( ) ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
bookmarkWriter . newLine ( ) ;
bookmarkWriter . write ( object . toString ( ) ) ;
}
bookmarkWriter . newLine ( ) ;
bookmarkWriter . close ( ) ;
}
Toast . makeText (
if ( mContext ! = null )
mContext ,
Toast . makeText (
mContext . getString ( R . string . bookmark_export_path ) + " "
mContext ,
+ bookmarksExport . getPath ( ) , Toast . LENGTH_SHORT ) . show ( ) ;
mContext . getString ( R . string . bookmark_export_path ) + ' '
} catch ( IOException | JSONException e ) {
+ bookmarksExport . getPath ( ) , Toast . LENGTH_SHORT ) . show ( ) ;
e . printStackTrace ( ) ;
} catch ( IOException | JSONException e ) {
}
e . printStackTrace ( ) ;
} finally {
Utils . close ( bookmarkWriter ) ;
}
}
}
/ * *
/ * *
* This method returns a list of all stored bookmarks
* This method returns a list of all stored bookmarks
*
*
* @return
* @return returns a list of bookmarks that can be sorted
* /
* /
public synchronized List < HistoryItem > getBookmarks ( boolean sort ) {
public synchronized List < HistoryItem > getBookmarks ( boolean sort ) {
List < HistoryItem > bookmarks = new ArrayList < > ( ) ;
List < HistoryItem > bookmarks = new ArrayList < > ( ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
try {
BufferedReader bookmarksReader = null ;
BufferedReader bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
try {
String line ;
bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
String line ;
JSONObject object = new JSONObject ( line ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
HistoryItem item = new HistoryItem ( ) ;
JSONObject object = new JSONObject ( line ) ;
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 ) ) ;
item . setImageId ( R . drawable . ic_bookmark ) ;
item . setOrder ( object . getInt ( ORDER ) ) ;
bookmarks . add ( item ) ;
item . setImageId ( R . drawable . ic_bookmark ) ;
}
bookmarks . add ( item ) ;
bookmarksReader . close ( ) ;
}
} catch ( IOException | JSONException e ) {
} catch ( IOException | JSONException e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
}
} finally {
if ( sort ) {
Utils . close ( bookmarksReader ) ;
Collections . sort ( bookmarks , new SortIgnoreCase ( ) ) ;
}
}
if ( sort ) {
return bookmarks ;
Collections . sort ( bookmarks , new SortIgnoreCase ( ) ) ;
}
}
return bookmarks ;
}
/ * *
/ * *
* This method returns a list of bookmarks located in the specified folder
* This method returns a list of bookmarks located in the specified folder
*
*
* @param folder
* @param folder the name of the folder to retrieve bookmarks from
* @return
* @return a list of bookmarks found in that folder
* /
* /
public synchronized List < HistoryItem > getBookmarksFromFolder ( String folder ) {
public synchronized List < HistoryItem > getBookmarksFromFolder ( String folder ) {
List < HistoryItem > bookmarks = new ArrayList < > ( ) ;
List < HistoryItem > bookmarks = new ArrayList < > ( ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
try {
BufferedReader bookmarksReader = null ;
BufferedReader bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
try {
String line ;
bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
String line ;
JSONObject object = new JSONObject ( line ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
if ( object . getString ( FOLDER ) . equals ( folder ) ) {
JSONObject object = new JSONObject ( line ) ;
HistoryItem item = new HistoryItem ( ) ;
if ( object . getString ( FOLDER ) . equals ( folder ) ) {
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 ) ) ;
item . setImageId ( R . drawable . ic_bookmark ) ;
item . setOrder ( object . getInt ( ORDER ) ) ;
bookmarks . add ( item ) ;
item . setImageId ( R . drawable . ic_bookmark ) ;
}
bookmarks . add ( item ) ;
}
}
bookmarksReader . close ( ) ;
}
} catch ( IOException | JSONException e ) {
bookmarksReader . close ( ) ;
e . printStackTrace ( ) ;
} catch ( IOException | JSONException e ) {
}
e . printStackTrace ( ) ;
return bookmarks ;
} finally {
}
Utils . close ( bookmarksReader ) ;
}
return bookmarks ;
}
/ * *
/ * *
* Method is used internally for searching the bookmarks
* Method is used internally for searching the bookmarks
*
*
* @return
* @return a sorted map of all bookmarks , useful for seeing if a bookmark exists
* /
* /
private synchronized SortedMap < String , Integer > getBookmarkUrls ( ) {
private synchronized SortedMap < String , Integer > getBookmarkUrls ( ) {
SortedMap < String , Integer > map = new TreeMap < > ( String . CASE_INSENSITIVE_ORDER ) ;
SortedMap < String , Integer > map = new TreeMap < > ( String . CASE_INSENSITIVE_ORDER ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
try {
BufferedReader bookmarksReader = null ;
BufferedReader bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
try {
String line ;
bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
String line ;
JSONObject object = new JSONObject ( line ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
map . put ( object . getString ( URL ) , 1 ) ;
JSONObject object = new JSONObject ( line ) ;
}
map . put ( object . getString ( URL ) , 1 ) ;
bookmarksReader . close ( ) ;
}
} catch ( JSONException | IOException e ) {
bookmarksReader . close ( ) ;
e . printStackTrace ( ) ;
} catch ( JSONException | IOException e ) {
}
e . printStackTrace ( ) ;
return map ;
} finally {
}
Utils . close ( bookmarksReader ) ;
}
return map ;
}
/ * *
/ * *
* This method returns a list of all folders
* This method returns a list of all folders
*
*
* @return
* @return a list of all folders
* /
* /
public synchronized List < HistoryItem > getFolders ( ) {
public synchronized List < HistoryItem > getFolders ( ) {
List < HistoryItem > folders = new ArrayList < > ( ) ;
List < HistoryItem > folders = new ArrayList < > ( ) ;
SortedMap < String , Integer > folderMap = new TreeMap < > (
SortedMap < String , Integer > folderMap = new TreeMap < > (
String . CASE_INSENSITIVE_ORDER ) ;
String . CASE_INSENSITIVE_ORDER ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
try {
BufferedReader bookmarksReader = null ;
BufferedReader bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
try {
String line ;
bookmarksReader = new BufferedReader ( new FileReader ( bookmarksFile ) ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
String line ;
JSONObject object = new JSONObject ( line ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
String folderName = object . getString ( FOLDER ) ;
JSONObject object = new JSONObject ( line ) ;
if ( ! folderName . isEmpty ( ) & & ! folderMap . containsKey ( folderName ) ) {
String folderName = object . getString ( FOLDER ) ;
HistoryItem item = new HistoryItem ( ) ;
if ( ! folderName . isEmpty ( ) & & ! folderMap . containsKey ( folderName ) ) {
item . setTitle ( folderName ) ;
HistoryItem item = new HistoryItem ( ) ;
item . setUrl ( Constants . FOLDER + folderName ) ;
item . setTitle ( folderName ) ;
folderMap . put ( folderName , 1 ) ;
item . setUrl ( Constants . FOLDER + folderName ) ;
folders . add ( item ) ;
folderMap . put ( folderName , 1 ) ;
}
folders . add ( item ) ;
}
}
bookmarksReader . close ( ) ;
}
} catch ( IOException | JSONException e ) {
} catch ( IOException | JSONException e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
}
} finally {
return folders ;
Utils . close ( bookmarksReader ) ;
}
}
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
* /
* /
public synchronized void importBookmarksFromBrowser ( Context context ) {
public synchronized void importBookmarksFromBrowser ( Context context ) {
if ( PreferenceManager . getInstance ( ) . getSystemBrowserPresent ( ) ) {
if ( PreferenceManager . getInstance ( ) . getSystemBrowserPresent ( ) ) {
List < HistoryItem > bookmarkList = new ArrayList < > ( ) ;
List < HistoryItem > bookmarkList = new ArrayList < > ( ) ;
String [ ] columns = new String [ ] { Browser . BookmarkColumns . TITLE ,
String [ ] columns = new String [ ] { Browser . BookmarkColumns . TITLE ,
Browser . BookmarkColumns . URL } ;
Browser . BookmarkColumns . URL } ;
String selection = Browser . BookmarkColumns . BOOKMARK + " = 1" ;
String selection = Browser . BookmarkColumns . BOOKMARK + " = 1" ;
Cursor cursor = mContext . getContentResolver ( ) . query ( Browser . BOOKMARKS_URI , columns ,
Cursor cursor = mContext . getContentResolver ( ) . query ( Browser . BOOKMARKS_URI , columns ,
selection , null , null ) ;
selection , null , null ) ;
if ( cursor = = null )
return ;
String title , url ;
int number = 0 ;
if ( cursor . moveToFirst ( ) ) {
do {
title = cursor . getString ( 0 ) ;
url = cursor . getString ( 1 ) ;
if ( title . isEmpty ( ) ) {
title = Utils . getDomainName ( url ) ;
}
number + + ;
bookmarkList . add ( new HistoryItem ( url , title ) ) ;
} while ( cursor . moveToNext ( ) ) ;
}
String title , url ;
cursor . close ( ) ;
int number = 0 ;
addBookmarkList ( bookmarkList ) ;
if ( cursor . moveToFirst ( ) ) {
do {
title = cursor . getString ( 0 ) ;
url = cursor . getString ( 1 ) ;
if ( title . isEmpty ( ) ) {
title = Utils . getDomainName ( url ) ;
}
number + + ;
bookmarkList . add ( new HistoryItem ( url , title ) ) ;
} while ( cursor . moveToNext ( ) ) ;
}
cursor . close ( ) ;
Utils . showToast ( context ,
addBookmarkList ( bookmarkList ) ;
number + " " + mContext . getResources ( ) . getString ( R . string . message_import ) ) ;
} else {
Utils . createInformativeDialog ( context ,
mContext . getResources ( ) . getString ( R . string . title_error ) , mContext
. getResources ( ) . getString ( R . string . dialog_import_error ) ) ;
}
}
Utils . showToast ( mContext ,
/ * *
number + " " + mContext . getResources ( ) . getString ( R . string . message_import ) ) ;
* This method imports the bookmarks from a backup file that is located on
} else {
* external storage
Utils . createInformativeDialog ( context ,
*
mContext . getResources ( ) . getString ( R . string . title_error ) , mContext
* @param file the file to attempt to import bookmarks from
. getResources ( ) . getString ( R . string . dialog_import_error ) ) ;
* /
}
public synchronized void importBookmarksFromFile ( File file , Context context ) {
}
if ( file = = null ) {
return ;
}
List < HistoryItem > list = new ArrayList < > ( ) ;
BufferedReader bookmarksReader = null ;
try {
bookmarksReader = new BufferedReader ( new FileReader ( file ) ) ;
String line ;
int number = 0 ;
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 ) ) ;
list . add ( item ) ;
number + + ;
}
addBookmarkList ( list ) ;
Utils . showToast ( context ,
number + " " + mContext . getResources ( ) . getString ( R . string . message_import ) ) ;
} catch ( IOException | JSONException e ) {
e . printStackTrace ( ) ;
Utils . createInformativeDialog ( context ,
mContext . getResources ( ) . getString ( R . string . title_error ) , mContext
. getResources ( ) . getString ( R . string . import_bookmark_error ) ) ;
} finally {
Utils . close ( bookmarksReader ) ;
}
}
/ * *
/ * *
* This method imports the bookmarks from a backup file that is located on
* This method overwrites the entire bookmark file with the list of
* external storage
* bookmarks . This is useful when an edit has been made to one or more
*
* bookmarks in the list
* @param dir
*
* @param file
* @param list the list of bookmarks to overwrite the old ones with
* /
* /
public synchronized void importBookmarksFromFile ( File file , Context context ) {
public synchronized void overwriteBookmarks ( List < HistoryItem > list ) {
if ( file = = null ) {
File bookmarksFile = new File ( mContext . getFilesDir ( ) , FILE_BOOKMARKS ) ;
return ;
BufferedWriter bookmarkWriter = null ;
}
try {
List < HistoryItem > list = new ArrayList < > ( ) ;
bookmarkWriter = new BufferedWriter ( new FileWriter ( bookmarksFile , false ) ) ;
try {
JSONObject object = new JSONObject ( ) ;
BufferedReader bookmarksReader = new BufferedReader ( new FileReader ( file ) ) ;
for ( HistoryItem item : list ) {
String line ;
object . put ( TITLE , item . getTitle ( ) ) ;
int number = 0 ;
object . put ( URL , item . getUrl ( ) ) ;
while ( ( line = bookmarksReader . readLine ( ) ) ! = null ) {
object . put ( FOLDER , item . getFolder ( ) ) ;
JSONObject object = new JSONObject ( line ) ;
object . put ( ORDER , item . getOrder ( ) ) ;
HistoryItem item = new HistoryItem ( ) ;
bookmarkWriter . write ( object . toString ( ) ) ;
item . setTitle ( object . getString ( TITLE ) ) ;
bookmarkWriter . newLine ( ) ;
item . setUrl ( object . getString ( URL ) ) ;
}
item . setFolder ( object . getString ( FOLDER ) ) ;
} catch ( IOException | JSONException e ) {
item . setOrder ( object . getInt ( ORDER ) ) ;
e . printStackTrace ( ) ;
list . add ( item ) ;
} finally {
number + + ;
Utils . close ( bookmarkWriter ) ;
}
}
bookmarksReader . close ( ) ;
}
addBookmarkList ( list ) ;
Utils . showToast ( mContext ,
number + " " + mContext . getResources ( ) . getString ( R . string . message_import ) ) ;
} catch ( IOException | JSONException e ) {
e . printStackTrace ( ) ;
Utils . createInformativeDialog ( context ,
mContext . getResources ( ) . getString ( R . string . title_error ) , mContext
. getResources ( ) . getString ( R . string . import_bookmark_error ) ) ;
}
}
/ * *
private class SortIgnoreCase implements Comparator < HistoryItem > {
* 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 synchronized 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 | JSONException e ) {
e . printStackTrace ( ) ;
}
}
private class SortIgnoreCase implements Comparator < HistoryItem > {
public int compare ( HistoryItem o1 , HistoryItem o2 ) {
if ( o1 = = null | | o2 = = null | | o1 . getTitle ( ) = = null | | o2 . getTitle ( ) = = null ) {
return 0 ;
}
return o1 . getTitle ( ) . toLowerCase ( Locale . getDefault ( ) )
. compareTo ( o2 . getTitle ( ) . toLowerCase ( Locale . getDefault ( ) ) ) ;
}
public int compare ( HistoryItem o1 , HistoryItem o2 ) {
}
if ( o1 = = null | | o2 = = null | | o1 . getTitle ( ) = = null | | o2 . getTitle ( ) = = null ) {
return 0 ;
}
return o1 . getTitle ( ) . toLowerCase ( Locale . getDefault ( ) )
. compareTo ( o2 . getTitle ( ) . toLowerCase ( Locale . getDefault ( ) ) ) ;
}
}
private static final String [ ] DEV = { "https://twitter.com/RestainoAnthony" , "The Developer" } ;
private static final String [ ] FACEBOOK = { "https://www.facebook.com/" , "Facebook" } ;
private static final String [ ] DEV = { "https://twitter.com/RestainoAnthony" , "The Developer" } ;
private static final String [ ] TWITTER = { "https://twitter.com" , "Twitter" } ;
private static final String [ ] FACEBOOK = { "https://www.facebook.com/" , "Facebook" } ;
private static final String [ ] GOOGLE = { "https://www.google.com/" , "Google" } ;
private static final String [ ] TWITTER = { "https://twitter.com" , "Twitter" } ;
private static final String [ ] YAHOO = { "https://www.yahoo.com/" , "Yahoo" } ;
private static final String [ ] GOOGLE = { "https://www.google.com/" , "Google" } ;
public static final String [ ] [ ] DEFAULT_BOOKMARKS = {
private static final String [ ] YAHOO = { "https://www.yahoo.com/" , "Yahoo" } ;
DEV ,
public static final String [ ] [ ] DEFAULT_BOOKMARKS = {
FACEBOOK ,
DEV ,
TWITTER ,
FACEBOOK ,
GOOGLE ,
TWITTER ,
YAHOO
GOOGLE ,
} ;
YAHOO
} ;
}
}