KeypadInputManager.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "KeypadInputManager.h"
  19. #include "TvApp-JNI.h"
  20. #include <app-common/zap-generated/ids/Clusters.h>
  21. #include <lib/support/CHIPJNIError.h>
  22. #include <lib/support/JniReferences.h>
  23. using namespace chip;
  24. using namespace chip::app::Clusters::KeypadInput;
  25. void emberAfKeypadInputClusterInitCallback(EndpointId endpoint)
  26. {
  27. ChipLogProgress(Zcl, "TV Android App: KeypadInput::PostClusterInit");
  28. TvAppJNIMgr().PostClusterInit(chip::app::Clusters::KeypadInput::Id, endpoint);
  29. }
  30. void KeypadInputManager::NewManager(jint endpoint, jobject manager)
  31. {
  32. ChipLogProgress(Zcl, "TV Android App: KeypadInput::SetDefaultDelegate");
  33. KeypadInputManager * mgr = new KeypadInputManager();
  34. mgr->InitializeWithObjects(manager);
  35. chip::app::Clusters::KeypadInput::SetDefaultDelegate(static_cast<EndpointId>(endpoint), mgr);
  36. }
  37. void KeypadInputManager::HandleSendKey(CommandResponseHelper<SendKeyResponseType> & helper, const CecKeyCode & keyCode)
  38. {
  39. Commands::SendKeyResponse::Type response;
  40. jint ret = -1;
  41. CHIP_ERROR err = CHIP_NO_ERROR;
  42. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  43. ChipLogProgress(Zcl, "Received keypadInputClusterSendKey: %c", to_underlying(keyCode));
  44. VerifyOrExit(mKeypadInputManagerObject != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
  45. VerifyOrExit(mSendKeyMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
  46. VerifyOrExit(env != NULL, err = CHIP_JNI_ERROR_NO_ENV);
  47. env->ExceptionClear();
  48. ret = env->CallIntMethod(mKeypadInputManagerObject, mSendKeyMethod, static_cast<jint>(keyCode));
  49. VerifyOrExit(!env->ExceptionCheck(), err = CHIP_JNI_ERROR_EXCEPTION_THROWN);
  50. exit:
  51. if (err != CHIP_NO_ERROR)
  52. {
  53. response.status = chip::app::Clusters::KeypadInput::KeypadInputStatusEnum::kInvalidKeyInCurrentState;
  54. }
  55. else
  56. {
  57. response.status = static_cast<chip::app::Clusters::KeypadInput::KeypadInputStatusEnum>(ret);
  58. }
  59. helper.Success(response);
  60. }
  61. void KeypadInputManager::InitializeWithObjects(jobject managerObject)
  62. {
  63. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  64. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for KeypadInputManager"));
  65. mKeypadInputManagerObject = env->NewGlobalRef(managerObject);
  66. VerifyOrReturn(mKeypadInputManagerObject != nullptr, ChipLogError(Zcl, "Failed to NewGlobalRef KeypadInputManager"));
  67. jclass KeypadInputManagerClass = env->GetObjectClass(managerObject);
  68. VerifyOrReturn(KeypadInputManagerClass != nullptr, ChipLogError(Zcl, "Failed to get KeypadInputManager Java class"));
  69. mSendKeyMethod = env->GetMethodID(KeypadInputManagerClass, "sendKey", "(I)I");
  70. if (mSendKeyMethod == nullptr)
  71. {
  72. ChipLogError(Zcl, "Failed to access KeypadInputManager 'sendKey' method");
  73. env->ExceptionClear();
  74. }
  75. }