jaulib v1.3.0
Jau Support Library (C++, Java, ..)
AndroidUtil.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) 2012 Gothel Software e.K.
5 * Copyright (c) 2012 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.io.File;
29import java.lang.reflect.Method;
30
31import org.jau.lang.ReflectionUtil;
32
33public class AndroidUtil {
34
35 private static final Method androidGetPackageInfoVersionNameMethod;
36 private static final Method androidGetPackageInfoVersionCodeMethod;
37 private static final Method androidGetTempRootMethod;
38
39 static {
41 final ClassLoader cl = AndroidUtil.class.getClassLoader();
42 final Class<?> androidAndroidUtilImplClz = ReflectionUtil.getClass("jau.sys.android.AndroidUtilImpl", true, cl);
43 androidGetPackageInfoVersionCodeMethod = ReflectionUtil.getMethod(androidAndroidUtilImplClz, "getPackageInfoVersionCode", String.class);
44 androidGetPackageInfoVersionNameMethod = ReflectionUtil.getMethod(androidAndroidUtilImplClz, "getPackageInfoVersionName", String.class);
45 androidGetTempRootMethod = ReflectionUtil.getMethod(androidAndroidUtilImplClz, "getTempRoot");
46 } else {
47 androidGetPackageInfoVersionCodeMethod = null;
48 androidGetPackageInfoVersionNameMethod = null;
49 androidGetTempRootMethod = null;
50 }
51 }
52
53 /**
54 * @return null if platform is not Android or no Android Context is registered
55 * via {@link jogamp.common.os.android.StaticContext#init(android.content.Context) StaticContext.init(..)},
56 * otherwise the found package version code of <code>packageName</code> is returned.
57 */
58 public static final int getPackageInfoVersionCode(final String packageName) {
59 if(null != androidGetPackageInfoVersionCodeMethod) {
60 return ((Integer) ReflectionUtil.callStaticMethod(androidGetPackageInfoVersionCodeMethod, packageName)).intValue();
61 }
62 return -1;
63 }
64
65 /**
66 * @return null if platform is not Android or no Android Context is registered
67 * via {@link jogamp.common.os.android.StaticContext#init(android.content.Context) StaticContext.init(..)},
68 * otherwise the found package version name of <code>packageName</code> is returned.
69 */
70 public static final String getPackageInfoVersionName(final String packageName) {
71 if(null != androidGetPackageInfoVersionNameMethod) {
72 return (String) ReflectionUtil.callStaticMethod(androidGetPackageInfoVersionNameMethod, packageName);
73 }
74 return null;
75 }
76
77 /**
78 * @return null if platform is not Android or no Android Context is registered
79 * via {@link jogamp.common.os.android.StaticContext#init(android.content.Context) StaticContext.init(..)},
80 * otherwise the context relative world readable <code>temp</code> directory returned.
81 */
82 public static File getTempRoot()
83 throws RuntimeException {
84 if(null != androidGetTempRootMethod) {
85 return (File) ReflectionUtil.callStaticMethod(androidGetTempRootMethod);
86 }
87 return null;
88 }
89
90}
Utility methods to simplify reflection access.
static final< R > R callStaticMethod(final Method method, final Object ... args)
static final Class<?> getClass(final String clazzName, final boolean initializeClazz, final ClassLoader cl)
Loads and returns the class or null.
static final Method getMethod(final Class<?> clazz, final String methodName, final Class<?> ... argTypes)
static final String getPackageInfoVersionName(final String packageName)
static final int getPackageInfoVersionCode(final String packageName)
static File getTempRoot()
static final boolean isAvailable