From 23433219c36de5d1b1eee5f12bc6418534e50822 Mon Sep 17 00:00:00 2001 From: "Hui.Li-TCL" Date: Tue, 23 Nov 2021 01:27:30 +0800 Subject: [PATCH] Keypad input cluster to android (#11952) * fix package name error * fix debug not work error * added KeypadInputManager in java * remove meaningless comment * Restyled by whitespace * Restyled by google-java-format * Restyled by clang-format * Restyled by gn Co-authored-by: Restyled.io --- examples/tv-app/android/App/app/build.gradle | 6 + .../tcl/chip/chiptvserver/MainActivity.java | 5 +- examples/tv-app/android/BUILD.gn | 10 +- .../keypad-input/KeypadInputManager.cpp | 48 ------- .../android/java/KeypadInputManager.cpp | 77 +++++++++++ .../KeypadInputManager.h | 18 ++- examples/tv-app/android/java/TVApp-JNI.cpp | 13 +- .../tcl/chip/tvapp/KeypadInputManager.java | 120 ++++++++++++++++++ .../chip/tvapp/KeypadInputManagerStub.java | 32 +++++ .../src/com/tcl/{ => chip}/tvapp/TvApp.java | 4 +- 10 files changed, 272 insertions(+), 61 deletions(-) delete mode 100644 examples/tv-app/android/include/keypad-input/KeypadInputManager.cpp create mode 100644 examples/tv-app/android/java/KeypadInputManager.cpp rename examples/tv-app/android/{include/keypad-input => java}/KeypadInputManager.h (64%) create mode 100644 examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManager.java create mode 100644 examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManagerStub.java rename examples/tv-app/android/java/src/com/tcl/{ => chip}/tvapp/TvApp.java (88%) diff --git a/examples/tv-app/android/App/app/build.gradle b/examples/tv-app/android/App/app/build.gradle index 35b5cff38413e6..8c66ebcb42b815 100644 --- a/examples/tv-app/android/App/app/build.gradle +++ b/examples/tv-app/android/App/app/build.gradle @@ -25,6 +25,12 @@ android { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } + + debug { + packagingOptions{ + doNotStrip "**/*.so" + } + } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java index 7e0e8cc18a2abc..53177ccd8ef420 100644 --- a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java @@ -15,7 +15,8 @@ import chip.setuppayload.DiscoveryCapability; import chip.setuppayload.SetupPayload; import chip.setuppayload.SetupPayloadParser; -import com.tcl.tvapp.TvApp; +import com.tcl.chip.tvapp.KeypadInputManagerStub; +import com.tcl.chip.tvapp.TvApp; import java.util.HashSet; public class MainActivity extends AppCompatActivity { @@ -32,6 +33,8 @@ protected void onCreate(Bundle savedInstanceState) { mQrCodeTxt = findViewById(R.id.qrCodeTxt); mManualPairingCodeTxt = findViewById(R.id.manualPairingCodeTxt); TvApp tvApp = new TvApp(); + tvApp.setKeypadInputManager(new KeypadInputManagerStub()); + AndroidChipPlatform chipPlatform = new AndroidChipPlatform( new AndroidBleManager(), diff --git a/examples/tv-app/android/BUILD.gn b/examples/tv-app/android/BUILD.gn index ecb6b5964a0f95..d18ec637558f2b 100644 --- a/examples/tv-app/android/BUILD.gn +++ b/examples/tv-app/android/BUILD.gn @@ -38,8 +38,6 @@ shared_library("jni") { "include/content-launcher/ContentLauncherManager.h", "include/endpoint-configuration/EndpointConfigurationStorage.cpp", "include/endpoint-configuration/EndpointConfigurationStorage.h", - "include/keypad-input/KeypadInputManager.cpp", - "include/keypad-input/KeypadInputManager.h", "include/low-power/LowPowerManager.cpp", "include/low-power/LowPowerManager.h", "include/media-input/MediaInputManager.cpp", @@ -52,6 +50,8 @@ shared_library("jni") { "include/tv-channel/TvChannelManager.h", "include/wake-on-lan/WakeOnLanManager.cpp", "include/wake-on-lan/WakeOnLanManager.h", + "java/KeypadInputManager.cpp", + "java/KeypadInputManager.h", "java/TVApp-JNI.cpp", ] @@ -82,7 +82,11 @@ android_library("java") { "${chip_root}/build/chip/java:shared_cpplib", ] - sources = [ "java/src/com/tcl/tvapp/TvApp.java" ] + sources = [ + "java/src/com/tcl/chip/tvapp/KeypadInputManager.java", + "java/src/com/tcl/chip/tvapp/KeypadInputManagerStub.java", + "java/src/com/tcl/chip/tvapp/TvApp.java", + ] javac_flags = [ "-Xlint:deprecation" ] diff --git a/examples/tv-app/android/include/keypad-input/KeypadInputManager.cpp b/examples/tv-app/android/include/keypad-input/KeypadInputManager.cpp deleted file mode 100644 index 58a39ab91bb6b4..00000000000000 --- a/examples/tv-app/android/include/keypad-input/KeypadInputManager.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * - * Copyright (c) 2021 Project CHIP Authors - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "KeypadInputManager.h" - -#include -#include - -#include -#include - -using namespace std; - -CHIP_ERROR KeypadInputManager::Init() -{ - CHIP_ERROR err = CHIP_NO_ERROR; - - // TODO: Store feature map once it is supported - map featureMap; - featureMap["NV"] = true; - featureMap["LK"] = true; - featureMap["NK"] = true; - - SuccessOrExit(err); -exit: - return err; -} - -EmberAfKeypadInputStatus keypadInputClusterSendKey(EmberAfKeypadInputCecKeyCode keyCode) -{ - // TODO: Insert code here - return EMBER_ZCL_KEYPAD_INPUT_STATUS_SUCCESS; -} diff --git a/examples/tv-app/android/java/KeypadInputManager.cpp b/examples/tv-app/android/java/KeypadInputManager.cpp new file mode 100644 index 00000000000000..acb6878ff20d37 --- /dev/null +++ b/examples/tv-app/android/java/KeypadInputManager.cpp @@ -0,0 +1,77 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "KeypadInputManager.h" +#include +#include +#include +#include +#include +#include + +using namespace chip; + +KeypadInputManager KeypadInputManager::sInstance; + +EmberAfKeypadInputStatus keypadInputClusterSendKey(EmberAfKeypadInputCecKeyCode keyCode) +{ + return KeypadInputMgr().SendKey(keyCode); +} + +EmberAfKeypadInputStatus KeypadInputManager::SendKey(EmberAfKeypadInputCecKeyCode keyCode) +{ + jint ret = -1; + CHIP_ERROR err = CHIP_NO_ERROR; + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); + + ChipLogProgress(Zcl, "Received keypadInputClusterSendKey: %d", keyCode); + VerifyOrExit(mKeypadInputManagerObject != nullptr, err = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mSendKeyMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(env != NULL, err = CHIP_JNI_ERROR_NO_ENV); + + env->ExceptionClear(); + ret = env->CallIntMethod(mKeypadInputManagerObject, mSendKeyMethod, static_cast(keyCode)); + VerifyOrExit(!env->ExceptionCheck(), err = CHIP_JNI_ERROR_EXCEPTION_THROWN); + +exit: + if (err != CHIP_NO_ERROR) + { + return EMBER_ZCL_KEYPAD_INPUT_STATUS_SUCCESS; + } + + return static_cast(ret); +} + +void KeypadInputManager::InitializeWithObjects(jobject managerObject) +{ + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); + VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for KeypadInputManager")); + + mKeypadInputManagerObject = env->NewGlobalRef(managerObject); + VerifyOrReturn(mKeypadInputManagerObject != nullptr, ChipLogError(Zcl, "Failed to NewGlobalRef KeypadInputManager")); + + jclass KeypadInputManagerClass = env->GetObjectClass(managerObject); + VerifyOrReturn(KeypadInputManagerClass != nullptr, ChipLogError(Zcl, "Failed to get KeypadInputManager Java class")); + + mSendKeyMethod = env->GetMethodID(KeypadInputManagerClass, "sendKey", "(I)I"); + if (mSendKeyMethod == nullptr) + { + ChipLogError(Zcl, "Failed to access KeypadInputManager 'sendKey' method"); + env->ExceptionClear(); + } +} diff --git a/examples/tv-app/android/include/keypad-input/KeypadInputManager.h b/examples/tv-app/android/java/KeypadInputManager.h similarity index 64% rename from examples/tv-app/android/include/keypad-input/KeypadInputManager.h rename to examples/tv-app/android/java/KeypadInputManager.h index d55161a5e1cad4..44a91cea66180d 100644 --- a/examples/tv-app/android/include/keypad-input/KeypadInputManager.h +++ b/examples/tv-app/android/java/KeypadInputManager.h @@ -19,12 +19,24 @@ #pragma once #include - +#include #include -#include class KeypadInputManager { public: - CHIP_ERROR Init(); + void InitializeWithObjects(jobject managerObject); + EmberAfKeypadInputStatus SendKey(EmberAfKeypadInputCecKeyCode keyCode); + +private: + friend KeypadInputManager & KeypadInputMgr(); + + static KeypadInputManager sInstance; + jobject mKeypadInputManagerObject = nullptr; + jmethodID mSendKeyMethod = nullptr; }; + +inline KeypadInputManager & KeypadInputMgr() +{ + return KeypadInputManager::sInstance; +} diff --git a/examples/tv-app/android/java/TVApp-JNI.cpp b/examples/tv-app/android/java/TVApp-JNI.cpp index 4f5d09b5445776..9cbb0461ce9d86 100644 --- a/examples/tv-app/android/java/TVApp-JNI.cpp +++ b/examples/tv-app/android/java/TVApp-JNI.cpp @@ -16,16 +16,14 @@ * */ -/** - * @file - * Implementation of JNI bridge for Tv App on Android. - * - */ +#include "KeypadInputManager.h" #include #include #include #include +#define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_com_tcl_chip_tvapp_TvApp_##METHOD_NAME + jint JNI_OnLoad(JavaVM * jvm, void * reserved) { return AndroidAppServerJNI_OnLoad(jvm, reserved); @@ -35,3 +33,8 @@ void JNI_OnUnload(JavaVM * jvm, void * reserved) { return AndroidAppServerJNI_OnUnload(jvm, reserved); } + +JNI_METHOD(void, setKeypadInputManager)(JNIEnv *, jobject, jobject manager) +{ + KeypadInputMgr().InitializeWithObjects(manager); +} diff --git a/examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManager.java b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManager.java new file mode 100644 index 00000000000000..8f5d873445a5d1 --- /dev/null +++ b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManager.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2021 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.tcl.chip.tvapp; + +public interface KeypadInputManager { + + int KEY_CODE_SELECT = 0; + int KEY_CODE_UP = 1; + int KEY_CODE_DOWN = 2; + int KEY_CODE_LEFT = 3; + int KEY_CODE_RIGHT = 4; + int KEY_CODE_RIGHT_UP = 5; + int KEY_CODE_RIGHT_DOWN = 6; + int KEY_CODE_LEFT_UP = 7; + int KEY_CODE_LEFT_DOWN = 8; + int KEY_CODE_ROOT_MENU = 9; + int KEY_CODE_SETUP_MENU = 10; + int KEY_CODE_CONTENTS_MENU = 11; + int KEY_CODE_FAVORITE_MENU = 12; + int KEY_CODE_EXIT = 13; + int KEY_CODE_MEDIA_TOP_MENU = 16; + int KEY_CODE_MEDIA_CONTEXT_SENSITIVE_MENU = 17; + int KEY_CODE_NUMBER_ENTRY_MODE = 29; + int KEY_CODE_NUMBER11 = 30; + int KEY_CODE_NUMBER12 = 31; + int KEY_CODE_NUMBER0_OR_NUMBER10 = 32; + int KEY_CODE_NUMBERS1 = 33; + int KEY_CODE_NUMBERS2 = 34; + int KEY_CODE_NUMBERS3 = 35; + int KEY_CODE_NUMBERS4 = 36; + int KEY_CODE_NUMBERS5 = 37; + int KEY_CODE_NUMBERS6 = 38; + int KEY_CODE_NUMBERS7 = 39; + int KEY_CODE_NUMBERS8 = 40; + int KEY_CODE_NUMBERS9 = 41; + int KEY_CODE_DOT = 42; + int KEY_CODE_ENTER = 43; + int KEY_CODE_CLEAR = 44; + int KEY_CODE_NEXT_FAVORITE = 47; + int KEY_CODE_CHANNEL_UP = 48; + int KEY_CODE_CHANNEL_DOWN = 49; + int KEY_CODE_PREVIOUS_CHANNEL = 50; + int KEY_CODE_SOUND_SELECT = 51; + int KEY_CODE_INPUT_SELECT = 52; + int KEY_CODE_DISPLAY_INFORMATION = 53; + int KEY_CODE_HELP = 54; + int KEY_CODE_PAGE_UP = 55; + int KEY_CODE_PAGE_DOWN = 56; + int KEY_CODE_POWER = 64; + int KEY_CODE_VOLUME_UP = 65; + int KEY_CODE_VOLUME_DOWN = 66; + int KEY_CODE_MUTE = 67; + int KEY_CODE_PLAY = 68; + int KEY_CODE_STOP = 69; + int KEY_CODE_PAUSE = 70; + int KEY_CODE_RECORD = 71; + int KEY_CODE_REWIND = 72; + int KEY_CODE_FAST_FORWARD = 73; + int KEY_CODE_EJECT = 74; + int KEY_CODE_FORWARD = 75; + int KEY_CODE_BACKWARD = 76; + int KEY_CODE_STOP_RECORD = 77; + int KEY_CODE_PAUSE_RECORD = 78; + int KEY_CODE_RESERVED = 79; + int KEY_CODE_ANGLE = 80; + int KEY_CODE_SUB_PICTURE = 81; + int KEY_CODE_VIDEO_ON_DEMAND = 82; + int KEY_CODE_ELECTRONIC_PROGRAM_GUIDE = 83; + int KEY_CODE_TIMER_PROGRAMMING = 84; + int KEY_CODE_INITIAL_CONFIGURATION = 85; + int KEY_CODE_SELECT_BROADCAST_TYPE = 86; + int KEY_CODE_SELECT_SOUND_PRESENTATION = 87; + int KEY_CODE_PLAY_FUNCTION = 96; + int KEY_CODE_PAUSE_PLAY_FUNCTION = 97; + int KEY_CODE_RECORD_FUNCTION = 98; + int KEY_CODE_PAUSE_RECORD_FUNCTION = 99; + int KEY_CODE_STOP_FUNCTION = 100; + int KEY_CODE_MUTE_FUNCTION = 101; + int KEY_CODE_RESTORE_VOLUME_FUNCTION = 102; + int KEY_CODE_TUNE_FUNCTION = 103; + int KEY_CODE_SELECT_MEDIA_FUNCTION = 104; + int KEY_CODE_SELECT_AV_INPUT_FUNCTION = 105; + int KEY_CODE_SELECT_AUDIO_INPUT_FUNCTION = 106; + int KEY_CODE_POWER_TOGGLE_FUNCTION = 107; + int KEY_CODE_POWER_OFF_FUNCTION = 108; + int KEY_CODE_POWER_ON_FUNCTION = 109; + int KEY_CODE_F1_BLUE = 113; + int KEY_CODE_F2_RED = 114; + int KEY_CODE_F3_GREEN = 115; + int KEY_CODE_F4_YELLOW = 116; + int KEY_CODE_F5 = 117; + int KEY_CODE_DATA = 118; + + int KEY_STATUS_SUCCESS = 0; + int KEY_STATUS_UNSUPPORTED_KEY = 1; + int KEY_STATUS_INVALID_KEY_IN_CURRENT_STATE = 1; + + /** + * Inject a key code to system + * + * @param keyCode keys in KEY_CODE_XXX + * @return KEY_STATUS_XXX above + */ + int sendKey(int keyCode); +} diff --git a/examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManagerStub.java b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManagerStub.java new file mode 100644 index 00000000000000..0c5d2f9734efaf --- /dev/null +++ b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/KeypadInputManagerStub.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.tcl.chip.tvapp; + +import android.util.Log; + +/** Stub implement of KeypadInputManager, TV manufacture should have there own implements */ +public class KeypadInputManagerStub implements KeypadInputManager { + + private final String TAG = KeypadInputManagerStub.class.getSimpleName(); + + @Override + public int sendKey(int keyCode) { + Log.d(TAG, "sendKey:" + keyCode); + return KEY_STATUS_SUCCESS; + } +} diff --git a/examples/tv-app/android/java/src/com/tcl/tvapp/TvApp.java b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/TvApp.java similarity index 88% rename from examples/tv-app/android/java/src/com/tcl/tvapp/TvApp.java rename to examples/tv-app/android/java/src/com/tcl/chip/tvapp/TvApp.java index f4afa50734fcf4..52c48fab24767b 100644 --- a/examples/tv-app/android/java/src/com/tcl/tvapp/TvApp.java +++ b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/TvApp.java @@ -15,11 +15,13 @@ * limitations under the License. * */ -package com.tcl.tvapp; +package com.tcl.chip.tvapp; public class TvApp { public TvApp() {} + public native void setKeypadInputManager(KeypadInputManager manager); + static { System.loadLibrary("TvApp"); }