AndroidAPS/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java

200 lines
9.7 KiB
Java
Raw Normal View History

package info.nightscout.androidaps;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
import android.preference.PreferenceManager;
import android.text.TextUtils;
2017-01-26 12:07:25 +01:00
import info.nightscout.androidaps.events.EventPreferenceChange;
2016-07-06 22:50:25 +02:00
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
2018-03-31 00:36:03 +02:00
import info.nightscout.androidaps.interfaces.PluginType;
2017-11-01 20:43:59 +01:00
import info.nightscout.androidaps.plugins.Careportal.CareportalPlugin;
import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin;
2017-09-18 22:58:18 +02:00
import info.nightscout.androidaps.plugins.Insulin.InsulinOrefFreePeakPlugin;
2017-11-01 20:43:59 +01:00
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
2018-03-23 22:44:37 +01:00
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
2017-11-01 20:43:59 +01:00
import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin;
import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin;
2018-03-22 22:18:40 +01:00
import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin;
2017-04-21 12:15:08 +02:00
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
2017-09-13 00:35:03 +02:00
import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin;
2017-04-30 21:08:00 +02:00
import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin;
2017-04-21 12:15:08 +02:00
import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
2017-06-30 11:56:36 +02:00
import info.nightscout.androidaps.plugins.SensitivityAAPS.SensitivityAAPSPlugin;
import info.nightscout.androidaps.plugins.SensitivityOref0.SensitivityOref0Plugin;
2017-07-07 15:08:21 +02:00
import info.nightscout.androidaps.plugins.SensitivityWeightedAverage.SensitivityWeightedAveragePlugin;
2017-11-01 20:43:59 +01:00
import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorPlugin;
2018-03-31 00:36:03 +02:00
import info.nightscout.androidaps.plugins.Source.SourceDexcomG5Plugin;
2017-02-18 18:13:13 +01:00
import info.nightscout.androidaps.plugins.Wear.WearPlugin;
2017-05-06 15:18:19 +02:00
import info.nightscout.androidaps.plugins.XDripStatusline.StatuslinePlugin;
2016-07-06 22:50:25 +02:00
import info.nightscout.utils.LocaleHelper;
2017-07-04 00:16:35 +02:00
import info.nightscout.utils.OKDialog;
import info.nightscout.utils.SP;
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
MyPreferenceFragment myPreferenceFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPreferenceFragment = new MyPreferenceFragment();
Bundle args = new Bundle();
args.putInt("id", getIntent().getIntExtra("id", -1));
myPreferenceFragment.setArguments(args);
getFragmentManager().beginTransaction().replace(android.R.id.content, myPreferenceFragment).commit();
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
2017-02-17 16:16:20 +01:00
MainApp.bus().post(new EventPreferenceChange(key));
2016-07-06 22:50:25 +02:00
if (key.equals("language")) {
String lang = sharedPreferences.getString("language", "en");
2016-07-06 22:50:25 +02:00
LocaleHelper.setLocale(getApplicationContext(), lang);
2017-12-31 16:44:13 +01:00
MainApp.bus().post(new EventRefreshGui(true));
//recreate() does not update language so better close settings
finish();
2016-07-06 22:50:25 +02:00
}
2017-01-21 23:48:05 +01:00
if (key.equals("short_tabtitles")) {
2017-06-16 08:37:07 +02:00
MainApp.bus().post(new EventRefreshGui());
2017-01-21 23:48:05 +01:00
}
2018-03-22 22:18:40 +01:00
if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) {
2018-04-21 20:46:55 +02:00
OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null);
2017-07-04 00:16:35 +02:00
}
updatePrefSummary(myPreferenceFragment.getPreference(key));
2017-02-09 10:41:02 +01:00
}
2017-04-14 15:33:39 +02:00
private static void updatePrefSummary(Preference pref) {
2017-11-08 18:02:13 +01:00
if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry());
}
if (pref instanceof EditTextPreference) {
EditTextPreference editTextPref = (EditTextPreference) pref;
2017-04-14 15:33:39 +02:00
if (pref.getKey().contains("password") || pref.getKey().contains("secret")) {
pref.setSummary("******");
2018-04-21 20:46:55 +02:00
} else if (pref.getKey().equals(MainApp.gs(R.string.key_danars_name))) {
2017-11-01 20:43:59 +01:00
pref.setSummary(SP.getString(R.string.key_danars_name, ""));
} else if (editTextPref.getText() != null && !editTextPref.getText().equals("")) {
((EditTextPreference) pref).setDialogMessage(editTextPref.getDialogMessage());
pref.setSummary(editTextPref.getText());
2017-11-08 18:02:13 +01:00
} else if (pref.getKey().contains("smscommunicator_allowednumbers") && TextUtils.isEmpty(editTextPref.getText().trim())) {
2018-04-21 20:46:55 +02:00
pref.setSummary(MainApp.gs(R.string.smscommunicator_allowednumbers_summary));
}
2017-11-01 20:43:59 +01:00
}
}
2017-02-22 20:29:41 +01:00
public static void initSummary(Preference p) {
if (p instanceof PreferenceGroup) {
PreferenceGroup pGrp = (PreferenceGroup) p;
for (int i = 0; i < pGrp.getPreferenceCount(); i++) {
initSummary(pGrp.getPreference(i));
}
} else {
updatePrefSummary(p);
}
}
public static class MyPreferenceFragment extends PreferenceFragment {
private Integer id;
2017-11-01 20:43:59 +01:00
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
id = args.getInt("id");
2017-11-01 20:43:59 +01:00
}
2018-03-31 00:36:03 +02:00
void addPreferencesFromResourceIfEnabled(PluginBase p, PluginType type) {
2017-11-01 20:43:59 +01:00
if (p.isEnabled(type) && p.getPreferencesId() != -1)
addPreferencesFromResource(p.getPreferencesId());
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-11-01 20:43:59 +01:00
2017-11-08 18:02:13 +01:00
if (savedInstanceState != null && savedInstanceState.containsKey("id")) {
id = savedInstanceState.getInt("id");
}
2017-11-01 20:43:59 +01:00
if (id != -1) {
addPreferencesFromResource(id);
} else {
2018-02-15 21:00:09 +01:00
2017-11-29 16:07:26 +01:00
if (!Config.NSCLIENT && !Config.G5UPLOADER) {
2017-11-01 20:43:59 +01:00
addPreferencesFromResource(R.xml.pref_password);
2017-10-10 19:14:43 +02:00
}
2017-11-01 20:43:59 +01:00
addPreferencesFromResource(R.xml.pref_age);
addPreferencesFromResource(R.xml.pref_language);
2018-04-21 21:31:42 +02:00
addPreferencesFromResource(R.xml.pref_overview);
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(SourceDexcomG5Plugin.getPlugin(), PluginType.BGSOURCE);
addPreferencesFromResourceIfEnabled(CareportalPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(SafetyPlugin.getPlugin(), PluginType.CONSTRAINTS);
2017-11-01 20:43:59 +01:00
if (Config.APS) {
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(LoopPlugin.getPlugin(), PluginType.LOOP);
addPreferencesFromResourceIfEnabled(OpenAPSMAPlugin.getPlugin(), PluginType.APS);
addPreferencesFromResourceIfEnabled(OpenAPSAMAPlugin.getPlugin(), PluginType.APS);
addPreferencesFromResourceIfEnabled(OpenAPSSMBPlugin.getPlugin(), PluginType.APS);
2017-09-13 00:35:03 +02:00
}
2017-11-01 20:43:59 +01:00
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(SensitivityAAPSPlugin.getPlugin(), PluginType.SENSITIVITY);
addPreferencesFromResourceIfEnabled(SensitivityWeightedAveragePlugin.getPlugin(), PluginType.SENSITIVITY);
addPreferencesFromResourceIfEnabled(SensitivityOref0Plugin.getPlugin(), PluginType.SENSITIVITY);
2017-11-01 20:43:59 +01:00
if (Config.HWPUMPS) {
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(DanaRPlugin.getPlugin(), PluginType.PUMP);
addPreferencesFromResourceIfEnabled(DanaRKoreanPlugin.getPlugin(), PluginType.PUMP);
addPreferencesFromResourceIfEnabled(DanaRv2Plugin.getPlugin(), PluginType.PUMP);
addPreferencesFromResourceIfEnabled(DanaRSPlugin.getPlugin(), PluginType.PUMP);
if (DanaRPlugin.getPlugin().isEnabled(PluginType.PROFILE)
|| DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PROFILE)
|| DanaRv2Plugin.getPlugin().isEnabled(PluginType.PROFILE)
|| DanaRSPlugin.getPlugin().isEnabled(PluginType.PROFILE)) {
2017-11-01 20:43:59 +01:00
addPreferencesFromResource(R.xml.pref_danarprofile);
}
2017-05-24 18:24:23 +02:00
}
2017-11-29 16:07:26 +01:00
if (!Config.NSCLIENT && !Config.G5UPLOADER) {
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(VirtualPumpPlugin.getPlugin(), PluginType.PUMP);
2017-11-29 16:07:26 +01:00
}
2017-02-18 18:13:13 +01:00
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(InsulinOrefFreePeakPlugin.getPlugin(), PluginType.INSULIN);
2017-11-01 20:43:59 +01:00
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(NSClientPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.getPlugin(), PluginType.GENERAL);
2017-11-01 20:43:59 +01:00
2017-11-29 16:07:26 +01:00
if (!Config.NSCLIENT && !Config.G5UPLOADER) {
2017-11-01 20:43:59 +01:00
addPreferencesFromResource(R.xml.pref_others);
}
addPreferencesFromResource(R.xml.pref_advanced);
2017-04-14 15:33:39 +02:00
2018-03-31 00:36:03 +02:00
addPreferencesFromResourceIfEnabled(WearPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(StatuslinePlugin.getPlugin(), PluginType.GENERAL);
2017-05-06 15:18:19 +02:00
}
2017-04-14 15:33:39 +02:00
initSummary(getPreferenceScreen());
}
2017-11-08 18:02:13 +01:00
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("id", id);
}
2017-04-14 15:33:39 +02:00
public Preference getPreference(String key) {
return findPreference(key);
}
}
2016-11-07 01:02:08 +01:00
}