7. Sad News…
• You will have to
implement business
rules…
• Authentication Handling
• (Custom) Content Provider
• Data Handling
8. Good Use Cases
• Requires Authentication Step
to connect to remote DS
• Has a Custom Content
Provider
• Will require a new Sync Adapter
• Application Suite backed by a
Custom ContentProvider
• Requires non-authentication
permissions
9. (Bad?) Use Cases
• Does not require permissions or
login
• RSS/public data feed
• Write a parser the reads the feed
upon user need (poetry)
• Sync Adapter not necessary to
write to a ContentProvider
• If Service does not need to
constantly run in background
• Conserve battery power, or user will
delete your app
10. The Big Picture
AccountManager
• Requires
Authentication
Service to add
Account
• Adds Account to
Account List
SyncManager
• Uses Account
List as a basis
for it’s Sync List
• Requires Sync
Service to start
data syncs
ContentProvider
• Holds the data
• Called in
onPerformSync()
12. Authentication
• Write an AuthActivity
• serves UI for user to enter account info
• Write it to complete the authorization process
• Write an Authenticator
• MUST extend AbstractAccountAuthenticator
• Returns the AuthActivity in a Bundle object
• Write an Authentication Service
• MUST extend Service
• onBind return IBinder for the Authenticator
13. Synchronization
• Write a SyncAdapter
• MUST extend AbstractThreadedSyncAdapter
• Overwrite onPerformSync()
• Write a Sync Service
• MUST extend Service
• Allow creation of ONE SyncAdapter
• onBind returns a IBinder for SyncAdapter
15. Resource XMLs
• Provide configuration info
• Map account type to objects
• SyncAdapter and Authenticator are connected via
android:accountType
• Map content authority to SyncAdapter
19. Manifest
• Include several appropriate permissions
• Declare AuthenticationService with <service> tag
• Intent filter: “android.accounts.AccountAuthenticator”
• Meta-data tag points AccountAuthenticator to xml resource file
• Declare SyncService with <service> tag
• Intent filter: “android.content.SyncAdapter”
• Meta-data tag points SyncAdapter to xml resource file
• Other Meta-data tag defines custom MIME-types
• Declare AuthActivity (no Intent filter necessary)
21. Why Services?
• Why do we need these <service> tags?
• SyncAdapter is bound to a service, Authentication is bound to a
service… but why?
• SyncManager finds all syncAdapters via a SyncAdaptersCache
• AccountManager finds all Accounts via AccountAuthenticatorCache
• RegisteredServicesCache
• Reads the XML files created (account-authenticator and syncadapter)
• Generates service maps
22. Code Summary
• Create a syncadapter.xml file
• Create an authenticator.xml file
• Add an authentication <service> to manifest
• Add a sync <service> to manifest
• Add correct permissions to manifest
• Write some java… but what does any of this mean?
25. Content Provider
• Manages central repository of data.
• SyncAdapters wouldn’t exist without ContentProviders
• SyncAdapter mentioned in ContentProvider section of API Guide
• Built in ContentProviders contain a SyncState table
• Use to store sync state, meta-data or any sync related data
• Sync related columns in other tables:
• DIRTY, VERSION, SOURCE_ID
26. Important Columns
• DIRTY (local modifications)
• Indicates data row has been changed locally since last sync.
• When SyncAdapter adds/updates a row, append
“CALLER_IS_SYNCADAPTER” to Content URI
• VERSION
• Incremented by Provider whenever the data row is changed.
• High-water marking (aka lastSyncState)
• SOURCE_ID
• Unique ID for data row (primary key on the remote datasource)
• If set to null, indicates to SyncAdapter to create new row on remote
source
27. Custom Content Provider
• What properties make the built-in ContentProviders
“syncable”?
• Extra “For Sync use only” table that holds metadata
• “CALLER_IS_SYNCADAPTER” query param
• Use of “high-water-mark” is essential
• These properties should be applied to Custom
SyncAdapter
28. Data Considerations
• Determine structure of serialized/transmitted data
• Using json? xml? yaml?
• What does a single row (data unit) look like?
• How does the server parse and save the transmitted
data?
• Likely defined by business limitations.
• How does the device parse/save the data?
• ContentProviderClient or ContentResolver (batch ops)
30. Accessing Content
The Device User
Android Application(s)
Context
ContentResolver
ContentProviderClient
ContentProvider
Data Storage
• SharedPreferences
• External Storage
• SQLLiteDatabase
31. A Comparison
Content Resolver
• Thread safe
• Many calls to many
ContentAuthorities (CA)
• Expensive call
(Many CA lookups)
• Resolver contains a Client
ContentProviderClient
• Not thread safe
• Repeated calls to same
CA
• Cheaper call (one CA
lookup)
• You MUST call release()
to avoid memory leak
33. Account
• Account object contains an Account name and Account
type.
• Note, the Account object does NOT contain a password
• Example: user has multiple twitter accounts
• AccountManager maps an Account object to it’s
“password”
• Recommended strategy: store Account and authToken on
device
• SampleSyncAdapter project stores password (possible human
error)
• SyncManager can only schedule syncs per Account
34. Account Manager
• Contains centralized registry of user’s online accounts
• Used to determine list of data sources to be synced
• AccountManager provides list of Accounts
• SyncManager grabs the list of Accounts for syncing
• AccountAuthenticatorCache maps accountType to objects
extending AbstractAccountAuthenticator
• Android Component; you don’t write this
35. Authentication
• User chooses to Add an Account
• List compiled from array of allowable account types
• System chooses Authenticator based on
“android:accountType”
• Binds to a Service which returns the Authenticator’s IBinder
• Authenticator’s overridden addAccount() method is called
• Ultimately the Login UI displays, new Account is created
38. SyncManager
• Responsible for scheduling, cancelling syncs.
• Maintains a list of active syncs (mActiveSyncContext)
• Sync handling strategy very tightly coupled to Accounts
• Account is part of most SyncManager methods
• How do the SyncHandler and Account interact?
• SyncHandler receives a message containing an
ActiveSyncContext. ActiveSyncContext contains all information
about the sync that needs to begin including the Account.
39. Synchronization
• SyncManager contains ONE SyncHandler
• SyncHandler’s job: receive/handle sync operation
messages
• On receiving a Connection Message, calls startSync for the
syncAdapter in question
• startSync() spins up a new SyncThread
• The method onPerformSync() that was implemented is invoked
when run() is invoked on the thread
40. SyncAdapter
• Where the main party happens. The method
onPerformSync() gets called within a syncThread.run()
• Make use of the DIRTY/VERSION/SOURCE_ID rows
• Instantiate like any class.
• However, create only one for thread safety.
41. onPerformSync()
• What should I do in the onPerformSync() method?
• Merge remote data with local data
• DIRTY/VERSION/SOURCE_ID columns
• Use business rules to determine order of operations
• Whether local data overwrites remote data or vice versa
• Implement a remote connection strategy to access remote
data
43. User Preferences
• Intended for use with Preferences Framework
• Add “android:accountPreferences” to
<account-authenticator>
• Build Preferences resource file
• Create Activity which gathers user prefs
• Must extend PreferenceActivity
• Noticing a pattern?
46. Wrap Up
• We learned…
• What SyncAdapters are
• Set up
• Content (ContentProvider/data characteristics)
• Authorization (Accounts, Account Management)
• Synchronization (SyncManager, SyncService)
• User Preferences
• Debugging/Dev Tools