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

CATROID-1237 changes SensorLoudness.java into SensorLoudness.kt #5036

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -34,13 +34,17 @@ import org.catrobat.catroid.camera.VisualDetectionHandler.updateFaceDetectionSta
import org.catrobat.catroid.camera.VisualDetectionHandler.updateFaceSensorValues
import org.catrobat.catroid.camera.VisualDetectionHandlerFace
import org.catrobat.catroid.content.Project
import org.catrobat.catroid.formulaeditor.SensorCustomEvent
import org.catrobat.catroid.formulaeditor.SensorCustomEventListener
import org.catrobat.catroid.formulaeditor.SensorHandler
import org.catrobat.catroid.formulaeditor.SensorLoudness
import org.catrobat.catroid.formulaeditor.Sensors
import org.catrobat.catroid.soundrecorder.SoundRecorder
import org.catrobat.catroid.test.utils.TestUtils
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -117,9 +121,8 @@ class SensorHandlerTest {
@Test
@UiThreadTest
fun testMicRelease() {
val loudnessSensor = SensorLoudness()
val soundRecorder = Mockito.mock(SoundRecorder::class.java)
loudnessSensor.soundRecorder = soundRecorder
val loudnessSensor = SensorLoudness(soundRecorder)

Mockito.`when`(soundRecorder.isRecording).thenReturn(false)
SensorHandler.getInstance(ApplicationProvider.getApplicationContext()).setSensorLoudness(loudnessSensor)
Expand All @@ -132,6 +135,25 @@ class SensorHandlerTest {
Mockito.verify(soundRecorder).stop()
}

@Test
@UiThreadTest
fun testSensorLoudnessStatusChecker() {
val soundRecorder = Mockito.mock(SoundRecorder::class.java)
val loudnessSensor = SensorLoudness(soundRecorder)

Mockito.`when`(soundRecorder.maxAmplitude).thenReturn(10)
Mockito.`when`(soundRecorder.isRecording).thenReturn(true)
val listener = SensorCustomEventListener {event: SensorCustomEvent? ->
event?.let {
assertTrue(event.sensor == Sensors.LOUDNESS)
assertEquals((10 / 32767).toFloat(), event.value as Float, 0.05f)
} ?: fail()
}
loudnessSensor.registerListener(listener)
loudnessSensor.unregisterListener(listener)
Mockito.verify(soundRecorder).stop()
}

@After
fun tearDown() {
SensorHandler.destroy()
Expand All @@ -144,4 +166,4 @@ class SensorHandlerTest {
companion object {
private const val DELTA = 0.01
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2023 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.formulaeditor

import android.os.Handler
import android.util.Log
import androidx.annotation.VisibleForTesting
import org.catrobat.catroid.soundrecorder.SoundRecorder
import java.io.IOException

class SensorLoudness(soundRecorderVar: SoundRecorder) {
private val listenerList: MutableList<SensorCustomEventListener> = ArrayList()
private val handler = Handler()
private var lastValue = 0.0
var soundRecorder = soundRecorderVar

var statusChecker: Runnable = object : Runnable {
override fun run() {
val loudness = ((SCALE_RANGE / MAX_AMP_VALUE) * soundRecorder.maxAmplitude)
if (!loudness.equals(lastValue) && !loudness.equals(0.0)) {
lastValue = loudness
val event = SensorCustomEvent(Sensors.LOUDNESS, loudness)

for (listener in listenerList) {
listener.onCustomSensorChanged(event)
}
}
handler.postDelayed(this, UPDATE_INTERVAL.toLong())
}
}

@Synchronized
fun registerListener(listener: SensorCustomEventListener) {
listenerList.add(listener)
if (!soundRecorder.isRecording) {
try {
soundRecorder.start()
statusChecker.run()
} catch (ioException: IOException) {
Log.d(TAG, "Could not start recorder", ioException)
listenerList.remove(listener)
soundRecorder = SoundRecorder("/dev/null")
} catch (runtimeException: RuntimeException) {
Log.d(TAG, "Could not start recorder", runtimeException)
listenerList.remove(listener)
soundRecorder = SoundRecorder("/dev/null")
}
}
}

@Synchronized
fun unregisterListener(listener: SensorCustomEventListener) {
if (listenerList.contains(listener)) {
listenerList.remove(listener)
if (listenerList.size == 0) {
handler.removeCallbacks(statusChecker)
if (soundRecorder.isRecording) {
try {
soundRecorder.stop()
} catch (ioException: IOException) {
// ignored, nothing we can do
Log.d(TAG, "Could not stop recorder", ioException)
}
soundRecorder = SoundRecorder("/dev/null")
}
lastValue = 0.0
}
}
}

companion object {
private const val UPDATE_INTERVAL = 50
private const val SCALE_RANGE = 100.0
private const val MAX_AMP_VALUE = 32767.0
private val TAG: String = SensorLoudness::class.java.simpleName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.catrobat.catroid.formulaeditor.SensorHandler;
import org.catrobat.catroid.formulaeditor.SensorLoudness;
import org.catrobat.catroid.sensing.GatherCollisionInformationTask;
import org.catrobat.catroid.soundrecorder.SoundRecorder;
import org.catrobat.catroid.ui.runtimepermissions.BrickResourcesToRuntimePermissions;
import org.catrobat.catroid.ui.settingsfragments.SettingsFragment;
import org.catrobat.catroid.utils.MobileServiceAvailability;
Expand Down Expand Up @@ -130,7 +131,7 @@ public void initResources() {
}

if (requiredResourcesSet.contains(Brick.MICROPHONE)) {
sensorHandler.setSensorLoudness(new SensorLoudness());
sensorHandler.setSensorLoudness(new SensorLoudness(new SoundRecorder("/dev/null")));
resourceInitialized();
}

Expand Down Expand Up @@ -289,18 +290,18 @@ public void onClick(DialogInterface dialog, int id) {
if (requiredResourcesSet.contains(Brick.NETWORK_CONNECTION)) {
if (!Utils.isNetworkAvailable(stageActivity)) {
new AlertDialog.Builder(new ContextThemeWrapper(stageActivity, R.style.Theme_AppCompat_Dialog))
.setTitle(R.string.error_no_network_title)
.setPositiveButton(R.string.preference_title, (dialog, whichButton) -> {
stageActivity.startActivity(new Intent(Settings.ACTION_SETTINGS));
})
.setNegativeButton(R.string.cancel, (dialog, whichButton) -> {
endStageActivity();
})
.setOnDismissListener(dialog -> {
endStageActivity();
})
.create()
.show();
.setTitle(R.string.error_no_network_title)
.setPositiveButton(R.string.preference_title, (dialog, whichButton) -> {
stageActivity.startActivity(new Intent(Settings.ACTION_SETTINGS));
})
.setNegativeButton(R.string.cancel, (dialog, whichButton) -> {
endStageActivity();
})
.setOnDismissListener(dialog -> {
endStageActivity();
})
.create()
.show();
} else {
resourceInitialized();
}
Expand Down Expand Up @@ -549,4 +550,4 @@ private void nfcInitialize() {
public void onFinished() {
resourceInitialized();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* Copyright (C) 2010-2024 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -43,6 +43,7 @@
import org.catrobat.catroid.formulaeditor.FormulaElement.ElementType;
import org.catrobat.catroid.formulaeditor.SensorHandler;
import org.catrobat.catroid.formulaeditor.SensorLoudness;
import org.catrobat.catroid.soundrecorder.SoundRecorder;
import org.catrobat.catroid.utils.ShowTextUtils.AndroidStringProvider;

import androidx.appcompat.app.AlertDialog;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void setFormula(Formula formula) {
formula.addRequiredResources(resourcesSet);

if (resourcesSet.contains(Brick.MICROPHONE)) {
SensorHandler.getInstance(getContext()).setSensorLoudness(new SensorLoudness());
SensorHandler.getInstance(getContext()).setSensorLoudness(new SensorLoudness(new SoundRecorder("/dev/null")));
}

if (resourcesSet.contains(Brick.BLUETOOTH_LEGO_NXT)) {
Expand Down