Skip to content

Commit

Permalink
Automated test ask modal (ooni#440)
Browse files Browse the repository at this point in the history
* Modal implementation

* Add functions after accepting auto test

* Change modal count
  • Loading branch information
lorenzoPrimi authored Jun 14, 2021
1 parent eff3fab commit 43f444f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ActivityCompat;

import com.google.android.material.bottomnavigation.BottomNavigationView;

Expand All @@ -29,6 +31,7 @@
public class MainActivity extends AbstractActivity implements ConfirmDialogFragment.OnConfirmedListener {
private static final String RES_ITEM = "resItem";
public static final String NOTIFICATION_DIALOG = "notification";
public static final String AUTOTEST_DIALOG = "automatic_testing";

@BindView(R.id.bottomNavigation)
BottomNavigationView bottomNavigation;
Expand Down Expand Up @@ -64,19 +67,31 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
});
bottomNavigation.setSelectedItemId(getIntent().getIntExtra(RES_ITEM, R.id.dashboard));
if (getPreferenceManager().getAppOpenCount() != 0
&& getPreferenceManager().getAppOpenCount() % PreferenceManager.AUTOTEST_DIALOG_COUNT == 0
&& !getPreferenceManager().isAutomaticTestEnabled()
&& !getPreferenceManager().isAskAutomaticTestDialogDisabled()) {
new ConfirmDialogFragment.Builder()
.withTitle(getString(R.string.Modal_Autorun_Modal_Title))
.withMessage(getString(R.string.Modal_Autorun_Modal_Text))
.withPositiveButton(getString(R.string.Modal_SoundsGreat))
.withNegativeButton(getString(R.string.Modal_NoThanks))
.withNeutralButton(getString(R.string.Modal_DontAskAgain))
.withExtra(AUTOTEST_DIALOG)
.build().show(getSupportFragmentManager(), null);
}
else if (getPreferenceManager().getAppOpenCount() != 0
&& getPreferenceManager().getAppOpenCount() % PreferenceManager.NOTIFICATION_DIALOG_COUNT == 0
&& !getPreferenceManager().isNotifications()
&& !getPreferenceManager().isAskNotificationDialogDisabled()) {
new ConfirmDialogFragment.Builder()
.withTitle(getString(R.string.Modal_EnableNotifications_Title))
.withMessage(getString(R.string.Modal_EnableNotifications_Paragraph))
.withPositiveButton(getString(R.string.Modal_OK))
.withPositiveButton(getString(R.string.Modal_SoundsGreat))
.withNegativeButton(getString(R.string.Modal_NoThanks))
.withNeutralButton(getString(R.string.Modal_DontAskAgain))
.withExtra(NOTIFICATION_DIALOG)
.build().show(getSupportFragmentManager(), null);
}

}

if (android.os.Build.VERSION.SDK_INT >= 29){
Expand Down Expand Up @@ -125,5 +140,47 @@ else if (i == DialogInterface.BUTTON_NEUTRAL){
getPreferenceManager().disableAskNotificationDialog();
}
}
if (extra.equals(AUTOTEST_DIALOG)) {
getPreferenceManager().setNotificationsFromDialog(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){
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 {
getPreferenceManager().enableAutomatedTesting();
ServiceUtil.scheduleJob(this);
}
}
else if (i == DialogInterface.BUTTON_NEUTRAL){
getPreferenceManager().disableAskAutomaticTestDialog();
}
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PreferenceManager.IGNORE_OPTIMIZATION_REQUEST) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//For API < 23 we ignore battery optimization
boolean isIgnoringBatteryOptimizations = true;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
}
if (isIgnoringBatteryOptimizations) {
getPreferenceManager().enableAutomatedTesting();
ServiceUtil.scheduleJob(this);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class PreferenceManager {
static final String GEO_VER = "geo_ver";
public static final Integer MAX_RUNTIME_DISABLED = -1;
private static final String IS_NOTIFICATION_DIALOG = "isNotificationDialog";
public static final int NOTIFICATION_DIALOG_COUNT = 5;
public static final int NOTIFICATION_DIALOG_COUNT = 7;
public static final int AUTOTEST_DIALOG_COUNT = 5;
private static final String NOTIFICATION_DIALOG_DISABLE = "isNotificationDialogDisabled";
private static final String NOTIFICATION_AUTOTEST_DISABLE = "isAutomaticTestDialogDisabled";
private static final String TOKEN = "token";
private static final String SHOW_ONBOARDING = "first_run";
//This is in ms, set to one day
Expand All @@ -34,6 +36,7 @@ public class PreferenceManager {
private static final String UUID4 = "uuid4";
public static final String AUTORUN_COUNT = "autorun_count";
public static final String AUTORUN_DATE = "autorun_last_date";
public static final int IGNORE_OPTIMIZATION_REQUEST = 15;

private final SharedPreferences sp;
private final Resources r;
Expand Down Expand Up @@ -104,12 +107,10 @@ public void disableAskNotificationDialog() {
.apply();
}


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


public boolean isDarkTheme() {
return sp.getBoolean(r.getString(R.string.theme_enabled), false);
}
Expand Down Expand Up @@ -289,10 +290,27 @@ public void setAppOpenCount(Long value){
sp.edit().putLong(IS_NOTIFICATION_DIALOG, value).apply();
}

/*
* This method is used to ask user to enable push notifications.
*/
public boolean isAskAutomaticTestDialogDisabled() {
return sp.getBoolean(NOTIFICATION_AUTOTEST_DISABLE, false);
}

public void disableAskAutomaticTestDialog() {
sp.edit().putBoolean(NOTIFICATION_AUTOTEST_DISABLE, true)
.apply();
}

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

public void enableAutomatedTesting() {
sp.edit().putBoolean(r.getString(R.string.automated_testing_enabled), true)
.apply();
}

public void disableAutomaticTest() {
sp.edit().putBoolean(r.getString(R.string.automated_testing_enabled), false)
.apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
public class PreferenceFragment extends ExtendedPreferenceFragment<PreferenceFragment> implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String ARG_PREFERENCES_RES_ID = "org.openobservatory.ooniprobe.fragment.PreferenceFragment.PREF_RES_ID";
private static final String ARG_CONTAINER_RES_ID = "org.openobservatory.ooniprobe.fragment.PreferenceFragment.CONTAINER_VIEW_ID";
public static final int IGNORE_OPTIMIZATION_REQUEST = 15;
private String rootKey;

public static PreferenceFragment newInstance(@XmlRes int preferencesResId, @IdRes int preferencesContainerResId, String rootKey) {
Expand Down Expand Up @@ -150,7 +149,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
startActivityForResult(intent, IGNORE_OPTIMIZATION_REQUEST);
startActivityForResult(intent, PreferenceManager.IGNORE_OPTIMIZATION_REQUEST);
}
else
ServiceUtil.scheduleJob(getContext());
Expand Down Expand Up @@ -233,7 +232,7 @@ protected PreferenceFragment newConcreteInstance(String rootKey) {
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IGNORE_OPTIMIZATION_REQUEST) {
if (requestCode == PreferenceManager.IGNORE_OPTIMIZATION_REQUEST) {
PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
//For API < 23 we ignore battery optimization
boolean isIgnoringBatteryOptimizations = true;
Expand Down

0 comments on commit 43f444f

Please sign in to comment.