Skip to content

Commit

Permalink
start implementing media_player functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffc committed Oct 23, 2024
1 parent 9b43d12 commit 3973097
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 167 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.14.0
hooks:
- id: pretty-format-java
args: [--autofix]
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
package com.thejeffcooper.hassmic;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.util.Map;
import java.util.HashMap;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;


public class AutostartModule extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;

public class BackgroundEventService extends HeadlessJsTaskService {

@Nullable
@Override
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
@Nullable
@Override
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();

// Configure headless JS task
return new HeadlessJsTaskConfig(
"HassmicBackgroundTask",
extras != null ? Arguments.fromBundle(extras) : Arguments.createMap(),
0, // timeout for the task
true // allow task in foreground
// Configure headless JS task
return new HeadlessJsTaskConfig(
"HassmicBackgroundTask",
extras != null ? Arguments.fromBundle(extras) : Arguments.createMap(),
0, // timeout for the task
true // allow task in foreground
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,47 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.HashMap;
import java.util.Map;

public class BackgroundTaskModule extends ReactContextBaseJavaModule {

private static ReactApplicationContext reactContext;

public static final String KEY_FIRE_JS_EVENT = "HassMicFireJSEvent";
public static final String KEY_JS_EVENT_NAME = "HassMicJSEventName";


BackgroundTaskModule(ReactApplicationContext context) {
super(context);
reactContext = context;

BroadcastReceiver jsEventRec = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String eventName = intent.getStringExtra(KEY_JS_EVENT_NAME);
if (eventName == null || eventName.equals("")) {
Log.e("HassmicBackgroundTaskModule", "Was asked to send a JS event, but didn't get an event name");
return;
}
Log.d("HassmicBackgroundTaskModule", "Sending event JS event " + eventName);
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, null);
}
};

ContextCompat.registerReceiver(reactContext, jsEventRec, new IntentFilter(KEY_FIRE_JS_EVENT), ContextCompat.RECEIVER_NOT_EXPORTED);
BroadcastReceiver jsEventRec =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String eventName = intent.getStringExtra(KEY_JS_EVENT_NAME);
if (eventName == null || eventName.equals("")) {
Log.e(
"HassmicBackgroundTaskModule",
"Was asked to send a JS event, but didn't get an event name");
return;
}
Log.d("HassmicBackgroundTaskModule", "Sending event JS event " + eventName);
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, null);
}
};

ContextCompat.registerReceiver(
reactContext,
jsEventRec,
new IntentFilter(KEY_FIRE_JS_EVENT),
ContextCompat.RECEIVER_NOT_EXPORTED);
}

@Override
Expand All @@ -73,10 +72,12 @@ public void stopService() {
}

@ReactMethod
public void playSpeech(String url) {
public void playAudio(String url, boolean announce) {
Log.d("HassmicBackgroundTaskModule", "Sending play media intent: " + url);
Intent playIntent = new Intent(BackgroundTaskService.PLAY_SPEECH_ACTION)
.putExtra(BackgroundTaskService.URL_KEY, url);
Intent playIntent =
new Intent(BackgroundTaskService.PLAY_AUDIO_ACTION)
.putExtra(BackgroundTaskService.URL_KEY, url)
.putExtra(BackgroundTaskService.ANNOUNCE_KEY, announce);
Log.d("HassmicBackgroundTaskModule", playIntent.toString());
this.reactContext.sendBroadcast(playIntent);
}
Expand Down
Loading

0 comments on commit 3973097

Please sign in to comment.