Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Android service demo #868

Merged
merged 1 commit into from
Oct 22, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018-2023 Picovoice Inc.
Copyright 2018-2024 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 @@ -18,6 +18,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
Expand All @@ -28,6 +29,8 @@
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private ServiceBroadcastReceiver receiver;
Expand All @@ -36,12 +39,23 @@ public class MainActivity extends AppCompatActivity {
private TextView errorTextView;

private boolean hasRecordPermission() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED;
return ActivityCompat.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}

private boolean hasNotificationPermission() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ||
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED;
}

private void requestRecordPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 0);
private void requestRecordPermissions(String[] permissions) {
ActivityCompat.requestPermissions(
this,
permissions,
0);
}

@Override
Expand All @@ -53,7 +67,7 @@ public void onRequestPermissionsResult(
recordButton = findViewById(R.id.startButton);

if (grantResults.length == 0 || grantResults[0] == PackageManager.PERMISSION_DENIED) {
recordButton.toggle();
onPicovoiceInitError("Microphone/notification permissions are required for this demo");
} else {
startService();
}
Expand Down Expand Up @@ -83,10 +97,18 @@ protected void onCreate(Bundle savedInstanceState) {

recordButton.setOnClickListener(v -> {
if (recordButton.isChecked()) {
if (hasRecordPermission()) {
ArrayList<String> permissionsToRequest = new ArrayList<>();
if (!hasNotificationPermission()) {
permissionsToRequest.add(Manifest.permission.POST_NOTIFICATIONS);
}
if (!hasRecordPermission()) {
permissionsToRequest.add(Manifest.permission.RECORD_AUDIO);
}

if (permissionsToRequest.size() == 0) {
startService();
} else {
requestRecordPermission();
requestRecordPermissions(permissionsToRequest.toArray(new String[0]));
}
} else {
stopService();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector
android:height="108dp"
android:width="108dp"
android:viewportHeight="108"
android:viewportWidth="108"
xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#00000000"
android:pathData="M0,0h108v108h-108z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="85.84757"
android:endY="92.4963"
android:startX="42.9492"
android:startY="49.59793"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<group android:scaleX="0.14410481"
android:scaleY="0.14410481"
android:translateX="21"
android:translateY="21">
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
android:pathData="M229.001,229.001m-225,0a225,225 0,1 1,450 0a225,225 0,1 1,-450 0"
android:strokeWidth="4.73"
android:fillColor="#377DFF"
android:strokeColor="#377DFF"/>
<path
android:pathData="M256.589,371.942C256.091,373.327 255.71,374.759 255.094,376.094C254.142,378.156 250.875,384.312 250.875,384.312C245.83,391.304 237.706,395.829 228.611,390.694C218.037,384.723 213.436,371.931 208.404,360.05C191.091,319.173 179.784,276.17 160.169,236.032C153.837,223.073 147.369,208.605 135.835,201.484C127.794,196.521 117.418,206.788 110.95,215.688C100.884,229.537 93.804,245.604 88.71,261.771C88.71,261.771 87.123,264.966 87.123,264.966C85.434,267.134 83.152,268.813 80.528,269.466C78.303,270.019 78.21,269.822 77.453,269.801C70.478,269.621 64.864,262.324 66.767,254.966C66.784,254.902 66.735,255.071 66.69,255.239C77.509,212.127 85.65,166.996 106.52,127.111C112.526,115.632 119.886,103.567 133.849,103.245C134.322,103.245 134.32,103.245 134.793,103.253C148.568,103.78 159.166,116.105 166.082,126.324C192.473,165.314 231.086,267.59 234.642,272.645L300.86,88.115L358.853,88.115L266.944,343.202C266.944,343.202 258.565,366.46 256.589,371.942Z"
android:fillColor="#ffffff"/>
</group>
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
<string name="context_label">Context:</string>
<string name="context_name">Smart Lighting</string>
<string name="phrase_label">Example Phrase:</string>
<string name="phrase_text">\"Hey Porcupine, make the living room lights purple.\"</string>
<string name="button_text">Show Context</string>
<string name="phrase_text">\"Porcupine, turn off the living room lights.\"</string>
</resources>
Loading