Skip to content

Commit

Permalink
Merge branch 'issues/2595' 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 Feb 13, 2024
2 parents cc9f5a2 + d096681 commit 5830cf0
Show file tree
Hide file tree
Showing 91 changed files with 1,042 additions and 168 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.oonirun.OoniRunV2Activity"
android:exported="true"
android:parentActivityName=".activity.MainActivity"
android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.App.NoActionBar">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,171 +306,22 @@ private void requestNotificationPermission() {
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
/**
* Check if we are starting the activity from a link [Intent.ACTION_VIEW].
* This is invoked when a v2 link is opened.
* @see {@link org.openobservatory.ooniprobe.activity.OoniRunActivity#newIntent}. for v1 links.
* Check if we are starting the activity with an intent extra.
* This is invoked when we are starting the activity from a notification or
* when the activity is launched from the onboarding fragment
* @see {@link org.openobservatory.ooniprobe.fragment.onboarding.Onboarding3Fragment#masterClick}.
*/
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
// If the intent does not contain a link, do nothing.
if (uri == null) {
return;
}

Optional<Long> possibleRunId = getRunId(uri);

// If the intent contains a link, but it is not a supported link or has a non-numerical `link_id`.
if (!possibleRunId.isPresent()) {
return;
}

// If the intent contains a link, but the `link_id` is zero.
if (possibleRunId.get() == 0) {
return;
}

TaskExecutor executor = new TaskExecutor();

displayAddLinkProgressFragment(executor);

executor.executeTask(() -> {
try {
return descriptorManager.fetchDescriptorFromRunId(possibleRunId.get(), this);
} catch (Exception exception) {
exception.printStackTrace();
ThirdPartyServices.logException(exception);
return null;
}
}, this::fetchDescriptorComplete);
} else {
/**
* Check if we are starting the activity with an intent extra.
* This is invoked when we are starting the activity from a notification or
* when the activity is launched from the onboarding fragment
* @see {@link org.openobservatory.ooniprobe.fragment.onboarding.Onboarding3Fragment#masterClick}.
*/
if (intent.getExtras() != null) {
if (intent.getExtras().containsKey(RES_ITEM)) {
binding.bottomNavigation.setSelectedItemId(intent.getIntExtra(RES_ITEM, R.id.dashboard));
} else if (intent.getExtras().containsKey(NOTIFICATION_DIALOG)) {
new ConfirmDialogFragment.Builder()
.withTitle(intent.getExtras().getString("title"))
.withMessage(intent.getExtras().getString("message"))
.withNegativeButton("")
.withPositiveButton(getString(R.string.Modal_OK))
.build().show(getSupportFragmentManager(), null);
}
}
}
}

/**
* The task to fetch the descriptor from the link is completed.
* <p>
* This method is called when the `fetchDescriptorFromRunId` task is completed.
* The `descriptorResponse` is the result of the task.
* If the task is successful, the `descriptorResponse` is the descriptor.
* Otherwise, the `descriptorResponse` is null.
* <p>
* If the `descriptorResponse` is not null, start the `AddDescriptorActivity`.
* Otherwise, show an error message.
*
* @param descriptorResponse The result of the task.
* @return null.
*/
private Unit fetchDescriptorComplete(TestDescriptor descriptorResponse) {
if (descriptorResponse != null) {
startActivity(AddDescriptorActivity.newIntent(this, descriptorResponse));
} else {
// TODO(aanorbel): Provide a better error message.
Snackbar.make(binding.getRoot(), R.string.Modal_Error, Snackbar.LENGTH_LONG)
.setAnchorView(binding.bottomNavigation) // NOTE:To avoid the `snackbar` from covering the bottom navigation.
.show();
}
removeProgressFragment(R.id.dynamic_progress_fragment);
return Unit.INSTANCE;

}

/**
* Display the progress fragment.
* <p>
* The progress fragment is used to display the progress of the task.
* e.g. Fetching the descriptor from the link.
*
* @param executor The executor that will be used to execute the task.
*/
private void displayAddLinkProgressFragment(TaskExecutor executor) {
binding.dynamicProgressFragment.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction()
.add(
R.id.dynamic_progress_fragment,
OONIRunDynamicProgressBar.newInstance(ProgressType.ADD_LINK, new OnActionListener() {
@Override
public void onActionButtonCLicked() {
executor.cancelTask();
removeProgressFragment(R.id.dynamic_progress_fragment);
}

@Override
public void onCloseButtonClicked() {
removeProgressFragment(R.id.dynamic_progress_fragment);
}
}),
OONIRunDynamicProgressBar.getTAG()
).commit();
}

