From ba7e9609408eea9d55ed3a305c5a1b1d2615e920 Mon Sep 17 00:00:00 2001 From: nathan Date: Sat, 3 Nov 2018 21:10:57 +0100 Subject: [PATCH] Added getting elementaryOS version info. Extracted common method `getDistribReleaseInfo()` --- src/dorkbox/util/OSUtil.java | 79 ++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/src/dorkbox/util/OSUtil.java b/src/dorkbox/util/OSUtil.java index cd6f2b4..123ee27 100644 --- a/src/dorkbox/util/OSUtil.java +++ b/src/dorkbox/util/OSUtil.java @@ -206,6 +206,10 @@ class OSUtil { public static class Linux { private static String info = null; + + /** + * @return os release info or "" + */ public static String getInfo() { if (info != null) { @@ -394,30 +398,49 @@ class OSUtil { return ubuntuVersion; } - String info = getInfo(); - String releaseString = "DISTRIB_RELEASE="; - int index = info.indexOf(releaseString); - try { - if (index > -1) { - index += releaseString.length(); - int newLine = info.indexOf(OS.LINE_SEPARATOR_UNIX, index); - if (newLine > index) { - String versionInfo = info.substring(index, newLine); - if (versionInfo.indexOf('.') > 0) { - String[] split = versionInfo.split("\\."); + String distribReleaseInfo = getDistribReleaseInfo(); + if (distribReleaseInfo != null) { + String[] split = distribReleaseInfo.split("\\."); - ubuntuVersion = new int[] {Integer.parseInt(split[0]), Integer.parseInt(split[1])}; - return ubuntuVersion; - } - } - } - } catch (Throwable ignored) { + ubuntuVersion = new int[] {Integer.parseInt(split[0]), Integer.parseInt(split[1])}; + return ubuntuVersion; } + ubuntuVersion = new int[]{0,0}; return ubuntuVersion; } + private static volatile int[] elementaryOSVersion = null; + public static + int[] getElementaryOSVersion() { + // 0.1 Jupiter. The first stable version of elementary OS was Jupiter, published on 31 March 2011 and based on Ubuntu 10.10. ... + // 0.2 Luna. elementary OS 0.2 "Luna" ... + // 0.3 Freya. elementary OS 0.3 "Freya" ... + // 0.4 Loki. elementary OS 0.4, known by its codename, "Loki", was released on 9 September 2016. ... + // 5.0 Juno + + if (elementaryOSVersion != null) { + return elementaryOSVersion; + } + + if (!isElementaryOS()) { + elementaryOSVersion = new int[]{0,0}; + return elementaryOSVersion; + } + + String distribReleaseInfo = getDistribReleaseInfo(); + if (distribReleaseInfo != null) { + String[] split = distribReleaseInfo.split("\\."); + + elementaryOSVersion = new int[] {Integer.parseInt(split[0]), Integer.parseInt(split[1])}; + return elementaryOSVersion; + } + + elementaryOSVersion = new int[]{0,0}; + return elementaryOSVersion; + } + private static Boolean isKali = null; public static @@ -428,6 +451,28 @@ class OSUtil { return isKali; } + /** + * @return the `DISTRIB_RELEASE` info as a String, if possible. Otherwise NULL + */ + public static + String getDistribReleaseInfo() { + String info = getInfo(); + String releaseString = "DISTRIB_RELEASE="; + int index = info.indexOf(releaseString); + try { + if (index > -1) { + index += releaseString.length(); + int newLine = info.indexOf(OS.LINE_SEPARATOR_UNIX, index); + if (newLine > index) { + return info.substring(index, newLine); + } + } + } catch (Throwable ignored) { + } + + return null; + } + public static boolean isRoot() { // this means we are running as sudo