Skip to content

Commit

Permalink
Merge branch 'dev/design-update' of github.com:ooni/probe-android int…
Browse files Browse the repository at this point in the history
…o chore/migrate-dashboard-and-overview-to-descriptors
  • Loading branch information
aanorbel committed Nov 26, 2023
2 parents d2dd31b + 0bf531c commit 369adfa
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 285 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
</activity>
<activity
android:name=".activity.OoniRunActivity"
android:label="@string/APP_NAME"
android:label=""
android:exported="true"
android:launchMode="singleTop"
android:parentActivityName=".activity.MainActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,173 +0,0 @@
package org.openobservatory.ooniprobe.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;

import org.openobservatory.ooniprobe.BuildConfig;
import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.databinding.ActivityOonirunBinding;
import org.openobservatory.ooniprobe.domain.GetTestSuite;
import org.openobservatory.ooniprobe.domain.VersionCompare;
import org.openobservatory.ooniprobe.domain.models.Attribute;
import org.openobservatory.ooniprobe.item.TextItem;
import org.openobservatory.ooniprobe.test.suite.AbstractSuite;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerAdapter;
import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem;

public class OoniRunActivity extends AbstractActivity {
ActivityOonirunBinding binding;
private ArrayList<HeterogeneousRecyclerItem> items;
private HeterogeneousRecyclerAdapter<HeterogeneousRecyclerItem> adapter;

@Inject
PreferenceManager preferenceManager;

@Inject
VersionCompare versionCompare;

@Inject
GetTestSuite getSuite;

@Inject
Gson gson;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivityComponent().inject(this);
binding = ActivityOonirunBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
binding.recycler.setLayoutManager(layoutManager);
binding.recycler.addItemDecoration(new DividerItemDecoration(this, layoutManager.getOrientation()));
items = new ArrayList<>();
adapter = new HeterogeneousRecyclerAdapter<>(this, items);
binding.recycler.setAdapter(adapter);
manageIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
manageIntent(intent);
}

private void manageIntent(Intent intent) {
if (isTestRunning()) {
Toast.makeText(this, getString(R.string.OONIRun_TestRunningError), Toast.LENGTH_LONG).show();
finish();
}
else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
String mv = uri == null ? null : uri.getQueryParameter("mv");
String tn = uri == null ? null : uri.getQueryParameter("tn");
String ta = uri == null ? null : uri.getQueryParameter("ta");
loadScreen(mv, tn, ta);
}
else if (Intent.ACTION_SEND.equals(intent.getAction())) {
String url = intent.getStringExtra(Intent.EXTRA_TEXT);
if (url != null && Patterns.WEB_URL.matcher(url).matches()) {
List<String> urls = Collections.singletonList(url);
AbstractSuite suite = getSuite.get("web_connectivity", urls);
if (suite != null) {
loadSuite(suite, urls);
} else {
loadInvalidAttributes();
}
} else {
loadInvalidAttributes();
}
}
}

private void loadScreen(String mv, String tn, String ta){
String[] split = BuildConfig.VERSION_NAME.split("-");
String version_name = split[0];
if (mv != null && tn != null) {
if (versionCompare.compare(version_name, mv) >= 0) {
try {
Attribute attribute = gson.fromJson(ta, Attribute.class);
List<String> urls = (attribute!=null && attribute.urls != null) ? attribute.urls : null;
AbstractSuite suite = getSuite.get(tn, urls);
if (suite != null) {
loadSuite(suite, urls);
} else {
loadInvalidAttributes();
}
} catch (Exception e) {
loadInvalidAttributes();
}
} else {
loadOutOfDate();
}
} else {
loadInvalidAttributes();
}
}

private void loadOutOfDate() {
binding.title.setText(R.string.OONIRun_OONIProbeOutOfDate);
binding.desc.setText(R.string.OONIRun_OONIProbeNewerVersion);
binding.run.setText(R.string.OONIRun_Update);
binding.icon.setImageResource(R.drawable.update);
binding.iconBig.setImageResource(R.drawable.update);
binding.iconBig.setVisibility(View.VISIBLE);
binding.run.setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName())));
finish();
});
}

private void loadSuite(AbstractSuite suite, List<String> urls) {
binding.icon.setImageResource(suite.getIcon());
binding.title.setText(suite.getTestList(preferenceManager)[0].getLabelResId());
binding.desc.setText(getString(R.string.OONIRun_YouAreAboutToRun));
if (urls != null) {
for (String url : urls) {
if (URLUtil.isValidUrl(url))
items.add(new TextItem(url));
}
adapter.notifyTypesChanged();
binding.iconBig.setVisibility(View.GONE);
} else {
binding.iconBig.setImageResource(suite.getIcon());
binding.iconBig.setVisibility(View.VISIBLE);
}
binding.run.setOnClickListener(v -> {

RunningActivity.runAsForegroundService(OoniRunActivity.this, suite.asArray(),this::finish, preferenceManager);

});
}

