Skip to content
syedecryptr edited this page Dec 29, 2020 · 2 revisions

ReadLocator

open class ReadLocator : Locator, Parcelable

ReadLocator is extended from Locator class of r2-shared-kotlin.
Locator concept is based on this document.

It has bookId field for identifying EPUB uniquely.
ReadLocator currently relies only on EPUB CFI as locations.
locations.cfi generated is partial CFI which points to the exact location of resource href.

Get ReadLocator

Implement ReadLocatorListener or create it's anonymous implementation -

folioReader.setReadLocatorListener(new ReadLocatorListener() {
    @Override
    public void saveReadLocator(ReadLocator readLocator) {
        Log.i(LOG_TAG, "-> saveReadLocator -> " + readLocator.toJson());

        /*ReadLocator has toJson() method which gives JSON in following format - 
        {
            bookId : string,
            href : string,
            created : integer,
            locations : {
                cfi : string
            }
        }
        You can save this last read position in your local or remote db*/
    }
});

See Also - Clean up code

ReadLocator is broadcasted every time when FolioPageFragment goes through onPause() which means that can happen at following events -

  • Back button, Home button or Recent App button is clicked.
  • Drawer icon is clicked.
  • Search menu item is clicked.
  • Device rotation.
  • App goes in background.

Set ReadLocator

Let us suppose you have following JSON in String jsonString variable -

{
  "bookId": "_simple_book",
  "href": "/OEBPS/ch03.xhtml",
  "created": 1539934158390,
  "locations": {
    "cfi": "epubcfi(/0!/4/4/8/1:0)"
  }
}

You can construct ReadLocator using any of these ways -

ReadLocator readLocator = ReadLocator.fromJson(jsonString);

Or

Locations locations = new Locations();
locations.setCfi("epubcfi(/0!/4/4/8/1:0)");
ReadLocator readLocator = new ReadLocator("_simple_book", "/OEBPS/ch03.xhtml", 1539934158390L, locations);

Then pass it to setReadLocator() before opening book -

folioReader.setReadLocator(readLocator);