From bcbb01c29d9a45c10a68f531e6671c8311f0f7e5 Mon Sep 17 00:00:00 2001 From: Norbel Ambanumben Date: Thu, 3 Aug 2023 17:49:04 +0100 Subject: [PATCH] Chore: Update `OverviewActivity` to `ViewBinding` --- .../ooniprobe/activity/OverviewActivity.java | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java b/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java index 415744384..d7b9c4997 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/activity/OverviewActivity.java @@ -5,18 +5,14 @@ import android.os.Bundle; import android.text.format.DateUtils; import android.view.View; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.TextView; import androidx.annotation.Nullable; -import androidx.appcompat.widget.Toolbar; -import androidx.core.app.ActivityCompat; import androidx.core.text.TextUtilsCompat; import androidx.core.view.ViewCompat; import org.openobservatory.ooniprobe.R; import org.openobservatory.ooniprobe.common.PreferenceManager; +import org.openobservatory.ooniprobe.databinding.ActivityOverviewBinding; import org.openobservatory.ooniprobe.model.database.Result; import org.openobservatory.ooniprobe.test.suite.AbstractSuite; import org.openobservatory.ooniprobe.test.suite.ExperimentalSuite; @@ -27,20 +23,12 @@ import javax.inject.Inject; -import butterknife.BindView; -import butterknife.ButterKnife; -import butterknife.OnClick; import ru.noties.markwon.Markwon; public class OverviewActivity extends AbstractActivity { private static final String TEST = "test"; - @BindView(R.id.toolbar) Toolbar toolbar; - @BindView(R.id.icon) ImageView icon; - @BindView(R.id.runtime) TextView runtime; - @BindView(R.id.lastTime) TextView lastTime; - @BindView(R.id.desc) TextView desc; - @BindView(R.id.customUrl) Button customUrl; - @BindView(R.id.run) Button run; + + ActivityOverviewBinding binding; private AbstractSuite testSuite; @Inject @@ -55,16 +43,16 @@ public static Intent newIntent(Context context, AbstractSuite testSuite) { getActivityComponent().inject(this); testSuite = (AbstractSuite) getIntent().getSerializableExtra(TEST); setTheme(testSuite.getThemeLight()); - setContentView(R.layout.activity_overview); - ButterKnife.bind(this); - setSupportActionBar(toolbar); + binding = ActivityOverviewBinding.inflate(getLayoutInflater()); + setContentView(binding.getRoot()); + setSupportActionBar(binding.toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setTitle(testSuite.getTitle()); - icon.setImageResource(testSuite.getIcon()); - customUrl.setVisibility(testSuite.getName().equals(WebsitesSuite.NAME) ? View.VISIBLE : View.GONE); + binding.icon.setImageResource(testSuite.getIcon()); + binding.customUrl.setVisibility(testSuite.getName().equals(WebsitesSuite.NAME) ? View.VISIBLE : View.GONE); if(testSuite.isTestEmpty(preferenceManager)){ - run.setAlpha(0.5F); - run.setEnabled(false); + binding.run.setAlpha(0.5F); + binding.run.setEnabled(false); } if (testSuite.getName().equals(ExperimentalSuite.NAME)) { String experimentalLinks = @@ -72,24 +60,31 @@ public static Intent newIntent(Context context, AbstractSuite testSuite) { "\n\n* [DNS Check](https://github.com/ooni/spec/blob/master/nettests/ts-028-dnscheck.md)" + "\n\n* [Tor Snowflake](https://ooni.org/nettest/tor-snowflake/) "+ String.format(" ( %s )",getString(R.string.Settings_TestOptions_LongRunningTest))+ "\n\n* [Vanilla Tor](https://github.com/ooni/spec/blob/master/nettests/ts-016-vanilla-tor.md) " + String.format(" ( %s )",getString(R.string.Settings_TestOptions_LongRunningTest)); - Markwon.setMarkdown(desc, getString(testSuite.getDesc1(), experimentalLinks)); + Markwon.setMarkdown(binding.desc, getString(testSuite.getDesc1(), experimentalLinks)); if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL) - desc.setTextDirection(View.TEXT_DIRECTION_RTL); + binding.desc.setTextDirection(View.TEXT_DIRECTION_RTL); } else - Markwon.setMarkdown(desc, getString(testSuite.getDesc1())); + Markwon.setMarkdown(binding.desc, getString(testSuite.getDesc1())); Result lastResult = Result.getLastResult(testSuite.getName()); if (lastResult == null) - lastTime.setText(R.string.Dashboard_Overview_LastRun_Never); + binding.lastTime.setText(R.string.Dashboard_Overview_LastRun_Never); else - lastTime.setText(DateUtils.getRelativeTimeSpanString(lastResult.start_time.getTime())); + binding.lastTime.setText(DateUtils.getRelativeTimeSpanString(lastResult.start_time.getTime())); + + setUpOnCLickListeners(); + } + + private void setUpOnCLickListeners() { + binding.run.setOnClickListener(view -> onRunClick()); + binding.customUrl.setOnClickListener(view -> customUrlClick()); } @Override protected void onResume() { super.onResume(); testSuite.setTestList((AbstractTest[]) null); testSuite.getTestList(preferenceManager); - runtime.setText(getString(R.string.twoParam, getString(testSuite.getDataUsage()), getString(R.string.Dashboard_Card_Seconds, testSuite.getRuntime(preferenceManager).toString()))); + binding.runtime.setText(getString(R.string.twoParam, getString(testSuite.getDataUsage()), getString(R.string.Dashboard_Card_Seconds, testSuite.getRuntime(preferenceManager).toString()))); } @Override @@ -98,13 +93,13 @@ public boolean onSupportNavigateUp() { return true; } - @OnClick(R.id.run) void onRunClick() { + void onRunClick() { if(!testSuite.isTestEmpty(preferenceManager)){ RunningActivity.runAsForegroundService(this, testSuite.asArray(), this::bindTestService, preferenceManager); } } - @OnClick(R.id.customUrl) void customUrlClick() { + void customUrlClick() { startActivity(new Intent(this, CustomWebsiteActivity.class)); } }