Search code examples
blackberryotajadover-the-air

BlackBerry: How to read in a JAD and extract the version number


I know the method

ApplicationDescriptor.currentApplicationDescriptor();

But I aim to download another JAD and compare it's version number to the current applications version. What is the best/easiest way to do this? Is there a way to construct an ApplicationDescriptor from a simple String?


Solution

  • Get the version from the current Apps JAD:

    String currentVersion = ApplicationDescriptor.currentApplicationDescriptor().getVersion();
    

    Get the version from a downloaded JAD as String:

    public static String getJADProperty(String jadString, String propKey) {
        int indexFrom = jadString.indexOf(propKey) + propKey.length();
    
        // Value reaches until line break (Unix vs. Win)
        int indexTo = jadString.indexOf("\r", indexFrom);
        if (indexTo == -1) { indexTo = jadString.indexOf("\n", indexFrom); }
    
        return jadString.substring(indexFrom, indexTo).trim();
    }
    

    Compare the version strings:

    /**
     * Compares two version strings that are in the format 1.1.1
     * 
     * @param current   Version String in the format 1.1.1 (current version of the app)
     * @param remote    Version String in the format 1.1.1 (remote version of the app)
     * 
     * @return true if the remote version is newer
     */
    public static boolean compareVersionStrings(String current, String remote) {
        int lastIndexCurrent = 0;
        int indexCurrent = 0;
    
        int lastIndexRemote = 0;
        int indexRemote = 0;
    
        String currentVersionSubstring = "";
        String remoteVersionSubstring = "";
    
        do {
            lastIndexCurrent = indexCurrent + currentVersionSubstring.length();
            indexCurrent = current.indexOf(".", lastIndexCurrent);
            lastIndexRemote = indexRemote + remoteVersionSubstring.length();
            indexRemote = remote.indexOf(".", lastIndexRemote);
    
            // Needed because there is no "." at the last number of the version string
            if (indexCurrent != -1) {
                currentVersionSubstring = current.substring(lastIndexCurrent, indexCurrent);
            } else {
                currentVersionSubstring = current.substring(lastIndexCurrent);
            }
            if (indexRemote != -1) {
                remoteVersionSubstring = remote.substring(lastIndexRemote, indexRemote);
            } else {
                remoteVersionSubstring = remote.substring(lastIndexRemote);
            }
    
            if (Integer.parseInt(currentVersionSubstring) < Integer.parseInt(remoteVersionSubstring)) {
                return true;
            }
        } while (indexCurrent != -1);
    
        // 1.0 < 1.0.1
        if (indexRemote != -1) {
            return true;
        }
    
        return false;
    }
    

    Any corrections and improvements are appreciated. Feel free to edit and share your experiences with me.