Skip to content

Commit

Permalink
added option to select/unselect all website categories (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel authored Jun 29, 2023
1 parent a358731 commit 00e0754
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -33,6 +39,8 @@

import java.util.Arrays;

import javax.inject.Inject;

import localhost.toolkit.app.fragment.MessageDialogFragment;
import localhost.toolkit.preference.ExtendedPreferenceFragment;

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/menu/website_categories.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/selectAll"
android:title="@string/Settings_Websites_Categories_Selection_All" />
<item
android:id="@+id/selectNone"
android:title="@string/Settings_Websites_Categories_Selection_None" />
</menu>

0 comments on commit 00e0754

Please sign in to comment.