Refactor Android tutorial to ask the user for setting URLs in case they are not set in code

Previously there was a settings button that opened a dialog to modify the URLs. Now that button has been removed and when the app starts, it checks if the variables are not set and if so, it starts a new activity to configure them
This commit is contained in:
juancarmore 2024-07-31 13:06:01 +02:00
parent 2fd5c8f9fa
commit 93a6357d66
10 changed files with 218 additions and 179 deletions

View File

@ -21,6 +21,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.BasicAndroid"
tools:targetApi="31">
<activity
android:name=".ConfigureUrlsActivity"
android:exported="false" />
<activity
android:name=".RoomLayoutActivity"
android:configChanges="orientation|screenSize|screenLayout"

View File

@ -0,0 +1,33 @@
package io.openvidu.android
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import io.openvidu.android.databinding.ActivityConfigureUrlsBinding
class ConfigureUrlsActivity : AppCompatActivity() {
private lateinit var binding: ActivityConfigureUrlsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityConfigureUrlsBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.saveButton.setOnClickListener {
onSaveUrls()
}
}
private fun onSaveUrls() {
val serverUrl = binding.serverUrl.text.toString()
val livekitUrl = binding.livekitUrl.text.toString()
if (serverUrl.isNotEmpty() && livekitUrl.isNotEmpty()) {
Urls.livekitUrl = binding.livekitUrl.text.toString()
Urls.applicationServerUrl = binding.serverUrl.text.toString()
finish()
} else {
Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show()
}
}
}

View File

