jaulib v1.3.0
Jau Support Library (C++, Java, ..)
AndroidVersion.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2011 Gothel Software e.K.
5 * Copyright (c) 2011 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package org.jau.sys;
27
28import java.lang.reflect.Field;
29import java.util.HashMap;
30
31import org.jau.lang.ReflectionUtil;
32import org.jau.sys.PlatformTypes.ABIType;
33import org.jau.sys.PlatformTypes.CPUType;
34
35public class AndroidVersion {
36 public static final boolean isAvailable;
37
38 /** The name of the instruction set (CPU type + ABI convention) of native code. API-4. All lower case.*/
39 public static final String CPU_ABI_NAME;
40 public static final CPUType CPU;
41 public static final ABIType ABI;
42
43 /** The name of the second instruction set (CPU type + ABI convention) of native code. API-8. All lower case.*/
44 public static final String CPU_ABI2_NAME;
45 public static final CPUType CPU2;
46 public static final ABIType ABI2;
47
48 /** Development codename, or the string "REL" for official release */
49 public static final String CODENAME;
50
51 /** internal build value used by the underlying source control. */
52 public static final String INCREMENTAL;
53
54 /** official build version string */
55 public static final String RELEASE;
56
57 /** SDK Version number, key to VERSION_CODES */
58 public static final int SDK_INT;
59
60 /** SDK Version string */
61 public static final String SDK_NAME;
62
63 private static final String androidBuild = "android.os.Build";
64 private static final String androidBuildVersion = "android.os.Build$VERSION";
65 private static final String androidBuildVersionCodes = "android.os.Build$VERSION_CODES";
66
67 static {
68 final ClassLoader cl = AndroidVersion.class.getClassLoader();
69 Class<?> abClass = null;
70 Object abObject= null;
71 Class<?> abvClass = null;
72 Object abvObject= null;
73 Class<?> abvcClass = null;
74 Object abvcObject= null;
75
76 final boolean isDalvikVm = "Dalvik".equals(System.getProperty("java.vm.name"));
77
78 if (isDalvikVm) {
79 try {
80 abClass = ReflectionUtil.getClass(androidBuild, true, cl);
81 abObject = abClass.getDeclaredConstructor().newInstance();
82 abvClass = ReflectionUtil.getClass(androidBuildVersion, true, cl);
83 abvObject = abvClass.getDeclaredConstructor().newInstance();
84 abvcClass = ReflectionUtil.getClass(androidBuildVersionCodes, true, cl);
85 abvcObject = abvcClass.getDeclaredConstructor().newInstance();
86 } catch (final Exception e) { /* n/a */ }
87 }
88 isAvailable = isDalvikVm && null != abObject && null != abvObject;
89 if(isAvailable) {
90 CPU_ABI_NAME = getString(abClass, abObject, "CPU_ABI", true);
91 CPU_ABI2_NAME = getString(abClass, abObject, "CPU_ABI2", true);
92 CODENAME = getString(abvClass, abvObject, "CODENAME", false);
93 INCREMENTAL = getString(abvClass, abvObject, "INCREMENTAL", false);
94 RELEASE = getString(abvClass, abvObject, "RELEASE", false);
95 SDK_INT = getInt(abvClass, abvObject, "SDK_INT");
96 final String sdk_name;
97 if( null != abvcObject ) {
98 final HashMap<Integer, String> version_codes = getVersionCodes(abvcClass, abvcObject);
99 sdk_name = version_codes.get(SDK_INT);
100 } else {
101 sdk_name = null;
102 }
103 SDK_NAME = ( null != sdk_name ) ? sdk_name : "SDK_"+SDK_INT ;
104
105 /**
106 * <p>
107 * FIXME: Where is a comprehensive list of known 'android.os.Build.CPU_ABI' and 'android.os.Build.CPU_ABI2' strings ?<br/>
108 * Fount this one: <code>http://www.kandroid.org/ndk/docs/CPU-ARCH-ABIS.html</code>
109 * <pre>
110 * lib/armeabi/libfoo.so
111 * lib/armeabi-v7a/libfoo.so
112 * lib/arm64-v8a/libfoo.so
113 * lib/x86/libfoo.so
114 * lib/mips/libfoo.so
115 * </pre>
116 * </p>
117 */
120 if( null != CPU_ABI2_NAME && CPU_ABI2_NAME.length() > 0 ) {
123 } else {
124 CPU2 = null;
125 ABI2 = null;
126 }
127 } else {
128 CPU_ABI_NAME = null;
129 CPU_ABI2_NAME = null;
130 CODENAME = null;
131 INCREMENTAL = null;
132 RELEASE = null;
133 SDK_INT = -1;
134 SDK_NAME = null;
135 CPU = null;
136 ABI = null;
137 CPU2 = null;
138 ABI2 = null;
139 }
140 }
141
142 private static final HashMap<Integer, String> getVersionCodes(final Class<?> cls, final Object obj) {
143 final Field[] fields = cls.getFields();
144 final HashMap<Integer, String> map = new HashMap<Integer, String>( 3 * fields.length / 2, 0.75f );
145 for(int i=0; i<fields.length; i++) {
146 try {
147 final int version = fields[i].getInt(obj);
148 final String version_name = fields[i].getName();
149 // System.err.println(i+": "+version+": "+version_name);
150 map.put(Integer.valueOf(version), version_name);
151 } catch (final Exception e) { e.printStackTrace(); /* n/a */ }
152 }
153 return map;
154 }
155
156 private static final String getString(final Class<?> cls, final Object obj, final String name, final boolean lowerCase) {
157 try {
158 final Field f = cls.getField(name);
159 final String s = (String) f.get(obj);
160 if( lowerCase && null != s ) {
161 return s.toLowerCase();
162 } else {
163 return s;
164 }
165 } catch (final Exception e) { e.printStackTrace(); /* n/a */ }
166 return null;
167 }
168
169 private static final int getInt(final Class<?> cls, final Object obj, final String name) {
170 try {
171 final Field f = cls.getField(name);
172 return f.getInt(obj);
173 } catch (final Exception e) { e.printStackTrace(); /* n/a */ }
174 return -1;
175 }
176
177 // android.os.Build.VERSION
178}
Utility methods to simplify reflection access.
static final Class<?> getClass(final String clazzName, final boolean initializeClazz, final ClassLoader cl)
Loads and returns the class or null.
static final int SDK_INT
SDK Version number, key to VERSION_CODES.
static final CPUType CPU
static final boolean isAvailable
static final String CPU_ABI2_NAME
The name of the second instruction set (CPU type + ABI convention) of native code.
static final CPUType CPU2
static final String SDK_NAME
SDK Version string.
static final ABIType ABI
static final String CPU_ABI_NAME
The name of the instruction set (CPU type + ABI convention) of native code.
static final ABIType ABI2
static final String RELEASE
official build version string
static final String INCREMENTAL
internal build value used by the underlying source control.
static final String CODENAME
Development codename, or the string "REL" for official release.
Exposing types describing the underlying platform.
static final ABIType query(final CPUType cpuType, final String cpuABILower)
static final CPUType query(final String cpuNameLower)