Skip to content

Commit

Permalink
ui: add apps to Orbot
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainmohd-a committed Jul 22, 2023
1 parent 5ead9e3 commit 0192308
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 8 deletions.
105 changes: 104 additions & 1 deletion app/src/full/java/com/celzero/bravedns/ui/OrbotBottomSheetFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,28 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.text.HtmlCompat
import androidx.lifecycle.lifecycleScope
import com.celzero.bravedns.R
import com.celzero.bravedns.adapter.WgIncludeAppsAdapter
import com.celzero.bravedns.animation.Rotate3dAnimation
import com.celzero.bravedns.data.AppConfig
import com.celzero.bravedns.databinding.BottomSheetOrbotBinding
import com.celzero.bravedns.databinding.DialogInfoRulesLayoutBinding
import com.celzero.bravedns.service.FirewallManager
import com.celzero.bravedns.service.PersistentState
import com.celzero.bravedns.service.ProxyManager
import com.celzero.bravedns.service.VpnController
import com.celzero.bravedns.util.Constants
import com.celzero.bravedns.util.OrbotHelper
import com.celzero.bravedns.util.Themes
import com.celzero.bravedns.util.Utilities
import com.celzero.bravedns.util.Utilities.isAtleastQ
import com.celzero.bravedns.viewmodel.ProxyAppsMappingViewModel
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
* One touch Orbot Integration. Bottom sheet dialog fragment shows UI that enables One touch
Expand All @@ -66,6 +71,7 @@ class OrbotBottomSheetFragment : BottomSheetDialogFragment() {
private val persistentState by inject<PersistentState>()
private val appConfig by inject<AppConfig>()
private val orbotHelper by inject<OrbotHelper>()
private val mappingViewModel: ProxyAppsMappingViewModel by viewModel()

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -130,13 +136,35 @@ class OrbotBottomSheetFragment : BottomSheetDialogFragment() {

b.bsOrbotRadioSocks5.setOnCheckedChangeListener(null)
b.bsOrbotRadioSocks5.setOnClickListener {
if (!isAnyAppSelected()) {
Toast.makeText(
context,
"Select atleast one app to enable Orbot",
Toast.LENGTH_SHORT
)
.show()
b.bsOrbotRadioNone.isChecked = true
b.bsOrbotRadioSocks5.isChecked = false
return@setOnClickListener
}
if (!b.bsOrbotRadioSocks5.isSelected) {
persistentState.orbotConnectionStatus.postValue(true)
enableSocks5Orbot()
}
}

b.bsSocks5OrbotRl.setOnClickListener {
if (!isAnyAppSelected()) {
Toast.makeText(
context,
"Select atleast one app to enable Orbot",
Toast.LENGTH_SHORT
)
.show()
b.bsOrbotRadioNone.isChecked = true
b.bsOrbotRadioSocks5.isChecked = false
return@setOnClickListener
}
if (!b.bsOrbotRadioSocks5.isChecked) {
b.bsOrbotRadioSocks5.isChecked = true
persistentState.orbotConnectionStatus.postValue(true)
Expand All @@ -146,13 +174,35 @@ class OrbotBottomSheetFragment : BottomSheetDialogFragment() {

b.bsOrbotRadioHttp.setOnCheckedChangeListener(null)
b.bsOrbotRadioHttp.setOnClickListener {
if (!isAnyAppSelected()) {
Toast.makeText(
context,
"Select atleast one app to enable Orbot",
Toast.LENGTH_SHORT
)
.show()
b.bsOrbotRadioNone.isChecked = true
b.bsOrbotRadioHttp.isChecked = false
return@setOnClickListener
}
if (!b.bsOrbotRadioHttp.isSelected) {
persistentState.orbotConnectionStatus.postValue(true)
enableHttpOrbot()
}
}

b.bsOrbotHttpRl.setOnClickListener {
if (!isAnyAppSelected()) {
Toast.makeText(
context,
"Select atleast one app to enable Orbot",
Toast.LENGTH_SHORT
)
.show()
b.bsOrbotRadioNone.isChecked = true
b.bsOrbotRadioHttp.isChecked = false
return@setOnClickListener
}
if (!b.bsOrbotRadioHttp.isChecked) {
persistentState.orbotConnectionStatus.postValue(true)
b.bsOrbotRadioHttp.isChecked = true
Expand All @@ -162,13 +212,35 @@ class OrbotBottomSheetFragment : BottomSheetDialogFragment() {

b.bsOrbotRadioBoth.setOnCheckedChangeListener(null)
b.bsOrbotRadioBoth.setOnClickListener {
if (!isAnyAppSelected()) {
Toast.makeText(
context,
"Select atleast one app to enable Orbot",
Toast.LENGTH_SHORT
)
.show()
b.bsOrbotRadioNone.isChecked = true
b.bsOrbotRadioBoth.isChecked = false
return@setOnClickListener
}
if (!b.bsOrbotRadioBoth.isSelected) {
persistentState.orbotConnectionStatus.postValue(true)
enableSocks5HttpOrbot()
}
}

b.bsOrbotBothRl.setOnClickListener {
if (!isAnyAppSelected()) {
Toast.makeText(
context,
"Select atleast one app to enable Orbot",
Toast.LENGTH_SHORT
)
.show()
b.bsOrbotRadioNone.isChecked = true
b.bsOrbotRadioBoth.isChecked = false
return@setOnClickListener
}
if (!b.bsOrbotRadioBoth.isChecked) {
persistentState.orbotConnectionStatus.postValue(true)
b.bsOrbotRadioBoth.isChecked = true
Expand All @@ -193,6 +265,12 @@ class OrbotBottomSheetFragment : BottomSheetDialogFragment() {
}
}
}

b.includeApplications.setOnClickListener { openAppsDialog() }
}

private fun isAnyAppSelected(): Boolean {
return ProxyManager.isAnyAppSelected(ProxyManager.ID_ORBOT_BASE)
}

private fun handleOrbotStop() {
Expand Down Expand Up @@ -254,6 +332,31 @@ class OrbotBottomSheetFragment : BottomSheetDialogFragment() {
}
}

private fun openAppsDialog() {
// treat proxyId and proxyName of Orbot as base
val themeId = Themes.getCurrentTheme(isDarkThemeOn(), persistentState.theme)
val appsAdapter =
WgIncludeAppsAdapter(
requireContext(),
ProxyManager.ID_ORBOT_BASE,
ProxyManager.ORBOT_PROXY_NAME
)
mappingViewModel.apps.observe(this.viewLifecycleOwner) {
appsAdapter.submitData(lifecycle, it)
}
val includeAppsDialog =
WgIncludeAppsDialog(
requireActivity(),
appsAdapter,
mappingViewModel,
themeId,
ProxyManager.ID_ORBOT_BASE,
ProxyManager.ID_ORBOT_BASE
)
includeAppsDialog.setCanceledOnTouchOutside(false)
includeAppsDialog.show()
}

private fun updateOrbotNone() {
b.bsOrbotRadioNone.isChecked = true
b.bsOrbotRadioSocks5.isChecked = false
Expand Down Expand Up @@ -372,7 +475,7 @@ class OrbotBottomSheetFragment : BottomSheetDialogFragment() {
}

private fun showStopOrbotDialog(isOrbotDns: Boolean) {
val builder = AlertDialog.Builder(requireContext())
val builder = MaterialAlertDialogBuilder(requireContext())
builder.setTitle(getString(R.string.orbot_stop_dialog_title))

builder.setCancelable(true)
Expand Down
30 changes: 23 additions & 7 deletions app/src/full/res/layout/bottom_sheet_orbot.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bs_conn_top_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -38,10 +39,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="5dp"
android:gravity="center"
android:padding="5dp"
android:text="@string/settings_orbot_header"
android:layout_marginStart="5dp"
android:textAlignment="center"
android:textColor="?attr/primaryTextColor"
android:textSize="@dimen/heading_font_text_view" />
Expand All @@ -58,6 +59,21 @@
android:src="@drawable/brave_mode_info" />
</RelativeLayout>

<com.google.android.material.button.MaterialButton
android:id="@+id/include_applications"
style="@style/Widget.Material3.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_remove_apps"
android:textColor="?attr/secondaryTextColor"
android:textSize="@dimen/large_font_text_view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/config_apps_text"
app:layout_constraintWidth_max="@dimen/max_btn_size"
app:rippleColor="?attr/secondaryTextColor" />

<TextView
android:id="@+id/orbot_status"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -116,9 +132,9 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.75"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:alpha="0.75"
android:text="@string/orbot_http_desc"
android:textColor="?attr/primaryLightColorText"
android:textSize="@dimen/default_font_text_view" />
Expand Down Expand Up @@ -167,9 +183,9 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.75"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:alpha="0.75"
android:text="@string/orbot_socks5_desc"
android:textColor="?attr/primaryLightColorText"
android:textSize="@dimen/default_font_text_view" />
Expand All @@ -183,10 +199,10 @@
android:id="@+id/bs_orbot_both_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginBottom="1dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
android:paddingBottom="5dp"
android:visibility="gone">

<com.google.android.material.radiobutton.MaterialRadioButton
android:id="@+id/bs_orbot_radio_both"
Expand Down Expand Up @@ -222,10 +238,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:alpha="0.75"
android:gravity="center_vertical"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:alpha="0.75"
android:text="@string/orbot_both_desc"
android:textColor="?attr/primaryLightColorText"
android:textSize="@dimen/default_font_text_view" />
Expand Down Expand Up @@ -276,10 +292,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:alpha="0.75"
android:gravity="center_vertical"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:alpha="0.75"
android:text="@string/orbot_none_desc"
android:textColor="?attr/primaryLightColorText"
android:textSize="@dimen/default_font_text_view" />
Expand Down

0 comments on commit 0192308

Please sign in to comment.