Library checks for updates using configuration from some resource.
- Load update configuration from network resource or from input stream resource
- Accept custom loader for loading update configuration resource
- Use predefined parser for parsing update configuration in JSON format
- Accept custom parser for parsing update configuration
- Make asynchronous loading and use callback for notifying result
- Loading and verifying versions happen outside of UI thread
- Use thread pool to cap concurrent resource usage.
- Provide functionality for canceling once started verifications
If you are using a default parser, version in your application and the JSON file has to follow Semantic Versioning.JSON file has to look like this:
{
"ios": {
"minimum_version": "1.2.3",
"optional_update": {
"version": "2.4.5",
"notification_type": "ALWAYS"
}
},
"android": {
"minimum_version": "1.2.3",
"optional_update": {
"version": "2.4.5",
"notification_type": "ONCE"
}
},
"meta": {
"key1": "value1",
"key2": "value2"
}
}
Depending on notification_type
property, the user can be notified ONCE
or ALWAYS
. The library handles this for you, and if notification type is set to ONCE
, it will notify you via onNewUpdate(String version, boolean isMandatory)
method only once. Every other time the library will return onNoUpdate
for that specific version.
Key-value pairs under "meta"
key are optional metadata of which any amount can be sent accompanying the required fields.
Full example application is available here.
-
Create new instance of updater associated with application context.
PrinceOfVersions updater = new PrinceOfVersions(this);
-
Create loader factory for loading from network passing resource URL.
LoaderFactory loaderFactory = new NetworkLoaderFactory("http://pastebin.com/raw/41N8stUD");
-
Create concrete callback for result implementing
co.infinum.princeofversions.callbacks.UpdaterCallback
interface.UpdaterCallback callback = new UpdaterCallback() { @Override public void onNewUpdate(String version, boolean isMandatory, Map<String, String> metadata) { } @Override public void onNoUpdate(Map<String, String> metadata) { } @Override public void onError(@ErrorCode int error) { } };
-
Use updater with previously created loader factory and callback. Call
checkForUpdates
method to start update check.UpdaterResult result = updater.checkForUpdates(loaderFactory, callback);
-
To cancel update check, call
cancel
method onUpdaterResult
object.
For testing purposes you can create your own LoaderFactory. For ease of use, StreamLoader object exists in the library. Here is an example of loading a JSON file from raw.
-
Create new instance of updater associated with application context.
PrinceOfVersions updater = new PrinceOfVersions(this);
-
Create loader factory for creating stream loader by passing new input stream in its constructor.
LoaderFactory loaderFactory = new LoaderFactory() { @Override public UpdateConfigLoader newInstance() { return new StreamLoader(getResources().openRawResource(R.raw.update)); } };
Note: Be aware that once used input stream in
StreamLoader
is read and closed. For that purpose always create new stream innewInstance
method ofLoaderFactory
.
3rd, 4th and 5th step are same as in previous example.
Feedback and code contributions are very much welcome. Just make a pull request with a short description of your changes. By making contributions to this project you give permission for your code to be used under the same license.