-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Apply 'AppDelegate' for android app server (#28286)
* Apply 'AppDelegate' for android app server * Add 'starAppWithDelegate' api * Add 'ChipAppServerDelegate' interface Signed-off-by: Jaehoon You <[email protected]> Signed-off-by: Charles Kim <[email protected]> * Restyled by whitespace * Restyled by google-java-format * Restyled by clang-format Signed-off-by: Charles Kim <[email protected]> * Restyled by google-java-format Signed-off-by: Charles Kim <[email protected]> --------- Signed-off-by: Jaehoon You <[email protected]> Signed-off-by: Charles Kim <[email protected]> Co-authored-by: Jaehoon You <[email protected]> Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
1 parent
d7a7dee
commit 2b76339
Showing
9 changed files
with
321 additions
and
2 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...roid/App/core/matter/src/main/java/com/matter/virtual/device/app/core/matter/MatterApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.matter.virtual.device.app.core.matter | ||
|
||
import chip.appserver.ChipAppServer | ||
import chip.appserver.ChipAppServerDelegate | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import timber.log.Timber | ||
|
||
@Singleton | ||
class MatterApp @Inject constructor() { | ||
|
||
private var chipAppServer: ChipAppServer? = null | ||
|
||
fun start() { | ||
chipAppServer = ChipAppServer() | ||
chipAppServer?.startAppWithDelegate( | ||
object : ChipAppServerDelegate { | ||
override fun onCommissioningSessionEstablishmentStarted() { | ||
Timber.d("onCommissioningSessionEstablishmentStarted()") | ||
} | ||
|
||
override fun onCommissioningSessionStarted() { | ||
Timber.d("onCommissioningSessionStarted()") | ||
} | ||
|
||
override fun onCommissioningSessionEstablishmentError(errorCode: Int) { | ||
Timber.d("onCommissioningSessionEstablishmentError():$errorCode") | ||
} | ||
|
||
override fun onCommissioningSessionStopped() { | ||
Timber.d("onCommissioningSessionStopped()") | ||
} | ||
|
||
override fun onCommissioningWindowOpened() { | ||
Timber.d("onCommissioningWindowOpened()") | ||
} | ||
|
||
override fun onCommissioningWindowClosed() { | ||
Timber.d("onCommissioningWindowClosed()") | ||
} | ||
} | ||
) | ||
} | ||
|
||
fun stop() { | ||
chipAppServer?.stopApp() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 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. | ||
*/ | ||
|
||
#include "ChipAppServerDelegate.h" | ||
#include <lib/support/CHIPJNIError.h> | ||
#include <lib/support/JniReferences.h> | ||
|
||
using namespace chip; | ||
|
||
void ChipAppServerDelegate::OnCommissioningSessionEstablishmentStarted() | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "JNIEnv is nullptr")); | ||
VerifyOrReturn(mOnCommissioningSessionEstablishmentStartedMethod != nullptr, | ||
ChipLogError(AppServer, "mOnCommissioningSessionEstablishmentStartedMethod is nullptr")); | ||
|
||
env->ExceptionClear(); | ||
env->CallVoidMethod(mChipAppServerDelegateObject, mOnCommissioningSessionEstablishmentStartedMethod); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Java exception in OnCommissioningSessionEstablishmentStartedMethod"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void ChipAppServerDelegate::OnCommissioningSessionStarted() | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "JNIEnv is nullptr")); | ||
VerifyOrReturn(mOnCommissioningSessionStartedMethod != nullptr, | ||
ChipLogError(AppServer, "mOnCommissioningSessionStartedMethod is nullptr")); | ||
|
||
env->ExceptionClear(); | ||
env->CallVoidMethod(mChipAppServerDelegateObject, mOnCommissioningSessionStartedMethod); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Java exception in OnCommissioningSessionStartedMethod"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void ChipAppServerDelegate::OnCommissioningSessionEstablishmentError(CHIP_ERROR err) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "JNIEnv is nullptr")); | ||
VerifyOrReturn(mOnCommissioningSessionEstablishmentErrorMethod != nullptr, | ||
ChipLogError(AppServer, "mOnCommissioningSessionEstablishmentErrorMethod is nullptr")); | ||
|
||
env->ExceptionClear(); | ||
env->CallVoidMethod(mChipAppServerDelegateObject, mOnCommissioningSessionEstablishmentErrorMethod, | ||
static_cast<jint>(err.AsInteger())); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Java exception in OnCommissioningSessionEstablishmentErrorMethod"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void ChipAppServerDelegate::OnCommissioningSessionStopped() | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "JNIEnv is nullptr")); | ||
VerifyOrReturn(mOnCommissioningSessionStoppedMethod != nullptr, | ||
ChipLogError(AppServer, "mOnCommissioningSessionStoppedMethod is nullptr")); | ||
|
||
env->ExceptionClear(); | ||
env->CallVoidMethod(mChipAppServerDelegateObject, mOnCommissioningSessionStoppedMethod); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Java exception in OnCommissioningSessionStoppedMethod"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void ChipAppServerDelegate::OnCommissioningWindowOpened() | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "JNIEnv is nullptr")); | ||
VerifyOrReturn(mOnCommissioningWindowOpenedMethod != nullptr, | ||
ChipLogError(AppServer, "mOnCommissioningWindowOpenedMethod is nullptr")); | ||
|
||
env->ExceptionClear(); | ||
env->CallVoidMethod(mChipAppServerDelegateObject, mOnCommissioningWindowOpenedMethod); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Java exception in OnCommissioningWindowOpenedMethod"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void ChipAppServerDelegate::OnCommissioningWindowClosed() | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "JNIEnv is nullptr")); | ||
VerifyOrReturn(mOnCommissioningWindowClosedMethod != nullptr, | ||
ChipLogError(AppServer, "mOnCommissioningWindowClosedMethod is nullptr")); | ||
|
||
env->ExceptionClear(); | ||
env->CallVoidMethod(mChipAppServerDelegateObject, mOnCommissioningWindowClosedMethod); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Java exception in OnCommissioningWindowClosedMethod"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
CHIP_ERROR ChipAppServerDelegate::InitializeWithObjects(jobject appDelegateObject) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturnLogError(env != nullptr, CHIP_JNI_ERROR_NO_ENV); | ||
|
||
mChipAppServerDelegateObject = env->NewGlobalRef(appDelegateObject); | ||
VerifyOrReturnLogError(mChipAppServerDelegateObject != nullptr, CHIP_JNI_ERROR_NULL_OBJECT); | ||
|
||
jclass chipAppServerDelegateClass = env->GetObjectClass(mChipAppServerDelegateObject); | ||
VerifyOrReturnLogError(chipAppServerDelegateClass != nullptr, CHIP_JNI_ERROR_JAVA_ERROR); | ||
|
||
mOnCommissioningSessionEstablishmentStartedMethod = | ||
env->GetMethodID(chipAppServerDelegateClass, "onCommissioningSessionEstablishmentStarted", "()V"); | ||
VerifyOrReturnLogError(mOnCommissioningSessionEstablishmentStartedMethod != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND); | ||
|
||
mOnCommissioningSessionStartedMethod = env->GetMethodID(chipAppServerDelegateClass, "onCommissioningSessionStarted", "()V"); | ||
VerifyOrReturnLogError(mOnCommissioningSessionStartedMethod != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND); | ||
|
||
mOnCommissioningSessionEstablishmentErrorMethod = | ||
env->GetMethodID(chipAppServerDelegateClass, "onCommissioningSessionEstablishmentError", "(I)V"); | ||
VerifyOrReturnLogError(mOnCommissioningSessionEstablishmentErrorMethod != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND); | ||
|
||
mOnCommissioningSessionStoppedMethod = env->GetMethodID(chipAppServerDelegateClass, "onCommissioningSessionStopped", "()V"); | ||
VerifyOrReturnLogError(mOnCommissioningSessionStoppedMethod != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND); | ||
|
||
mOnCommissioningWindowOpenedMethod = env->GetMethodID(chipAppServerDelegateClass, "onCommissioningWindowOpened", "()V"); | ||
VerifyOrReturnLogError(mOnCommissioningWindowOpenedMethod != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND); | ||
|
||
mOnCommissioningWindowClosedMethod = env->GetMethodID(chipAppServerDelegateClass, "onCommissioningWindowClosed", "()V"); | ||
VerifyOrReturnLogError(mOnCommissioningWindowClosedMethod != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND); | ||
|
||
return CHIP_NO_ERROR; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 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 <app/server/AppDelegate.h> | ||
#include <jni.h> | ||
#include <lib/core/CHIPError.h> | ||
|
||
class ChipAppServerDelegate : public AppDelegate | ||
{ | ||
public: | ||
void OnCommissioningSessionEstablishmentStarted() override; | ||
void OnCommissioningSessionStarted() override; | ||
void OnCommissioningSessionEstablishmentError(CHIP_ERROR err) override; | ||
void OnCommissioningSessionStopped() override; | ||
void OnCommissioningWindowOpened() override; | ||
void OnCommissioningWindowClosed() override; | ||
|
||
CHIP_ERROR InitializeWithObjects(jobject appDelegateObject); | ||
|
||
private: | ||
jobject mChipAppServerDelegateObject = nullptr; | ||
jmethodID mOnCommissioningSessionEstablishmentStartedMethod = nullptr; | ||
jmethodID mOnCommissioningSessionStartedMethod = nullptr; | ||
jmethodID mOnCommissioningSessionEstablishmentErrorMethod = nullptr; | ||
jmethodID mOnCommissioningSessionStoppedMethod = nullptr; | ||
jmethodID mOnCommissioningWindowOpenedMethod = nullptr; | ||
jmethodID mOnCommissioningWindowClosedMethod = nullptr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/app/server/java/src/chip/appserver/ChipAppServerDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2023 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 chip.appserver; | ||
|
||
public interface ChipAppServerDelegate { | ||
void onCommissioningSessionEstablishmentStarted(); | ||
|
||
void onCommissioningSessionStarted(); | ||
|
||
void onCommissioningSessionEstablishmentError(int errorCode); | ||
|
||
void onCommissioningSessionStopped(); | ||
|
||
void onCommissioningWindowOpened(); | ||
|
||
void onCommissioningWindowClosed(); | ||
} |