Skip to content

Commit

Permalink
feat: Convert kotlin to java to avoid kotlin version conflict (#2)
Browse files Browse the repository at this point in the history
* feat: Convert kotlin to java to avoid kotlin version conflict
  • Loading branch information
littleGnAl committed Jul 22, 2022
1 parent e7d6c84 commit e972026
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 264 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
pull_request:
branches:
- main
- release/**

jobs:
flutter_codestyle_check:
Expand Down
33 changes: 11 additions & 22 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,55 +1,46 @@
group 'io.agora.agora_rtc_ng'
version '1.0-SNAPSHOT'

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

buildscript {
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://www.jitpack.io' }
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 31
compileSdkVersion safeExtGet('compileSdkVersion', 31)

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)

consumerProguardFiles 'consumer-rules.pro'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = '1.8'
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'

if (isDev(project)) {
main.jniLibs.srcDirs += 'libs'
}
}

defaultConfig {
minSdkVersion 16

consumerProguardFiles 'consumer-rules.pro'
}
}

dependencies {
Expand All @@ -59,8 +50,6 @@ dependencies {
api 'io.agora.rtc:iris-rtc:3.8.201'
api 'io.agora.rtc:agora-special-full:3.8.201.2.2'
}

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

static boolean isDev(Project project) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.agora.agora_rtc_ng;

import android.content.Context;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.agora.iris.IrisApiEngine;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class AgoraPlatformViewFactory extends PlatformViewFactory {

private final String viewType;
private final BinaryMessenger messenger;
private final PlatformViewProvider viewProvider;

AgoraPlatformViewFactory(
String viewType,
BinaryMessenger messenger,
PlatformViewProvider viewProvider) {
super(StandardMessageCodec.INSTANCE);
this.viewType = viewType;
this.messenger = messenger;
this.viewProvider = viewProvider;
}

interface PlatformViewProvider {
View provide(Context context);
}

public static class PlatformViewProviderTextureView implements PlatformViewProvider {

@Override
public View provide(Context context) {
return new TextureView(context);
}
}

public static class PlatformViewProviderSurfaceView implements PlatformViewProvider {

@Override
public View provide(Context context) {
return new SurfaceView(context);
}
}

static class AgoraPlatformView implements PlatformView, MethodChannel.MethodCallHandler {
private View innerView;

private final MethodChannel methodChannel;


private long platformViewPtr;

AgoraPlatformView(Context context,
String viewType,
int viewId,
PlatformViewProvider viewProvider,
BinaryMessenger messenger) {
methodChannel = new MethodChannel(messenger, "agora_rtc_ng/" + viewType + "_" + viewId);
methodChannel.setMethodCallHandler(this);
innerView = viewProvider.provide(context);

platformViewPtr = IrisApiEngine.GetJObjectAddress(innerView);
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
if (call.method.equals("getNativeViewPtr")) {
result.success(platformViewPtr);
} else if (call.method.equals("deleteNativeViewPtr")) {
if (platformViewPtr != 0L) {
IrisApiEngine.FreeJObjectByAddress(platformViewPtr);
platformViewPtr = 0;
}
innerView = null;
result.success(0);
}
}

@Nullable
@Override
public View getView() {
return innerView;
}

@Override
public void dispose() {

}
}

@NonNull
@Override
public PlatformView create(@Nullable Context context, int viewId, @Nullable Object args) {
return new AgoraPlatformView(
context,
viewType,
viewId,
viewProvider,
messenger
);
}
}
84 changes: 84 additions & 0 deletions android/src/main/java/io/agora/agora_rtc_ng/AgoraRtcNgPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.agora.agora_rtc_ng;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.lang.ref.WeakReference;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class AgoraRtcNgPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {

static {
System.loadLibrary("AgoraRtcWrapper");
}

private MethodChannel channel;
private WeakReference<FlutterPluginBinding> flutterPluginBindingRef;
private VideoViewController videoViewController;

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "agora_rtc_ng");
channel.setMethodCallHandler(this);
flutterPluginBindingRef = new WeakReference<>(flutterPluginBinding);

flutterPluginBinding.getPlatformViewRegistry().registerViewFactory(
"AgoraTextureView",
new AgoraPlatformViewFactory(
"AgoraTextureView",
flutterPluginBinding.getBinaryMessenger(),
new AgoraPlatformViewFactory.PlatformViewProviderTextureView()));

flutterPluginBinding.getPlatformViewRegistry().registerViewFactory(
"AgoraSurfaceView",
new AgoraPlatformViewFactory(
"AgoraSurfaceView",
flutterPluginBinding.getBinaryMessenger(),
new AgoraPlatformViewFactory.PlatformViewProviderSurfaceView()));

videoViewController = new VideoViewController(flutterPluginBinding.getBinaryMessenger());
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
videoViewController.dispose();
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
if ("getAssetAbsolutePath".equals(call.method)) {
getAssetAbsolutePath(call, result);
} else {
result.notImplemented();
}
}

private void getAssetAbsolutePath(MethodCall call, MethodChannel.Result result) {
final String path = call.arguments();

if (path != null) {
if (this.flutterPluginBindingRef.get() != null
) {
final String assetKey = this.flutterPluginBindingRef.get()
.getFlutterAssets()
.getAssetFilePathByName(path);
try {
this.flutterPluginBindingRef.get().getApplicationContext()
.getAssets()
.openFd(assetKey)
.close();
result.success("/assets/" + assetKey);
return;
} catch (IOException e) {
result.error(e.getClass().getSimpleName(), e.getMessage(), e.getCause());
return;
}
}
}
result.error("IllegalArgumentException", "The parameter should not be null", null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.agora.agora_rtc_ng;

import androidx.annotation.NonNull;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class VideoViewController implements MethodChannel.MethodCallHandler {

private final MethodChannel methodChannel;

VideoViewController(BinaryMessenger binaryMessenger) {
methodChannel = new MethodChannel(binaryMessenger, "agora_rtc_ng/video_view_controller");
methodChannel.setMethodCallHandler(this);
}

private long createPlatformRender(){
return 0L;
}

private boolean destroyPlatformRender(long platformRenderId) {
return true;
}

private long createTextureRender() {
return 0L;
}

private boolean destroyTextureRender(long textureId){
return false;
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
switch (call.method) {
case "attachVideoFrameBufferManager":
case "detachVideoFrameBufferManager":
result.success(true);
break;

case "createTextureRender":
case "destroyTextureRender":
case "updateTextureRenderData":
default:
result.notImplemented();
break;
}
}

public void dispose() {
methodChannel.setMethodCallHandler(null);
}
}
Loading

0 comments on commit e972026

Please sign in to comment.