Move plugin related code from preferences to plugin

This commit is contained in:
Milos Kozak 2019-11-20 00:20:08 +01:00
parent 2319536adc
commit 5d768bd368
6 changed files with 123 additions and 80 deletions

View file

@ -9,28 +9,28 @@ import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup; import android.preference.PreferenceGroup;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.text.TextUtils;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin;
import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin;
import info.nightscout.androidaps.plugins.general.tidepool.TidepoolPlugin;
import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader;
import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin;
import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin;
import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin;
import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin;
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.plugins.general.tidepool.TidepoolPlugin;
import info.nightscout.androidaps.plugins.general.wear.WearPlugin;
import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin;
import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin;
import info.nightscout.androidaps.plugins.pump.combo.ComboPlugin; import info.nightscout.androidaps.plugins.pump.combo.ComboPlugin;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin;
@ -43,16 +43,9 @@ import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin;
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.plugins.general.wear.WearPlugin;
import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin;
import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin;
import info.nightscout.androidaps.utils.LocaleHelper;
import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.OKDialog;
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
import com.andreabaccega.widget.ValidatingEditTextPreference;
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
MyPreferenceFragment myPreferenceFragment; MyPreferenceFragment myPreferenceFragment;
@ -83,7 +76,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) { if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) {
OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null); OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null);
} }
updatePrefSummary(myPreferenceFragment.getPreference(key)); updatePrefSummary(myPreferenceFragment.findPreference(key));
} }
private static void updatePrefSummary(Preference pref) { private static void updatePrefSummary(Preference pref) {
@ -95,13 +88,13 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
EditTextPreference editTextPref = (EditTextPreference) pref; EditTextPreference editTextPref = (EditTextPreference) pref;
if (pref.getKey().contains("password") || pref.getKey().contains("secret")) { if (pref.getKey().contains("password") || pref.getKey().contains("secret")) {
pref.setSummary("******"); pref.setSummary("******");
} else if (pref.getKey().equals(MainApp.gs(R.string.key_danars_name))) {
pref.setSummary(SP.getString(R.string.key_danars_name, ""));
} else if (editTextPref.getText() != null) { } else if (editTextPref.getText() != null) {
((EditTextPreference) pref).setDialogMessage(editTextPref.getDialogMessage()); ((EditTextPreference) pref).setDialogMessage(editTextPref.getDialogMessage());
pref.setSummary(editTextPref.getText()); pref.setSummary(editTextPref.getText());
} else if (pref.getKey().contains("smscommunicator_allowednumbers") && (editTextPref.getText() == null || TextUtils.isEmpty(editTextPref.getText().trim()))) { } else {
pref.setSummary(MainApp.gs(R.string.smscommunicator_allowednumbers_summary)); for (PluginBase plugin : MainApp.getPluginsList()) {
plugin.updatePreferenceSummary(pref);
}
} }
} }
} }
@ -201,58 +194,11 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
addPreferencesFromResourceIfEnabled(StatuslinePlugin.getPlugin(), PluginType.GENERAL); addPreferencesFromResourceIfEnabled(StatuslinePlugin.getPlugin(), PluginType.GENERAL);
} }
if (Config.NSCLIENT) {
PreferenceScreen scrnAdvancedSettings = (PreferenceScreen) findPreference(getString(R.string.key_advancedsettings));
if (scrnAdvancedSettings != null) {
scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_res_warning)));
scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_res_critical)));
scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_bat_warning)));
scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_bat_critical)));
scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_show_statuslights)));
scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_show_statuslights_extended)));
}
}
initSummary(getPreferenceScreen()); initSummary(getPreferenceScreen());
final Preference tidepoolTestLogin = findPreference(MainApp.gs(R.string.key_tidepool_test_login)); for (PluginBase plugin : MainApp.getPluginsList()) {
if (tidepoolTestLogin != null) plugin.preprocessPreferences(this);
tidepoolTestLogin.setOnPreferenceClickListener(preference -> {
TidepoolUploader.INSTANCE.testLogin(getActivity());
return false;
});
final ValidatingEditTextPreference distance = (ValidatingEditTextPreference)findPreference(getString(R.string.key_smscommunicator_remotebolusmindistance));
final EditTextPreference allowedNumbers = (EditTextPreference)findPreference(getString(R.string.key_smscommunicator_allowednumbers));
if (distance != null && allowedNumbers != null) {
if (!SmsCommunicatorPlugin.areMoreNumbers(allowedNumbers.getText())) {
distance.setTitle(getString(R.string.smscommunicator_remotebolusmindistance)
+ ".\n"
+ getString(R.string.smscommunicator_remotebolusmindistance_caveat));
distance.setEnabled(false);
} else {
distance.setTitle(getString(R.string.smscommunicator_remotebolusmindistance));
distance.setEnabled(true);
} }
allowedNumbers.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (!SmsCommunicatorPlugin.areMoreNumbers(((String)newValue))) {
distance.setText(String.valueOf(Constants.remoteBolusMinDistance/(60 * 1000L)));
distance.setTitle(getString(R.string.smscommunicator_remotebolusmindistance)
+ ".\n"
+ getString(R.string.smscommunicator_remotebolusmindistance_caveat));
distance.setEnabled(false);
} else {
distance.setTitle(getString(R.string.smscommunicator_remotebolusmindistance));
distance.setEnabled(true);
}
return true;
}
});
}
} }
@Override @Override
@ -260,9 +206,5 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putInt("id", id); outState.putInt("id", id);
} }
public Preference getPreference(String key) {
return findPreference(key);
}
} }
} }

