mirror of
https://github.com/SimpleMobileTools/Simple-Dialer.git
synced 2025-06-05 21:49:23 +02:00
catch exceptions at initiating dialpad tone generator
This commit is contained in:
@@ -9,7 +9,12 @@ import android.os.Looper
|
|||||||
|
|
||||||
class ToneGeneratorHelper(context: Context, private val minToneLengthMs: Long) {
|
class ToneGeneratorHelper(context: Context, private val minToneLengthMs: Long) {
|
||||||
private val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
private val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||||
private val toneGenerator = ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME)
|
private val toneGenerator: ToneGenerator? = try {
|
||||||
|
ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME)
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
private var toneStartTimeMs = 0L
|
private var toneStartTimeMs = 0L
|
||||||
|
|
||||||
private fun isSilent(): Boolean {
|
private fun isSilent(): Boolean {
|
||||||
@@ -23,7 +28,7 @@ class ToneGeneratorHelper(context: Context, private val minToneLengthMs: Long) {
|
|||||||
|
|
||||||
private fun startTone(tone: Int) {
|
private fun startTone(tone: Int) {
|
||||||
if (tone != -1 && !isSilent()) {
|
if (tone != -1 && !isSilent()) {
|
||||||
toneGenerator.startTone(tone)
|
toneGenerator?.startTone(tone)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,10 +36,10 @@ class ToneGeneratorHelper(context: Context, private val minToneLengthMs: Long) {
|
|||||||
val timeSinceStartMs = System.currentTimeMillis() - toneStartTimeMs
|
val timeSinceStartMs = System.currentTimeMillis() - toneStartTimeMs
|
||||||
if (timeSinceStartMs < minToneLengthMs) {
|
if (timeSinceStartMs < minToneLengthMs) {
|
||||||
Handler(Looper.getMainLooper()).postDelayed({
|
Handler(Looper.getMainLooper()).postDelayed({
|
||||||
toneGenerator.stopTone()
|
toneGenerator?.stopTone()
|
||||||
}, minToneLengthMs - timeSinceStartMs)
|
}, minToneLengthMs - timeSinceStartMs)
|
||||||
} else {
|
} else {
|
||||||
toneGenerator.stopTone()
|
toneGenerator?.stopTone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user