From b009d7d5fa4a5865028831c967ac6d60220e881b Mon Sep 17 00:00:00 2001 From: Norbel AMBANUMBEN Date: Mon, 19 Feb 2024 12:36:27 +0100 Subject: [PATCH] Match result colors with descriptor theme color --- .../activity/MeasurementDetailActivity.java | 14 +++-- .../activity/ResultDetailActivity.kt | 54 +++++++++++++++---- .../ooniprobe/activity/RunningActivity.java | 6 ++- .../RunTestsExpandableListViewAdapter.kt | 4 +- .../res/layout/activity_result_detail.xml | 1 + 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java index 5035f2136..513aaa5ed 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/MeasurementDetailActivity.java @@ -13,8 +13,11 @@ import androidx.fragment.app.Fragment; import com.google.android.material.snackbar.Snackbar; import com.google.common.base.Optional; +import com.google.common.collect.Lists; import com.google.gson.Gson; import localhost.toolkit.app.fragment.ConfirmDialogFragment; + +import org.openobservatory.engine.BaseNettest; import org.openobservatory.ooniprobe.R; import org.openobservatory.ooniprobe.common.OONITests; import org.openobservatory.ooniprobe.common.PreferenceManager; @@ -71,15 +74,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { assert measurement != null; measurement.result.load(); if (measurement.is_failed) { - setTheme((int) R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Failed); + setTheme(R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Failed); } else { - if (measurement.result.test_group_name.equals(OONITests.PERFORMANCE.getLabel())) { - Optional optionalSuite = measurement.result.getTestSuite(this); - if (optionalSuite.isPresent()){ - setTheme(optionalSuite.get().getThemeLight()); - } else { - setTheme(R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Experimental); - } + if (Lists.transform(OONITests.PERFORMANCE.getNettests(), BaseNettest::getName).contains(measurement.test_name)) { + setTheme(R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Performance); } else { if (measurement.is_anomaly) { setTheme(R.style.Theme_MaterialComponents_Light_DarkActionBar_App_NoActionBar_Failure); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.kt index 1c6448e91..d2f51f73b 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/ResultDetailActivity.kt @@ -8,6 +8,7 @@ import android.view.Menu import android.view.MenuItem import android.view.View import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.viewpager2.adapter.FragmentStateAdapter @@ -19,6 +20,7 @@ import localhost.toolkit.app.fragment.ConfirmDialogFragment.OnConfirmedListener import org.openobservatory.ooniprobe.R import org.openobservatory.ooniprobe.adapters.MeasurementGroup import org.openobservatory.ooniprobe.adapters.ResultDetailExpandableListAdapter +import org.openobservatory.ooniprobe.common.OONIDescriptor import org.openobservatory.ooniprobe.common.OONITests import org.openobservatory.ooniprobe.common.PreferenceManager import org.openobservatory.ooniprobe.common.ResubmitTask @@ -160,6 +162,8 @@ class ResultDetailActivity : AbstractActivity(), View.OnClickListener, OnConfirm private fun load() { result = getResults[result.id] + setThemeFromDescriptor() + val groupedItemList = mutableListOf() val groupedItems = result.getMeasurementsSorted().groupBy { it.test_name } for ((_, itemList) in groupedItems) { @@ -193,19 +197,51 @@ class ResultDetailActivity : AbstractActivity(), View.OnClickListener, OnConfirm ) snackbar.show() else snackbar.dismiss() } + /** + * Set the theme of the activity from the descriptor of the test suite. + * The color of the toolbar, the app bar, the tab layout and the status bar will be set to the color of the descriptor. + */ + private fun setThemeFromDescriptor() { + result.getDescriptor(this).get().let { desriptor -> + if (desriptor is OONIDescriptor<*>) { + val color = ContextCompat.getColor(this@ResultDetailActivity, desriptor.color) + binding.toolbar.setBackgroundColor(color) + binding.appBar.setBackgroundColor(color) + binding.tabLayout.setBackgroundColor(color) + window.statusBarColor = color + } + } + } + + /** + * Open the [TextActivity] or the [MeasurementDetailActivity] based on the test name of the measurement test name. + * If the test name is in the list of [OONITests.EXPERIMENTAL.nettests] or [OONITests.EXPERIMENTAL.longRunningTests], + * the [TextActivity] will be opened otherwise, the [MeasurementDetailActivity] will be opened. + * + * @param v The view that was clicked. + */ override fun onClick(v: View) { val measurement = v.tag as Measurement - if (Objects.equals(result.test_group_name, OONITests.EXPERIMENTAL.label)) startActivity( - TextActivity.newIntent( + + val textActivityItems = (OONITests.EXPERIMENTAL.nettests).map { it.name }.toMutableList() + (OONITests.EXPERIMENTAL.longRunningTests)?.map { it.name } + ?.let { textActivityItems.addAll(it) } + + if (textActivityItems.contains(measurement.test_name)) { + startActivity( + TextActivity.newIntent( + this, + TextActivity.TYPE_JSON, + measurement + ) + ) + } else { + ActivityCompat.startActivity( this, - TextActivity.TYPE_JSON, - measurement + MeasurementDetailActivity.newIntent(this, measurement.id), + null ) - ) else ActivityCompat.startActivity( - this, - MeasurementDetailActivity.newIntent(this, measurement.id), - null - ) + } } override fun onConfirmation(extra: Serializable, buttonClicked: Int) { diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java index d5401b045..228d75eb6 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/RunningActivity.java @@ -32,6 +32,7 @@ import java.io.Serializable; import java.util.ArrayList; +import java.util.Objects; import javax.inject.Inject; @@ -169,10 +170,11 @@ private void applyUIChanges(RunTestService service) { binding.eta.setText(R.string.Dashboard_Running_CalculatingETA); } - if (service.task.currentSuite.getName().equals(OONITests.EXPERIMENTAL.name())) + if (Objects.equals(service.task.currentTest.getLabelResId(),R.string.Test_Experimental_Fullname)) { binding.name.setText(service.task.currentTest.getName()); - else + } else { binding.name.setText(getString(service.task.currentTest.getLabelResId())); + } getWindow().setBackgroundDrawableResource(service.task.currentSuite.getColor()); if (Build.VERSION.SDK_INT >= 21) { getWindow().setStatusBarColor(service.task.currentSuite.getColor()); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt index aa690592e..59fe79b54 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/runtests/adapter/RunTestsExpandableListViewAdapter.kt @@ -162,8 +162,8 @@ class RunTestsExpandableListViewAdapter( val groupItem = getGroup(groupPosition) val nettest = AbstractTest.getTestByName(childItem.name) convertView.findViewById(R.id.child_name)?.apply { - text = when (groupItem.name) { - OONITests.EXPERIMENTAL.label -> { + text = when (nettest.labelResId) { + R.string.Test_Experimental_Fullname -> { childItem.name } diff --git a/app/src/main/res/layout/activity_result_detail.xml b/app/src/main/res/layout/activity_result_detail.xml index f75b15857..83726362c 100644 --- a/app/src/main/res/layout/activity_result_detail.xml +++ b/app/src/main/res/layout/activity_result_detail.xml @@ -6,6 +6,7 @@ android:layout_height="match_parent">