1/*------------------------------------------------------------------------- 2 * drawElements Quality Program Tester Core 3 * ---------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief Android utilities. 22 *//*--------------------------------------------------------------------*/ 23 24#include "tcuAndroidUtil.hpp" 25 26namespace tcu 27{ 28namespace Android 29{ 30 31using std::string; 32 33static string getIntentStringExtra (JNIEnv* env, jobject activity, const char* name) 34{ 35 // \todo [2013-05-12 pyry] Clean up references on error. 36 37 jclass activityCls = env->GetObjectClass(activity); 38 jobject intent = env->CallObjectMethod(activity, env->GetMethodID(activityCls, "getIntent", "()Landroid/content/Intent;")); 39 TCU_CHECK(intent); 40 41 jstring extraName = env->NewStringUTF(name); 42 jclass intentCls = env->GetObjectClass(intent); 43 TCU_CHECK(extraName && intentCls); 44 45 jvalue getExtraArgs[1]; 46 getExtraArgs[0].l = extraName; 47 jstring extraStr = (jstring)env->CallObjectMethodA(intent, env->GetMethodID(intentCls, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;"), getExtraArgs); 48 49 env->DeleteLocalRef(extraName); 50 51 if (extraStr) 52 { 53 const char* ptr = env->GetStringUTFChars(extraStr, DE_NULL); 54 string str = string(ptr); 55 env->ReleaseStringUTFChars(extraStr, ptr); 56 return str; 57 } 58 else 59 return string(); 60} 61 62string getIntentStringExtra (ANativeActivity* activity, const char* name) 63{ 64 return getIntentStringExtra(activity->env, activity->clazz, name); 65} 66 67static void setRequestedOrientation (JNIEnv* env, jobject activity, ScreenOrientation orientation) 68{ 69 jclass activityCls = env->GetObjectClass(activity); 70 jmethodID setOrientationId = env->GetMethodID(activityCls, "setRequestedOrientation", "(I)V"); 71 72 env->CallVoidMethod(activity, setOrientationId, (int)orientation); 73} 74 75void setRequestedOrientation (ANativeActivity* activity, ScreenOrientation orientation) 76{ 77 setRequestedOrientation(activity->env, activity->clazz, orientation); 78} 79 80ScreenOrientation mapScreenRotation (ScreenRotation rotation) 81{ 82 switch (rotation) 83 { 84 case SCREENROTATION_0: return SCREEN_ORIENTATION_PORTRAIT; 85 case SCREENROTATION_90: return SCREEN_ORIENTATION_LANDSCAPE; 86 case SCREENROTATION_180: return SCREEN_ORIENTATION_REVERSE_PORTRAIT; 87 case SCREENROTATION_270: return SCREEN_ORIENTATION_REVERSE_LANDSCAPE; 88 default: 89 print("Warning: Unsupported rotation"); 90 return SCREEN_ORIENTATION_PORTRAIT; 91 } 92} 93 94} // Android 95} // tcu 96