From 50af72b7caacb7ca4274efeec0a16e2dda84ff80 Mon Sep 17 00:00:00 2001 From: Danesh M Date: Mon, 20 Jun 2016 14:23:39 -0700 Subject: [PATCH] Settings : Add country code to blacklist entry dialog Enforce country code when adding an entry. If the user has regex enabled, * is added to the list as a choice. This avoids the confusion users are currently facing where blocking only the number + regex doesn't work, since country code is expected. Eg : 206* won't work +1206* works CYNGNOS-3049 BACON-4953 Change-Id: I9236898f17fcef425e561aaa10e56c5e8c2171a7 --- Android.mk | 3 +- res/layout/dialog_blacklist_edit_entry.xml | 14 ++++ res/values/cm_strings.xml | 2 +- .../blacklist/EntryEditDialogFragment.java | 75 +++++++++++++++++++++- 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/Android.mk b/Android.mk index 51c74e953b..db32ed1c25 100644 --- a/Android.mk +++ b/Android.mk @@ -6,7 +6,8 @@ LOCAL_STATIC_JAVA_LIBRARIES := \ android-support-v4 \ android-support-v13 \ jsr305 \ - org.cyanogenmod.platform.internal + org.cyanogenmod.platform.internal \ + libphonenumber LOCAL_MODULE_TAGS := optional diff --git a/res/layout/dialog_blacklist_edit_entry.xml b/res/layout/dialog_blacklist_edit_entry.xml index 30b7c2434f..ef66325c06 100644 --- a/res/layout/dialog_blacklist_edit_entry.xml +++ b/res/layout/dialog_blacklist_edit_entry.xml @@ -31,6 +31,20 @@ android:paddingStart="10dip" android:paddingEnd="10dip"> + + + + + + Wallpaper Not available with %1$s - + + diff --git a/src/com/android/settings/blacklist/EntryEditDialogFragment.java b/src/com/android/settings/blacklist/EntryEditDialogFragment.java index 57797b95f7..11c9909e34 100644 --- a/src/com/android/settings/blacklist/EntryEditDialogFragment.java +++ b/src/com/android/settings/blacklist/EntryEditDialogFragment.java @@ -38,14 +38,26 @@ import android.text.method.DialerKeyListener; import android.util.Pair; import android.view.LayoutInflater; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; +import android.widget.Spinner; import android.widget.Toast; import com.android.internal.telephony.util.BlacklistUtils; import com.android.settings.R; +import com.google.i18n.phonenumbers.PhoneNumberUtil; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; public class EntryEditDialogFragment extends DialogFragment implements TextWatcher, DialogInterface.OnClickListener { @@ -55,6 +67,7 @@ public class EntryEditDialogFragment extends DialogFragment private CheckBox mBlockCalls; private CheckBox mBlockMessages; private Button mOkButton; + private Spinner mCountryCode; private static final String[] BLACKLIST_PROJECTION = { Blacklist.NUMBER, Blacklist.PHONE_MODE, Blacklist.MESSAGE_MODE @@ -73,6 +86,7 @@ public class EntryEditDialogFragment extends DialogFragment private static final String STATE_PHONE = "phone"; private static final String STATE_MESSAGE = "message"; private static final String STATE_EDIT_ENABLED = "edit_enabled"; + private static final String STATE_COUNTRY_CODE = "edit_country_code"; private static final String DELETE_CONFIRM_FRAGMENT_TAG = "delete_confirm"; @@ -147,6 +161,55 @@ public class EntryEditDialogFragment extends DialogFragment return getArguments().getLong("id", -1); } + private static String getLocaleCountry() { + final String country = Locale.getDefault().getCountry(); + if (TextUtils.isEmpty(country)) { + return null; + } + return country.toUpperCase(); + } + + private void populateCountryCodes(View view, Bundle savedState) { + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + // Get all supported country codes + Set countryCodes = new HashSet(); + for (String region : phoneUtil.getSupportedRegions()) { + countryCodes.add(String.valueOf(phoneUtil.getCountryCodeForRegion(region))); + } + List entries = new ArrayList(countryCodes); + Collections.sort(entries, new Comparator() { + @Override + public int compare(String lhs, String rhs) { + return Integer.parseInt(lhs) - Integer.parseInt(rhs); + } + }); + + // If regex is supported, insert regex character + if (BlacklistUtils.isBlacklistRegexEnabled(getContext())) { + entries.add(0, "*"); + } + + // Set current country code as selected position + int selectedIndex = 0; + if (savedState == null) { + String country = getLocaleCountry(); + int currentCode = phoneUtil.getCountryCodeForRegion(country); + selectedIndex = entries.indexOf(String.valueOf(currentCode)); + } else { + selectedIndex = savedState.getInt(STATE_COUNTRY_CODE); + } + + ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), + android.R.layout.simple_spinner_item, entries); + arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mCountryCode.setAdapter(arrayAdapter); + mCountryCode.setSelection(selectedIndex); + + // Ensure we make the layout visible + View parent = view.findViewById(R.id.country_code_layout); + parent.setVisibility(View.VISIBLE); + } + private View createDialogView(long id, Bundle savedState) { final Activity activity = getActivity(); final LayoutInflater inflater = (LayoutInflater) @@ -158,6 +221,8 @@ public class EntryEditDialogFragment extends DialogFragment mEditText.setKeyListener(DialerKeyListener.getInstance()); mEditText.addTextChangedListener(this); + mCountryCode = (Spinner) view.findViewById(R.id.number_country_code); + mContactPickButton = (ImageButton) view.findViewById(R.id.select_contact); mContactPickButton.setOnClickListener(new View.OnClickListener() { @Override @@ -202,13 +267,20 @@ public class EntryEditDialogFragment extends DialogFragment mEditText.setEnabled(true); } + // Only populate country codes if new entry + if (id < 0 || savedState != null && mEditText.isEnabled()) { + populateCountryCodes(view, savedState); + } + // Mirror contacts selector to state of editText mContactPickButton.setEnabled(mEditText.isEnabled()); return view; } private void updateBlacklistEntry() { - String number = mEditText.getText().toString(); + String plusSymbol = getString(R.string.blacklist_country_code_plus); + String number = plusSymbol + mCountryCode.getSelectedItem() + + mEditText.getText().toString(); int flags = 0; int valid = BlacklistUtils.BLOCK_CALLS | BlacklistUtils.BLOCK_MESSAGES; if (mBlockCalls.isChecked()) { @@ -263,6 +335,7 @@ public class EntryEditDialogFragment extends DialogFragment dialogState.putBoolean(STATE_PHONE, mBlockCalls.isChecked()); dialogState.putBoolean(STATE_MESSAGE, mBlockMessages.isChecked()); dialogState.putBoolean(STATE_EDIT_ENABLED, mEditText.isEnabled()); + dialogState.putInt(STATE_COUNTRY_CODE, mCountryCode.getSelectedItemPosition()); state.putBundle(DIALOG_STATE, dialogState); } -- 2.11.0