Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Dependencies

* JDK 21+ (source is Java 11 compatible)
* JDK 21+ (source is Java 11 compatible) (be sure to set `JAVA_HOME`)
* Run `make setup buf`

## Pure Java SDK
Expand Down
1 change: 1 addition & 0 deletions android/examples/module/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
62 changes: 62 additions & 0 deletions android/examples/module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Android Module Example

## Making a brand new module

1. Until this SDK is published, you should run this from the root directory of the
SDK `./gradlew install`
1. This will push artifacts to a mavenLocal repository that can be used in external projects on
the same host
2. Copy or work with the contents
in [standalone-examples/SimpleAndroidModule](../../../standalone-examples/SimpleAndroidModule) (
Java)
or [standalone-examples/SimpleAndroidModuleKT](../../../standalone-examples/SimpleAndroidModuleKT) (
Kotlin)
1. Note: Due to locally installing, you'll notice `mavenLocal` added to every repositories
configuration block. This will not be needed in the future.
2. `module.mainEntryClass` must be set and reflects the main class that has an entry point (
i.e. `public static void main`)
3. The next command depends on if you are wanting to use a local module or registry
* For local
1. `./gradlew pushModuleAdbDebug`
* This will push files to your selected AVD and you can use the following local module
in your machine config:
```json
{
"name": "android-module",
"executable_path": "/sdcard/Download/mod.sh",
"type": "local"
}
```
* For registry
1. `./gradlew copyModuleRelease --into </some/path>`
2. Use the CLI to package the contents of `/some/path` where `mod.sh` is the executable
and `module.jar` is the only additional file to be tarballed up.
4. The component to add will be a generic component with model `viam:generic:mygeneric`

## Building this example

1. From the root directory of the SDK,
run: `./gradlew :android:examples:viam-android-sdk-examples-module:build`
2. The next command depends on if you are wanting to use a local module or registry
* For local
1. `./gradlew :android:examples:viam-android-sdk-examples-module:pushModuleAdbDebug`
* This will push files to your selected AVD and you can use the following local module
in your machine config:
```json
{
"name": "android-module",
"executable_path": "/sdcard/Download/mod.sh",
"type": "local"
}
```
* For registry
1. `./gradlew :android:examples:viam-android-sdk-examples-module:copyModuleRelease --into </some/path>`
2. Use the CLI to package the contents of `/some/path` where `mod.sh` is the executable
and `module.jar` is the only additional file to be tarballed up.
3. The component to add will be a generic component with model `viam:generic:mygeneric`

## Development

You can use this example here for local development that does not require gradle installing. If you
do want to test the standalone example, be sure to `./gradlew install`

39 changes: 39 additions & 0 deletions android/examples/module/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:8.2.2'
}
}

plugins {
id 'com.viam.sdk.android.module' version '1.0-SNAPSHOT'
}

module {
mainEntryClass = "com.viam.sdk.android.examples.module.MyModule"
}

