Skip to content

Commit

Permalink
Merge pull request #98 from haraldrudell/nocrash
Browse files Browse the repository at this point in the history
Consolidated and Android 12–13–14+ compatible version 0.9.33-230715
  • Loading branch information
Dimowner committed Jan 14, 2024
2 parents ef641ec + bfb6023 commit 29d4024
Show file tree
Hide file tree
Showing 23 changed files with 342 additions and 264 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
//apply plugin: 'com.google.firebase.crashlytics'
//apply plugin: 'com.google.gms.google-services'
Expand All @@ -12,8 +12,8 @@ android {
applicationId "com.dimowner.audiorecorder"
minSdkVersion 21
targetSdkVersion 33
versionCode 933
versionName "0.9.33"
versionCode 934
versionName "0.9.33-230715"
}

buildFeatures {
Expand Down Expand Up @@ -92,7 +92,7 @@ android.variantFilter { variant ->

dependencies {
def androidX = "1.3.0"
def coroutines = "1.6.1"
def coroutines = "1.6.4"
def timber = "5.0.1"

//Kotlin
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Expand Down
142 changes: 0 additions & 142 deletions app/src/main/java/com/dimowner/audiorecorder/ARApplication.java

This file was deleted.

176 changes: 176 additions & 0 deletions app/src/main/java/com/dimowner/audiorecorder/ARApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright 2018 Dmytro Ponomarenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("DEPRECATION")

package com.dimowner.audiorecorder

import android.Manifest
import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.Handler
import android.telephony.PhoneStateListener
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.dimowner.audiorecorder.util.AndroidUtils
import timber.log.Timber
import timber.log.Timber.DebugTree


//import com.google.firebase.FirebaseApp;
class ARApplication : Application() {
private var audioOutputChangeReceiver: AudioOutputChangeReceiver? = null
override fun onCreate() {
if (BuildConfig.DEBUG) {
//Timber initialization
Timber.plant(DebugTree())
}
super.onCreate()
PACKAGE_NAME = applicationContext.packageName
applicationHandler = Handler(applicationContext.mainLooper)
screenWidthDp = AndroidUtils.pxToDp(
AndroidUtils.getScreenWidth(
applicationContext
)
)
val prefs = injector.providePrefs(applicationContext)
if (!prefs.isMigratedSettings) {
prefs.migrateSettings()
}
val intentFilter = IntentFilter()
intentFilter.addAction(AUDIO_BECOMING_NOISY)
audioOutputChangeReceiver = AudioOutputChangeReceiver()
registerReceiver(audioOutputChangeReceiver, intentFilter)

// feature: pause when phone functions ringing or off-hook
try {
val telephonyMgr = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.S) {
telephonyPreAndroid12(telephonyMgr)
} else {
telephonyAndroid12Plus(telephonyMgr)
}
} catch (e: Exception) {
Timber.e(e)
}
// FirebaseApp.initializeApp(this);
}

override fun onTerminate() {
super.onTerminate()
//This method is never called on real Android devices
injector.releaseMainPresenter()
injector.closeTasks()
unregisterReceiver(audioOutputChangeReceiver)
}

fun pausePlayback() {
//Pause playback when incoming call or on hold
val player = injector.provideAudioPlayer()
player.pause()
}

@RequiresApi(api = Build.VERSION_CODES.S)
fun telephonyAndroid12Plus(telephonyMgr: TelephonyManager) {
if (ContextCompat.checkSelfPermission(applicationContext,
Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
telephonyMgr.registerTelephonyCallback(mainExecutor, callStateListener!!)
}
}

@RequiresApi(api = Build.VERSION_CODES.S)
private abstract class CallStateListener : TelephonyCallback(),
TelephonyCallback.CallStateListener {
abstract override fun onCallStateChanged(state: Int)
}

private val callStateListener: CallStateListener? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) object : CallStateListener() {
override fun onCallStateChanged(state: Int) {
if (state == TelephonyManager.CALL_STATE_OFFHOOK ||
state == TelephonyManager.CALL_STATE_RINGING) {
pausePlayback()
}
}
} else null

private class AudioOutputChangeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val actionOfIntent = intent.action
if (actionOfIntent != null && actionOfIntent == AUDIO_BECOMING_NOISY) {
val player = injector.provideAudioPlayer()
player.pause()
}
}
}

@Suppress("DEPRECATION")
fun telephonyPreAndroid12(telephonyMgr: TelephonyManager) {
telephonyMgr.listen(mPhoneStateListenerPreAndroid12, PhoneStateListener.LISTEN_CALL_STATE)
}

private val mPhoneStateListenerPreAndroid12: PhoneStateListener = object : PhoneStateListener() {
@Deprecated("Deprecated in Java")
override fun onCallStateChanged(state: Int, incomingNumber: String) {
if (state == TelephonyManager.CALL_STATE_RINGING || state == TelephonyManager.CALL_STATE_OFFHOOK) {
pausePlayback()
}
super.onCallStateChanged(state, incomingNumber)
}
}

companion object {
const val AUDIO_BECOMING_NOISY = "android.media.AUDIO_BECOMING_NOISY"
private var PACKAGE_NAME: String? = null

@JvmField
@Volatile
var applicationHandler: Handler? = null

/** Screen width in dp */
private var screenWidthDp = 0f
@JvmStatic
var injector = Injector()
@JvmStatic
fun appPackage(): String? {
return PACKAGE_NAME
}

/**
* Calculate density pixels per second for record duration.
* Used for visualisation waveform in view.
* @param durationSec record duration in seconds
*/
@JvmStatic
fun getDpPerSecond(durationSec: Float): Float {
return if (durationSec > AppConstants.LONG_RECORD_THRESHOLD_SECONDS) {
AppConstants.WAVEFORM_WIDTH * screenWidthDp / durationSec
} else {
AppConstants.SHORT_RECORD_DP_PER_SECOND.toFloat()
}
}

@JvmStatic
val longWaveformSampleCount: Int
get() = (AppConstants.WAVEFORM_WIDTH * screenWidthDp).toInt()
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/dimowner/audiorecorder/AppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@

package com.dimowner.audiorecorder;

import android.app.PendingIntent;
import android.os.Build;

/**
* AppConstants that may be used in multiple classes.
*/
public class AppConstants {

private AppConstants() {}
static {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PENDING_INTENT_FLAGS = PendingIntent.FLAG_IMMUTABLE;
} else {
PENDING_INTENT_FLAGS = 0;
}
}

public static final int PENDING_INTENT_FLAGS;

public static final String REQUESTS_RECEIVER = "dmitriy.ponomarenko.ua@gmail.com";

Expand Down
Loading

0 comments on commit 29d4024

Please sign in to comment.