Example usage for java.util TreeMap headMap

List of usage examples for java.util TreeMap headMap

Introduction

In this page you can find the example usage for java.util TreeMap headMap.

Prototype

public SortedMap<K, V> headMap(K toKey) 

Source Link

Usage

From source file:edu.mit.mobile.android.appupdater.AppUpdateChecker.java

@SuppressWarnings("unchecked")
private void triggerFromJson(JSONObject jo) throws JSONException {

    final ArrayList<String> changelog = new ArrayList<String>();

    // keep a sorted map of versionCode to the version information objects.
    // Most recent is at the top.
    final TreeMap<Integer, JSONObject> versionMap = new TreeMap<Integer, JSONObject>(new Comparator<Integer>() {
        public int compare(Integer object1, Integer object2) {
            return object2.compareTo(object1);
        };//w  w w  . j  a v a 2 s.  com
    });

    for (final Iterator<String> i = jo.keys(); i.hasNext();) {
        final String versionName = i.next();
        if (versionName.equals("package")) {
            pkgInfo = jo.getJSONObject(versionName);
            continue;
        }
        final JSONObject versionInfo = jo.getJSONObject(versionName);
        versionInfo.put("versionName", versionName);

        final int versionCode = versionInfo.getInt("versionCode");
        versionMap.put(versionCode, versionInfo);
    }
    final int latestVersionNumber = versionMap.firstKey();
    final String latestVersionName = versionMap.get(latestVersionNumber).getString("versionName");
    final Uri downloadUri = Uri.parse(pkgInfo.getString("downloadUrl"));

    if (currentAppVersion > latestVersionNumber) {
        Log.d(TAG, "We're newer than the latest published version (" + latestVersionName
                + "). Living in the future...");
        mUpdateListener.appUpdateStatus(true, latestVersionName, null, downloadUri);
        return;
    }

    if (currentAppVersion == latestVersionNumber) {
        Log.d(TAG, "We're at the latest version (" + currentAppVersion + ")");
        mUpdateListener.appUpdateStatus(true, latestVersionName, null, downloadUri);
        return;
    }

    // construct the changelog. Newest entries are at the top.
    for (final Entry<Integer, JSONObject> version : versionMap.headMap(currentAppVersion).entrySet()) {
        final JSONObject versionInfo = version.getValue();
        final JSONArray versionChangelog = versionInfo.optJSONArray("changelog");
        if (versionChangelog != null) {
            final int len = versionChangelog.length();
            for (int i = 0; i < len; i++) {
                changelog.add(versionChangelog.getString(i));
            }
        }
    }

    mUpdateListener.appUpdateStatus(false, latestVersionName, changelog, downloadUri);
}