diff --git a/examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/service/MatterServant.java b/examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/service/MatterServant.java index 574aadb0313ca4..f6994264086033 100644 --- a/examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/service/MatterServant.java +++ b/examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/service/MatterServant.java @@ -34,6 +34,7 @@ import com.tcl.chip.tvapp.Clusters; import com.tcl.chip.tvapp.ContentLaunchManagerStub; import com.tcl.chip.tvapp.DACProviderStub; +import com.tcl.chip.tvapp.DeviceEventProvider; import com.tcl.chip.tvapp.KeypadInputManagerStub; import com.tcl.chip.tvapp.LevelManagerStub; import com.tcl.chip.tvapp.LowPowerManagerStub; @@ -113,6 +114,13 @@ public void init(@NonNull Context context) { }); mTvApp.setDACProvider(new DACProviderStub()); + mTvApp.setChipDeviceEventProvider( + new DeviceEventProvider() { + @Override + public void onCommissioningComplete() { + Log.d("lz", "onCommissioningComplete: "); + } + }); Context applicationContext = context.getApplicationContext(); AndroidChipPlatform chipPlatform = new AndroidChipPlatform( diff --git a/examples/tv-app/android/BUILD.gn b/examples/tv-app/android/BUILD.gn index 5267c38cb0f217..0790280116dde3 100644 --- a/examples/tv-app/android/BUILD.gn +++ b/examples/tv-app/android/BUILD.gn @@ -48,6 +48,8 @@ shared_library("jni") { "java/ContentAppCommandDelegate.h", "java/ContentLauncherManager.cpp", "java/ContentLauncherManager.h", + "java/DeviceCallbacks.cpp", + "java/DeviceCallbacks.h", "java/JNIDACProvider.cpp", "java/JNIDACProvider.h", "java/KeypadInputManager.cpp", @@ -110,6 +112,7 @@ android_library("java") { "java/src/com/tcl/chip/tvapp/ContentLaunchSearchParameter.java", "java/src/com/tcl/chip/tvapp/DACProvider.java", "java/src/com/tcl/chip/tvapp/DACProviderStub.java", + "java/src/com/tcl/chip/tvapp/DeviceEventProvider.java", "java/src/com/tcl/chip/tvapp/KeypadInputManager.java", "java/src/com/tcl/chip/tvapp/KeypadInputManagerStub.java", "java/src/com/tcl/chip/tvapp/LevelManager.java", diff --git a/examples/tv-app/android/java/DeviceCallbacks.cpp b/examples/tv-app/android/java/DeviceCallbacks.cpp new file mode 100644 index 00000000000000..3388d216c60d67 --- /dev/null +++ b/examples/tv-app/android/java/DeviceCallbacks.cpp @@ -0,0 +1,107 @@ +/* + * + * Copyright (c) 2022 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. + */ + +/** + * @file DeviceCallbacks.cpp + * + * Implements all the callbacks to the application from the CHIP Stack + * + **/ +#include "DeviceCallbacks.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace ::chip; +using namespace ::chip::Inet; +using namespace ::chip::System; +using namespace ::chip::DeviceLayer; +using namespace ::chip::Logging; +namespace { +void OnPlatformEventWrapper(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg) +{ + DeviceCallbacks * commissionMgr = reinterpret_cast(arg); + commissionMgr->OnPlatformEvent(event); +} +} // namespace +void DeviceCallbacks::NewManager(jobject manager) +{ + ChipLogProgress(AppServer, "TV Android App: set ChipDeviceEvent delegate"); + DeviceCallbacks * mgr = new DeviceCallbacks(); + PlatformMgr().AddEventHandler(OnPlatformEventWrapper, reinterpret_cast(mgr)); + mgr->InitializeWithObjects(manager); +} + +void DeviceCallbacks::InitializeWithObjects(jobject provider) +{ + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); + VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "Failed to GetEnvForCurrentThread for DeviceEventProvider")); + + mProvider = env->NewGlobalRef(provider); + VerifyOrReturn(mProvider != nullptr, ChipLogError(AppServer, "Failed to NewGlobalRef DeviceEventProvider")); + jclass deviceEventProviderCls = env->GetObjectClass(mProvider); + VerifyOrReturn(deviceEventProviderCls != nullptr, ChipLogError(AppServer, "Failed to get KeypadInputManager Java class")); + + mCommissioningCompleteMethod = env->GetMethodID(deviceEventProviderCls, "onCommissioningComplete", "()V"); + if (mCommissioningCompleteMethod == nullptr) + { + ChipLogError(AppServer, "Failed to access DeviceEventProvider 'onCommissioningComplete' method"); + env->ExceptionClear(); + } +} + +void DeviceCallbacks::OnPlatformEvent(const ChipDeviceEvent * event) +{ + switch (event->Type) + { + case DeviceEventType::kCommissioningComplete: + OnCommissioningComplete(event); + break; + case DeviceEventType::kSessionEstablished: + OnSessionEstablished(event); + break; + } +} + +void DeviceCallbacks::OnSessionEstablished(const ChipDeviceEvent * event) +{ + if (event->SessionEstablished.IsCommissioner) + { + ChipLogProgress(AppServer, "Commissioner detected!"); + } +} +void DeviceCallbacks::OnCommissioningComplete(const ChipDeviceEvent * event) +{ + JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); + VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for DeviceEventProvider")); + env->CallVoidMethod(mProvider, mCommissioningCompleteMethod); + if (env->ExceptionCheck()) + { + ChipLogError(AppServer, "Java exception in DeviceEventProvider::onCommissioningComplete"); + env->ExceptionDescribe(); + env->ExceptionClear(); + } +} diff --git a/examples/tv-app/android/java/DeviceCallbacks.h b/examples/tv-app/android/java/DeviceCallbacks.h new file mode 100644 index 00000000000000..15c336a10a9156 --- /dev/null +++ b/examples/tv-app/android/java/DeviceCallbacks.h @@ -0,0 +1,35 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * 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. + */ +#pragma once + +#include "lib/support/logging/CHIPLogging.h" +#include + +class DeviceCallbacks +{ + +public: + static void NewManager(jobject manager); + void OnPlatformEvent(const chip::DeviceLayer::ChipDeviceEvent * event); + void InitializeWithObjects(jobject manager); + +private: + jobject mProvider = nullptr; + jmethodID mCommissioningCompleteMethod = nullptr; + void OnSessionEstablished(const chip::DeviceLayer::ChipDeviceEvent * event); + void OnCommissioningComplete(const chip::DeviceLayer::ChipDeviceEvent * event); +}; diff --git a/examples/tv-app/android/java/TVApp-JNI.cpp b/examples/tv-app/android/java/TVApp-JNI.cpp index 2c02e5f24f98c1..fc4b69d702041c 100644 --- a/examples/tv-app/android/java/TVApp-JNI.cpp +++ b/examples/tv-app/android/java/TVApp-JNI.cpp @@ -20,6 +20,7 @@ #include "AppImpl.h" #include "ChannelManager.h" #include "ContentLauncherManager.h" +#include "DeviceCallbacks.h" #include "JNIDACProvider.h" #include "KeypadInputManager.h" #include "LevelManager.h" @@ -177,6 +178,11 @@ JNI_METHOD(jboolean, setCurrentLevel)(JNIEnv *, jobject, jint endpoint, jboolean return LevelManager::SetLevel(endpoint, value); } +JNI_METHOD(void, setChipDeviceEventProvider)(JNIEnv *, jobject, jobject provider) +{ + DeviceCallbacks::NewManager(provider); +} + JNI_METHOD(jint, addContentApp) (JNIEnv *, jobject, jstring vendorName, jint vendorId, jstring appName, jint productId, jstring appVersion, jobject manager) { diff --git a/examples/tv-app/android/java/src/com/tcl/chip/tvapp/DeviceEventProvider.java b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/DeviceEventProvider.java new file mode 100644 index 00000000000000..d4e4fd8efff121 --- /dev/null +++ b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/DeviceEventProvider.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 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 DeviceEventProvider { + void onCommissioningComplete(); +} diff --git a/examples/tv-app/android/java/src/com/tcl/chip/tvapp/TvApp.java b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/TvApp.java index 3b341a2a3ce8af..f1b3d1a15d7a5e 100644 --- a/examples/tv-app/android/java/src/com/tcl/chip/tvapp/TvApp.java +++ b/examples/tv-app/android/java/src/com/tcl/chip/tvapp/TvApp.java @@ -67,6 +67,8 @@ private void postClusterInit(int clusterId, int endpoint) { public native void setDACProvider(DACProvider provider); + public native void setChipDeviceEventProvider(DeviceEventProvider provider); + public native int addContentApp( String vendorName, int vendorId,