android {
namespace 'com.viam.sdk.android.examples.module'

defaultConfig {
minSdkVersion min_api
targetSdkVersion target_api
compileSdk target_api
versionCode 1
versionName "1.0"
}

buildTypes {
release {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

dependencies {
implementation project(':android:viam-android-sdk')
}
21 changes: 21 additions & 0 deletions android/examples/module/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions android/examples/module/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.viam.sdk.android.examples.module;


import com.google.protobuf.Struct;
import com.google.protobuf.Value;
import com.viam.common.v1.Common;
import com.viam.sdk.android.module.Module;
import com.viam.sdk.core.component.generic.Generic;
import com.viam.sdk.core.resource.Model;
import com.viam.sdk.core.resource.ModelFamily;
import com.viam.sdk.core.resource.Registry;
import com.viam.sdk.core.resource.Resource;
import com.viam.sdk.core.resource.ResourceCreatorRegistration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import viam.app.v1.Robot.ComponentConfig;

public class MyModule {

public static void main(String[] args) {
Registry.registerResourceCreator(
Generic.SUBTYPE,
MyGeneric.MODEL,
new ResourceCreatorRegistration(MyGeneric::new, MyGeneric::validateConfig)
);
final Module module = new Module(args);
module.start();
}

public static class MyGeneric extends Generic {

public static final Model MODEL = new Model(new ModelFamily("viam", "generic"), "mygeneric");

public MyGeneric(ComponentConfig config, Map<Common.ResourceName, Resource> dependencies) {
super(config.getName());
}

public static Set<String> validateConfig(final ComponentConfig config) {
return new HashSet<>();
}

@Override
public Struct doCommand(Map<String, Value> command) {
final Struct.Builder builder = Struct.newBuilder();
return builder.putFields("hello", Value.newBuilder().setBoolValue(true).build()).build();
}
}
}
4 changes: 2 additions & 2 deletions android/examples/simple/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
namespace 'com.viam.sdk.android.examples.simple'
}
Expand Down
38 changes: 20 additions & 18 deletions android/examples/simple/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ViamSDK">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ViamSDK">
<activity
android:exported="true"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.INTERNET" />

</manifest>
Original file line number Diff line number Diff line change
@@ -1,83 +1,81 @@
package com.viam.sdk.android.examples.simple;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.viam.component.movementsensor.v1.MovementSensorServiceGrpc;
import com.viam.component.movementsensor.v1.Movementsensor;
import com.viam.robot.v1.Robot;
import com.viam.robot.v1.RobotServiceGrpc;
import com.viam.sdk.core.DialOptions;
import com.viam.sdk.android.Dialer;
import android.support.v7.app.AppCompatActivity;
import com.viam.sdk.android.RobotClientFactory;
import com.viam.sdk.core.component.movementsensor.MovementSensor;
import com.viam.sdk.core.robot.RobotClient.Options;
import com.viam.sdk.core.rpc.DialOptions;
import com.viam.sdk.core.webrtc.DialWebRTCOptions;
import com.viam.sdk.core.webrtc.PeerConnection;
import org.webrtc.*;

import java.util.Optional;
import java.util.logging.Logger;

import org.webrtc.EglBase;
import org.webrtc.GlRectDrawer;
import org.webrtc.MediaStream;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoTrack;
import proto.rpc.v1.Auth;
import proto.stream.v1.Stream;
import proto.stream.v1.StreamServiceGrpc;

public class MainActivity extends AppCompatActivity {

static SurfaceViewRenderer fullscreenRenderer;
static SurfaceViewRenderer fullscreenRenderer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//noinspection resource
final RobotClientFactory robotClientFactory = new RobotClientFactory(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullscreenRenderer = findViewById(R.id.fullscreen_video_view);
fullscreenRenderer.init(
robotClientFactory.getEGLBase().getEglBaseContext(),
null,
EglBase.CONFIG_PLAIN,
new GlRectDrawer());

final Dialer dialer = new Dialer(this);
final DialOptions<MediaStream> dialOpts = new DialOptions<>();
dialOpts.webrtcOptions = new DialWebRTCOptions<>();
dialOpts.webrtcOptions.signalingServerAddress = "10.0.2.2:8080";

fullscreenRenderer = findViewById(R.id.fullscreen_video_view);
fullscreenRenderer.init(
dialer.getEGLBase().getEglBaseContext(),
null,
EglBase.CONFIG_PLAIN,
new GlRectDrawer());
// use normal creds?
dialOpts.webrtcOptions.signalingInsecure = true;
dialOpts.webrtcOptions.signalingCredentials = Auth.Credentials.newBuilder()
.setType("api-key")
.setPayload("sosecret")
.build();

final DialOptions dialOpts = new DialOptions();
dialOpts.webrtcOptions = new DialWebRTCOptions();
dialOpts.webrtcOptions.signalingInsecure = true;
dialOpts.webrtcOptions.signalingCredentials = Auth.Credentials.newBuilder()
.setType("api-key")
.setPayload("sosecret")
.build();
final Logger logger = Logger.getAnonymousLogger();
dialOpts.webrtcOptions.mediaStreamObserver = new PeerConnection.MediaStreamObserver<>() {
@Override
public void onAddStream(final MediaStream mediaStream) {
final VideoTrack vt = mediaStream.videoTracks.get(0);
vt.setEnabled(true);
vt.addSink(MainActivity.fullscreenRenderer);
}

final Logger logger = Logger.getAnonymousLogger();
dialer.dialWebRTC(
"10.0.2.2:8080",
"something-unique",
dialOpts,
new PeerConnection.MediaStreamObserver<MediaStream>() {
@Override
public void onAddStream(final MediaStream mediaStream) {
final VideoTrack vt = mediaStream.videoTracks.get(0);
vt.setEnabled(true);
vt.addSink(MainActivity.fullscreenRenderer);
}
@Override
public void onRemoveStream(final MediaStream mediaStream) {
logger.info("MainActivity:onRemoveStream");
}
};

@Override
public void onRemoveStream(final MediaStream mediaStream) {
logger.info("MainActivity:onRemoveStream");
}
}).thenAcceptAsync((chan) -> {
final RobotServiceGrpc.RobotServiceBlockingStub robotClient = RobotServiceGrpc.newBlockingStub(chan);
final Robot.ResourceNamesResponse resp = robotClient.resourceNames(Robot.ResourceNamesRequest.newBuilder().build());
logger.info(resp.toString());
final Options<MediaStream> options = new Options.Builder<MediaStream>().setDialOptions(dialOpts)
.build();
robotClientFactory.dialWebRTC("something-unique", options)
.thenAcceptAsync((client) -> {
logger.info(client.getResourceNames().toString());

final StreamServiceGrpc.StreamServiceBlockingStub streamClient = StreamServiceGrpc.newBlockingStub(chan);
final Stream.AddStreamResponse streamResp = streamClient.addStream(Stream.AddStreamRequest.newBuilder().setName("camera1").build());
logger.info(streamResp.toString());
client.addStream("camera1");

final MovementSensorServiceGrpc.MovementSensorServiceBlockingStub msClient = MovementSensorServiceGrpc.newBlockingStub(chan);
final Movementsensor.GetPositionResponse msResp = msClient.getPosition(Movementsensor.GetPositionRequest.newBuilder().setName("gps1").build());
logger.info(msResp.getCoordinate().toString());
final MovementSensor sensor = MovementSensor.fromRobot(client, "gps1");
logger.info(sensor.getPosition(Optional.empty()).getCoordinate().toString());
}).exceptionally((t) -> {
t.printStackTrace();
logger.warning("failed to dial: " + t);
return null;
t.printStackTrace();
logger.warning("failed to dial: " + t);
return null;
});
}
}
}
Loading