Using the Android SDK - AppGratis/unlock-android GitHub Wiki
In order to use the SDK, you will have to:
- Integrate the library into your application
- Generate a JSON offer file
- Implement offer redeeming and feature/resource unlocking
Integration is detailed in the README, so we'll skip that here. We'll also assume that you've already got your offer JSON.
Please keep in mind that the library comes with a full Sample App if you've got any doubt about how to use the library correctly.
Using the SDK
In order to use any of the SDK's classes, you'll need to import it:
import com.appgratis.unlock.UnlockManager
Then, you'll have to get an instance of UnlockManager in order to get available features, and pending offers.
You'll find full information about the methods in the class file.
Creating the offer source
An offer source is a class instance that will provide the JSON offer for the Unlock Manager to consume and work with.
Having a flexible implementation allows you to pick how to provide the offers to the library: for example, you could make one that gets it from your server and caches it on disk.
There are two offer source implementations in the library:
- StringOfferSource, which simply takes the configuration JSON as a string.
- BuiltinOfferSource, which will load the offer json from your
res/rawresource directory.
In order to see how they work, please check their default constructor and their documentation, or how the Sample app uses the builtin source.
Consuming an offer
The Unlock Manager will provide you a list of available features to redeem, after it has computed if the user is eligible for them at the time of checking.
If the SDK gives an offer to you when calling getPendingOffer(), it means that all the conditions are met, and you should consume them as soon as possible. You should check this property at least when your app comes in foreground, especially if you've got time-limited offers, as some might become available after your initial check.
When consuming an offer, you need to:
- Look for the
unlockMessage, and display it if needed. - Use your "custom data" as set when configurating the offer in the generator if needed
- Save resources
- Mark the offer as consumed in the Unlock Manager
As explained lower in this documentation, features will be managed by the library itself. You can still read the features brought by an offer if you want to tell your user about which ones just became available.
Getting available features
Once an offer has been redeemed, the SDK will store the available features in a SQLite database, so you can query them at any time, by calling getFeatures() on the Unlock Manager instance
If the feature has an expiration time, the SDK will check it for you. It's your responsibility to check again with the SDK that the feature is still available. You can get a hint of when the feature is set to expire by reading the remainingLifetime of the Feature instance.
Managing resources
As opposed to features, resources are consumable by nature. The SDK will not remember resources redeems: it is your responsibility to save them before marking the offer as consumed, otherwise they will be lost.
The sample app comes with a basic resource manager class to show you how it might work into your app. The resources are saved when consuming an offer.