Skip to content

Commit

Permalink
Start implementation of add link flow for ooni run v2
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Dec 11, 2023
1 parent b13c5a0 commit b7b4536
Show file tree
Hide file tree
Showing 19 changed files with 881 additions and 61 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ android {
buildFeatures {
viewBinding = true
}
dataBinding {
enabled = true
enabledForTests = true
}
namespace 'org.openobservatory.ooniprobe'
}

Expand Down
29 changes: 29 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,36 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<action android:name="${applicationId}.nettest" />

<data
android:host="run.test.ooni.org"
android:pathPrefix="/v2"
android:scheme="https" />
</intent-filter>

<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="runv2"
android:scheme="ooni" />
</intent-filter>
</activity>
<activity
android:name=".activity.add_descriptor.AddDescriptorActivity"
android:exported="false"
android:parentActivityName=".activity.MainActivity"
android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.App.NoActionBar" />
<activity
android:name=".activity.OnboardingActivity"
android:screenOrientation="userPortrait"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.os.PowerManager;
import android.provider.Settings;

import android.view.View;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
Expand All @@ -22,10 +23,8 @@
import com.google.android.material.snackbar.Snackbar;

import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.common.Application;
import org.openobservatory.ooniprobe.common.NotificationUtility;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.common.ThirdPartyServices;
import org.openobservatory.ooniprobe.activity.add_descriptor.AddDescriptorActivity;
import org.openobservatory.ooniprobe.common.*;
import org.openobservatory.ooniprobe.common.service.ServiceUtil;
import org.openobservatory.ooniprobe.databinding.ActivityMainBinding;
import org.openobservatory.ooniprobe.domain.UpdatesNotificationManager;
Expand Down Expand Up @@ -54,28 +53,30 @@ public class MainActivity extends AbstractActivity implements ConfirmDialogFragm
@Inject
PreferenceManager preferenceManager;

@Inject
TestDescriptorManager descriptorManager;

private ActivityResultLauncher<String> requestPermissionLauncher;

public static Intent newIntent(Context context, int resItem) {
return new Intent(context, MainActivity.class).putExtra(RES_ITEM, resItem).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}

