Code cleanups

This commit is contained in:
Juha Heinanen
2022-01-13 16:25:20 +02:00
parent 9540fc6e3a
commit 60a667aa2a
2 changed files with 46 additions and 50 deletions
@@ -114,9 +114,8 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater) binding = ActivityMainBinding.inflate(layoutInflater)
val intentAction = intent.getStringExtra("action") val extraAction = intent.getStringExtra("action")
Log.d(TAG, "MainActivity onCreate ${intent.action}/${intent.data}/$extraAction")
Log.d(TAG, "MainActivity onCreate ${intent.action}/${intent.data}/$intentAction")
if (intent?.action == ACTION_CALL && !BaresipService.isServiceRunning) if (intent?.action == ACTION_CALL && !BaresipService.isServiceRunning)
BaresipService.callActionUri = URLDecoder.decode(intent.data.toString(), "UTF-8") BaresipService.callActionUri = URLDecoder.decode(intent.data.toString(), "UTF-8")
@@ -128,6 +127,7 @@ class MainActivity : AppCompatActivity() {
// Must be done after view has been created // Must be done after view has been created
Utils.setShowWhenLocked(this, true) Utils.setShowWhenLocked(this, true)
Utils.setTurnScreenOn(this, true) Utils.setTurnScreenOn(this, true)
Utils.requestDismissKeyguard(this)
setSupportActionBar(binding.toolbar) setSupportActionBar(binding.toolbar)
@@ -608,39 +608,43 @@ class MainActivity : AppCompatActivity() {
atStartup = intent.hasExtra("onStartup") atStartup = intent.hasExtra("onStartup")
if (intent.hasExtra("action")) if (!BaresipService.isServiceRunning)
// MainActivity was not visible when call, message, or transfer request came in if (File(filesDir.absolutePath + "/accounts").exists()) {
handleIntent(intent, intent.getStringExtra("action")) val accounts = String(
else Utils.getFileContents(filesDir.absolutePath + "/accounts")!!,
if (!BaresipService.isServiceRunning) Charsets.UTF_8
if (File(filesDir.absolutePath + "/accounts").exists()) { ).lines().toMutableList()
val accounts = String( askPasswords(accounts)
Utils.getFileContents(filesDir.absolutePath + "/accounts")!!, } else {
Charsets.UTF_8 // Baresip is started for the first time
).lines().toMutableList() firstRun = true
askPasswords(accounts) startBaresip()
} else { }
// Baresip is started for the first time
firstRun = true
startBaresip()
}
intent.removeExtra("action")
if (Preferences(applicationContext).displayTheme != AppCompatDelegate.getDefaultNightMode()) { if (Preferences(applicationContext).displayTheme != AppCompatDelegate.getDefaultNightMode()) {
AppCompatDelegate.setDefaultNightMode(Preferences(applicationContext).displayTheme) AppCompatDelegate.setDefaultNightMode(Preferences(applicationContext).displayTheme)
delegate.applyDayNight() delegate.applyDayNight()
} }
requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) {} requestPermissionLauncher =
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
MIC_PERMISSION_REQUEST_CODE)
} // OnCreate } // OnCreate
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
Log.d(TAG, "Main onStart") Log.d(TAG, "Main onStart")
if (!Utils.checkPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO)))
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO),
MIC_PERMISSION_REQUEST_CODE)
val action = intent.getStringExtra("action")
if (action != null) {
// MainActivity was not visible when call, message, or transfer request came in
intent.removeExtra("action")
handleIntent(intent, action)
}
} }
override fun onResume() { override fun onResume() {
@@ -715,10 +719,8 @@ class MainActivity : AppCompatActivity() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
Log.d(TAG, "Main onDestroy") Log.d(TAG, "Main onDestroy")
LocalBroadcastManager.getInstance(this).unregisterReceiver(screenEventReceiver) LocalBroadcastManager.getInstance(this).unregisterReceiver(screenEventReceiver)
LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceEventReceiver) LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceEventReceiver)
BaresipService.activities.clear() BaresipService.activities.clear()
} }
@@ -763,8 +765,10 @@ class MainActivity : AppCompatActivity() {
} else { } else {
val action = intent.getStringExtra("action") val action = intent.getStringExtra("action")
Log.d(TAG, "onNewIntent action `$action'") Log.d(TAG, "onNewIntent action `$action'")
if (action != null) if (action != null) {
intent.removeExtra("action")
handleIntent(intent, action) handleIntent(intent, action)
}
} }
} }
@@ -1284,26 +1288,25 @@ class MainActivity : AppCompatActivity() {
return true return true
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults) super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) { when (requestCode) {
MIC_PERMISSION_REQUEST_CODE -> { MIC_PERMISSION_REQUEST_CODE -> {
if ((grantResults.isNotEmpty()) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) { if (grantResults.isNotEmpty()) {
Log.d(TAG, "Record audio permission granted") if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this, if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECORD_AUDIO)) { Manifest.permission.RECORD_AUDIO))
layout.showSnackBar( layout.showSnackBar(
binding.root, binding.root,
getString(R.string.no_calls), getString(R.string.no_calls),
Snackbar.LENGTH_INDEFINITE, Snackbar.LENGTH_INDEFINITE,
getString(R.string.ok) getString(R.string.ok)
) { ) {
requestPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO) ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.RECORD_AUDIO),
MIC_PERMISSION_REQUEST_CODE)
}
} }
} else {
requestPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
} }
} }
} }
@@ -5,7 +5,6 @@ import android.app.KeyguardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Bitmap.createScaledBitmap import android.graphics.Bitmap.createScaledBitmap
import android.graphics.Color import android.graphics.Color
@@ -694,10 +693,4 @@ object Utils {
} }
} }
@Suppress("unused")
fun darkTheme(ctx: Context): Boolean {
return ctx.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK) ==
Configuration.UI_MODE_NIGHT_YES
}
} }