Skip to content

Commit 16097f2

Browse files
committed
IDE-295 Refactor SoundRecorder files to Kotlin
# Conflicts: # catroid/build.gradle
1 parent e5f734e commit 16097f2

File tree

8 files changed

+545
-305
lines changed

8 files changed

+545
-305
lines changed

catroid/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ext {
5454
projectVersion = "0.9"
5555
gdxVersion = "1.13.0"
5656
mockitoVersion = '5.14.2'
57+
mockkVersion = "1.12.5"
5758
espressoVersion = '3.6.1'
5859
playServicesVersion = '18.1.1'
5960
cameraXVersion = '1.4.0'
@@ -478,6 +479,8 @@ dependencies {
478479

479480
testImplementation "org.mockito:mockito-core:$mockitoVersion"
480481

482+
testImplementation "io.mockk:mockk:${mockkVersion}"
483+
481484
testImplementation 'org.hamcrest:hamcrest-library:1.3'
482485

483486
testImplementation 'org.robolectric:robolectric:4.7.3'
@@ -494,6 +497,8 @@ dependencies {
494497
androidTestImplementation "org.mockito:mockito-android:$mockitoVersion"
495498
androidTestImplementation "org.mockito:mockito-core:$mockitoVersion"
496499

500+
androidTestImplementation "io.mockk:mockk-android:${mockkVersion}"
501+
497502
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
498503
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.5'
499504

catroid/src/androidTest/java/org/catrobat/catroid/test/formulaeditor/SensorHandlerTest.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import android.graphics.Rect
2828
import androidx.test.annotation.UiThreadTest
2929
import androidx.test.core.app.ApplicationProvider
3030
import androidx.test.rule.GrantPermissionRule
31+
import io.mockk.every
32+
import io.mockk.mockk
33+
import io.mockk.verify
3134
import org.catrobat.catroid.ProjectManager
3235
import org.catrobat.catroid.camera.VisualDetectionHandler.facesForSensors
3336
import org.catrobat.catroid.camera.VisualDetectionHandler.updateFaceDetectionStatusSensorValues
@@ -44,7 +47,6 @@ import org.junit.Assert.assertEquals
4447
import org.junit.Before
4548
import org.junit.Rule
4649
import org.junit.Test
47-
import org.mockito.Mockito
4850

4951
class SensorHandlerTest {
5052
@get:Rule
@@ -118,18 +120,19 @@ class SensorHandlerTest {
118120
@UiThreadTest
119121
fun testMicRelease() {
120122
val loudnessSensor = SensorLoudness()
121-
val soundRecorder = Mockito.mock(SoundRecorder::class.java)
123+
val soundRecorder = mockk<SoundRecorder>(relaxed = true)
122124
loudnessSensor.soundRecorder = soundRecorder
123125

124-
Mockito.`when`(soundRecorder.isRecording).thenReturn(false)
125-
SensorHandler.getInstance(ApplicationProvider.getApplicationContext()).setSensorLoudness(loudnessSensor)
126+
every { soundRecorder.isRecording } returns false
127+
SensorHandler.getInstance(ApplicationProvider.getApplicationContext())
128+
.setSensorLoudness(loudnessSensor)
126129

127130
SensorHandler.startSensorListener(ApplicationProvider.getApplicationContext())
128-
Mockito.`when`(soundRecorder.isRecording).thenReturn(true)
129-
Mockito.verify(soundRecorder).start()
131+
every { soundRecorder.isRecording } returns true
132+
verify { soundRecorder.start() }
130133

131134
SensorHandler.stopSensorListeners()
132-
Mockito.verify(soundRecorder).stop()
135+
verify { soundRecorder.stop() }
133136
}
134137

135138
@After

catroid/src/main/java/org/catrobat/catroid/soundrecorder/RecordButton.java renamed to catroid/src/main/java/org/catrobat/catroid/soundrecorder/RecordButton.kt

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,23 @@
2020
* You should have received a copy of the GNU Affero General Public License
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
23-
package org.catrobat.catroid.soundrecorder;
23+
package org.catrobat.catroid.soundrecorder
2424

25-
import android.annotation.SuppressLint;
26-
import android.content.Context;
27-
import android.util.AttributeSet;
28-
import android.widget.ImageButton;
25+
import android.annotation.SuppressLint
26+
import android.content.Context
27+
import android.util.AttributeSet
28+
import android.widget.ImageButton
2929

3030
@SuppressLint("AppCompatCustomView")
31-
public class RecordButton extends ImageButton {
32-
private RecordState state = RecordState.STOP;
31+
class RecordButton @JvmOverloads constructor(
32+
context: Context,
33+
attrs: AttributeSet? = null,
34+
defStyle: Int = 0
35+
) : ImageButton(context, attrs, defStyle) {
3336

34-
public RecordButton(Context context) {
35-
super(context);
36-
}
37+
var state: RecordState = RecordState.STOP
3738

38-
public RecordButton(Context context, AttributeSet attrs) {
39-
super(context, attrs);
40-
}
41-
42-
public RecordButton(Context context, AttributeSet attrs, int defStyle) {
43-
super(context, attrs, defStyle);
44-
}
45-
46-
public RecordState getState() {
47-
return state;
48-
}
49-
50-
public void setState(RecordState state) {
51-
this.state = state;
52-
}
53-
54-
public enum RecordState {
55-
RECORD, STOP;
56-
}
39+
enum class RecordState {
40+
RECORD, STOP
41+
}
5742
}

catroid/src/main/java/org/catrobat/catroid/soundrecorder/SoundRecorder.java

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Catroid: An on-device visual programming system for Android devices
3+
* Copyright (C) 2010-2025 The Catrobat Team
4+
* (<http://developer.catrobat.org/credits>)
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* An additional term exception under section 7 of the GNU Affero
12+
* General Public License, version 3, is available at
13+
* http://developer.catrobat.org/license_additional_term
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
package org.catrobat.catroid.soundrecorder
24+
25+
import android.media.MediaRecorder
26+
import android.util.Log
27+
import java.io.File
28+
import java.io.IOException
29+
30+
class SoundRecorder @JvmOverloads constructor(
31+
val path: String,
32+
private val recorder: MediaRecorder = MediaRecorder()
33+
) {
34+
var isRecording: Boolean = false
35+
private set
36+
37+
val maxAmplitude: Int
38+
get() = recorder.maxAmplitude
39+
40+
companion object {
41+
private val TAG: String = SoundRecorder::class.java.simpleName
42+
}
43+
44+
@Throws(IOException::class, RuntimeException::class)
45+
fun start() {
46+
val soundFile = File(path)
47+
if (soundFile.exists() && !soundFile.delete()) {
48+
throw IOException("Could not delete existing file at $path")
49+
}
50+
val directory = soundFile.parentFile
51+
if (directory == null || (!directory.exists() && !directory.mkdirs())) {
52+
throw IOException("Path to file could not be created.")
53+
}
54+
55+
try {
56+
recorder.reset()
57+
recorder.setAudioSource(MediaRecorder.AudioSource.MIC)
58+
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
59+
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
60+
recorder.setOutputFile(path)
61+
recorder.prepare()
62+
recorder.start()
63+
isRecording = true
64+
} catch (e: IllegalStateException) {
65+
throw e
66+
} catch (e: RuntimeException) {
67+
throw e
68+
}
69+
}
70+
71+
@Throws(IOException::class)
72+
fun stop() {
73+
try {
74+
recorder.stop()
75+
} catch (_: RuntimeException) {
76+
Log.d(
77+
TAG, ("Note that a RuntimeException is intentionally "
78+
+ "thrown to the application, if no valid audio/video data "
79+
+ "has been received when stop() is called. This happens if stop() "
80+
+ "is called immediately after start(). The failure lets the application "
81+
+ "take action accordingly to clean up the output file "
82+
+ "(delete the output file, for instance), since the output file "
83+
+ "is not properly constructed when this happens.")
84+
)
85+
}
86+
recorder.reset()
87+
recorder.release()
88+
isRecording = false
89+
}
90+
}

0 commit comments

Comments
 (0)