Prefill "prefixes to keep"
This commit is contained in:
parent
1be56ae4a0
commit
1e7a634f0d
|
@ -7,7 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
Nothing.
|
||||
### Changed
|
||||
|
||||
- \[Advanced\] The "prefixes to keep" parameter (of database filtering) is prefilled (once) based on recent calls.
|
||||
|
||||
|
||||
## [0.5.15] - 2021-06-07
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package dummydomain.yetanothercallblocker;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.AsyncTask;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.preference.EditTextPreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dummydomain.yetanothercallblocker.data.YacbHolder;
|
||||
|
@ -22,6 +23,8 @@ public class DbFilteringSettingsFragment extends BaseSettingsFragment {
|
|||
|
||||
private final Settings settings = App.getSettings();
|
||||
|
||||
private AsyncTask<Void, Void, List<String>> prefillPrefixesTask;
|
||||
|
||||
@Override
|
||||
protected String getScreenKey() {
|
||||
return PREF_SCREEN_DB_FILTERING;
|
||||
|
@ -34,6 +37,14 @@ public class DbFilteringSettingsFragment extends BaseSettingsFragment {
|
|||
|
||||
@Override
|
||||
protected void initScreen() {
|
||||
if (!settings.isDbFilteringPrefixesPrefilled()) {
|
||||
settings.setDbFilteringPrefixesPrefilled(true);
|
||||
|
||||
if (TextUtils.isEmpty(settings.getDbFilteringPrefixesToKeep())) {
|
||||
startPrefillPrefixesTask();
|
||||
}
|
||||
}
|
||||
|
||||
requirePreference(PREF_INFO).setOnPreferenceClickListener(pref -> {
|
||||
new AlertDialog.Builder(requireActivity())
|
||||
.setTitle(R.string.settings_screen_db_filtering)
|
||||
|
@ -46,11 +57,8 @@ public class DbFilteringSettingsFragment extends BaseSettingsFragment {
|
|||
setPrefChangeListener(PREF_DB_FILTERING_PREFIXES_TO_KEEP, (pref, newValue) -> {
|
||||
String value = (String) newValue;
|
||||
|
||||
List<String> prefixes = new ArrayList<>();
|
||||
for (String prefix : DbFilteringUtils.parsePrefixes(value)) {
|
||||
prefixes.add("+" + prefix);
|
||||
}
|
||||
String formattedPrefixes = TextUtils.join(",", prefixes);
|
||||
String formattedPrefixes = DbFilteringUtils.formatPrefixes(
|
||||
DbFilteringUtils.parsePrefixes(value));
|
||||
|
||||
if (!TextUtils.equals(formattedPrefixes, value)) {
|
||||
((EditTextPreference) pref).setText(formattedPrefixes);
|
||||
|
@ -69,9 +77,44 @@ public class DbFilteringSettingsFragment extends BaseSettingsFragment {
|
|||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
cancelPrefillPrefixesTask();
|
||||
|
||||
updateFilter();
|
||||
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
private void startPrefillPrefixesTask() {
|
||||
cancelPrefillPrefixesTask();
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
AsyncTask<Void, Void, List<String>> prefillPrefixesTask = this.prefillPrefixesTask
|
||||
= new AsyncTask<Void, Void, List<String>>() {
|
||||
@Override
|
||||
protected List<String> doInBackground(Void... voids) {
|
||||
return DbFilteringUtils.detectPrefixes(requireContext(),
|
||||
settings.getCachedAutoDetectedCountryCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<String> prefixList) {
|
||||
if (!prefixList.isEmpty()) {
|
||||
EditTextPreference preference = requirePreference(
|
||||
PREF_DB_FILTERING_PREFIXES_TO_KEEP);
|
||||
|
||||
if (TextUtils.isEmpty(preference.getText())) {
|
||||
preference.setText(DbFilteringUtils.formatPrefixes(prefixList));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
prefillPrefixesTask.execute();
|
||||
}
|
||||
|
||||
private void cancelPrefillPrefixesTask() {
|
||||
if (prefillPrefixesTask != null) {
|
||||
prefillPrefixesTask.cancel(true);
|
||||
prefillPrefixesTask = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateFilter() {
|
||||
|
|
|
@ -35,6 +35,7 @@ public class Settings extends GenericSettings {
|
|||
public static final String PREF_LAST_UPDATE_TIME = "lastUpdateTime";
|
||||
public static final String PREF_LAST_UPDATE_CHECK_TIME = "lastUpdateCheckTime";
|
||||
public static final String PREF_DB_FILTERING_ENABLED = "dbFilteringEnabled";
|
||||
public static final String PREF_DB_FILTERING_PREFIXES_PREFILLED = "dbFilteringPrefixesPrefilled";
|
||||
public static final String PREF_DB_FILTERING_PREFIXES_TO_KEEP = "dbFilteringPrefixesToKeep";
|
||||
public static final String PREF_DB_FILTERING_THOROUGH = "dbFilteringThorough";
|
||||
public static final String PREF_DB_FILTERING_KEEP_SHORT_NUMBERS = "dbFilteringKeepShortNumbers";
|
||||
|
@ -255,6 +256,14 @@ public class Settings extends GenericSettings {
|
|||
setBoolean(PREF_DB_FILTERING_ENABLED, enabled);
|
||||
}
|
||||
|
||||
public boolean isDbFilteringPrefixesPrefilled() {
|
||||
return getBoolean(PREF_DB_FILTERING_PREFIXES_PREFILLED);
|
||||
}
|
||||
|
||||
public void setDbFilteringPrefixesPrefilled(boolean prefilled) {
|
||||
setBoolean(PREF_DB_FILTERING_PREFIXES_PREFILLED, prefilled);
|
||||
}
|
||||
|
||||
public String getDbFilteringPrefixesToKeep() {
|
||||
return getString(PREF_DB_FILTERING_PREFIXES_TO_KEEP);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,20 @@
|
|||
package dummydomain.yetanothercallblocker.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import dummydomain.yetanothercallblocker.Settings;
|
||||
import dummydomain.yetanothercallblocker.data.CallLogHelper;
|
||||
import dummydomain.yetanothercallblocker.data.CallLogItem;
|
||||
import dummydomain.yetanothercallblocker.data.NumberFilter;
|
||||
import dummydomain.yetanothercallblocker.data.NumberUtils;
|
||||
|
||||
public class DbFilteringUtils {
|
||||
|
||||
|
@ -37,4 +44,36 @@ public class DbFilteringUtils {
|
|||
return prefixList;
|
||||
}
|
||||
|
||||
public static String formatPrefixes(Collection<String> prefixes) {
|
||||
List<String> formattedPrefixes = new ArrayList<>(prefixes.size());
|
||||
|
||||
for (String prefix : prefixes) {
|
||||
formattedPrefixes.add("+" + prefix);
|
||||
}
|
||||
|
||||
return TextUtils.join(",", formattedPrefixes);
|
||||
}
|
||||
|
||||
public static List<String> detectPrefixes(Context context, String countryCode) {
|
||||
Set<String> prefixes = new HashSet<>();
|
||||
|
||||
List<CallLogItem> callLogItems = CallLogHelper.loadCalls(context, null, false, 100);
|
||||
|
||||
for (CallLogItem callLogItem : callLogItems) {
|
||||
String number = NumberUtils.normalizeNumber(callLogItem.number, countryCode);
|
||||
|
||||
if (number != null && number.startsWith("+") && number.length() > 1) {
|
||||
char firstDigit = number.charAt(1);
|
||||
if (firstDigit >= '0' && firstDigit <= '9') {
|
||||
prefixes.add(String.valueOf(firstDigit));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<String> prefixList = new ArrayList<>(prefixes);
|
||||
Collections.sort(prefixList);
|
||||
|
||||
return prefixList;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue