Skip to content
This repository was archived by the owner on Oct 18, 2018. It is now read-only.

[DRAFT] Reusing sync code to persist data of other apps

esteewhy edited this page Mar 27, 2014 · 3 revisions

I want to implement a network persistence/synchronization layer for my Android app.

It looks like the most of server-side infrastructure can be borrowed from Firefox Sync Server codebase as it is.

From a client-side high level perspective, to accomplish the goal, all Fennec entities/collections (tabs, bookmarks, passwords, and so on) need to be replaced by those of my app.

Basing on my experience so far, the practical steps that need to be performed after obtaining sources of this project:

  • Run ./preprocess.py script to get real files out of *.in templates
  • Rename example.project to .project
  • Import .project into Eclipse
  • Download missing binary dependencies:
    1. https://code.google.com/p/httpclientandroidlib/
    2. https://code.google.com/p/json-simple/
  • Modify .classpath of imported project so that "src" entry looked like this: <classpathentry kind="src" path="src/main/java"/>
  • Set target Android version to be at least level 9

By this time code should be compilable and deployable to a hardware device.

Suitable starting point in the code to start with might be:

org.mozilla.gecko.sync.GlobalSession.prepareStages()

This method defines a synchronization workflow.


Prerequisites before implementing sync

Record type must have guid-like field to serve as a server-side unique ID. For each record involved into sync this field must be filled in.

Steps to implement synchronization of custom records (greatly simplified;-)

Synchronization essentials

  1. Define record type by extending Record class:
  2. Extend RepositorySession class for a record type, where two things are essential: 2.1 3 similar record querying methods to send outbound records 2.2 Method .save(Record) where all the sync logic is going to be implemented.
  3. Extend ServerSyncStage class, where plug in the above RepositorySession, also define there collection name.
  4. Override GlobalSession class, where plug in the above SyncStage class. Currently, original GlobalSession class is a source of problems, because the way it indexes SyncStages relies on enum and is not extensible. The workaround might be to reuse any pre-existing enum member, but for this certain amount of plumbing is necessary. Having reached this far is practically enough to run synchronization manually, although it will be necessary to fully configure session before execution. On the other hand, it's possible to further override Mozilla's functionality:
  5. Extend SyncAdapter class where instantiate custom GlobalSession.
  6. Implement custom SyncService class where provide the above SyncAdapter.

(to be continued)

Clone this wiki locally