@ -2,33 +2,31 @@ package io.openvidu.android
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import io.openvidu.android.databinding.ActivityMainBinding
import io.openvidu.android.databinding.DialogSettingsBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
// Configure this variables with correct URLs depending on your deployment
private var applicationServerUrl = "https://{YOUR-LAN-IP}.openvidu-local.dev:6443/"
private var livekitUrl = "wss://{YOUR-LAN-IP}.openvidu-local.dev:7443/"
override fun onCreate(savedInstanceState: Bundle?) {
checkUrls()
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.participantName.setText("Participant%d".format((1..100).random()))
binding.participantName.setText("Participant%d".format((1..99).random()))
binding.joinButton.setOnClickListener {
navigateToRoomLayoutActivity()
}
}
binding.settingsButton.setOnClickListener {
showSettingsDialog()
private fun checkUrls() {
if (Urls.livekitUrl.isEmpty() || Urls.applicationServerUrl.isEmpty()) {
val intent = Intent(this, ConfigureUrlsActivity::class.java)
startActivity(intent)
}
}
@ -42,8 +40,6 @@ class MainActivity : AppCompatActivity() {
val intent = Intent(this, RoomLayoutActivity::class.java)
intent.putExtra("participantName", participantName)
intent.putExtra("roomName", roomName)
intent.putExtra("serverUrl", applicationServerUrl)
intent.putExtra("livekitUrl", livekitUrl)
startActivity(intent)
} else {
Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show()
@ -51,30 +47,4 @@ class MainActivity : AppCompatActivity() {
binding.joinButton.isEnabled = true
}
/**
* This dialog allows to change the LiveKit URL and the application server URL
* from the application itself. This is useful for development purposes.
*/
private fun showSettingsDialog() {
val dialogBinding = DialogSettingsBinding.inflate(LayoutInflater.from(this))
dialogBinding.serverUrl.setText(applicationServerUrl)
dialogBinding.livekitUrl.setText(livekitUrl)
val builder = AlertDialog.Builder(this)
builder.setTitle("Configure URLs")
.setView(dialogBinding.root)
.setPositiveButton("Save") { dialog, _ ->
applicationServerUrl = dialogBinding.serverUrl.text.toString()
livekitUrl = dialogBinding.livekitUrl.text.toString()
dialog.dismiss()
}
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
val dialog = builder.create()
dialog.show()
}
}

View File

@ -39,9 +39,6 @@ class RoomLayoutActivity : AppCompatActivity() {
private lateinit var binding: ActivityRoomLayoutBinding
private lateinit var participantAdapter: ParticipantAdapter
private lateinit var APPLICATION_SERVER_URL: String
private lateinit var LIVEKIT_URL: String
private lateinit var room: Room
private val participantTracks: MutableList<TrackInfo> = mutableListOf()
@ -62,9 +59,6 @@ class RoomLayoutActivity : AppCompatActivity() {
leaveRoom()
}
APPLICATION_SERVER_URL = intent.getStringExtra("serverUrl") ?: ""
LIVEKIT_URL = intent.getStringExtra("livekitUrl") ?: ""
// Create Room object
room = LiveKit.create(applicationContext)
@ -106,7 +100,7 @@ class RoomLayoutActivity : AppCompatActivity() {
val token = getToken(roomName, participantName)
// Connect to the room with the LiveKit URL and the token
room.connect(LIVEKIT_URL, token)
room.connect(Urls.livekitUrl, token)
// Publish your camera and microphone
val localParticipant = room.localParticipant
@ -227,7 +221,7 @@ class RoomLayoutActivity : AppCompatActivity() {
* access to the endpoints.
*/
private suspend fun getToken(roomName: String, participantName: String): String {
val response = client.post(APPLICATION_SERVER_URL + "token") {
val response = client.post(Urls.applicationServerUrl + "token") {
contentType(ContentType.Application.Json)
setBody(TokenRequest(participantName, roomName))
}

View File

@ -0,0 +1,8 @@
package io.openvidu.android
object Urls {
// Configure these variables with correct URLs depending on your deployment
// If you leave them empty, the user will be prompted to enter the URLs
var applicationServerUrl = ""
var livekitUrl = ""
}

View File

@ -1,12 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z" />
</vector>

View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ConfigureUrlsActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<TextView
android:id="@+id/configureTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="@string/configure_urls"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/serverUrlLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:labelFor="@+id/serverUrl"
android:text="@string/application_server_url"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="@+id/serverUrl"
app:layout_constraintTop_toBottomOf="@+id/configureTitle" />
<EditText
android:id="@+id/serverUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:autofillHints="Application Server URL"
android:ems="10"
android:inputType="textUri"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/serverUrlLabel"
tools:text="http://localhost:6080" />
<TextView
android:id="@+id/livekitUrlLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:labelFor="@+id/livekitUrl"
android:text="@string/livekit_url"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="@+id/livekitUrl"
app:layout_constraintTop_toBottomOf="@+id/serverUrl" />
<EditText
android:id="@+id/livekitUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:autofillHints="LiveKit URL"
android:ems="10"
android:inputType="textUri"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/livekitUrlLabel"
tools:text="http://localhost:7880" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/save"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/livekitUrl" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

View File

@ -1,110 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:padding="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
<TextView
android:id="@+id/joinTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="@string/join_room"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/participantLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:labelFor="@+id/participantName"
android:text="@string/participant"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="@+id/participantName"
app:layout_constraintTop_toBottomOf="@+id/joinTitle" />
<EditText
android:id="@+id/participantName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:autofillHints="Participant Name"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/participantLabel"
tools:text="Participant1" />
<TextView
android:id="@+id/joinPageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="@string/join_room"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/roomLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:labelFor="@+id/roomName"
android:text="@string/room"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="@+id/roomName"
app:layout_constraintTop_toBottomOf="@+id/participantName" />
<TextView
android:id="@+id/participantLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:labelFor="@+id/participantName"
android:text="@string/participant"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="@+id/participantName"
app:layout_constraintTop_toBottomOf="@+id/joinPageTitle" />
<EditText
android:id="@+id/roomName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:autofillHints="Room Name"
android:ems="10"
android:inputType="text"
android:text="@string/room_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/roomLabel" />
<EditText
android:id="@+id/participantName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:autofillHints="Participant Name"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/participantLabel"
tools:text="Participant1" />
<Button
android:id="@+id/joinButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/joinBtn"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/roomName" />
<TextView
android:id="@+id/roomLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:labelFor="@+id/roomName"
android:text="@string/room"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="@+id/roomName"
app:layout_constraintTop_toBottomOf="@+id/participantName" />
</androidx.constraintlayout.widget.ConstraintLayout>
<EditText
android:id="@+id/roomName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:autofillHints="Room Name"
android:ems="10"
android:inputType="text"
android:text="@string/room_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/roomLabel" />
<Button
android:id="@+id/joinButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/joinBtn"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/roomName" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/settingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="@color/colorPrimary"
android:clickable="true"
android:contentDescription="@string/settings"
android:focusable="true"
android:src="@drawable/ic_settings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:tint="@color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

View File

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/serverUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="Application Server URL"
android:hint="@string/application_server_url"
android:inputType="textUri" />
<EditText
android:id="@+id/livekitUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:autofillHints="LiveKit URL"
android:hint="@string/livekit_url"
android:inputType="textUri" />
</LinearLayout>

View File

@ -7,6 +7,7 @@
<string name="joinBtn">Join!</string>
<string name="application_server_url">Application Server URL</string>
<string name="livekit_url">LiveKit URL</string>
<string name="settings">Settings</string>
<string name="configure_urls">Configure URLs</string>
<string name="save">Save</string>
<string name="leave_room">Leave Room</string>
</resources>