diff options
author | Danesh M <danesh@cyngn.com> | 2016-06-20 14:23:39 -0700 |
---|---|---|
committer | Danesh M <danesh@cyngn.com> | 2016-06-27 14:55:01 -0700 |
commit | 50af72b7caacb7ca4274efeec0a16e2dda84ff80 (patch) | |
tree | e1c30b9b88947d4f61f3fc0ce9ada8302a5ce51c /src/com/android | |
parent | 923b2a1a3422c2601b94c901a2576da20b3f4b92 (diff) | |
download | packages_apps_Settings-50af72b7caacb7ca4274efeec0a16e2dda84ff80.zip packages_apps_Settings-50af72b7caacb7ca4274efeec0a16e2dda84ff80.tar.gz packages_apps_Settings-50af72b7caacb7ca4274efeec0a16e2dda84ff80.tar.bz2 |
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
Diffstat (limited to 'src/com/android')
-rw-r--r-- | src/com/android/settings/blacklist/EntryEditDialogFragment.java | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/src/com/android/settings/blacklist/EntryEditDialogFragment.java b/src/com/android/settings/blacklist/EntryEditDialogFragment.java index 57797b9..11c9909 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<String> countryCodes = new HashSet<String>(); + for (String region : phoneUtil.getSupportedRegions()) { + countryCodes.add(String.valueOf(phoneUtil.getCountryCodeForRegion(region))); + } + List<String> entries = new ArrayList<String>(countryCodes); + Collections.sort(entries, new Comparator<String>() { + @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<String> arrayAdapter = new ArrayAdapter<String>(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); } |