-
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.
Adding the JNI layer for Android tv-casting-app (#15001)
- Loading branch information
1 parent
91ea3d5
commit 787d670
Showing
9 changed files
with
295 additions
and
5 deletions.
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletions
21
...es/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/TvCastingAppCallback.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,21 @@ | ||
/* | ||
* | ||
* 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. | ||
*/ | ||
package com.chip.casting; | ||
|
||
public interface TvCastingAppCallback { | ||
void onClusterInit(TvCastingApp app, int clusterId, int endpoint); | ||
} |
86 changes: 86 additions & 0 deletions
86
examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp
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,86 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#include "TvCastingApp-JNI.h" | ||
#include <app/server/java/AndroidAppServerWrapper.h> | ||
#include <jni.h> | ||
#include <lib/core/CHIPError.h> | ||
#include <lib/support/CHIPJNIError.h> | ||
#include <lib/support/JniReferences.h> | ||
|
||
using namespace chip; | ||
|
||
#define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_com_chip_casting_TvCastingApp_##METHOD_NAME | ||
|
||
TvCastingAppJNI TvCastingAppJNI::sInstance; | ||
|
||
void TvCastingAppJNI::InitializeWithObjects(jobject app) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "Failed to GetEnvForCurrentThread for TvCastingAppJNI")); | ||
|
||
mTvCastingAppObject = env->NewGlobalRef(app); | ||
VerifyOrReturn(mTvCastingAppObject != nullptr, ChipLogError(AppServer, "Failed to NewGlobalRef TvCastingAppJNI")); | ||
|
||
jclass managerClass = env->GetObjectClass(mTvCastingAppObject); | ||
VerifyOrReturn(managerClass != nullptr, ChipLogError(AppServer, "Failed to get TvCastingAppJNI Java class")); | ||
|
||
mPostClusterInitMethod = env->GetMethodID(managerClass, "postClusterInit", "(II)V"); | ||
if (mPostClusterInitMethod == nullptr) | ||
{ | ||
ChipLogError(AppServer, "Failed to access ChannelManager 'postClusterInit' method"); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void TvCastingAppJNI::PostClusterInit(chip::ClusterId clusterId, chip::EndpointId endpoint) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
VerifyOrReturn(env != nullptr, | ||
ChipLogError(AppServer, "Failed to GetEnvForCurrentThread for TvCastingAppJNI::PostClusterInit")); | ||
VerifyOrReturn(mTvCastingAppObject != nullptr, ChipLogError(AppServer, "TvCastingAppJNI::mTvCastingAppObject null")); | ||
VerifyOrReturn(mPostClusterInitMethod != nullptr, ChipLogError(AppServer, "TvCastingAppJNI::mPostClusterInitMethod null")); | ||
|
||
env->CallVoidMethod(mTvCastingAppObject, mPostClusterInitMethod, static_cast<jint>(clusterId), static_cast<jint>(endpoint)); | ||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(AppServer, "Failed to call TvCastingAppJNI 'postClusterInit' method"); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
jint JNI_OnLoad(JavaVM * jvm, void * reserved) | ||
{ | ||
return AndroidAppServerJNI_OnLoad(jvm, reserved); | ||
} | ||
|
||
void JNI_OnUnload(JavaVM * jvm, void * reserved) | ||
{ | ||
return AndroidAppServerJNI_OnUnload(jvm, reserved); | ||
} | ||
|
||
JNI_METHOD(void, nativeInit)(JNIEnv *, jobject app) | ||
{ | ||
TvCastingAppJNIMgr().InitializeWithObjects(app); | ||
} | ||
|
||
// TBD: Temp dummy function for testing | ||
JNI_METHOD(void, doSomethingInCpp)(JNIEnv *, jobject, jint endpoint) | ||
{ | ||
ChipLogProgress(AppServer, "JNI_METHOD doSomethingInCpp called with endpoint %d", endpoint); | ||
} |
41 changes: 41 additions & 0 deletions
41
examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.h
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,41 @@ | ||
/* | ||
* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <jni.h> | ||
#include <lib/core/DataModelTypes.h> | ||
|
||
class TvCastingAppJNI | ||
{ | ||
public: | ||
void InitializeWithObjects(jobject app); | ||
void PostClusterInit(chip::ClusterId clusterId, chip::EndpointId endpoint); | ||
|
||
private: | ||
friend TvCastingAppJNI & TvCastingAppJNIMgr(); | ||
|
||
static TvCastingAppJNI sInstance; | ||
jobject mTvCastingAppObject = nullptr; | ||
jmethodID mPostClusterInitMethod = nullptr; | ||
}; | ||
|
||
inline class TvCastingAppJNI & TvCastingAppJNIMgr() | ||
{ | ||
return TvCastingAppJNI::sInstance; | ||
} |
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,97 @@ | ||
# Matter TV Casting Android App Example | ||
|
||
This is a Matter TV Casting Android app that can be used to cast content to a | ||
TV. This app discovers TVs on the local network that act as commissioners, lets | ||
the user select one, sends the TV a User Directed Commissioning request, enters | ||
commissioning mode, advertises itself as a Commissionable Node and gets | ||
commissioned. Then it allows the user to send Matter ContentLauncher commands to | ||
the TV. | ||
|
||
<hr> | ||
|
||
- [Requirements for building](#requirements) | ||
- [ABIs and TARGET_CPU](#abi) | ||
- [Gradle & JDK Version](#jdk) | ||
- [Preparing for build](#preparing) | ||
- [Building & Installing the app](#building-installing) | ||
- [Running the app on Android](#running-the-app-on-android) | ||
|
||
<hr> | ||
|
||
<a name="requirements"></a> | ||
|
||
## Requirements for building | ||
|
||
You need Android SDK 21 & NDK downloaded to your machine. Set the | ||
`$ANDROID_HOME` environment variable to where the SDK is downloaded and the | ||
`$ANDROID_NDK_HOME` environment variable to point to where the NDK package is | ||
downloaded. | ||
|
||
<a name="abi"></a> | ||
|
||
### ABIs and TARGET_CPU | ||
|
||
`TARGET_CPU` can have the following values, depending on your smartphone CPU | ||
architecture: | ||
|
||
| ABI | TARGET_CPU | | ||
| ----------- | ---------- | | ||
| armeabi-v7a | arm | | ||
| arm64-v8a | arm64 | | ||
| x86 | x86 | | ||
| x86_64 | x64 | | ||
|
||
<a name="jdk"></a> | ||
|
||
### Gradle & JDK Version | ||
|
||
We are using Gradle 7.1.1 for all android project which does not support Java 17 | ||
(https://docs.gradle.org/current/userguide/compatibility.html) while the default | ||
JDK version on MacOS for Apple Silicon is 'openjdk 17.0.1' or above. | ||
|
||
Using JDK bundled with Android Studio will help with that. | ||
|
||
```shell | ||
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home/ | ||
``` | ||
|
||
<hr> | ||
|
||
<a name="preparing"></a> | ||
|
||
## Preparing for build | ||
|
||
Complete the following steps to prepare the Matter build: | ||
|
||
1. Check out the Matter repository. | ||
|
||
2. Run bootstrap (**only required first time**) | ||
|
||
```shell | ||
source scripts/bootstrap.sh | ||
``` | ||
|
||
<a name="building-installing"></a> | ||
|
||
## Building & Installing the app | ||
|
||
This is the simplest option. In the command line, run the following command from | ||
the top Matter directory: | ||
|
||
```shell | ||
./scripts/build/build_examples.py --target android-arm64-chip-tv-casting-app build | ||
``` | ||
|
||
See the table above for other values of `TARGET_CPU`. | ||
|
||
The debug Android package `app-debug.apk` will be generated at | ||
`out/android-$TARGET_CPU-chip-tv-casting-app/outputs/apk/debug/`, and can be | ||
installed with | ||
|
||
```shell | ||
adb install out/android-$TARGET_CPU-chip-tv-casting-app/outputs/apk/debug/app-debug.apk | ||
``` | ||
|
||
You can use Android Studio to edit the Android app itself and run it after | ||
build_examples.py, but you will not be able to edit Matter Android code from | ||
`src/controller/java`, or other Matter C++ code within Android Studio. |
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