summaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/common/os/AndroidVersion.java
blob: 0e3060265b2393795088a121d6d5205aa1f4c1b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * Copyright 2011 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package com.jogamp.common.os;

import java.lang.reflect.Field;

import com.jogamp.common.os.Platform.ABIType;
import com.jogamp.common.os.Platform.CPUFamily;
import com.jogamp.common.os.Platform.CPUType;
import com.jogamp.common.util.IntObjectHashMap;
import com.jogamp.common.util.ReflectionUtil;

public class AndroidVersion {
    public static final boolean isAvailable;

    /** The name of the instruction set (CPU type + ABI convention) of native code. API-4. All lower case.*/
    public static final String CPU_ABI;
    public static final CPUType CPU_TYPE;
    public static final ABIType ABI_TYPE;

    /** The name of the second instruction set (CPU type + ABI convention) of native code. API-8. All lower case.*/
    public static final String CPU_ABI2;
    public static final CPUType CPU_TYPE2;
    public static final ABIType ABI_TYPE2;

    /** Development codename, or the string "REL" for official release */
    public static final String CODENAME;

    /** internal build value used by the underlying source control. */
    public static final String INCREMENTAL;

    /** official build version string */
    public static final String RELEASE;

    /** SDK Version number, key to VERSION_CODES */
    public static final int SDK_INT;

    /** SDK Version string */
    public static final String SDK_NAME;

    private static final String androidBuild = "android.os.Build";
    private static final String androidBuildVersion = "android.os.Build$VERSION";
    private static final String androidBuildVersionCodes = "android.os.Build$VERSION_CODES";

    /**
     * Returns {@link CPUType} for matching <code>cpuABI<code>,
     * i.e. {@link #CPU_ABI} or {@link #CPU_ABI2},
     * or <code>null</code> for no match.
     * <p>
     * FIXME: Where is a comprehensive list of known 'android.os.Build.CPU_ABI' and 'android.os.Build.CPU_ABI2' strings ?<br/>
     * Fount this one: <code>http://www.kandroid.org/ndk/docs/CPU-ARCH-ABIS.html</code>
     * <pre>
     *  lib/armeabi/libfoo.so
     *  lib/armeabi-v7a/libfoo.so
     *  lib/x86/libfoo.so
     *  lib/mips/libfoo.so
     * </pre>
     * </p>
     */
    private static final CPUType getCPUTypeImpl(final String cpuABI) {
        if( null == cpuABI ) {
            return null;
        } else if( cpuABI.equals("armv8-a")   ||
                   cpuABI.equals("arm64-v8a") ) {
            return CPUType.ARMv8_A;
        } else if( cpuABI.equals("aarch64")   ||
                   cpuABI.startsWith("arm64") ) {
            return CPUType.ARM64;
        } else if( cpuABI.equals("armeabi-v7a") ) {
            return CPUType.ARMv7;
        } else if( cpuABI.equals("armeabi") ||
                   cpuABI.startsWith("arm") ) { // last 32bit chance ..
            return CPUType.ARM;
        } else if( cpuABI.equals("x86") ) {
            return CPUType.X86_32;
        } else if( cpuABI.equals("mips") ) {    // no 32bit vs 64bit identifier ?
            return CPUType.MIPS_32;
        } else {
            return null;
        }
    }
    private static final ABIType getABITypeImpl(final CPUType cpuType, final String cpuABI) {
        if( null == cpuType || null == cpuABI ) {
            return null;
        } else if( CPUFamily.ARM  != cpuType.family ) {
            return ABIType.GENERIC_ABI;
        } else if( CPUType.ARM64   == cpuType ||
                   CPUType.ARMv8_A == cpuType ) {
            return ABIType.EABI_AARCH64;
        }
        return ABIType.EABI_GNU_ARMEL; // FIXME: How will they name ABIType.EABI_GNU_ARMHF
    }

    static {
        final ClassLoader cl = AndroidVersion.class.getClassLoader();
        Class<?> abClass = null;
        Object abObject= null;
        Class<?> abvClass = null;
        Object abvObject= null;
        Class<?> abvcClass = null;
        Object abvcObject= null;
        try {
            abClass = ReflectionUtil.getClass(androidBuild, true, cl);
            abObject = abClass.newInstance();
            abvClass = ReflectionUtil.getClass(androidBuildVersion, true, cl);
            abvObject = abvClass.newInstance();
            abvcClass = ReflectionUtil.getClass(androidBuildVersionCodes, true, cl);
            abvcObject = abvcClass.newInstance();
        } catch (final Exception e) { /* n/a */ }
        isAvailable = null != abObject && null != abvObject && null != abvcObject;
        if(isAvailable) {
            CPU_ABI = getString(abClass, abObject, "CPU_ABI", true);
            CPU_ABI2 = getString(abClass, abObject, "CPU_ABI2", true);
            CODENAME = getString(abvClass, abvObject, "CODENAME", false);
            INCREMENTAL = getString(abvClass, abvObject, "INCREMENTAL", false);
            RELEASE = getString(abvClass, abvObject, "RELEASE", false);
            SDK_INT = getInt(abvClass, abvObject, "SDK_INT");
            final IntObjectHashMap version_codes = getVersionCodes(abvcClass, abvcObject);
            final String sdk_name = (String) version_codes.get(SDK_INT);
            SDK_NAME = ( null != sdk_name ) ? sdk_name : "SDK_"+SDK_INT ;
        } else {
            CPU_ABI = null;
            CPU_ABI2 = null;
            CODENAME = null;
            INCREMENTAL = null;
            RELEASE = null;
            SDK_INT = -1;
            SDK_NAME = null;
        }
        CPU_TYPE = getCPUTypeImpl(CPU_ABI);
        ABI_TYPE = getABITypeImpl(CPU_TYPE, CPU_ABI);
        CPU_TYPE2 = getCPUTypeImpl(CPU_ABI2);
        ABI_TYPE2 = getABITypeImpl(CPU_TYPE2, CPU_ABI2);
    }

    private static final IntObjectHashMap getVersionCodes(final Class<?> cls, final Object obj) {
        final Field[] fields = cls.getFields();
        final IntObjectHashMap map = new IntObjectHashMap( 3 * fields.length / 2, 0.75f );
        for(int i=0; i<fields.length; i++) {
            try {
                final int version = fields[i].getInt(obj);
                final String version_name = fields[i].getName();
                // System.err.println(i+": "+version+": "+version_name);
                map.put(new Integer(version), version_name);
            } catch (final Exception e) { e.printStackTrace(); /* n/a */ }
        }
        return map;
    }

    private static final String getString(final Class<?> cls, final Object obj, final String name, final boolean lowerCase) {
        try {
            final Field f = cls.getField(name);
            final String s = (String) f.get(obj);
            if( lowerCase && null != s ) {
                return s.toLowerCase();
            } else {
                return s;
            }
        } catch (final Exception e) { e.printStackTrace(); /* n/a */ }
        return null;
    }

    private static final int getInt(final Class<?> cls, final Object obj, final String name) {
        try {
            final Field f = cls.getField(name);
            return f.getInt(obj);
        } catch (final Exception e) { e.printStackTrace(); /* n/a */ }
        return -1;
    }

    // android.os.Build.VERSION
}