Skip to content

Commit

Permalink
v3.0 android (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
laves authored Oct 13, 2023
1 parent f4dc03b commit 07acef7
Show file tree
Hide file tree
Showing 28 changed files with 166 additions and 36 deletions.
2 changes: 1 addition & 1 deletion binding/android/Porcupine/porcupine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

ext {
PUBLISH_GROUP_ID = 'ai.picovoice'
PUBLISH_VERSION = '2.2.2'
PUBLISH_VERSION = '3.0.0'
PUBLISH_ARTIFACT_ID = 'porcupine-android'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021-2022 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Expand Down Expand Up @@ -43,13 +43,18 @@ public class Porcupine {

private static String DEFAULT_MODEL_PATH;
private static boolean isExtracted;
private static String _sdk = "android";

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

private long handle;

public static void setSdk(String sdk) {
Porcupine._sdk = sdk;
}

/**
* Constructor.
*
Expand All @@ -66,6 +71,8 @@ private Porcupine(
String modelPath,
String[] keywordPaths,
float[] sensitivities) throws PorcupineException {
PorcupineNative.setSdk(Porcupine._sdk);

handle = PorcupineNative.init(
accessKey,
modelPath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Picovoice Inc.
Copyright 2022-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Expand All @@ -20,6 +20,8 @@ class PorcupineNative {

static native int getSampleRate();

static native void setSdk(String sdk);

static native long init(
String accessKey,
String modelPath,
Expand All @@ -31,5 +33,4 @@ static native long init(
static native int process(
long object,
short[] pcm) throws PorcupineException;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineActivationException(Throwable cause) {
public PorcupineActivationException(String message) {
super(message);
}

public PorcupineActivationException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineActivationLimitException(Throwable cause) {
public PorcupineActivationLimitException(String message) {
super(message);
}

public PorcupineActivationLimitException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineActivationRefusedException(Throwable cause) {
public PorcupineActivationRefusedException(String message) {
super(message);
}

public PorcupineActivationRefusedException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineActivationThrottledException(Throwable cause) {
public PorcupineActivationThrottledException(String message) {
super(message);
}

public PorcupineActivationThrottledException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -10,12 +10,46 @@

package ai.picovoice.porcupine;

import android.annotation.SuppressLint;

public class PorcupineException extends Exception {
private final String message;
private final String[] messageStack;

public PorcupineException(Throwable cause) {
super(cause);
this.message = cause.getMessage();
this.messageStack = null;
}

public PorcupineException(String message) {
super(message);
this.message = message;
this.messageStack = null;
}

public PorcupineException(String message, String[] messageStack) {
super(message);
this.message = message;
this.messageStack = messageStack;
}

public String[] getMessageStack() {
return this.messageStack;
}

@SuppressLint("DefaultLocale")
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder(message);
if (messageStack != null) {
if (messageStack.length > 0) {
sb.append(":");
for (int i = 0; i < messageStack.length; i++) {
sb.append(String.format("\n [%d] %s", i, messageStack[i]));
}
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineIOException(Throwable cause) {
public PorcupineIOException(String message) {
super(message);
}

public PorcupineIOException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineInvalidArgumentException(Throwable cause) {
public PorcupineInvalidArgumentException(String message) {
super(message);
}

public PorcupineInvalidArgumentException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineInvalidStateException(Throwable cause) {
public PorcupineInvalidStateException(String message) {
super(message);
}

public PorcupineInvalidStateException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineKeyException(Throwable cause) {
public PorcupineKeyException(String message) {
super(message);
}

public PorcupineKeyException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineMemoryException(Throwable cause) {
public PorcupineMemoryException(String message) {
super(message);
}

public PorcupineMemoryException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineRuntimeException(Throwable cause) {
public PorcupineRuntimeException(String message) {
super(message);
}

public PorcupineRuntimeException(String message, String[] messageStack) {
super(message, messageStack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Picovoice Inc.
Copyright 2021-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
Expand All @@ -18,4 +18,8 @@ public PorcupineStopIterationException(Throwable cause) {
public PorcupineStopIterationException(String message) {
super(message);
}

public PorcupineStopIterationException(String message, String[] messageStack) {
super(message, messageStack);
}
}
6 changes: 6 additions & 0 deletions binding/android/PorcupineTestApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ buildscript {
repositories {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1266/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
Expand All @@ -21,6 +24,9 @@ allprojects {
repositories {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1266/'
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.code.gson:gson:2.10'
implementation 'ai.picovoice:porcupine-android:2.2.2'
implementation 'ai.picovoice:porcupine-android:3.0.0'

// Espresso UI Testing
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,33 @@ public void testInitWithNonAsciiModelName() throws PorcupineException {

p.delete();
}

@Test
public void testErrorStack() {
String[] error = {};
try {
new Porcupine.Builder()
.setAccessKey("invalid")
.setKeyword(Porcupine.BuiltInKeyword.PORCUPINE)
.build(appContext);
} catch (PorcupineException e) {
error = e.getMessageStack();
}

assertTrue(0 < error.length);
assertTrue(error.length <= 8);

try {
new Porcupine.Builder()
.setAccessKey("invalid")
.setKeyword(Porcupine.BuiltInKeyword.PORCUPINE)
.build(appContext);
} catch (PorcupineException e) {
for (int i = 0; i < error.length; i++) {
assertEquals(e.getMessageStack()[i], error[i]);
}
}
}
}

@RunWith(Parameterized.class)
Expand Down
6 changes: 6 additions & 0 deletions demo/android/Activity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ buildscript {
repositories {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1266/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
Expand All @@ -21,6 +24,9 @@ allprojects {
repositories {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1266/'
}
}
}

Expand Down
Loading

0 comments on commit 07acef7

Please sign in to comment.