diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManager.java b/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManager.java index 2e39f23b9..ba228a80b 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManager.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/PreferenceManager.java @@ -39,6 +39,7 @@ public class PreferenceManager { 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; + public static final int COUNT_WEBSITE_CATEGORIES = 31; public static final int ASK_UPDATE_APP = 16; private final SharedPreferences sp; @@ -264,6 +265,14 @@ public Integer countEnabledCategory() { return count; } + public void updateAllWebsiteCategories(boolean value) { + SharedPreferences.Editor ed = sp.edit(); + for (String key : r.getStringArray(R.array.CategoryCodes)){ + ed.putBoolean(key,value); + } + ed.apply(); + } + public boolean canCallDeleteJson(){ long lastCalled = sp.getLong(DELETE_JSON_KEY, 0); diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/PreferenceFragment.java b/app/src/main/java/org/openobservatory/ooniprobe/fragment/PreferenceFragment.java index d4d17b892..2135c214c 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/PreferenceFragment.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/PreferenceFragment.java @@ -1,5 +1,7 @@ package org.openobservatory.ooniprobe.fragment; +import static org.openobservatory.ooniprobe.common.PreferenceManager.COUNT_WEBSITE_CATEGORIES; + import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -10,9 +12,13 @@ import android.os.PowerManager; import android.provider.Settings; import android.text.TextUtils; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.widget.Toast; import androidx.annotation.IdRes; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.XmlRes; import androidx.preference.EditTextPreference; @@ -33,6 +39,8 @@ import java.util.Arrays; +import javax.inject.Inject; + import localhost.toolkit.app.fragment.MessageDialogFragment; import localhost.toolkit.preference.ExtendedPreferenceFragment; @@ -54,6 +62,7 @@ public static PreferenceFragment newInstance(@XmlRes int preferencesResId, @IdRe @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { this.rootKey = rootKey; + setHasOptionsMenu(true); } @Override @@ -226,4 +235,43 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d } } } + + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + PreferenceScreen ourPreferenceScreen = findPreference(getString(R.string.Settings_Websites_Categories_Label)); + if (getPreferenceScreen().equals(ourPreferenceScreen)) { + inflater.inflate(R.menu.website_categories, menu); + } + int enabledCategories = ((Application) getActivity().getApplication()).getPreferenceManager().countEnabledCategory(); + + if (enabledCategories>=COUNT_WEBSITE_CATEGORIES){ + MenuItem item = menu.findItem(R.id.selectAll); + if (item != null) { + item.setVisible(false); + } + } else if (enabledCategories<=0){ + MenuItem item = menu.findItem(R.id.selectNone); + if (item != null) { + item.setVisible(false); + } + } + + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + PreferenceManager preferenceManager = ((Application) getActivity().getApplication()).getPreferenceManager(); + int itemId = item.getItemId(); + if (itemId == R.id.selectAll) { + preferenceManager.updateAllWebsiteCategories(true); + getActivity().recreate(); + return true; + } else if (itemId == R.id.selectNone) { + preferenceManager.updateAllWebsiteCategories(false); + getActivity().recreate(); + return true; + } + return super.onOptionsItemSelected(item); + } } diff --git a/app/src/main/res/menu/website_categories.xml b/app/src/main/res/menu/website_categories.xml new file mode 100644 index 000000000..ce54ab80d --- /dev/null +++ b/app/src/main/res/menu/website_categories.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file