public static Intent newIntent(Context context, int resItem, String message) {
return new Intent(context, MainActivity.class)
.putExtra(RES_ITEM, resItem)
.putExtra(RES_SNACKBAR_MESSAGE, message)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
public static Intent newIntent(Context context, int resItem, String message) {
return new Intent(context, MainActivity.class)
.putExtra(RES_ITEM, resItem)
.putExtra(RES_SNACKBAR_MESSAGE, message)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}

@Override
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivityComponent().inject(this);
if (preferenceManager.isShowOnboarding()) {
startActivity(new Intent(MainActivity.this, OnboardingActivity.class));
finish();
}
else {
} else {
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.bottomNavigation.setOnItemSelectedListener(item -> {
Expand All @@ -96,15 +97,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
/* TODO(aanorbel): Fix change in state(theme change from notification) changes the selected item.
The proper fix would be to track the selected item as well as other properties in a `ViewModel`. */
binding.bottomNavigation.setSelectedItemId(getIntent().getIntExtra(RES_ITEM, R.id.dashboard));
/* Check if we are restoring the activity from a saved state first.
* If we have a message to show, show it as a snackbar.
* This is used to show the message from test completion.
*/
if (savedInstanceState == null && getIntent().hasExtra(RES_SNACKBAR_MESSAGE)) {
Snackbar.make(binding.getRoot(), getIntent().getStringExtra(RES_SNACKBAR_MESSAGE), Snackbar.LENGTH_SHORT)
.setAnchorView(binding.bottomNavigation)
.show();
}
/* Check if we are restoring the activity from a saved state first.
* If we have a message to show, show it as a snackbar.
* This is used to show the message from test completion.
*/
if (savedInstanceState == null && getIntent().hasExtra(RES_SNACKBAR_MESSAGE)) {
Snackbar.make(binding.getRoot(), getIntent().getStringExtra(RES_SNACKBAR_MESSAGE), Snackbar.LENGTH_SHORT)
.setAnchorView(binding.bottomNavigation)
.show();
}
if (notificationManager.shouldShowAutoTest()) {
new ConfirmDialogFragment.Builder()
.withTitle(getString(R.string.Modal_Autorun_Modal_Title))
Expand All @@ -128,16 +129,17 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
ThirdPartyServices.checkUpdates(this);
}

if (android.os.Build.VERSION.SDK_INT >= 29){
if (android.os.Build.VERSION.SDK_INT >= 29) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else{
} else {
if (preferenceManager.isDarkTheme()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
requestNotificationPermission();
onNewIntent(getIntent());
}

private void requestNotificationPermission() {
Expand All @@ -150,20 +152,20 @@ private void requestNotificationPermission() {
binding.getRoot(),
"Please grant Notification permission from App Settings",
Snackbar.LENGTH_LONG
).setAction(R.string.Settings_Title, view -> {
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
).setAction(R.string.Settings_Title, view -> {
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//for Android 5-7
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
//for Android 5-7
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);

// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());

startActivity(intent);
}).show();
startActivity(intent);
}).show();
}
}
);
Expand All @@ -181,16 +183,62 @@ private void requestNotificationPermission() {
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
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);
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
if (uri == null) return;

String host = uri.getHost();
long runId = 0;

try {
if ("runv2".equals(host)) {
runId = Long.parseLong(uri.getPathSegments().get(0));
} else if ("run.test.ooni.org".equals(host)) {
runId = Long.parseLong(uri.getPathSegments().get(1));
}
} catch (Exception e) {
e.printStackTrace();
}
if (runId == 0) return;
Snackbar.make(binding.getRoot(), getString(R.string.OONIBrowser_Loading), Snackbar.LENGTH_LONG).show();
TaskExecutor executor = new TaskExecutor();
long finalRunId = runId;
executor.executeTask(
() -> {
try {
return descriptorManager.fetchDescriptorFromRunId(finalRunId, this);
} catch (Exception exception) {
exception.printStackTrace();
ThirdPartyServices.logException(exception);
return null;
}
},
descriptorResponse -> {
if (descriptorResponse != null) {
//binding.progressIndicator.setVisibility(View.GONE);
startActivity(AddDescriptorActivity.newIntent(this, descriptorResponse));
} else {
//binding.progressIndicator.setVisibility(View.GONE);
//loadInvalidAttributes();
}
return null;
});


System.out.println(host);
System.out.println(runId);
} else {
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);
}
}
}
}
Expand All @@ -202,34 +250,31 @@ public void onConfirmation(Serializable extra, int i) {
notificationManager.getUpdates(i == DialogInterface.BUTTON_POSITIVE);

//If positive answer reload consents and init notification
if (i == DialogInterface.BUTTON_POSITIVE){
if (i == DialogInterface.BUTTON_POSITIVE) {
ThirdPartyServices.reloadConsents((Application) getApplication());
}
else if (i == DialogInterface.BUTTON_NEUTRAL){
} else if (i == DialogInterface.BUTTON_NEUTRAL) {
notificationManager.disableAskNotificationDialog();
}
}
if (extra.equals(AUTOTEST_DIALOG)) {
preferenceManager.setNotificationsFromDialog(i == DialogInterface.BUTTON_POSITIVE);
if (i == DialogInterface.BUTTON_POSITIVE){
if (i == DialogInterface.BUTTON_POSITIVE) {
//For API < 23 we ignore battery optimization
boolean isIgnoringBatteryOptimizations = true;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
}
if(!isIgnoringBatteryOptimizations){
if (!isIgnoringBatteryOptimizations) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PreferenceManager.IGNORE_OPTIMIZATION_REQUEST);
}
else {
} else {
preferenceManager.enableAutomatedTesting();
ServiceUtil.scheduleJob(this);
}
}
else if (i == DialogInterface.BUTTON_NEUTRAL){
} else if (i == DialogInterface.BUTTON_NEUTRAL) {
preferenceManager.disableAskAutomaticTestDialog();
}
}
Expand Down Expand Up @@ -257,17 +302,15 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
if (isIgnoringBatteryOptimizations) {
preferenceManager.enableAutomatedTesting();
ServiceUtil.scheduleJob(this);
}
else {
} else {
new ConfirmDialogFragment.Builder()
.withMessage(getString(R.string.Modal_Autorun_BatteryOptimization))
.withPositiveButton(getString(R.string.Modal_OK))
.withNegativeButton(getString(R.string.Modal_Cancel))
.withExtra(BATTERY_DIALOG)
.build().show(getSupportFragmentManager(), null);
}
}
else if (requestCode == PreferenceManager.ASK_UPDATE_APP) {
} else if (requestCode == PreferenceManager.ASK_UPDATE_APP) {
if (resultCode != RESULT_OK) {
//We don't need to check the result for now
}
Expand Down
Loading

0 comments on commit b7b4536

Please sign in to comment.