Skip to content

morristech/Android-prince-of-versions

 
 

Repository files navigation

Prince of versions

CircleCI Download

Library checks for updates using configuration from some resource.

Features

  • 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

Default parser and JSON file

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.

Examples

Full example application is available here.

Most common usage - loading from network resource

  1. Create new instance of updater associated with application context.

      PrinceOfVersions updater = new PrinceOfVersions(this);
  2. Create loader factory for loading from network passing resource URL.

      LoaderFactory loaderFactory = new NetworkLoaderFactory("http://pastebin.com/raw/41N8stUD");
  3. 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) {
            }
        };
  4. Use updater with previously created loader factory and callback. Call checkForUpdates method to start update check.

      UpdaterResult result = updater.checkForUpdates(loaderFactory, callback);
  5. To cancel update check, call cancel method on UpdaterResult object.

Writing tests

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.

  1. Create new instance of updater associated with application context.

      PrinceOfVersions updater = new PrinceOfVersions(this);
  2. 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 in newInstance method of LoaderFactory.

3rd, 4th and 5th step are same as in previous example.

Contributing

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%