David Liu

convenience saving of url/token to device

@@ -45,7 +45,7 @@ ext { @@ -45,7 +45,7 @@ ext {
45 ] 45 ]
46 versions = [ 46 versions = [
47 androidx_core : "1.2.0", 47 androidx_core : "1.2.0",
48 - androidx_lifecycle: "2.3.0", 48 + androidx_lifecycle: "2.3.1",
49 dagger : "2.27", 49 dagger : "2.27",
50 groupie : "2.9.0", 50 groupie : "2.9.0",
51 protobuf : "3.15.1", 51 protobuf : "3.15.1",
@@ -39,9 +39,10 @@ dependencies { @@ -39,9 +39,10 @@ dependencies {
39 implementation 'com.google.android.material:material:1.3.0' 39 implementation 'com.google.android.material:material:1.3.0'
40 implementation 'androidx.appcompat:appcompat:1.2.0' 40 implementation 'androidx.appcompat:appcompat:1.2.0'
41 implementation 'androidx.core:core-ktx:1.3.2' 41 implementation 'androidx.core:core-ktx:1.3.2'
42 - implementation "androidx.activity:activity-ktx:1.2.1"  
43 - implementation 'androidx.fragment:fragment-ktx:1.3.1'  
44 - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0' 42 + implementation "androidx.activity:activity-ktx:1.2.2"
  43 + implementation 'androidx.fragment:fragment-ktx:1.3.2'
  44 + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
  45 + implementation 'androidx.preference:preference:1.1.1'
45 implementation "androidx.viewpager2:viewpager2:1.0.0" 46 implementation "androidx.viewpager2:viewpager2:1.0.0"
46 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}" 47 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}"
47 implementation "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}" 48 implementation "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}"
@@ -9,17 +9,24 @@ import android.widget.Toast @@ -9,17 +9,24 @@ import android.widget.Toast
9 import androidx.activity.result.contract.ActivityResultContracts 9 import androidx.activity.result.contract.ActivityResultContracts
10 import androidx.appcompat.app.AppCompatActivity 10 import androidx.appcompat.app.AppCompatActivity
11 import androidx.core.content.ContextCompat 11 import androidx.core.content.ContextCompat
  12 +import androidx.core.content.edit
  13 +import androidx.preference.PreferenceManager
12 import io.livekit.android.sample.databinding.MainActivityBinding 14 import io.livekit.android.sample.databinding.MainActivityBinding
13 15
14 16
15 class MainActivity : AppCompatActivity() { 17 class MainActivity : AppCompatActivity() {
  18 +
16 override fun onCreate(savedInstanceState: Bundle?) { 19 override fun onCreate(savedInstanceState: Bundle?) {
17 super.onCreate(savedInstanceState) 20 super.onCreate(savedInstanceState)
18 21
19 val binding = MainActivityBinding.inflate(layoutInflater) 22 val binding = MainActivityBinding.inflate(layoutInflater)
  23 +
  24 + val preferences = PreferenceManager.getDefaultSharedPreferences(this)
  25 + val urlString = preferences.getString(PREFERENCES_KEY_URL, URL)
  26 + val tokenString = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN)
20 binding.run { 27 binding.run {
21 - url.editText?.text = URL  
22 - token.editText?.text = TOKEN 28 + url.editText?.text = SpannableStringBuilder(urlString)
  29 + token.editText?.text = SpannableStringBuilder(tokenString)
23 connectButton.setOnClickListener { 30 connectButton.setOnClickListener {
24 val intent = Intent(this@MainActivity, CallActivity::class.java).apply { 31 val intent = Intent(this@MainActivity, CallActivity::class.java).apply {
25 putExtra( 32 putExtra(
@@ -33,6 +40,33 @@ class MainActivity : AppCompatActivity() { @@ -33,6 +40,33 @@ class MainActivity : AppCompatActivity() {
33 40
34 startActivity(intent) 41 startActivity(intent)
35 } 42 }
  43 +
  44 + saveButton.setOnClickListener {
  45 + preferences.edit {
  46 + putString(PREFERENCES_KEY_URL, url.editText?.text.toString())
  47 + putString(PREFERENCES_KEY_TOKEN, token.editText?.text.toString())
  48 + }
  49 +
  50 + Toast.makeText(
  51 + this@MainActivity,
  52 + "Values saved.",
  53 + Toast.LENGTH_SHORT
  54 + ).show()
  55 + }
  56 +
  57 + resetButton.setOnClickListener {
  58 + preferences.edit {
  59 + clear()
  60 + }
  61 + url.editText?.text = SpannableStringBuilder(URL)
  62 + token.editText?.text = SpannableStringBuilder(TOKEN)
  63 +
  64 + Toast.makeText(
  65 + this@MainActivity,
  66 + "Values reset.",
  67 + Toast.LENGTH_SHORT
  68 + ).show()
  69 + }
36 } 70 }
37 71
38 setContentView(binding.root) 72 setContentView(binding.root)
@@ -71,8 +105,11 @@ class MainActivity : AppCompatActivity() { @@ -71,8 +105,11 @@ class MainActivity : AppCompatActivity() {
71 } 105 }
72 106
73 companion object { 107 companion object {
74 - val URL = SpannableStringBuilder("ws://192.168.91.198:7880")  
75 - val TOKEN =  
76 - SpannableStringBuilder("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTgyMDg0NTAsImlzcyI6IkFQSU1teGlMOHJxdUt6dFpFb1pKVjlGYiIsImp0aSI6ImZvcnRoIiwibmJmIjoxNjE1NjE2NDUwLCJ2aWRlbyI6eyJyb29tIjoibXlyb29tIiwicm9vbUpvaW4iOnRydWV9fQ.nu-fOZA-TPFvzleyXk2Zz9b5lFApCXV1npUAexttXQA") 108 + const val PREFERENCES_KEY_URL = "url"
  109 + const val PREFERENCES_KEY_TOKEN = "token"
  110 +
  111 + const val URL = "ws://192.168.11.5:7880"
  112 + const val TOKEN =
  113 + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTk0NDkwNTMsImlzcyI6IkFQSXdMZWFoN2c0ZnVMWURZQUplYUtzU0UiLCJqdGkiOiJwaG9uZTIiLCJuYmYiOjE2MTY4NTcwNTMsInZpZGVvIjp7InJvb20iOiJteXJvb20iLCJyb29tSm9pbiI6dHJ1ZX19.OiemzgYXe5269q_VDXb912LG3QoqoKy-52-xEWcTr3A"
77 } 114 }
78 } 115 }
@@ -32,6 +32,18 @@ @@ -32,6 +32,18 @@
32 android:id="@+id/connect_button" 32 android:id="@+id/connect_button"
33 android:layout_width="match_parent" 33 android:layout_width="match_parent"
34 android:layout_height="wrap_content" 34 android:layout_height="wrap_content"
35 - android:text="@string/connect"/> 35 + android:text="@string/connect" />
  36 +
  37 + <Button
  38 + android:id="@+id/save_button"
  39 + android:layout_width="match_parent"
  40 + android:layout_height="wrap_content"
  41 + android:text="@string/save_values" />
  42 +
  43 + <Button
  44 + android:id="@+id/reset_button"
  45 + android:layout_width="match_parent"
  46 + android:layout_height="wrap_content"
  47 + android:text="@string/reset_values" />
36 48
37 </LinearLayout> 49 </LinearLayout>
@@ -3,4 +3,6 @@ @@ -3,4 +3,6 @@
3 <string name="connect">Connect</string> 3 <string name="connect">Connect</string>
4 <string name="token">Token</string> 4 <string name="token">Token</string>
5 <string name="url">URL</string> 5 <string name="url">URL</string>
  6 + <string name="save_values">Save Values</string>
  7 + <string name="reset_values">Reset Values</string>
6 </resources> 8 </resources>