Skip to content

Commit

Permalink
Merge branch 'issues/2594' of github.com:ooni/probe-android into issu…
Browse files Browse the repository at this point in the history
…es/2594
  • Loading branch information
aanorbel committed Mar 21, 2024
2 parents cca3590 + 8e23e83 commit 3da3869
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.widget.Toolbar
import androidx.databinding.BindingAdapter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -113,7 +113,7 @@ class AddDescriptorActivity : AbstractActivity() {
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(false)
supportActionBar?.setDisplayShowHomeEnabled(false)
supportActionBar?.title = "Add New Link"
supportActionBar?.title = "Install New Link"
val descriptorExtra = if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(DESCRIPTOR, TestDescriptor::class.java)
} else {
Expand Down Expand Up @@ -144,21 +144,17 @@ class AddDescriptorActivity : AbstractActivity() {
for (i in 0 until adapter.groupCount) {
binding.expandableListView.expandGroup(i)
}
val bottomBarOnMenuItemClickListener: Toolbar.OnMenuItemClickListener =
Toolbar.OnMenuItemClickListener { item ->
when (item.itemId) {
R.id.add_descriptor -> {
viewModel.onAddButtonClicked(
disabledAutorunNettests = adapter.nettests.filter { it.selected },
automatedUpdates = binding.automaticUpdatesSwitch.isChecked
)
true
}

else -> false
}
}
binding.bottomBar.setOnMenuItemClickListener(bottomBarOnMenuItemClickListener)

binding.btnInstallLink.setOnClickListener {
viewModel.onAddButtonClicked(
disabledAutorunNettests = adapter.nettests.filter { it.selected },
automatedUpdates = binding.automaticUpdatesSwitch.isChecked
)
}

binding.btnCancel.setOnClickListener {
finish()
}

viewModel.selectedAllBtnStatus.observe(this) { state ->
binding.testsCheckbox.checkedState = state;
Expand All @@ -173,6 +169,7 @@ class AddDescriptorActivity : AbstractActivity() {
// This observer is used to finish the activity when the descriptor is added.
viewModel.finishActivity.observe(this) { shouldFinish ->
if (shouldFinish) {
Toast.makeText(this@AddDescriptorActivity, "Link installed", Toast.LENGTH_LONG).show()
finish()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import android.content.DialogInterface
import android.os.Bundle
import android.os.Parcelable
import android.util.Patterns
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.recyclerview.widget.LinearLayoutManager
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.AbstractActivity
Expand Down Expand Up @@ -50,15 +49,23 @@ class CustomWebsiteActivity : AbstractActivity(), ConfirmDialogFragment.OnClickL
)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setHomeAsUpIndicator(
ContextCompat.getDrawable(this@CustomWebsiteActivity, R.drawable.close)
?.apply { DrawableCompat.setTint(this, ContextCompat.getColor(this@CustomWebsiteActivity, R.color.color_black)) }
)
title = getString(R.string.Settings_Websites_CustomURL_Title).uppercase()
}

val layoutManager = LinearLayoutManager(this)
binding.urlContainer.isNestedScrollingEnabled = false
binding.urlContainer.layoutManager = layoutManager
val fatTextTemplate = "Test %s URLs"
adapter = CustomWebsiteRecyclerViewAdapter(
onItemChangedListener = object : ItemChangedListener {
override fun onItemRemoved(position: Int) {
binding.bottomBar.title = getString(
R.string.OONIRun_URLs, adapter.itemCount.toString()
)
binding.fabTestUrls.text = fatTextTemplate.format(adapter.itemCount)
viewModel.onItemRemoved(position)
}

Expand All @@ -68,12 +75,10 @@ class CustomWebsiteActivity : AbstractActivity(), ConfirmDialogFragment.OnClickL
},
)
viewModel.urls.observe(this) { urls ->
binding.bottomBar.title = getString(
R.string.OONIRun_URLs, urls.size.toString()
)
binding.fabTestUrls.text = fatTextTemplate.format(urls.size)
}

binding.bottomBar.setOnMenuItemClickListener { item: MenuItem? -> runTests() }
binding.fabTestUrls.setOnClickListener { runTests() }
binding.add.setOnClickListener { add() }

binding.urlContainer.adapter = adapter
Expand Down Expand Up @@ -147,23 +152,6 @@ class CustomWebsiteActivity : AbstractActivity(), ConfirmDialogFragment.OnClickL
return true
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.close, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.close_button -> {
onSupportNavigateUp()
true
}

else -> super.onOptionsItemSelected(item)
}
}

/**
* This function will add a new url to the list of urls.
* It will also scroll to the bottom of the list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class OoniRunV2Activity : AbstractActivity() {
val executor = TaskExecutor()
binding.cancelButton.setOnClickListener {
executor.cancelTask()
finishWithError(message = getString(R.string.Modal_Cancel))
finishWithError(message = "Link installation cancelled")
}
executor.executeTask({
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import org.openobservatory.engine.BaseNettest
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.AbstractActivity
Expand Down Expand Up @@ -54,7 +54,14 @@ class RunTestsActivity : AbstractActivity() {
setContentView(binding.getRoot())

setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setHomeAsUpIndicator(
ContextCompat.getDrawable(this@RunTestsActivity, R.drawable.close)
?.apply { DrawableCompat.setTint(this, ContextCompat.getColor(this@RunTestsActivity, R.color.color_black)) }
)
title = "Run tests".uppercase()
}

activityComponent?.inject(this)

Expand All @@ -78,55 +85,29 @@ class RunTestsActivity : AbstractActivity() {

viewModel.selectedAllBtnStatus.observe(this, this::selectAllBtnStatusObserver)

binding.bottomBar.setOnMenuItemClickListener { menuItem ->
onMenuItemClickListener(menuItem)
}
binding.fabRunTests.setOnClickListener { onRunTestsClickListener() }
} ?: run {
finish()
}

}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.close, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.close_button -> {
finish()
true
}

else -> super.onOptionsItemSelected(item)
}
}

private fun onMenuItemClickListener(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.runButton -> {
updatePreferences()
val selectedChildItems: List<String> = getChildItemsSelectedIdList()
if (selectedChildItems.isNotEmpty()) {
val testSuitesToRun = getGroupItemsAtLeastOneChildEnabled().map { groupItem ->
return@map groupItem.getTest(this)
}
RunningActivity.runAsForegroundService(
this@RunTestsActivity,
java.util.ArrayList(testSuitesToRun),
{ finish() },
preferenceManager
)

}
true
}

else -> false
}
}
private fun onRunTestsClickListener() {
updatePreferences()
val selectedChildItems: List<String> = getChildItemsSelectedIdList()
if (selectedChildItems.isNotEmpty()) {
val testSuitesToRun = getGroupItemsAtLeastOneChildEnabled().map { groupItem ->
return@map groupItem.getTest(this)
}
RunningActivity.runAsForegroundService(
this@RunTestsActivity,
java.util.ArrayList(testSuitesToRun),
{ finish() },
preferenceManager
)
} else {
Toast.makeText(this@RunTestsActivity, "Please select test to run", Toast.LENGTH_LONG).show()
}
}

/**
* Update the preferences based on the selected tests.
Expand Down Expand Up @@ -189,10 +170,10 @@ class RunTestsActivity : AbstractActivity() {
}


private fun updateStatusIndicator() {
//TODO(aanorbel): translate status indicator
binding.bottomBar.setTitle("${getChildItemsSelectedIdList().size} Tests")
}
private fun updateStatusIndicator() {
//TODO(aanorbel): translate status indicator
binding.fabRunTests.text = "Run ${getChildItemsSelectedIdList().size} test"
}

private fun getChildItemsSelectedIdList(): List<String> {
val childItemSelectedIdList: MutableList<String> = ArrayList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ typealias OnTaskComplete<R> = (R) -> Unit
class TaskExecutor {
private val executor = Executors.newSingleThreadExecutor()
private val handler = Handler(Looper.getMainLooper())
private var future: Future<*>? = null

private lateinit var future: Future<*>
/**
* Executes a task in a separate thread and posts the result on the main thread.
* @param task The task to be executed.
Expand All @@ -54,8 +53,10 @@ class TaskExecutor {
fun <R> executeTask(task: Task<R>, onComplete: OnTaskComplete<R>) {
future = executor.submit {
val result = task.call()
handler.post {
onComplete(result)
if (!future.isCancelled) {
handler.post {
onComplete(result)
}
}
}
}
Expand Down Expand Up @@ -90,6 +91,6 @@ class TaskExecutor {
* Cancels the currently running task.
*/
fun cancelTask() {
future?.cancel(true)
this.future.cancel(true)
}
}
34 changes: 29 additions & 5 deletions app/src/main/res/layout/activity_add_descriptor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,37 @@
android:layout_gravity="bottom"
android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.App.NoActionBar">

<androidx.appcompat.widget.Toolbar
android:id="@+id/bottomBar"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="?attr/actionBarSize"
android:background="@color/color_gray0"
app:menu="@menu/add_descriptor"
app:titleTextAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title.App" />
android:gravity="center">

<Button
android:id="@+id/btn_cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Modal_Cancel"
android:textColor="@color/color_base" />

<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />

<Button
android:id="@+id/btn_install_link"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:elevation="0dp"
android:text="Install Link"
android:textColor="@color/color_base" />

</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
Loading

0 comments on commit 3da3869

Please sign in to comment.