From 87ad140eb55e005877ba99f3444a53b27c9013fc Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 22 Feb 2017 14:11:05 +0100 Subject: [PATCH] SMS logging and resource refactor --- .../nightscout/androidaps/MainActivity.java | 27 ++++++++++--------- .../SmsCommunicatorPlugin.java | 23 +++++++++------- app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/pref_smscommunicator.xml | 4 +-- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 603b0e2699..0f676470c3 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -140,7 +140,8 @@ public class MainActivity extends AppCompatActivity { .setMessage(R.string.reset_db_confirm) .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override public void onClick(DialogInterface dialog, int which) { + @Override + public void onClick(DialogInterface dialog, int which) { MainApp.getDbHelper().resetDatabases(); } }) @@ -208,11 +209,12 @@ public class MainActivity extends AppCompatActivity { //check for sms permission if enable in prefernces @Subscribe public void onStatusEvent(final EventPreferenceChange ev) { - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { - SharedPreferences smssettings = PreferenceManager.getDefaultSharedPreferences(this); - synchronized (this){ - if (smssettings.getBoolean("smscommunicator_remotecommandsallowed", false)) { - setAskForSMS(); + if (ev.isChanged(R.string.key_smscommunicator_remotecommandsallowed)) { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { + synchronized (this) { + if (SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)) { + setAskForSMS(); + } } } } @@ -223,16 +225,15 @@ public class MainActivity extends AppCompatActivity { } @Override - protected void onResume(){ + protected void onResume() { super.onResume(); askForSMSPermissions(); } - private synchronized void askForSMSPermissions(){ + private synchronized void askForSMSPermissions() { if (askForSMS) { //only when settings were changed an MainActivity resumes. askForSMS = false; - SharedPreferences smssettings = PreferenceManager.getDefaultSharedPreferences(this); - if (smssettings.getBoolean("smscommunicator_remotecommandsallowed", false)) { + if (SP.getBoolean(R.string.smscommunicator_remotecommandsallowed, false)) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { askForPermission(new String[]{Manifest.permission.RECEIVE_SMS, Manifest.permission.SEND_SMS, @@ -244,7 +245,7 @@ public class MainActivity extends AppCompatActivity { private void askForPermission(String[] permission, Integer requestCode) { boolean test = false; - for (int i=0; i < permission.length; i++) { + for (int i = 0; i < permission.length; i++) { test = test || (ContextCompat.checkSelfPermission(this, permission[i]) != PackageManager.PERMISSION_GRANTED); } if (test) { @@ -276,10 +277,10 @@ public class MainActivity extends AppCompatActivity { public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { View v = getCurrentFocus(); - if ( v instanceof EditText) { + if (v instanceof EditText) { Rect outRect = new Rect(); v.getGlobalVisibleRect(outRect); - if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) { + if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) { v.clearFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index e216764a3d..02d09d6ecc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -160,15 +160,17 @@ public class SmsCommunicatorPlugin implements PluginBase { @Subscribe public void processSettings(final EventPreferenceChange ev) { - String settings = SP.getString("smscommunicator_allowednumbers", ""); + if (ev.isChanged(R.string.key_smscommunicator_allowednumbers)) { + String settings = SP.getString(R.string.key_smscommunicator_allowednumbers, ""); - String pattern = ";"; + String pattern = ";"; - String[] substrings = settings.split(pattern); - for (String number : substrings) { - String cleaned = number.replaceAll("\\s+", ""); - allowedNumbers.add(cleaned); - log.debug("Found allowed number: " + cleaned); + String[] substrings = settings.split(pattern); + for (String number : substrings) { + String cleaned = number.replaceAll("\\s+", ""); + allowedNumbers.add(cleaned); + log.debug("Found allowed number: " + cleaned); + } } } @@ -317,7 +319,7 @@ public class SmsCommunicatorPlugin implements PluginBase { break; case "BASAL": if (splited.length > 1) { - boolean remoteCommandsAllowed = SP.getBoolean("smscommunicator_remotecommandsallowed", false); + boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false); if (splited[1].toUpperCase().equals("CANCEL") || splited[1].toUpperCase().equals("STOP")) { if (remoteCommandsAllowed) { passCode = generatePasscode(); @@ -353,7 +355,7 @@ public class SmsCommunicatorPlugin implements PluginBase { } else if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); - boolean remoteCommandsAllowed = SP.getBoolean("smscommunicator_remotecommandsallowed", false); + boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false); if (amount > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode); @@ -370,7 +372,7 @@ public class SmsCommunicatorPlugin implements PluginBase { case "CAL": if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); - boolean remoteCommandsAllowed = SP.getBoolean("smscommunicator_remotecommandsallowed", false); + boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false); if (amount > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_calibrationreplywithcode), amount, passCode); @@ -478,6 +480,7 @@ public class SmsCommunicatorPlugin implements PluginBase { sms.text = stripAccents(sms.text); if (sms.text.length() > 140) sms.text = sms.text.substring(0, 139); try { + log.debug("Sending SMS to " + sms.phoneNumber + ": " + sms.text); smsManager.sendTextMessage(sms.phoneNumber, null, sms.text, null, null); messages.add(sms); } catch (IllegalArgumentException e) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 26d946581c..5c717d3061 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -542,4 +542,6 @@ Break down IOB into bolus and basal IOB on the watchface not successful - please check phone Not available + smscommunicator_allowednumbers + smscommunicator_remotecommandsallowed diff --git a/app/src/main/res/xml/pref_smscommunicator.xml b/app/src/main/res/xml/pref_smscommunicator.xml index 23816e8e29..0f9440a615 100644 --- a/app/src/main/res/xml/pref_smscommunicator.xml +++ b/app/src/main/res/xml/pref_smscommunicator.xml @@ -7,12 +7,12 @@