/**
* Extracts the run id from the provided Uri.
* The run id can be in two different formats:
* <p>
* 1. ooni://runv2/link_id
* 2. https://run.test.ooni.org/v2/link_id
* <p>
* The run id is the `link_id` in the link.
* If the Uri contains a link, but the `link_id` is not a number, an empty Optional is returned.
* If the Uri contains a link, but it is not a supported link, an empty Optional is returned.
* <p>
*
* @param uri The Uri data.
* @return An Optional containing the run id if the Uri contains a link with a valid `link_id`, or an empty Optional otherwise.
*/
private Optional<Long> getRunId(Uri uri) {
String host = uri.getHost();

try {
if ("runv2".equals(host)) {
/**
* The run id is the first segment of the path.
* Launched when `Open Link in OONI Probe` is clicked.
* e.g. ooni://runv2/link_id
*/
return Optional.of(
Long.parseLong(
uri.getPathSegments().get(0)
)
);
} else if ("run.test.ooni.org".equals(host)) {
/**
* The run id is the second segment of the path.
* Launched when the system recognizes this app can open this link
* and launches the app when a link is clicked.
* e.g. https://run.test.ooni.org/v2/link_id
*/
return Optional.of(
Long.parseLong(
uri.getPathSegments().get(1)
)
);
} else {
// If the intent contains a link, but it is not a supported link.
return Optional.absent();
if (intent.getExtras() != null) {
if (intent.getExtras().containsKey(RES_ITEM)) {
binding.bottomNavigation.setSelectedItemId(intent.getIntExtra(RES_ITEM, R.id.dashboard));
} else if (intent.getExtras().containsKey(NOTIFICATION_DIALOG)) {
new ConfirmDialogFragment.Builder()
.withTitle(intent.getExtras().getString("title"))
.withMessage(intent.getExtras().getString("message"))
.withNegativeButton("")
.withPositiveButton(getString(R.string.Modal_OK))
.build().show(getSupportFragmentManager(), null);
}
} catch (Exception e) {
// If the intent contains a link, but the `link_id` is not a number.
e.printStackTrace();
return Optional.absent();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import org.openobservatory.engine.OONIRunDescriptor
import org.openobservatory.engine.OONIRunNettest
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.AbstractActivity
import org.openobservatory.ooniprobe.activity.MainActivity
import org.openobservatory.ooniprobe.activity.adddescriptor.adapter.AddDescriptorExpandableListAdapter
import org.openobservatory.ooniprobe.activity.adddescriptor.adapter.GroupedItem
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.common.ReadMorePlugin
import org.openobservatory.ooniprobe.common.StringUtils
import org.openobservatory.ooniprobe.common.TestDescriptorManager
import org.openobservatory.ooniprobe.databinding.ActivityAddDescriptorBinding
import org.openobservatory.ooniprobe.model.database.TestDescriptor
Expand Down Expand Up @@ -87,7 +89,13 @@ class AddDescriptorActivity : AbstractActivity() {
fun setImageViewResource(imageView: ImageView, iconName: String?) {
/* TODO(aanorbel): Update to parse the icon name and set the correct icon.
* Remember to ignore icons generated when generated doing this.*/
imageView.setImageResource(R.drawable.ooni_empty_state)
imageView.setImageResource(
imageView.context.resources.getIdentifier(
StringUtils.camelToSnake(
iconName
), "drawable", imageView.context.packageName
)
)
}

}
Expand Down Expand Up @@ -191,4 +199,14 @@ class AddDescriptorActivity : AbstractActivity() {
else -> super.onOptionsItemSelected(item)
}
}

override fun onBackPressed() {
startActivity(MainActivity.newIntent(applicationContext, R.id.dashboard))
super.onBackPressed()
}

override fun finish() {
startActivity(MainActivity.newIntent(applicationContext, R.id.dashboard))
super.finish()
}
}
Loading

0 comments on commit 5830cf0

Please sign in to comment.