From 55e6952f1d88f3ecc009d9cf16451fafeb259452 Mon Sep 17 00:00:00 2001 From: percy-g2 Date: Sun, 14 Jan 2018 06:43:12 +0530 Subject: [PATCH] show country code as per default locale country for notification and signal number when opening. --- build.gradle | 2 +- .../org/havenapp/main/SettingsFragment.java | 28 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index e7ff27eb..3614179f 100644 --- a/build.gradle +++ b/build.gradle @@ -121,7 +121,7 @@ dependencies { compile 'com.maxproj.simplewaveform:app:1.0.0' compile 'com.takisoft.fix:preference-v7:27.0.2.0' compile 'com.wdullaer:materialdatetimepicker:3.5.0' - + compile 'com.googlecode.libphonenumber:libphonenumber:8.7.0' implementation('com.mikepenz:aboutlibraries:6.0.1@aar') { transitive = true } diff --git a/src/main/java/org/havenapp/main/SettingsFragment.java b/src/main/java/org/havenapp/main/SettingsFragment.java index ace220e0..6879a1be 100644 --- a/src/main/java/org/havenapp/main/SettingsFragment.java +++ b/src/main/java/org/havenapp/main/SettingsFragment.java @@ -22,6 +22,7 @@ import android.view.MenuItem; import android.widget.Toast; +import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat; import com.wdullaer.materialdatetimepicker.time.TimePickerDialog; @@ -31,6 +32,7 @@ import org.havenapp.main.ui.MicrophoneConfigureActivity; import java.io.File; +import java.util.Locale; import info.guardianproject.netcipher.proxy.OrbotHelper; @@ -76,6 +78,19 @@ public void onCreatePreferencesFix(Bundle bundle, String s) { ((SwitchPreferenceCompat) findPreference(PreferenceManager.SMS_ACTIVE)).setChecked(true); } + findPreference(PreferenceManager.SMS_NUMBER).setOnPreferenceClickListener(preference -> { + if (preferences.getSmsNumber().isEmpty()) { + ((EditTextPreference) findPreference(PreferenceManager.SMS_NUMBER)).setText(getCountryCode()); + } + return false; + }); + findPreference(PreferenceManager.REGISTER_SIGNAL).setOnPreferenceClickListener(preference -> { + if (preferences.getSignalUsername() == null) { + ((EditTextPreference) findPreference(PreferenceManager.REGISTER_SIGNAL)).setText(getCountryCode()); + } + return false; + }); + if (checkValidString(preferences.getSmsNumber())) { ((EditTextPreference) findPreference(PreferenceManager.SMS_NUMBER)).setText(preferences.getSmsNumber().trim()); findPreference(PreferenceManager.SMS_NUMBER).setSummary(preferences.getSmsNumber().trim()); @@ -254,7 +269,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin case PreferenceManager.REGISTER_SIGNAL: String signalNum = ((EditTextPreference) findPreference(PreferenceManager.REGISTER_SIGNAL)).getText(); - if (checkValidString(signalNum)) { + if (checkValidString(signalNum) && !getCountryCode().equalsIgnoreCase(signalNum)) { signalNum = "+" + signalNum.trim().replaceAll("[^0-9]", ""); preferences.setSignalUsername(signalNum); @@ -262,7 +277,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin resetSignal(preferences.getSignalUsername()); activateSignal(preferences.getSignalUsername(), null); - } else { + } else if (!getCountryCode().equalsIgnoreCase(signalNum)) { preferences.setSignalUsername(""); findPreference(PreferenceManager.REGISTER_SIGNAL).setSummary(R.string.register_signal_desc); } @@ -305,6 +320,11 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin } } + String getCountryCode() { + PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); + return "+" + String.valueOf(phoneUtil.getCountryCodeForRegion(Locale.getDefault().getCountry())); + } + private void setPhoneNumber() { boolean smsActive = ((SwitchPreferenceCompat) findPreference(PreferenceManager.SMS_ACTIVE)).isChecked(); String phoneNumber = ((EditTextPreference) findPreference(PreferenceManager.SMS_NUMBER)).getText(); @@ -314,10 +334,10 @@ private void setPhoneNumber() { preferences.activateSms(false); } - if (checkValidString(phoneNumber)) { + if (checkValidString(phoneNumber) && !getCountryCode().equalsIgnoreCase(phoneNumber)) { preferences.setSmsNumber(phoneNumber.trim()); findPreference(PreferenceManager.SMS_NUMBER).setSummary(phoneNumber.trim()); - } else { + } else if (!getCountryCode().equalsIgnoreCase(phoneNumber)){ preferences.setSmsNumber(""); findPreference(PreferenceManager.SMS_NUMBER).setSummary(R.string.sms_dialog_message); }