17 October 2010

Android Browser Bookmarks

In my previous post I already mentioned my Android phone. Soon after I got it, I wanted to import my bookmarks from my desktop PC to its standard browser. So being green I copied my favourites to the sdcard and tried to open them. Well it didn't work.

Google AndroidWorking Around
If I lived in "Google-land", I could use Google Bookmarks. But I don't and had to look for alternatives. After some searching I found a possible way to import bookmarks:
  1. Upload your single html file of exported bookmarks to a document on Google Docs.
  2. In your browser navigate to said page.
  3. Manually click on each link and save as bookmark.
Noooo, you must be kidding me. This is hideous. Then I found a nice instruction how to
import the bookmarks directly into the browser's database. That would be cool. But I'm not bold enough to root my new phone and "play with that, probably break it a bit - and then cry about it later." (Ward from androidcommunity.com)

My Solution
On several places I read hints that some Android apps were able to import bookmarks, but I couldn't find any. Instead I found Matthieu Guenebaud's Bookmarks Manager. It's able to backup and restore the browser bookmarks and uses a plain zip file to store them.
Viewing .ZIP: Bookmarks_2010-09-10_14-11-24.zip

Length  Method Size  Ratio    Date    Time   Name
------  ------ ----- -----    ----    ----   ----
  2273 DeflatN   710 68.8% 09.10.2010  2:11p bookmarks.xml
   526 DeflatN   531  0.0% 09.10.2010  2:11p 23.png
   764 DeflatN   769  0.0% 09.10.2010  2:11p 24.png
   326 DeflatN   331  0.0% 09.10.2010  2:11p 51.png
   684 DeflatN   689  0.0% 09.10.2010  2:11p 57.png
   239 DeflatN   238  0.5% 09.10.2010  2:11p 69.png
   541 DeflatN   546  0.0% 09.10.2010  2:11p 90.png
  1266 DeflatN  1271  0.0% 09.10.2010  2:11p 198.png
   490 DeflatN   495  0.0% 09.10.2010  2:11p 164.png
   304 DeflatN   309  0.0% 09.10.2010  2:11p 124.png
   408 DeflatN   413  0.0% 09.10.2010  2:11p 229.png
------         ----- -----                   ----
  7821          6302 19.5%                     11
The file bookmarks.xml has a simple XML structure of <bookmark>s inside a <bookmarks> element. Yes, that's something I can use.
  1. Backup the bookmarks, even if empty, to get an initial file.
  2. Unpack the archive.
  3. Insert bookmarks into the existing XML structure.
  4. Repack the archive.
  5. Restore from the modified zip file.
  6. Enjoy your new wealth of bookmarks.
In case you don't feel like editing the bookmarks.xml by hand, here is some Scala code.:
val bookmarkXml = scala.xml.XML.loadFile(targetFolder + "/bookmarks.xml")
val lastOrder = Integer.parseInt((bookmarkXml \\ "order").last.text)
val oldNodes = bookmarkXml \\ "bookmark"
val newNodes = to_xml_list(bookmarks, lastOrder + 1000)
val root = <bookmarks>{ oldNodes }{ newNodes }</bookmarks>
scala.xml.XML.save(targetFolder + "/bookmarks.xml", root, "UTF8", true, null)
Thanks to Scala's excellent XML support, reading and modifying the bookmarks file is easy. The method to_xml_list() iterates all favourites and creates XML fragments for each one using the following method.
def to_xml(bm: Favorite, order: Int) = {
  <bookmark>
    <title>{ bm.name }</title>
    <url>{ bm.url }</url>
    <order>{ order }</order>
    <created>{ bm.fileDate.getTime }</created>
  </bookmark>
}
Favorite is a class representing an Internet Explorer favourite that I wrote long ago. (Yeah baby, code reuse!) Value order is the number Bookmark Explorer uses for sorting bookmarks. See the complete source of GuenmatBookmarks.scala.

3 comments:

Peter Kofler said...

This method works well on HTC Desire. But there is a minor glitch in Guenebaud's Bookmarks Manager when used on the Samsung Galaxy S: The import of bookmarks works, but they are not displayed in the bookmark list of the browser. Instead they are shown in the history of the browser and there they are marked as bookmark (little yellow star). To fix this you have to open each of these pages in the browser. Once it’s loaded, it’s shown in the list of bookmarks together with the proper icon as expected.

Anonymous said...

@Peter Kofler - Thank you very much regarding Samsung Galaxy issue. But could you please let me know, how can i resolve this issue programmatically ?

Peter Kofler said...

I'm not sure. It could be an Android browser problem. At least it looks like one because the pages are marked as bookmarked, but not displayed in the main list. AFAIK Galaxy and HTC use different browsers. The one by HTC is a special version or custom at all. Maybe it's missing an optional field, e.g.date of last access (if exists).