srctree

Robin Linden parent 73b0703d ecd8d15f
Allow switching between UDP and TCP mode

inlinesplit
atox/src/main/kotlin/Extensions.kt added: 112, removed: 11, total 101
@@ -1,15 +1,21 @@
package ltd.evilcorp.atox
 
import android.content.Context
import android.os.Build
import android.view.View
import android.view.WindowInsets
import androidx.fragment.app.Fragment
import androidx.preference.PreferenceManager
import ltd.evilcorp.atox.di.ViewModelFactory
 
fun Context.getPreferences() =
PreferenceManager.getDefaultSharedPreferences(this)
 
val Fragment.vmFactory: ViewModelFactory
get() = (requireActivity() as MainActivity).vmFactory
 
fun Fragment.requireStringArg(key: String) = arguments?.getString(key) ?: throw Exception("Missing argument $key")
fun Fragment.requireStringArg(key: String) =
arguments?.getString(key) ?: throw Exception("Missing argument $key")
 
fun View.setUpFullScreenUi(listener: (v: View, insets: WindowInsets) -> WindowInsets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 
atox/src/main/kotlin/MainActivity.kt added: 112, removed: 11, total 101
@@ -25,7 +25,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
 
AppCompatDelegate.setDefaultNightMode(
PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt("theme", 0)
applicationContext.getPreferences().getInt("theme", 0)
)
 
setContentView(R.layout.activity_main)
 
atox/src/main/kotlin/tox/ToxStarter.kt added: 112, removed: 11, total 101
@@ -6,6 +6,7 @@ import android.os.Build
import android.util.Log
import im.tox.tox4j.core.exceptions.ToxNewException
import ltd.evilcorp.atox.ToxService
import ltd.evilcorp.atox.getPreferences
import ltd.evilcorp.domain.feature.UserManager
import ltd.evilcorp.domain.tox.*
import javax.inject.Inject
@@ -24,7 +25,12 @@ class ToxStarter @Inject constructor(
fun startTox(save: ByteArray? = null): Boolean = try {
listenerCallbacks.setUp(eventListener)
listenerCallbacks.setUp(avEventListener)
tox.start(SaveOptions(save, udpEnabled = false), eventListener, avEventListener)
tox.start(
SaveOptions(
save,
udpEnabled = context.getPreferences().getBoolean("udp_enabled", false)
), eventListener, avEventListener
)
startService()
true
} catch (e: ToxNewException) {
 
atox/src/main/kotlin/ui/settings/SettingsFragment.kt added: 112, removed: 11, total 101
@@ -75,7 +75,17 @@ class SettingsFragment : Fragment() {
}
}
 
settings_udp_enabled.isChecked = vm.getUdpEnabled()
settings_udp_enabled.setOnClickListener {
vm.setUdpEnabled(settings_udp_enabled.isChecked)
}
 
version.text =
getString(R.string.version_display, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)
}
 
override fun onStop() {
super.onStop()
vm.commit()
}
}
 
atox/src/main/kotlin/ui/settings/SettingsViewModel.kt added: 112, removed: 11, total 101
@@ -1,6 +1,40 @@
package ltd.evilcorp.atox.ui.settings
 
import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import ltd.evilcorp.atox.getPreferences
import ltd.evilcorp.atox.tox.ToxStarter
import ltd.evilcorp.domain.tox.Tox
import javax.inject.Inject
 
class SettingsViewModel @Inject constructor() : ViewModel()
class SettingsViewModel @Inject constructor(
private val context: Context,
private val toxStarter: ToxStarter,
private val tox: Tox
) : ViewModel(), CoroutineScope by GlobalScope {
private var restartNeeded = false
 
fun getUdpEnabled(): Boolean = context.getPreferences().getBoolean("udp_enabled", false)
fun setUdpEnabled(enabled: Boolean) {
context.getPreferences().edit().putBoolean("udp_enabled", enabled).apply()
restartNeeded = true
}
 
fun commit() {
if (!restartNeeded) return
toxStarter.stopTox()
 
launch {
while (tox.started) {
Log.e("asdf", "sleepin")
delay(200)
}
toxStarter.tryLoadTox()
}
}
}
 
atox/src/main/res/layout/fragment_settings.xml added: 112, removed: 11, total 101
@@ -58,6 +58,49 @@
</LinearLayout>
</androidx.cardview.widget.CardView>
 
<androidx.cardview.widget.CardView
android:id="@+id/tox_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="4dp"
app:layout_constraintTop_toBottomOf="@id/appearance_settings"
tools:targetApi="21">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/pref_heading_network"
android:textSize="16sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/dividerColor"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
style="@style/OptionItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pref_udp_enabled"/>
<Switch
android:id="@+id/settings_udp_enabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
 
<TextView
android:id="@+id/version"
android:layout_width="match_parent"
 
atox/src/main/res/values/strings.xml added: 112, removed: 11, total 101
@@ -80,6 +80,8 @@
<string name="profile">Profile</string>
<string name="tox_id_error_already_exists">A contact with that ID already exists</string>
<string name="tox_service_running">aTox is running</string>
<string name="pref_heading_network">Network</string>
<string name="pref_udp_enabled">UDP enabled</string>
<string-array name="user_statuses">
<item>Available</item>
<item>Away</item>