1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements Quality Program Tester Core
3e5c31af7Sopenharmony_ci * ----------------------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief Android utilities.
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "tcuAndroidUtil.hpp"
25e5c31af7Sopenharmony_ci
26e5c31af7Sopenharmony_ci#include "deSTLUtil.hpp"
27e5c31af7Sopenharmony_ci#include "deMath.h"
28e5c31af7Sopenharmony_ci
29e5c31af7Sopenharmony_ci#include <vector>
30e5c31af7Sopenharmony_ci
31e5c31af7Sopenharmony_cinamespace tcu
32e5c31af7Sopenharmony_ci{
33e5c31af7Sopenharmony_cinamespace Android
34e5c31af7Sopenharmony_ci{
35e5c31af7Sopenharmony_ci
36e5c31af7Sopenharmony_ciusing std::string;
37e5c31af7Sopenharmony_ciusing std::vector;
38e5c31af7Sopenharmony_ci
39e5c31af7Sopenharmony_cinamespace
40e5c31af7Sopenharmony_ci{
41e5c31af7Sopenharmony_ci
42e5c31af7Sopenharmony_ciclass ScopedJNIEnv
43e5c31af7Sopenharmony_ci{
44e5c31af7Sopenharmony_cipublic:
45e5c31af7Sopenharmony_ci
46e5c31af7Sopenharmony_ci					ScopedJNIEnv	(JavaVM* vm);
47e5c31af7Sopenharmony_ci					~ScopedJNIEnv	(void);
48e5c31af7Sopenharmony_ci
49e5c31af7Sopenharmony_ci	JavaVM*			getVM			(void) const { return m_vm;		}
50e5c31af7Sopenharmony_ci	JNIEnv*			getEnv			(void) const { return m_env;	}
51e5c31af7Sopenharmony_ci
52e5c31af7Sopenharmony_ciprivate:
53e5c31af7Sopenharmony_ci	JavaVM* const	m_vm;
54e5c31af7Sopenharmony_ci	JNIEnv*			m_env;
55e5c31af7Sopenharmony_ci	bool			m_detach;
56e5c31af7Sopenharmony_ci};
57e5c31af7Sopenharmony_ci
58e5c31af7Sopenharmony_ciScopedJNIEnv::ScopedJNIEnv (JavaVM* vm)
59e5c31af7Sopenharmony_ci	: m_vm		(vm)
60e5c31af7Sopenharmony_ci	, m_env		(DE_NULL)
61e5c31af7Sopenharmony_ci	, m_detach	(false)
62e5c31af7Sopenharmony_ci{
63e5c31af7Sopenharmony_ci	const int	getEnvRes	= m_vm->GetEnv((void**)&m_env, JNI_VERSION_1_6);
64e5c31af7Sopenharmony_ci
65e5c31af7Sopenharmony_ci	if (getEnvRes == JNI_EDETACHED)
66e5c31af7Sopenharmony_ci	{
67e5c31af7Sopenharmony_ci		if (m_vm->AttachCurrentThread(&m_env, DE_NULL) != JNI_OK)
68e5c31af7Sopenharmony_ci			throw std::runtime_error("JNI AttachCurrentThread() failed");
69e5c31af7Sopenharmony_ci
70e5c31af7Sopenharmony_ci		m_detach = true;
71e5c31af7Sopenharmony_ci	}
72e5c31af7Sopenharmony_ci	else if (getEnvRes != JNI_OK)
73e5c31af7Sopenharmony_ci		throw std::runtime_error("JNI GetEnv() failed");
74e5c31af7Sopenharmony_ci
75e5c31af7Sopenharmony_ci	DE_ASSERT(m_env);
76e5c31af7Sopenharmony_ci}
77e5c31af7Sopenharmony_ci
78e5c31af7Sopenharmony_ciScopedJNIEnv::~ScopedJNIEnv (void)
79e5c31af7Sopenharmony_ci{
80e5c31af7Sopenharmony_ci	if (m_detach)
81e5c31af7Sopenharmony_ci		m_vm->DetachCurrentThread();
82e5c31af7Sopenharmony_ci}
83e5c31af7Sopenharmony_ci
84e5c31af7Sopenharmony_ciclass LocalRef
85e5c31af7Sopenharmony_ci{
86e5c31af7Sopenharmony_cipublic:
87e5c31af7Sopenharmony_ci					LocalRef		(JNIEnv* env, jobject ref);
88e5c31af7Sopenharmony_ci					~LocalRef		(void);
89e5c31af7Sopenharmony_ci
90e5c31af7Sopenharmony_ci	jobject			operator*		(void) const { return m_ref;	}
91e5c31af7Sopenharmony_ci	operator		bool			(void) const { return !!m_ref;	}
92e5c31af7Sopenharmony_ci
93e5c31af7Sopenharmony_ciprivate:
94e5c31af7Sopenharmony_ci					LocalRef		(const LocalRef&);
95e5c31af7Sopenharmony_ci	LocalRef&		operator=		(const LocalRef&);
96e5c31af7Sopenharmony_ci
97e5c31af7Sopenharmony_ci	JNIEnv* const	m_env;
98e5c31af7Sopenharmony_ci	const jobject	m_ref;
99e5c31af7Sopenharmony_ci};
100e5c31af7Sopenharmony_ci
101e5c31af7Sopenharmony_ciLocalRef::LocalRef (JNIEnv* env, jobject ref)
102e5c31af7Sopenharmony_ci	: m_env(env)
103e5c31af7Sopenharmony_ci	, m_ref(ref)
104e5c31af7Sopenharmony_ci{
105e5c31af7Sopenharmony_ci}
106e5c31af7Sopenharmony_ci
107e5c31af7Sopenharmony_ciLocalRef::~LocalRef (void)
108e5c31af7Sopenharmony_ci{
109e5c31af7Sopenharmony_ci	if (m_ref)
110e5c31af7Sopenharmony_ci		m_env->DeleteLocalRef(m_ref);
111e5c31af7Sopenharmony_ci}
112e5c31af7Sopenharmony_ci
113e5c31af7Sopenharmony_civoid checkException (JNIEnv* env)
114e5c31af7Sopenharmony_ci{
115e5c31af7Sopenharmony_ci	if (env->ExceptionCheck())
116e5c31af7Sopenharmony_ci	{
117e5c31af7Sopenharmony_ci		env->ExceptionDescribe();
118e5c31af7Sopenharmony_ci		env->ExceptionClear();
119e5c31af7Sopenharmony_ci		throw std::runtime_error("Got JNI exception");
120e5c31af7Sopenharmony_ci	}
121e5c31af7Sopenharmony_ci}
122e5c31af7Sopenharmony_ci
123e5c31af7Sopenharmony_cijclass findClass (JNIEnv* env, const char* className)
124e5c31af7Sopenharmony_ci{
125e5c31af7Sopenharmony_ci	const jclass	cls		= env->FindClass(className);
126e5c31af7Sopenharmony_ci
127e5c31af7Sopenharmony_ci	checkException(env);
128e5c31af7Sopenharmony_ci	TCU_CHECK_INTERNAL(cls);
129e5c31af7Sopenharmony_ci
130e5c31af7Sopenharmony_ci	return cls;
131e5c31af7Sopenharmony_ci}
132e5c31af7Sopenharmony_ci
133e5c31af7Sopenharmony_cijclass getObjectClass (JNIEnv* env, jobject object)
134e5c31af7Sopenharmony_ci{
135e5c31af7Sopenharmony_ci	const jclass	cls		= env->GetObjectClass(object);
136e5c31af7Sopenharmony_ci
137e5c31af7Sopenharmony_ci	checkException(env);
138e5c31af7Sopenharmony_ci	TCU_CHECK_INTERNAL(cls);
139e5c31af7Sopenharmony_ci
140e5c31af7Sopenharmony_ci	return cls;
141e5c31af7Sopenharmony_ci}
142e5c31af7Sopenharmony_ci
143e5c31af7Sopenharmony_cijmethodID getMethodID (JNIEnv* env, jclass cls, const char* methodName, const char* signature)
144e5c31af7Sopenharmony_ci{
145e5c31af7Sopenharmony_ci	const jmethodID		id		= env->GetMethodID(cls, methodName, signature);
146e5c31af7Sopenharmony_ci
147e5c31af7Sopenharmony_ci	checkException(env);
148e5c31af7Sopenharmony_ci	TCU_CHECK_INTERNAL(id);
149e5c31af7Sopenharmony_ci
150e5c31af7Sopenharmony_ci	return id;
151e5c31af7Sopenharmony_ci}
152e5c31af7Sopenharmony_ci
153e5c31af7Sopenharmony_cistring getStringValue (JNIEnv* env, jstring jniStr)
154e5c31af7Sopenharmony_ci{
155e5c31af7Sopenharmony_ci	const char*		ptr		= env->GetStringUTFChars(jniStr, DE_NULL);
156e5c31af7Sopenharmony_ci	const string	str		= string(ptr);
157e5c31af7Sopenharmony_ci
158e5c31af7Sopenharmony_ci	env->ReleaseStringUTFChars(jniStr, ptr);
159e5c31af7Sopenharmony_ci
160e5c31af7Sopenharmony_ci	return str;
161e5c31af7Sopenharmony_ci}
162e5c31af7Sopenharmony_ci
163e5c31af7Sopenharmony_cistring getIntentStringExtra (JNIEnv* env, jobject activity, const char* name)
164e5c31af7Sopenharmony_ci{
165e5c31af7Sopenharmony_ci	// \todo [2013-05-12 pyry] Clean up references on error.
166e5c31af7Sopenharmony_ci
167e5c31af7Sopenharmony_ci	const jclass	activityCls		= getObjectClass(env, activity);
168e5c31af7Sopenharmony_ci	const LocalRef	intent			(env, env->CallObjectMethod(activity, getMethodID(env, activityCls, "getIntent", "()Landroid/content/Intent;")));
169e5c31af7Sopenharmony_ci	TCU_CHECK_INTERNAL(intent);
170e5c31af7Sopenharmony_ci
171e5c31af7Sopenharmony_ci	const LocalRef	extraName		(env, env->NewStringUTF(name));
172e5c31af7Sopenharmony_ci	const jclass	intentCls		= getObjectClass(env, *intent);
173e5c31af7Sopenharmony_ci	TCU_CHECK_INTERNAL(extraName && intentCls);
174e5c31af7Sopenharmony_ci
175e5c31af7Sopenharmony_ci	jvalue getExtraArgs[1];
176e5c31af7Sopenharmony_ci	getExtraArgs[0].l = *extraName;
177e5c31af7Sopenharmony_ci
178e5c31af7Sopenharmony_ci	const LocalRef	extraStr		(env, env->CallObjectMethodA(*intent, getMethodID(env, intentCls, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;"), getExtraArgs));
179e5c31af7Sopenharmony_ci
180e5c31af7Sopenharmony_ci	if (extraStr)
181e5c31af7Sopenharmony_ci		return getStringValue(env, (jstring)*extraStr);
182e5c31af7Sopenharmony_ci	else
183e5c31af7Sopenharmony_ci		return string();
184e5c31af7Sopenharmony_ci}
185e5c31af7Sopenharmony_ci
186e5c31af7Sopenharmony_civoid setRequestedOrientation (JNIEnv* env, jobject activity, ScreenOrientation orientation)
187e5c31af7Sopenharmony_ci{
188e5c31af7Sopenharmony_ci	const jclass	activityCls			= getObjectClass(env, activity);
189e5c31af7Sopenharmony_ci	const jmethodID	setOrientationId	= getMethodID(env, activityCls, "setRequestedOrientation", "(I)V");
190e5c31af7Sopenharmony_ci
191e5c31af7Sopenharmony_ci	env->CallVoidMethod(activity, setOrientationId, (int)orientation);
192e5c31af7Sopenharmony_ci}
193e5c31af7Sopenharmony_ci
194e5c31af7Sopenharmony_citemplate<typename Type>
195e5c31af7Sopenharmony_ciconst char* getJNITypeStr (void);
196e5c31af7Sopenharmony_ci
197e5c31af7Sopenharmony_citemplate<>
198e5c31af7Sopenharmony_ciconst char* getJNITypeStr<int> (void)
199e5c31af7Sopenharmony_ci{
200e5c31af7Sopenharmony_ci	return "I";
201e5c31af7Sopenharmony_ci}
202e5c31af7Sopenharmony_ci
203e5c31af7Sopenharmony_citemplate<>
204e5c31af7Sopenharmony_ciconst char* getJNITypeStr<deInt64> (void)
205e5c31af7Sopenharmony_ci{
206e5c31af7Sopenharmony_ci	return "J";
207e5c31af7Sopenharmony_ci}
208e5c31af7Sopenharmony_ci
209e5c31af7Sopenharmony_citemplate<>
210e5c31af7Sopenharmony_ciconst char* getJNITypeStr<string> (void)
211e5c31af7Sopenharmony_ci{
212e5c31af7Sopenharmony_ci	return "Ljava/lang/String;";
213e5c31af7Sopenharmony_ci}
214e5c31af7Sopenharmony_ci
215e5c31af7Sopenharmony_citemplate<>
216e5c31af7Sopenharmony_ciconst char* getJNITypeStr<vector<string> > (void)
217e5c31af7Sopenharmony_ci{
218e5c31af7Sopenharmony_ci	return "[Ljava/lang/String;";
219e5c31af7Sopenharmony_ci}
220e5c31af7Sopenharmony_ci
221e5c31af7Sopenharmony_citemplate<typename FieldType>
222e5c31af7Sopenharmony_ciFieldType getStaticFieldValue (JNIEnv* env, jclass cls, jfieldID fieldId);
223e5c31af7Sopenharmony_ci
224e5c31af7Sopenharmony_citemplate<>
225e5c31af7Sopenharmony_ciint getStaticFieldValue<int> (JNIEnv* env, jclass cls, jfieldID fieldId)
226e5c31af7Sopenharmony_ci{
227e5c31af7Sopenharmony_ci	DE_ASSERT(cls && fieldId);
228e5c31af7Sopenharmony_ci	return env->GetStaticIntField(cls, fieldId);
229e5c31af7Sopenharmony_ci}
230e5c31af7Sopenharmony_ci
231e5c31af7Sopenharmony_citemplate<>
232e5c31af7Sopenharmony_cistring getStaticFieldValue<string> (JNIEnv* env, jclass cls, jfieldID fieldId)
233e5c31af7Sopenharmony_ci{
234e5c31af7Sopenharmony_ci	const jstring	jniStr	= (jstring)env->GetStaticObjectField(cls, fieldId);
235e5c31af7Sopenharmony_ci
236e5c31af7Sopenharmony_ci	if (jniStr)
237e5c31af7Sopenharmony_ci		return getStringValue(env, jniStr);
238e5c31af7Sopenharmony_ci	else
239e5c31af7Sopenharmony_ci		return string();
240e5c31af7Sopenharmony_ci}
241e5c31af7Sopenharmony_ci
242e5c31af7Sopenharmony_citemplate<>
243e5c31af7Sopenharmony_civector<string> getStaticFieldValue<vector<string> > (JNIEnv* env, jclass cls, jfieldID fieldId)
244e5c31af7Sopenharmony_ci{
245e5c31af7Sopenharmony_ci	const jobjectArray	array		= (jobjectArray)env->GetStaticObjectField(cls, fieldId);
246e5c31af7Sopenharmony_ci	vector<string>		result;
247e5c31af7Sopenharmony_ci
248e5c31af7Sopenharmony_ci	checkException(env);
249e5c31af7Sopenharmony_ci
250e5c31af7Sopenharmony_ci	if (array)
251e5c31af7Sopenharmony_ci	{
252e5c31af7Sopenharmony_ci		const int	numElements		= env->GetArrayLength(array);
253e5c31af7Sopenharmony_ci
254e5c31af7Sopenharmony_ci		for (int ndx = 0; ndx < numElements; ndx++)
255e5c31af7Sopenharmony_ci		{
256e5c31af7Sopenharmony_ci			const jstring	jniStr	= (jstring)env->GetObjectArrayElement(array, ndx);
257e5c31af7Sopenharmony_ci
258e5c31af7Sopenharmony_ci			checkException(env);
259e5c31af7Sopenharmony_ci
260e5c31af7Sopenharmony_ci			if (jniStr)
261e5c31af7Sopenharmony_ci				result.push_back(getStringValue(env, jniStr));
262e5c31af7Sopenharmony_ci		}
263e5c31af7Sopenharmony_ci	}
264e5c31af7Sopenharmony_ci
265e5c31af7Sopenharmony_ci	return result;
266e5c31af7Sopenharmony_ci}
267e5c31af7Sopenharmony_ci
268e5c31af7Sopenharmony_citemplate<typename FieldType>
269e5c31af7Sopenharmony_ciFieldType getStaticField (JNIEnv* env, const char* className, const char* fieldName)
270e5c31af7Sopenharmony_ci{
271e5c31af7Sopenharmony_ci	const jclass	cls			= findClass(env, className);
272e5c31af7Sopenharmony_ci	const jfieldID	fieldId		= env->GetStaticFieldID(cls, fieldName, getJNITypeStr<FieldType>());
273e5c31af7Sopenharmony_ci
274e5c31af7Sopenharmony_ci	checkException(env);
275e5c31af7Sopenharmony_ci
276e5c31af7Sopenharmony_ci	if (fieldId)
277e5c31af7Sopenharmony_ci		return getStaticFieldValue<FieldType>(env, cls, fieldId);
278e5c31af7Sopenharmony_ci	else
279e5c31af7Sopenharmony_ci		throw std::runtime_error(string(fieldName) + " not found in " + className);
280e5c31af7Sopenharmony_ci}
281e5c31af7Sopenharmony_ci
282e5c31af7Sopenharmony_citemplate<typename FieldType>
283e5c31af7Sopenharmony_ciFieldType getFieldValue (JNIEnv* env, jobject obj, jfieldID fieldId);
284e5c31af7Sopenharmony_ci
285e5c31af7Sopenharmony_citemplate<>
286e5c31af7Sopenharmony_cideInt64 getFieldValue<deInt64> (JNIEnv* env, jobject obj, jfieldID fieldId)
287e5c31af7Sopenharmony_ci{
288e5c31af7Sopenharmony_ci	DE_ASSERT(obj && fieldId);
289e5c31af7Sopenharmony_ci	return env->GetLongField(obj, fieldId);
290e5c31af7Sopenharmony_ci}
291e5c31af7Sopenharmony_ci
292e5c31af7Sopenharmony_citemplate<typename FieldType>
293e5c31af7Sopenharmony_ciFieldType getField (JNIEnv* env, jobject obj, const char* fieldName)
294e5c31af7Sopenharmony_ci{
295e5c31af7Sopenharmony_ci	const jclass	cls			= getObjectClass(env, obj);
296e5c31af7Sopenharmony_ci	const jfieldID	fieldId		= env->GetFieldID(cls, fieldName, getJNITypeStr<FieldType>());
297e5c31af7Sopenharmony_ci
298e5c31af7Sopenharmony_ci	checkException(env);
299e5c31af7Sopenharmony_ci
300e5c31af7Sopenharmony_ci	if (fieldId)
301e5c31af7Sopenharmony_ci		return getFieldValue<FieldType>(env, obj, fieldId);
302e5c31af7Sopenharmony_ci	else
303e5c31af7Sopenharmony_ci		throw std::runtime_error(string(fieldName) + " not found in object");
304e5c31af7Sopenharmony_ci}
305e5c31af7Sopenharmony_ci
306e5c31af7Sopenharmony_civoid describePlatform (JNIEnv* env, std::ostream& dst)
307e5c31af7Sopenharmony_ci{
308e5c31af7Sopenharmony_ci	const char* const	buildClass		= "android/os/Build";
309e5c31af7Sopenharmony_ci	const char* const	versionClass	= "android/os/Build$VERSION";
310e5c31af7Sopenharmony_ci
311e5c31af7Sopenharmony_ci	static const struct
312e5c31af7Sopenharmony_ci	{
313e5c31af7Sopenharmony_ci		const char*		classPath;
314e5c31af7Sopenharmony_ci		const char*		className;
315e5c31af7Sopenharmony_ci		const char*		fieldName;
316e5c31af7Sopenharmony_ci	} s_stringFields[] =
317e5c31af7Sopenharmony_ci	{
318e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"BOARD"			},
319e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"BRAND"			},
320e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"DEVICE"		},
321e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"DISPLAY"		},
322e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"FINGERPRINT"	},
323e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"HARDWARE"		},
324e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"MANUFACTURER"	},
325e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"MODEL"			},
326e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"PRODUCT"		},
327e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"TAGS"			},
328e5c31af7Sopenharmony_ci		{ buildClass,	"Build",			"TYPE"			},
329e5c31af7Sopenharmony_ci		{ versionClass,	"Build.VERSION",	"RELEASE"		},
330e5c31af7Sopenharmony_ci	};
331e5c31af7Sopenharmony_ci
332e5c31af7Sopenharmony_ci	for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(s_stringFields); ndx++)
333e5c31af7Sopenharmony_ci		dst << s_stringFields[ndx].className << "." << s_stringFields[ndx].fieldName
334e5c31af7Sopenharmony_ci			<< ": " << getStaticField<string>(env, s_stringFields[ndx].classPath, s_stringFields[ndx].fieldName)
335e5c31af7Sopenharmony_ci			<< "\n";
336e5c31af7Sopenharmony_ci
337e5c31af7Sopenharmony_ci	dst << "Build.VERSION.SDK_INT: " << getStaticField<int>(env, versionClass, "SDK_INT") << "\n";
338e5c31af7Sopenharmony_ci
339e5c31af7Sopenharmony_ci	{
340e5c31af7Sopenharmony_ci		const vector<string>	supportedAbis	= getStaticField<vector<string> >(env, buildClass, "SUPPORTED_ABIS");
341e5c31af7Sopenharmony_ci
342e5c31af7Sopenharmony_ci		dst << "Build.SUPPORTED_ABIS: ";
343e5c31af7Sopenharmony_ci
344e5c31af7Sopenharmony_ci		for (size_t ndx = 0; ndx < supportedAbis.size(); ndx++)
345e5c31af7Sopenharmony_ci			dst << (ndx != 0 ? ", " : "") << supportedAbis[ndx];
346e5c31af7Sopenharmony_ci
347e5c31af7Sopenharmony_ci		dst << "\n";
348e5c31af7Sopenharmony_ci	}
349e5c31af7Sopenharmony_ci}
350e5c31af7Sopenharmony_ci
351e5c31af7Sopenharmony_ci} // anonymous
352e5c31af7Sopenharmony_ci
353e5c31af7Sopenharmony_ciScreenOrientation mapScreenRotation (ScreenRotation rotation)
354e5c31af7Sopenharmony_ci{
355e5c31af7Sopenharmony_ci	switch (rotation)
356e5c31af7Sopenharmony_ci	{
357e5c31af7Sopenharmony_ci		case SCREENROTATION_UNSPECIFIED:	return SCREEN_ORIENTATION_UNSPECIFIED;
358e5c31af7Sopenharmony_ci		case SCREENROTATION_0:				return SCREEN_ORIENTATION_PORTRAIT;
359e5c31af7Sopenharmony_ci		case SCREENROTATION_90:				return SCREEN_ORIENTATION_LANDSCAPE;
360e5c31af7Sopenharmony_ci		case SCREENROTATION_180:			return SCREEN_ORIENTATION_REVERSE_PORTRAIT;
361e5c31af7Sopenharmony_ci		case SCREENROTATION_270:			return SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
362e5c31af7Sopenharmony_ci		default:
363e5c31af7Sopenharmony_ci			print("Warning: Unsupported rotation");
364e5c31af7Sopenharmony_ci			return SCREEN_ORIENTATION_PORTRAIT;
365e5c31af7Sopenharmony_ci	}
366e5c31af7Sopenharmony_ci}
367e5c31af7Sopenharmony_ci
368e5c31af7Sopenharmony_cistring getIntentStringExtra (ANativeActivity* activity, const char* name)
369e5c31af7Sopenharmony_ci{
370e5c31af7Sopenharmony_ci	const ScopedJNIEnv	env(activity->vm);
371e5c31af7Sopenharmony_ci
372e5c31af7Sopenharmony_ci	return getIntentStringExtra(env.getEnv(), activity->clazz, name);
373e5c31af7Sopenharmony_ci}
374e5c31af7Sopenharmony_ci
375e5c31af7Sopenharmony_civoid setRequestedOrientation (ANativeActivity* activity, ScreenOrientation orientation)
376e5c31af7Sopenharmony_ci{
377e5c31af7Sopenharmony_ci	const ScopedJNIEnv	env(activity->vm);
378e5c31af7Sopenharmony_ci
379e5c31af7Sopenharmony_ci	setRequestedOrientation(env.getEnv(), activity->clazz, orientation);
380e5c31af7Sopenharmony_ci}
381e5c31af7Sopenharmony_ci
382e5c31af7Sopenharmony_civoid describePlatform (ANativeActivity* activity, std::ostream& dst)
383e5c31af7Sopenharmony_ci{
384e5c31af7Sopenharmony_ci	const ScopedJNIEnv	env(activity->vm);
385e5c31af7Sopenharmony_ci
386e5c31af7Sopenharmony_ci	describePlatform(env.getEnv(), dst);
387e5c31af7Sopenharmony_ci}
388e5c31af7Sopenharmony_ci
389e5c31af7Sopenharmony_cisize_t				getTotalAndroidSystemMemory		(ANativeActivity* activity)
390e5c31af7Sopenharmony_ci{
391e5c31af7Sopenharmony_ci	const ScopedJNIEnv	scopedJniEnv				(activity->vm);
392e5c31af7Sopenharmony_ci	JNIEnv* env = scopedJniEnv.getEnv();
393e5c31af7Sopenharmony_ci
394e5c31af7Sopenharmony_ci	// Get activity manager instance:
395e5c31af7Sopenharmony_ci	// ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
396e5c31af7Sopenharmony_ci	const jclass	activityManagerClass	= findClass(env, "android/app/ActivityManager");
397e5c31af7Sopenharmony_ci	const LocalRef	activityString			(env, env->NewStringUTF("activity")); // Context.ACTIVITY_SERVICE == "activity"
398e5c31af7Sopenharmony_ci	const jclass	activityClass			= getObjectClass(env, activity->clazz);
399e5c31af7Sopenharmony_ci	const jmethodID	getServiceID			= getMethodID(env, activityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
400e5c31af7Sopenharmony_ci	LocalRef		activityManager			(env, env->CallObjectMethod(activity->clazz, getServiceID, *activityString));
401e5c31af7Sopenharmony_ci	checkException(env);
402e5c31af7Sopenharmony_ci	TCU_CHECK_INTERNAL(activityManager);
403e5c31af7Sopenharmony_ci
404e5c31af7Sopenharmony_ci	// Crete memory info instance:
405e5c31af7Sopenharmony_ci	// ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
406e5c31af7Sopenharmony_ci	const jclass	memoryInfoClass			= findClass(env, "android/app/ActivityManager$MemoryInfo");
407e5c31af7Sopenharmony_ci	const jmethodID	memoryInfoCtor			= getMethodID(env, memoryInfoClass, "<init>", "()V");
408e5c31af7Sopenharmony_ci	LocalRef		memoryInfo				(env, env->NewObject(memoryInfoClass, memoryInfoCtor));
409e5c31af7Sopenharmony_ci	checkException(env);
410e5c31af7Sopenharmony_ci	TCU_CHECK_INTERNAL(memoryInfo);
411e5c31af7Sopenharmony_ci
412e5c31af7Sopenharmony_ci	// Get memory info from activity manager:
413e5c31af7Sopenharmony_ci	// activityManager.getMemoryInfo(memoryInfo);
414e5c31af7Sopenharmony_ci	const jmethodID	getMemoryInfoID			= getMethodID(env, activityManagerClass, "getMemoryInfo", "(Landroid/app/ActivityManager$MemoryInfo;)V");
415e5c31af7Sopenharmony_ci	checkException(env);
416e5c31af7Sopenharmony_ci	env->CallVoidMethod(*activityManager, getMemoryInfoID, *memoryInfo);
417e5c31af7Sopenharmony_ci
418e5c31af7Sopenharmony_ci	// Return 'totalMem' field from the memory info instance.
419e5c31af7Sopenharmony_ci	return static_cast<size_t>(getField<deInt64>(env, *memoryInfo, "totalMem"));
420e5c31af7Sopenharmony_ci}
421e5c31af7Sopenharmony_ci
422e5c31af7Sopenharmony_ci} // Android
423e5c31af7Sopenharmony_ci} // tcu
424