private void loadInvalidAttributes() {
binding.title.setText(R.string.OONIRun_InvalidParameter);
binding.desc.setText(R.string.OONIRun_InvalidParameter_Msg);
binding.run.setText(R.string.OONIRun_Close);
binding.icon.setImageResource(R.drawable.question_mark);
binding.iconBig.setImageResource(R.drawable.question_mark);
binding.iconBig.setVisibility(View.VISIBLE);
binding.run.setOnClickListener(v -> finish());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package org.openobservatory.ooniprobe.activity

import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.util.Patterns
import android.webkit.URLUtil
import android.widget.Toast
import androidx.core.graphics.ColorUtils
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import org.openobservatory.ooniprobe.BuildConfig
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.adapters.StringListRecyclerViewAdapter
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.databinding.ActivityOonirunBinding
import org.openobservatory.ooniprobe.domain.GetTestSuite
import org.openobservatory.ooniprobe.domain.VersionCompare
import org.openobservatory.ooniprobe.domain.models.Attribute
import org.openobservatory.ooniprobe.test.suite.AbstractSuite
import javax.inject.Inject

class OoniRunActivity : AbstractActivity() {
lateinit var binding: ActivityOonirunBinding

@Inject
lateinit var preferenceManager: PreferenceManager

@Inject
lateinit var versionCompare: VersionCompare

@Inject
lateinit var getSuite: GetTestSuite

@Inject
lateinit var gson: Gson
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityComponent.inject(this)
binding = ActivityOonirunBinding.inflate(
layoutInflater
)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}
manageIntent(intent)
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
manageIntent(intent)
}

private fun manageIntent(intent: Intent) {
if (isTestRunning) {
Toast.makeText(this, getString(R.string.OONIRun_TestRunningError), Toast.LENGTH_LONG)
.show()
finish()
} else if (Intent.ACTION_VIEW == intent.action) {
val uri = intent.data
val mv = uri?.getQueryParameter("mv")
val tn = uri?.getQueryParameter("tn")
val ta = uri?.getQueryParameter("ta")
loadScreen(mv, tn, ta)
} else if (Intent.ACTION_SEND == intent.action) {
val url = intent.getStringExtra(Intent.EXTRA_TEXT)
if (url != null && Patterns.WEB_URL.matcher(url).matches()) {
val urls = listOf(url)
val suite = getSuite["web_connectivity", urls]
if (suite != null) {
loadSuite(suite, urls)
} else {
loadInvalidAttributes()
}
} else {
loadInvalidAttributes()
}
}
}

private fun loadScreen(mv: String?, tn: String?, ta: String?) {
val split = BuildConfig.VERSION_NAME.split("-".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()
val versionName = split.first()
if (mv != null && tn != null) {
if (versionCompare.compare(versionName, mv) >= 0) {
try {
val attribute = gson.fromJson(ta, Attribute::class.java)
val urls = attribute?.urls
val suite = getSuite[tn, urls]
if (suite != null) {
loadSuite(suite, urls)
} else {
loadInvalidAttributes()
}
} catch (e: Exception) {
loadInvalidAttributes()
}
} else {
loadOutOfDate()
}
} else {
loadInvalidAttributes()
}
}

private fun loadOutOfDate() {
setThemeColor(resources.getColor(R.color.color_gray4))
setTextColor(Color.BLACK)
binding.title.setText(R.string.OONIRun_OONIProbeOutOfDate)
binding.desc.setText(R.string.OONIRun_OONIProbeNewerVersion)
binding.icon.setImageResource(R.drawable.update)
binding.run.apply {
setText(R.string.OONIRun_Update)
setOnClickListener {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$packageName")
)
)
finish()
}
}
}

private fun loadSuite(suite: AbstractSuite, urls: List<String>?) {
val items = ArrayList<String>()
binding.icon.setImageResource(suite.icon)
binding.title.setText(suite.getTestList(preferenceManager).first().labelResId)
binding.desc.text = getString(R.string.OONIRun_YouAreAboutToRun)
urls?.let { urls ->
urls.filterTo(items) { URLUtil.isValidUrl(it) }
binding.recycler.apply {
layoutManager = LinearLayoutManager(this@OoniRunActivity)
adapter = StringListRecyclerViewAdapter(items)
}
}
setThemeColor(resources.getColor(suite.color))
binding.run.setOnClickListener {
RunningActivity.runAsForegroundService(
this@OoniRunActivity, suite.asArray(), { finish() }, preferenceManager
)
}
}

private fun setThemeColor(color: Int) {
val window = window
window.statusBarColor = color
binding.appbarLayout.setBackgroundColor(color)
when {
ColorUtils.calculateLuminance(color) > 0.5 -> {
setTextColor(Color.WHITE)
}

else -> {
setTextColor(Color.WHITE)
}
}
}

private fun setTextColor(color: Int) {
binding.title.setTextColor(color)
binding.icon.setColorFilter(color)
binding.desc.setTextColor(color)
binding.run.apply {
setTextColor(color)
strokeColor = ColorStateList.valueOf(color)
}
}

private fun loadInvalidAttributes() {
setThemeColor(resources.getColor(R.color.color_gray4))
setTextColor(Color.BLACK)
binding.title.setText(R.string.OONIRun_InvalidParameter)
binding.desc.setText(R.string.OONIRun_InvalidParameter_Msg)
binding.icon.setImageResource(R.drawable.question_mark)
binding.run.apply {
setText(R.string.OONIRun_Close)
setOnClickListener { finish() }
}
}
}
Loading

0 comments on commit 369adfa

Please sign in to comment.