Skip to content

Commit

Permalink
Keypad input cluster to android (#11952)
Browse files Browse the repository at this point in the history
* 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 <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Dec 12, 2023
1 parent 0f124f5 commit 2343321
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 61 deletions.
6 changes: 6 additions & 0 deletions examples/tv-app/android/App/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand Down
10 changes: 7 additions & 3 deletions examples/tv-app/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
]

Expand Down Expand Up @@ -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" ]

Expand Down

This file was deleted.

77 changes: 77 additions & 0 deletions examples/tv-app/android/java/KeypadInputManager.cpp
Original file line number Diff line number Diff line change
@@ -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 <app/util/af.h>
#include <app/util/basic-types.h>
#include <cstddef>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/JniReferences.h>

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<jint>(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<EmberAfKeypadInputStatus>(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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@
#pragma once

#include <app-common/zap-generated/af-structs.h>

#include <jni.h>
#include <lib/core/CHIPError.h>
#include <list>

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;
}
13 changes: 8 additions & 5 deletions examples/tv-app/android/java/TVApp-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
*
*/

/**
* @file
* Implementation of JNI bridge for Tv App on Android.
*
*/
#include "KeypadInputManager.h"
#include <app/server/java/AndroidAppServerWrapper.h>
#include <jni.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.h>

#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);
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit 2343321

Please sign in to comment.