Skip to content

Commit

Permalink
added notification prompt for test progress
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Aug 22, 2023
1 parent 6e1bed8 commit a76e563
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import javax.inject.Inject;

import butterknife.ButterKnife;
import localhost.toolkit.app.fragment.ConfirmDialogFragment;

public class MainActivity extends AbstractActivity implements ConfirmDialogFragment.OnConfirmedListener {
Expand Down Expand Up @@ -57,7 +56,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
finish();
}
else {
ButterKnife.bind(this);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.bottomNavigation.setOnItemSelectedListener(item -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ private void setUpClickListeners() {
OnPromptAction actions;
switch (prompt) {
case CENSORSHIP_CONSENT:
actions = new ConsentActions();
actions = new InternetCensorshipConsentActions();
break;
case TEST_PROGRESS_CONSENT:
actions = new ConsentActions();
actions = new TestProgressNotificationConsentActions();
break;
default:
actions = new OnPromptAction() {
Expand All @@ -105,15 +105,18 @@ public void onClickNegative(View view) {
}

public void requestNotificationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
}
}

enum Prompt {
CENSORSHIP_CONSENT(R.string.Modal_EnableNotifications_Title, R.string.Modal_EnableNotifications_Paragraph), TEST_PROGRESS_CONSENT(R.string.Modal_EnableNotifications_Title, R.string.Modal_EnableNotifications_Paragraph);
CENSORSHIP_CONSENT(R.string.Modal_EnableNotifications_Title, R.string.Modal_EnableNotifications_Paragraph),
TEST_PROGRESS_CONSENT(R.string.Prompt_EnableTestProgressNotifications_Title, R.string.Prompt_EnableTestProgressNotifications_Paragraph);

private final int title;
private final int paragraph;
Expand Down Expand Up @@ -147,12 +150,19 @@ interface OnPromptAction {
void onClickNegative(View view);
}

public class ConsentActions implements OnPromptAction {
public class InternetCensorshipConsentActions implements OnPromptAction {

@Override
public void onClickPositive(View view) {
notificationManager.getUpdates(true);
PromptActivity.this.requestNotificationPermission();
if (ContextCompat.checkSelfPermission(
PromptActivity.this,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED){
PromptActivity.this.requestNotificationPermission();
} else {
setResult(Activity.RESULT_OK);
finish();
}
}

@Override
Expand All @@ -168,4 +178,32 @@ public void onClickNegative(View view) {
}
}

public class TestProgressNotificationConsentActions implements OnPromptAction {

@Override
public void onClickPositive(View view) {
notificationManager.setTestProgressNotificationConsent(true);
if (ContextCompat.checkSelfPermission(
PromptActivity.this,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED){
PromptActivity.this.requestNotificationPermission();
} else {
setResult(Activity.RESULT_OK);
finish();
}
}

@Override
public void onClickNeutral(View view) {
PromptActivity.this.setResult(Activity.RESULT_CANCELED);
PromptActivity.this.finish();
}

@Override
public void onClickNegative(View view) {
notificationManager.disableAskTestProgressNotificationConsent();
onClickNeutral(view);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.airbnb.lottie.LottieAnimationView;
Expand Down Expand Up @@ -94,6 +93,11 @@ public static void runAsForegroundService(AbstractActivity context,
ArrayList<AbstractSuite> testSuites,
OnTestServiceStartedListener onTestServiceStartedListener,
PreferenceManager iPreferenceManager) {

if (iPreferenceManager.shouldShowTestProgressConsent()){
context.startActivity(PromptActivity.newIntent(context, PromptActivity.Prompt.TEST_PROGRESS_CONSENT));
}

if (ReachabilityManager.getNetworkType(context).equals(ReachabilityManager.NO_INTERNET)) {
new MessageDialogFragment.Builder()
.withTitle(context.getString(R.string.Modal_Error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class PreferenceManager {
public static final int NOTIFICATION_DIALOG_COUNT = 7;
public static final int AUTOTEST_DIALOG_COUNT = 5;
static final String NOTIFICATION_DIALOG_DISABLE = "isNotificationDialogDisabled";
static final String TEST_PROGRESS_NOTIFICATION_DISABLE = "isTestProgressNotificationDisabled";
private static final String AUTOTEST_DIALOG_DISABLE = "isAutomaticTestDialogDisabled";
private static final String TOKEN = "token";
static final String SHOW_ONBOARDING = "first_run";
Expand Down Expand Up @@ -115,6 +116,31 @@ public boolean isNotifications() {
return sp.getBoolean(r.getString(R.string.notifications_enabled), false);
}

public boolean isTestProgressNotifications() {
return sp.getBoolean(r.getString(R.string.test_progress_notifications_enabled), false);
}

public boolean isAskTestProgressNotificationConsent() {
return sp.getBoolean(TEST_PROGRESS_NOTIFICATION_DISABLE, false);
}

public void disableAskTestProgressNotificationConsent() {
sp.edit().putBoolean(TEST_PROGRESS_NOTIFICATION_DISABLE, true)
.apply();
}

public void setTestProgressNotificationConsent(boolean enabled) {
//set notification value and increment app open
sp.edit()
.putBoolean(r.getString(R.string.test_progress_notifications_enabled), enabled)
.apply();
}

public boolean shouldShowTestProgressConsent() {
return !isTestProgressNotifications()
&& !isAskTestProgressNotificationConsent();
}

public boolean isDarkTheme() {
return sp.getBoolean(r.getString(R.string.theme_enabled), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public void getUpdates(boolean notificationUpdates) {
public void disableAskNotificationDialog() {
pm.disableAskNotificationDialog();
}

public void disableAskTestProgressNotificationConsent() {
pm.disableAskTestProgressNotificationConsent();
}

public void setTestProgressNotificationConsent(boolean enabled) {
pm.setTestProgressNotificationConsent(enabled);
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_prompt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:layout_width="250dp"
android:layout_height="250dp"
android:paddingTop="50dp"
android:paddingBottom="20dp"
android:paddingBottom="50dp"
android:src="@drawable/ooni_phone"
android:contentDescription="@string/APP_NAME"
app:layout_constraintBaseline_toTopOf="parent"
Expand Down Expand Up @@ -74,7 +74,7 @@
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Modal_DontAskAgain"
android:text="@string/Prompt_DontAskAgain"
android:textAllCaps="false"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,7 @@
<string name="CategoryCode_CTRL_Description">Benign or innocuous content used for control</string>
<string name="CategoryCode_IGO_Description">Intergovernmental organizations including The United Nations</string>
<string name="CategoryCode_MISC_Description">Sites that haven\'t been categorized yet</string>
<string name="Prompt_DontAskAgain">Don’t ask again</string>
<string name="Prompt_EnableTestProgressNotifications_Title">Enable test progress notifications</string>
<string name="Prompt_EnableTestProgressNotifications_Paragraph">Would you like to enable notifications on OONI Probe test progress and display running tests in the notifications drawer?</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/untraslatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<string name="performance_datausage" translatable="false">5 - 200 MB</string>

<string name="notifications_enabled" translatable="false">notifications_enabled</string>
<string name="test_progress_notifications_enabled" translatable="false">test_progress_notifications_enabled</string>
<string name="notifications_completion" translatable="false">notifications_completion</string>
<string name="notifications_news" translatable="false">notifications_news</string>
<string name="automated_testing_enabled" translatable="false">automated_testing_enabled</string>
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/xml/preferences_global.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
android:title="@string/Settings_Notifications_Label">
<SwitchPreferenceCompat
android:defaultValue="false"
app:iconSpaceReserved="false"
android:key="@string/notifications_enabled"
android:title="@string/Settings_Notifications_Enabled" />
android:title="@string/Modal_EnableNotifications_Title" />
<SwitchPreferenceCompat
android:defaultValue="false"
app:iconSpaceReserved="false"
android:key="@string/test_progress_notifications_enabled"
android:title="@string/Prompt_EnableTestProgressNotifications_Title" />
<PreferenceCategory
android:title="@string/Modal_EnableNotifications_Paragraph"
app:iconSpaceReserved="false" />
Expand Down

0 comments on commit a76e563

Please sign in to comment.