PorcupineService.java
Go to the documentation of this file.
1 /*
2  Copyright 2021 Picovoice Inc.
3 
4  You may not use this file except in compliance with the license. A copy of the license is
5  located in the "LICENSE" file accompanying this source.
6 
7  Unless required by applicable law or agreed to in writing, software distributed under the
8  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
9  express or implied. See the License for the specific language governing permissions and
10  limitations under the License.
11 */
12 
13 package ai.picovoice.porcupinedemoservice;
14 
15 import android.app.Notification;
16 import android.app.NotificationChannel;
17 import android.app.NotificationManager;
18 import android.app.PendingIntent;
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Build;
23 import android.os.IBinder;
24 import android.util.Log;
25 
26 import androidx.annotation.Nullable;
27 import androidx.core.app.NotificationCompat;
28 
38 
39 public class PorcupineService extends Service {
40  private static final String CHANNEL_ID = "PorcupineServiceChannel";
41  private static final String ACCESS_KEY = "${YOUR_ACCESS_KEY_HERE}";
42 
44  private int numUtterances;
45  private final PorcupineManagerCallback porcupineManagerCallback = (keywordIndex) -> {
46  numUtterances++;
47 
48  final String contentText = numUtterances == 1 ? " time!" : " times!";
49  Notification n = getNotification(
50  "Wake word",
51  "Detected " + numUtterances + contentText);
52 
53  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
54  assert notificationManager != null;
55  notificationManager.notify(1234, n);
56  };
57 
58  private void createNotificationChannel() {
59  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
60  NotificationChannel notificationChannel = new NotificationChannel(
61  CHANNEL_ID,
62  "Porcupine",
63  NotificationManager.IMPORTANCE_HIGH);
64 
65  NotificationManager manager = getSystemService(NotificationManager.class);
66  manager.createNotificationChannel(notificationChannel);
67  }
68  }
69 
70  @Override
71  public int onStartCommand(Intent intent, int flags, int startId) {
72 
73  numUtterances = 0;
75 
76  try {
79  .setKeyword(Porcupine.BuiltInKeyword.COMPUTER)
80  .setSensitivity(0.7f).build(
81  getApplicationContext(),
84 
87  String.format("%s\nEnsure your accessKey '%s' is a valid access key.", e.getMessage(), ACCESS_KEY)
88  );
89  } catch (PorcupineActivationException e) {
90  onPorcupineInitError("AccessKey activation error");
92  onPorcupineInitError("AccessKey reached its device limit");
94  onPorcupineInitError("AccessKey refused");
96  onPorcupineInitError("AccessKey has been throttled");
97  } catch (PorcupineException e) {
98  onPorcupineInitError("Failed to initialize Porcupine " + e.getMessage());
99  }
100 
101  Notification notification = porcupineManager == null ?
102  getNotification("Porcupine init failed", "Service will be shut down") :
103  getNotification("Wake word", "Service running");
104  startForeground(1234, notification);
105 
106  return super.onStartCommand(intent, flags, startId);
107  }
108 
109  private void onPorcupineInitError(String message) {
110  Intent i = new Intent("PorcupineInitError");
111  i.putExtra("errorMessage", message);
112  sendBroadcast(i);
113  }
114 
115  private Notification getNotification(String title, String message) {
116  PendingIntent pendingIntent = PendingIntent.getActivity(
117  this,
118  0,
119  new Intent(this, MainActivity.class),
120  0);
121 
122  return new NotificationCompat.Builder(this, CHANNEL_ID)
123  .setContentTitle(title)
124  .setContentText(message)
125  .setSmallIcon(R.drawable.ic_launcher_foreground)
126  .setContentIntent(pendingIntent)
127  .build();
128  }
129 
130  @Nullable
131  @Override
132  public IBinder onBind(Intent intent) {
133  return null;
134  }
135 
136  @Override
137  public void onDestroy() {
138  if (porcupineManager != null) {
139  try {
142  } catch (PorcupineException e) {
143  Log.e("PORCUPINE", e.toString());
144  }
145  }
146 
147  super.onDestroy();
148  }
149 }
R
#define R
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:5104
ai.picovoice.porcupine
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineActivationException.java:11
ai.picovoice.porcupinedemoservice.PorcupineService.onBind
IBinder onBind(Intent intent)
Definition: PorcupineService.java:132
ai.picovoice.porcupine.PorcupineManager.start
void start()
Definition: PorcupineManager.java:70
ai.picovoice.porcupine.PorcupineManager.Builder.setAccessKey
PorcupineManager.Builder setAccessKey(String accessKey)
Definition: PorcupineManager.java:100
ai.picovoice.porcupine.PorcupineManager.Builder
Definition: PorcupineManager.java:91
ai.picovoice.porcupinedemoservice.PorcupineService.CHANNEL_ID
static final String CHANNEL_ID
Definition: PorcupineService.java:40
ai.picovoice.porcupine.PorcupineManager.delete
void delete()
Definition: PorcupineManager.java:62
ai.picovoice.porcupinedemoservice.PorcupineService.onDestroy
void onDestroy()
Definition: PorcupineService.java:137
ai.picovoice.porcupine.Porcupine.BuiltInKeyword.COMPUTER
COMPUTER
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:150
f
f
ai.picovoice.porcupinedemoservice.PorcupineService.getNotification
Notification getNotification(String title, String message)
Definition: PorcupineService.java:115
ai.picovoice.porcupinedemoservice.MainActivity
Definition: porcupine/demo/android/Service/porcupine-service-demo-app/src/main/java/ai/picovoice/porcupinedemoservice/MainActivity.java:31
ai.picovoice.porcupine.PorcupineActivationRefusedException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineActivationRefusedException.java:13
ai.picovoice.porcupine.PorcupineException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineException.java:13
ai.picovoice.porcupinedemoservice.PorcupineService.porcupineManager
PorcupineManager porcupineManager
Definition: PorcupineService.java:43
ai.picovoice.porcupine.PorcupineActivationException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineActivationException.java:13
ai.picovoice.porcupinedemoservice.PorcupineService.numUtterances
int numUtterances
Definition: PorcupineService.java:44
ai.picovoice.porcupine.PorcupineActivationLimitException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineActivationLimitException.java:13
ai
ai.picovoice
ai.picovoice.porcupinedemoservice.PorcupineService
Definition: PorcupineService.java:39
ai.picovoice.porcupinedemoservice.PorcupineService.onPorcupineInitError
void onPorcupineInitError(String message)
Definition: PorcupineService.java:109
ai.picovoice.porcupine.PorcupineManagerCallback
Definition: PorcupineManagerCallback.java:15
ai.picovoice.porcupinedemoservice.PorcupineService.porcupineManagerCallback
final PorcupineManagerCallback porcupineManagerCallback
Definition: PorcupineService.java:45
ai.picovoice.porcupinedemoservice.PorcupineService.ACCESS_KEY
static final String ACCESS_KEY
Definition: PorcupineService.java:41
ai.picovoice.porcupinedemoservice.PorcupineService.createNotificationChannel
void createNotificationChannel()
Definition: PorcupineService.java:58
ai.picovoice.porcupine.Porcupine
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:35
ai.picovoice.porcupine.PorcupineActivationThrottledException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineActivationThrottledException.java:13
ai.picovoice.porcupine.PorcupineManager
Definition: PorcupineManager.java:35
ai.picovoice.porcupinedemoservice.PorcupineService.onStartCommand
int onStartCommand(Intent intent, int flags, int startId)
Definition: PorcupineService.java:71
ai.picovoice.porcupine.Porcupine.BuiltInKeyword
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/Porcupine.java:145
ai.picovoice.porcupine.PorcupineInvalidArgumentException
Definition: android/Porcupine/porcupine/src/main/java/ai/picovoice/porcupine/exception/PorcupineInvalidArgumentException.java:13
ai.picovoice.porcupine.PorcupineManager.stop
void stop()
Definition: PorcupineManager.java:80


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50