Monday, March 17, 2014

Determine running OS programmatically in Java

I know that most people don't use this, but for the moment you do need it, here is a nice way that you scan detect the running OS in Java:

public enum OSType {
    MacOS("mac", "darwin"),

    private static OSType detectedOS;

    private final String[] keys;

    private OSType(String... keys) {
        this.keys = keys;

    private boolean match(String osKey) {
        for (int i = 0; i < keys.length; i++) {
            if (osKey.indexOf(keys[i]) != -1)
                return true;
        return false;

    public static OSType getOS_Type() {
        if (detectedOS == null)
            detectedOS = getOperatingSystemType(System.getProperty("", Other.keys[0]).toLowerCase());
        return detectedOS;

    private static OSType getOperatingSystemType(String osKey) {
        for (OSType osType : values()) {
            if (osType.match(osKey))
                return osType;
        return Other;

Use it wisely, and leave your comments below :)

Thursday, March 13, 2014

Android - Type Safe SharedPreferences

OK, so how many time did you get so pissed because of the way you had to use the SharedPreferences and store stuff on Android?

I know this drove me mad more than once, and the fact I have to copy paste some implementation from one app to another was even more frustrating...

So.......  I've thought a while back to make a generic storage utility object, specifically for Android, (I already have something like that for Pure Java) and after a long while I have. I know this is a bit overkill, but hell, I would gladly pay with few extra lines of code to save hundreds, for a readable, comfortable and quick coding.

If you find the code useful, leave me a comment... I would like to know I'm not doing this for no good reason!

For me the code is pretty obvious, but then again I've been at it for years... let me know what is not clear so I can elaborate more about these subjects.

-- UPDATE --

I've released Cyborg not too long ago, and this sort of generic shared preferences is build in and optimized further in terms of how much code you need to write, You can find it here.