From a55ae3a3f09d5caeac6dbe8dc7c292c0e1acdca6 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 1 Aug 2019 19:42:24 -0700 Subject: [PATCH] Implement SoundManager TurboModule in JS and Android Summary: This diff implements the Turbo Module SoundManager, this will be used by following diffs of the stack Reviewed By: JoshuaGross Differential Revision: D16543430 fbshipit-source-id: 34ba545f54b759fe4e49d4e3c5f8867205de907c --- Libraries/Components/Sound/SoundManager.js | 20 ++++++++++ .../com/facebook/react/modules/sound/BUCK | 21 ++++++++++ .../modules/sound/SoundManagerModule.java | 40 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 Libraries/Components/Sound/SoundManager.js create mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/sound/BUCK create mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.java diff --git a/Libraries/Components/Sound/SoundManager.js b/Libraries/Components/Sound/SoundManager.js new file mode 100644 index 00000000000..71847bd7883 --- /dev/null +++ b/Libraries/Components/Sound/SoundManager.js @@ -0,0 +1,20 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow + */ + +'use strict'; +import NativeSoundManager from './NativeSoundManager'; + +const SoundManager = { + playTouchSound: function(): void { + NativeSoundManager.playTouchSound(); + }, +}; + +module.exports = SoundManager; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/sound/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/sound/BUCK new file mode 100644 index 00000000000..a2b08ec0d4b --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/sound/BUCK @@ -0,0 +1,21 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library") + +rn_android_library( + name = "sound", + srcs = glob(["**/*.java"]), + is_androidx = True, + provided_deps = [ + ], + visibility = [ + "PUBLIC", + ], + deps = [ + react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"), + react_native_dep("third-party/java/infer-annotations:infer-annotations"), + react_native_dep("third-party/java/jsr-305:jsr-305"), + react_native_target("java/com/facebook/react/bridge:bridge"), + react_native_target("java/com/facebook/react/common:common"), + react_native_target("java/com/facebook/react/module/annotations:annotations"), + react_native_target("java/com/facebook/react/uimanager:uimanager"), + ], +) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.java new file mode 100644 index 00000000000..5fbc3598b81 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/sound/SoundManagerModule.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + *

This source code is licensed under the MIT license found in the LICENSE file in the root + * directory of this source tree. + */ +package com.facebook.react.modules.sound; + +import android.content.Context; +import android.media.AudioManager; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.module.annotations.ReactModule; + +/** {@link NativeModule} that allows Playing device sounds from JS. */ +@ReactModule(name = SoundManagerModule.NAME) +public class SoundManagerModule extends ReactContextBaseJavaModule { + + public static final String NAME = "SoundManager"; + + public SoundManagerModule(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return NAME; + } + + @ReactMethod + public void playTouchSound() { + AudioManager audioManager = + (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE); + if (audioManager != null) { + audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK); + } + } +}