From f97b23514bea993233d0aab9500a7593cbf32c38 Mon Sep 17 00:00:00 2001 From: zhangbin Date: Thu, 25 Aug 2016 09:56:32 +0800 Subject: [PATCH] Use java reflection to avoid API level dependent, Use API Level 10. --- .../platform/android/java/project.properties | 2 +- .../org/cocos2dx/lib/Cocos2dxActivity.java | 29 ++++++-- .../cocos2dx/lib/Cocos2dxEditBoxHelper.java | 1 - .../lib/Cocos2dxReflectionHelper.java | 74 +++++++++++++++++++ .../proj.android/project.properties | 2 +- .../app/project.properties | 2 +- .../proj.android/project.properties | 2 +- .../app/project.properties | 2 +- .../proj.android/project.properties | 2 +- native/tools/cocos2d-console | 2 +- 10 files changed, 102 insertions(+), 16 deletions(-) create mode 100644 native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxReflectionHelper.java diff --git a/native/cocos/platform/android/java/project.properties b/native/cocos/platform/android/java/project.properties index 61afc8fe54..88ca83f9d0 100755 --- a/native/cocos/platform/android/java/project.properties +++ b/native/cocos/platform/android/java/project.properties @@ -12,4 +12,4 @@ android.library=true # Project target. -target=android-19 +target=android-10 diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java index 05d97985e8..1f860ae99c 100644 --- a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -391,15 +391,28 @@ public abstract class Cocos2dxActivity extends Activity { protected void hideVirtualButton() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + if (Build.VERSION.SDK_INT >= 19) { + // use reflection to remove dependence of API level - getWindow().getDecorView().setSystemUiVisibility( - View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar - | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar - | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); + Class viewClass = View.class; + final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = Cocos2dxReflectionHelper.getConstantValue(viewClass, "SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION"); + final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = Cocos2dxReflectionHelper.getConstantValue(viewClass, "SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN"); + final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = Cocos2dxReflectionHelper.getConstantValue(viewClass, "SYSTEM_UI_FLAG_HIDE_NAVIGATION"); + final int SYSTEM_UI_FLAG_FULLSCREEN = Cocos2dxReflectionHelper.getConstantValue(viewClass, "SYSTEM_UI_FLAG_FULLSCREEN"); + final int SYSTEM_UI_FLAG_IMMERSIVE_STICKY = Cocos2dxReflectionHelper.getConstantValue(viewClass, "SYSTEM_UI_FLAG_IMMERSIVE_STICKY"); + final int SYSTEM_UI_FLAG_LAYOUT_STABLE = Cocos2dxReflectionHelper.getConstantValue(viewClass, "SYSTEM_UI_FLAG_LAYOUT_STABLE"); + + // getWindow().getDecorView().setSystemUiVisibility(); + final Object[] parameters = new Object[]{SYSTEM_UI_FLAG_LAYOUT_STABLE + | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar + | SYSTEM_UI_FLAG_FULLSCREEN // hide status bar + | SYSTEM_UI_FLAG_IMMERSIVE_STICKY}; + Cocos2dxReflectionHelper.invokeInstanceMethod(getWindow().getDecorView(), + "setSystemUiVisibility", + new Class[]{Integer.TYPE}, + parameters); } } diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java index 57057147ac..5394af927a 100644 --- a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java @@ -29,7 +29,6 @@ import android.content.res.Resources; import android.graphics.Color; import android.graphics.Typeface; import android.os.Looper; -import android.renderscript.Type; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxReflectionHelper.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxReflectionHelper.java new file mode 100644 index 0000000000..a1c9f566d8 --- /dev/null +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxReflectionHelper.java @@ -0,0 +1,74 @@ +/**************************************************************************** +Copyright (c) 2016 cocos2d-x.org +Copyright (c) 2016 Chukong Technologies Inc. + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ****************************************************************************/ +package org.cocos2dx.lib; + +import android.util.Log; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class Cocos2dxReflectionHelper { + public static T getConstantValue(final Class aClass, final String constantName) { + try { + return (T)aClass.getDeclaredField(constantName).get(null); + } catch (NoSuchFieldException e) { + Log.e("error", "can not find " + constantName + " in " + aClass.getName()); + } + catch (IllegalAccessException e) { + Log.e("error", constantName + " is not accessable"); + } + catch (IllegalArgumentException e) { + Log.e("error", "arguments error when get " + constantName); + } + catch (Exception e) { + Log.e("error", "can not get constant" + constantName); + } + + return null; + } + + public static T invokeInstanceMethod(final Object instance, final String methodName, + final Class[] parameterTypes, final Object[] parameters) { + + final Class aClass = instance.getClass(); + try { + final Method method = aClass.getMethod(methodName, parameterTypes); + return (T)method.invoke(instance, parameters); + } catch (NoSuchMethodException e) { + Log.e("error", "can not find " + methodName + " in " + aClass.getName()); + } + catch (IllegalAccessException e) { + Log.e("error", methodName + " is not accessible"); + } + catch (IllegalArgumentException e) { + Log.e("error", "arguments are error when invoking " + methodName); + } + catch (InvocationTargetException e) { + Log.e("error", "an exception was thrown by the invoked method when invoking " + methodName); + } + + return null; + } +} diff --git a/native/templates/js-template-binary/frameworks/runtime-src/proj.android/project.properties b/native/templates/js-template-binary/frameworks/runtime-src/proj.android/project.properties index ae36aa50be..b91cae6366 100755 --- a/native/templates/js-template-binary/frameworks/runtime-src/proj.android/project.properties +++ b/native/templates/js-template-binary/frameworks/runtime-src/proj.android/project.properties @@ -8,5 +8,5 @@ # project structure. # Project target. -target=android-19 +target=android-10 diff --git a/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/project.properties b/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/project.properties index 9ad60c9606..8e5895514b 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/project.properties +++ b/native/templates/js-template-default/frameworks/runtime-src/proj.android-studio/app/project.properties @@ -8,6 +8,6 @@ # project structure. # Project target. -target=android-19 +target=android-10 android.library.reference.1=../../cocos2d-x/cocos/platform/android/java diff --git a/native/templates/js-template-default/frameworks/runtime-src/proj.android/project.properties b/native/templates/js-template-default/frameworks/runtime-src/proj.android/project.properties index 9ad60c9606..8e5895514b 100755 --- a/native/templates/js-template-default/frameworks/runtime-src/proj.android/project.properties +++ b/native/templates/js-template-default/frameworks/runtime-src/proj.android/project.properties @@ -8,6 +8,6 @@ # project structure. # Project target. -target=android-19 +target=android-10 android.library.reference.1=../../cocos2d-x/cocos/platform/android/java diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/project.properties b/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/project.properties index a5578ba094..f049142c17 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/project.properties +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.android-studio/app/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-19 +target=android-10 diff --git a/native/templates/js-template-link/frameworks/runtime-src/proj.android/project.properties b/native/templates/js-template-link/frameworks/runtime-src/proj.android/project.properties index a5578ba094..f049142c17 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/proj.android/project.properties +++ b/native/templates/js-template-link/frameworks/runtime-src/proj.android/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-19 +target=android-10 diff --git a/native/tools/cocos2d-console b/native/tools/cocos2d-console index fdad71adcf..cd6f0d8ec3 160000 --- a/native/tools/cocos2d-console +++ b/native/tools/cocos2d-console @@ -1 +1 @@ -Subproject commit fdad71adcf580741398d3ef93ac2f835023ef06c +Subproject commit cd6f0d8ec3e717ef0f811af079f88d50c1f27721