Moved more constants to Constants

Use accountRequest in AccountListAdapter
No need for activity result in ContactListAdapter
Minor edits
This commit is contained in:
Juha Heinanen
2021-06-13 10:26:39 +03:00
parent a0abd8ca79
commit cc9d2d4b28
5 changed files with 25 additions and 29 deletions
@@ -50,7 +50,7 @@ class AccountListAdapter(private val cxt: Context, private val rows: ArrayList<A
val b = Bundle() val b = Bundle()
b.putString("aor", UserAgent.uas()[position].account.aor) b.putString("aor", UserAgent.uas()[position].account.aor)
i.putExtras(b) i.putExtras(b)
(cxt as Activity).startActivityForResult(i, MainActivity.ACCOUNT_CODE) (cxt as MainActivity).accountRequest.launch(i)
} }
viewHolder.actionView.setOnClickListener { viewHolder.actionView.setOnClickListener {
@@ -1,3 +1,8 @@
package com.tutpro.baresip package com.tutpro.baresip
const val TAG = "Baresip" const val TAG = "Baresip"
const val BACKUP_PERMISSION_REQUEST_CODE = 1
const val RESTORE_PERMISSION_REQUEST_CODE = 2
const val RECORD_PERMISSION_REQUEST_CODE = 3
const val CONTACTS_PERMISSION_REQUEST_CODE = 4
@@ -266,7 +266,7 @@ class ContactActivity : AppCompatActivity() {
if (contact.androidContact) { if (contact.androidContact) {
Utils.requestPermission(this, Manifest.permission.READ_CONTACTS + Utils.requestPermission(this, Manifest.permission.READ_CONTACTS +
"|" + Manifest.permission.WRITE_CONTACTS, "|" + Manifest.permission.WRITE_CONTACTS,
MainActivity.CONTACTS_PERMISSION_REQUEST_CODE) CONTACTS_PERMISSION_REQUEST_CODE)
return false return false
} }
} }
@@ -300,7 +300,7 @@ class ContactActivity : AppCompatActivity() {
when (requestCode) { when (requestCode) {
MainActivity.CONTACTS_PERMISSION_REQUEST_CODE -> CONTACTS_PERMISSION_REQUEST_CODE ->
if (grantResults.isNotEmpty() && permissions.size == grantResults.size) { if (grantResults.isNotEmpty() && permissions.size == grantResults.size) {
for (res in grantResults) for (res in grantResults)
if (res != PackageManager.PERMISSION_GRANTED) if (res != PackageManager.PERMISSION_GRANTED)
@@ -16,6 +16,7 @@ import android.widget.ArrayAdapter
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.core.content.ContextCompat.startActivity
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@@ -158,7 +159,7 @@ class ContactListAdapter(private val ctx: Context, private val rows: ArrayList<C
b.putBoolean("new", false) b.putBoolean("new", false)
b.putInt("index", position) b.putInt("index", position)
i.putExtras(b) i.putExtras(b)
(ctx as Activity).startActivityForResult(i, MainActivity.CONTACT_CODE) startActivity(ctx, i, null)
} }
} }
@@ -70,7 +70,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var swipeRefresh: SwipeRefreshLayout private lateinit var swipeRefresh: SwipeRefreshLayout
private lateinit var accountsRequest: ActivityResultLauncher<Intent> private lateinit var accountsRequest: ActivityResultLauncher<Intent>
private lateinit var accountRequest: ActivityResultLauncher<Intent> lateinit var accountRequest: ActivityResultLauncher<Intent>
private lateinit var chatRequests: ActivityResultLauncher<Intent> private lateinit var chatRequests: ActivityResultLauncher<Intent>
private lateinit var configRequest: ActivityResultLauncher<Intent> private lateinit var configRequest: ActivityResultLauncher<Intent>
private lateinit var backupRequest: ActivityResultLauncher<Intent> private lateinit var backupRequest: ActivityResultLauncher<Intent>
@@ -1176,7 +1176,7 @@ class MainActivity : AppCompatActivity() {
R.id.backup -> { R.id.backup -> {
if (Build.VERSION.SDK_INT >= 29) { if (Build.VERSION.SDK_INT >= 29) {
pickupFileFromDownloads(BACKUP_CODE) pickupFileFromDownloads("backup")
} else { } else {
if (Utils.requestPermission( if (Utils.requestPermission(
this, this,
@@ -1237,7 +1237,7 @@ class MainActivity : AppCompatActivity() {
private fun startRestore() { private fun startRestore() {
if (Build.VERSION.SDK_INT >= 29) { if (Build.VERSION.SDK_INT >= 29) {
pickupFileFromDownloads(RESTORE_CODE) pickupFileFromDownloads("restore")
} else { } else {
val path = Utils.downloadsPath("baresip.bs") val path = Utils.downloadsPath("baresip.bs")
downloadsInputStream = FileInputStream(File(path)) downloadsInputStream = FileInputStream(File(path))
@@ -1246,9 +1246,9 @@ class MainActivity : AppCompatActivity() {
} }
@RequiresApi(29) @RequiresApi(29)
private fun pickupFileFromDownloads(activityCode: Int) { private fun pickupFileFromDownloads(action: String) {
when (activityCode) { when (action) {
BACKUP_CODE -> { "backup" -> {
backupRequest.launch(Intent(Intent.ACTION_CREATE_DOCUMENT).apply { backupRequest.launch(Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE) addCategory(Intent.CATEGORY_OPENABLE)
type = "application/octet-stream" type = "application/octet-stream"
@@ -1256,7 +1256,7 @@ class MainActivity : AppCompatActivity() {
putExtra(DocumentsContract.EXTRA_INITIAL_URI, MediaStore.Downloads.EXTERNAL_CONTENT_URI) putExtra(DocumentsContract.EXTRA_INITIAL_URI, MediaStore.Downloads.EXTERNAL_CONTENT_URI)
}) })
} }
RESTORE_CODE -> { "restore" -> {
restoreRequest.launch(Intent(Intent.ACTION_OPEN_DOCUMENT).apply { restoreRequest.launch(Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE) addCategory(Intent.CATEGORY_OPENABLE)
type = "application/octet-stream" type = "application/octet-stream"
@@ -1846,16 +1846,6 @@ class MainActivity : AppCompatActivity() {
// <aor, password> of those accounts that have auth username without auth password // <aor, password> of those accounts that have auth username without auth password
val aorPasswords = mutableMapOf<String, String>() val aorPasswords = mutableMapOf<String, String>()
const val ACCOUNT_CODE = 6
const val CONTACT_CODE = 7
const val BACKUP_CODE = 10
const val RESTORE_CODE = 11
const val BACKUP_PERMISSION_REQUEST_CODE = 1
const val RESTORE_PERMISSION_REQUEST_CODE = 2
const val RECORD_PERMISSION_REQUEST_CODE = 3
const val CONTACTS_PERMISSION_REQUEST_CODE = 4
} }
init { init {