View file

@ -1,10 +1,13 @@
package info.nightscout.androidaps.interfaces; package info.nightscout.androidaps.interfaces;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -215,4 +218,10 @@ public abstract class PluginBase {
protected void onStateChange(PluginType type, State oldState, State newState) { protected void onStateChange(PluginType type, State oldState, State newState) {
} }
public void preprocessPreferences(@NotNull final PreferenceFragment preferenceFragment) {
}
public void updatePreferenceSummary(@NotNull final Preference pref) {
}
} }

View file

@ -7,9 +7,12 @@ import android.content.ServiceConnection;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.text.Html; import android.text.Html;
import android.text.Spanned; import android.text.Spanned;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -157,6 +160,23 @@ public class NSClientPlugin extends PluginBase {
super.onStop(); super.onStop();
} }
@Override
public void preprocessPreferences(@NotNull PreferenceFragment preferenceFragment) {
super.preprocessPreferences(preferenceFragment);
if (Config.NSCLIENT) {
PreferenceScreen scrnAdvancedSettings = (PreferenceScreen) preferenceFragment.findPreference(MainApp.gs(R.string.key_advancedsettings));
if (scrnAdvancedSettings != null) {
scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_res_warning)));
scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_res_critical)));
scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_bat_warning)));
scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_bat_critical)));
scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_show_statuslights)));
scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_show_statuslights_extended)));
}
}
}
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {

View file

@ -2,10 +2,17 @@ package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.telephony.SmsManager; import android.telephony.SmsManager;
import android.telephony.SmsMessage; import android.telephony.SmsMessage;
import android.text.TextUtils;
import com.andreabaccega.widget.ValidatingEditTextPreference;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -111,6 +118,51 @@ public class SmsCommunicatorPlugin extends PluginBase {
super.onStop(); super.onStop();
} }
@Override
public void preprocessPreferences(@NotNull final PreferenceFragment preferenceFragment) {
super.preprocessPreferences(preferenceFragment);
final ValidatingEditTextPreference distance = (ValidatingEditTextPreference) preferenceFragment.findPreference(MainApp.gs(R.string.key_smscommunicator_remotebolusmindistance));
final EditTextPreference allowedNumbers = (EditTextPreference) preferenceFragment.findPreference(MainApp.gs(R.string.key_smscommunicator_allowednumbers));
if (distance != null && allowedNumbers != null) {
if (!areMoreNumbers(allowedNumbers.getText())) {
distance.setTitle(MainApp.gs(R.string.smscommunicator_remotebolusmindistance)
+ ".\n"
+ MainApp.gs(R.string.smscommunicator_remotebolusmindistance_caveat));
distance.setEnabled(false);
} else {
distance.setTitle(MainApp.gs(R.string.smscommunicator_remotebolusmindistance));
distance.setEnabled(true);
}
allowedNumbers.setOnPreferenceChangeListener((preference, newValue) -> {
if (!areMoreNumbers(((String) newValue))) {
distance.setText(String.valueOf(Constants.remoteBolusMinDistance / (60 * 1000L)));
distance.setTitle(MainApp.gs(R.string.smscommunicator_remotebolusmindistance)
+ ".\n"
+ MainApp.gs(R.string.smscommunicator_remotebolusmindistance_caveat));
distance.setEnabled(false);
} else {
distance.setTitle(MainApp.gs(R.string.smscommunicator_remotebolusmindistance));
distance.setEnabled(true);
}
return true;
});
}
}
@Override
public void updatePreferenceSummary(@NotNull final Preference pref) {
super.updatePreferenceSummary(pref);
if (pref instanceof EditTextPreference) {
EditTextPreference editTextPref = (EditTextPreference) pref;
if (pref.getKey().contains("smscommunicator_allowednumbers") && (editTextPref.getText() == null || TextUtils.isEmpty(editTextPref.getText().trim()))) {
pref.setSummary(MainApp.gs(R.string.smscommunicator_allowednumbers_summary));
}
}
}
private void processSettings(final EventPreferenceChange ev) { private void processSettings(final EventPreferenceChange ev) {
if (ev == null || ev.isChanged(R.string.key_smscommunicator_allowednumbers)) { if (ev == null || ev.isChanged(R.string.key_smscommunicator_allowednumbers)) {
String settings = SP.getString(R.string.key_smscommunicator_allowednumbers, ""); String settings = SP.getString(R.string.key_smscommunicator_allowednumbers, "");
@ -965,9 +1017,9 @@ public class SmsCommunicatorPlugin extends PluginBase {
for (String number : substrings) { for (String number : substrings) {
String cleaned = number.replaceAll("\\s+", ""); String cleaned = number.replaceAll("\\s+", "");
if (cleaned.length()<4) continue; if (cleaned.length() < 4) continue;
if (cleaned.substring(0,1).compareTo("+")!=0) continue; if (cleaned.substring(0, 1).compareTo("+") != 0) continue;
cleaned = cleaned.replace("+",""); cleaned = cleaned.replace("+", "");
if (!cleaned.matches("[0-9]+")) continue; if (!cleaned.matches("[0-9]+")) continue;
countNumbers++; countNumbers++;
} }

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.general.tidepool package info.nightscout.androidaps.plugins.general.tidepool
import android.preference.PreferenceFragment
import android.text.Spanned import android.text.Spanned
import info.nightscout.androidaps.Constants import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.MainApp
@ -34,7 +35,6 @@ object TidepoolPlugin : PluginBase(PluginDescription()
.preferencesId(R.xml.pref_tidepool) .preferencesId(R.xml.pref_tidepool)
.description(R.string.description_tidepool) .description(R.string.description_tidepool)
) { ) {
private val log = LoggerFactory.getLogger(L.TIDEPOOL) private val log = LoggerFactory.getLogger(L.TIDEPOOL)
private var disposable: CompositeDisposable = CompositeDisposable() private var disposable: CompositeDisposable = CompositeDisposable()
@ -111,6 +111,16 @@ object TidepoolPlugin : PluginBase(PluginDescription()
super.onStop() super.onStop()
} }
override fun preprocessPreferences(preferenceFragment: PreferenceFragment) {
super.preprocessPreferences(preferenceFragment)
val tidepoolTestLogin = preferenceFragment.findPreference(MainApp.gs(R.string.key_tidepool_test_login))
tidepoolTestLogin?.setOnPreferenceClickListener {
TidepoolUploader.testLogin(preferenceFragment.getActivity())
false
}
}
private fun doUpload() = private fun doUpload() =
when (TidepoolUploader.connectionStatus) { when (TidepoolUploader.connectionStatus) {
TidepoolUploader.ConnectionStatus.FAILED -> {} TidepoolUploader.ConnectionStatus.FAILED -> {}

View file

@ -5,10 +5,12 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.preference.Preference;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -110,6 +112,14 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
} }
} }
@Override
public void updatePreferenceSummary(@NotNull Preference pref) {
super.updatePreferenceSummary(pref);
if (pref.getKey().equals(MainApp.gs(R.string.key_danars_name)))
pref.setSummary(SP.getString(R.string.key_danars_name, ""));
}
@Override @Override
protected void onStart() { protected void onStart() {
Context context = MainApp.instance().getApplicationContext(); Context context = MainApp.instance().getApplicationContext();