From f4e82ec022791b79ef6678c7741ed5d6e6f80652 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Fri, 9 Dec 2016 09:56:16 +0100 Subject: [PATCH] lazy loading on optional plugins --- .../Careportal/CareportalFragment.java | 5 ++- .../plugins/DanaR/DanaRFragment.java | 17 ++++---- .../androidaps/plugins/Loop/LoopFragment.java | 21 +++++----- .../Objectives/ObjectivesFragment.java | 39 ++++++++++--------- .../plugins/OpenAPSMA/OpenAPSMAFragment.java | 25 ++++++------ .../SmsCommunicatorFragment.java | 14 ++++--- 6 files changed, 70 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java index 047dd3e7c5..05245bd0b1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java @@ -16,9 +16,12 @@ import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialo public class CareportalFragment extends Fragment implements FragmentBase, View.OnClickListener { - static CareportalPlugin careportalPlugin = new CareportalPlugin(); + static CareportalPlugin careportalPlugin; static public CareportalPlugin getPlugin() { + if (careportalPlugin == null) { + careportalPlugin = new CareportalPlugin(); + } return careportalPlugin; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java index 3595193ce1..4ec54cb37e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java @@ -38,9 +38,12 @@ import info.nightscout.utils.SetWarnColor; public class DanaRFragment extends Fragment implements FragmentBase { private static Logger log = LoggerFactory.getLogger(DanaRFragment.class); - private static DanaRPlugin danaRPlugin = new DanaRPlugin(); + private static DanaRPlugin danaRPlugin; public static DanaRPlugin getPlugin() { + if(danaRPlugin==null){ + danaRPlugin = new DanaRPlugin(); + } return danaRPlugin; } @@ -204,20 +207,20 @@ public class DanaRFragment extends Fragment implements FragmentBase { Long agoMsec = new Date().getTime() - DanaRPlugin.getDanaRPump().lastBolusTime.getTime(); double agoHours = agoMsec / 60d / 60d / 1000d; if (agoHours < 6) // max 6h back - lastBolusView.setText(formatTime.format(DanaRPlugin.getDanaRPump().lastBolusTime) + " (" + DecimalFormatter.to1Decimal(agoHours) + " " + getString(R.string.hoursago) + ") " + DecimalFormatter.to2Decimal(danaRPlugin.getDanaRPump().lastBolusAmount) + " U"); + lastBolusView.setText(formatTime.format(DanaRPlugin.getDanaRPump().lastBolusTime) + " (" + DecimalFormatter.to1Decimal(agoHours) + " " + getString(R.string.hoursago) + ") " + DecimalFormatter.to2Decimal(getPlugin().getDanaRPump().lastBolusAmount) + " U"); else lastBolusView.setText(""); } dailyUnitsView.setText(DecimalFormatter.to0Decimal(DanaRPlugin.getDanaRPump().dailyTotalUnits) + " / " + DanaRPlugin.getDanaRPump().maxDailyTotalUnits + " U"); SetWarnColor.setColor(dailyUnitsView, DanaRPlugin.getDanaRPump().dailyTotalUnits, DanaRPlugin.getDanaRPump().maxDailyTotalUnits * 0.75d, DanaRPlugin.getDanaRPump().maxDailyTotalUnits * 0.9d); - basaBasalRateView.setText("( " + (DanaRPlugin.getDanaRPump().activeProfile + 1) + " ) " + DecimalFormatter.to2Decimal(danaRPlugin.getBaseBasalRate()) + " U/h"); - if (danaRPlugin.isRealTempBasalInProgress()) { - tempBasalView.setText(danaRPlugin.getRealTempBasal().toString()); + basaBasalRateView.setText("( " + (DanaRPlugin.getDanaRPump().activeProfile + 1) + " ) " + DecimalFormatter.to2Decimal(getPlugin().getBaseBasalRate()) + " U/h"); + if (getPlugin().isRealTempBasalInProgress()) { + tempBasalView.setText(getPlugin().getRealTempBasal().toString()); } else { tempBasalView.setText(""); } - if (danaRPlugin.isExtendedBoluslInProgress()) { - extendedBolusView.setText(danaRPlugin.getExtendedBolus().toString()); + if (getPlugin().isExtendedBoluslInProgress()) { + extendedBolusView.setText(getPlugin().getExtendedBolus().toString()); } else { extendedBolusView.setText(""); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java index 329cc7e4d7..94676f8f3e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java @@ -24,9 +24,12 @@ import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui; public class LoopFragment extends Fragment implements View.OnClickListener, FragmentBase { private static Logger log = LoggerFactory.getLogger(LoopFragment.class); - private static LoopPlugin loopPlugin = new LoopPlugin(); + private static LoopPlugin loopPlugin; public static LoopPlugin getPlugin() { + if (loopPlugin == null){ + loopPlugin = new LoopPlugin(); + } return loopPlugin; } @@ -73,7 +76,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Frag public void onClick(View view) { switch (view.getId()) { case R.id.loop_run: - loopPlugin.invoke(true); + getPlugin().invoke(true); break; } @@ -104,13 +107,13 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Frag activity.runOnUiThread(new Runnable() { @Override public void run() { - if (loopPlugin.lastRun != null) { - requestView.setText(loopPlugin.lastRun.request != null ? loopPlugin.lastRun.request.toSpanned() : ""); - constraintsProcessedView.setText(loopPlugin.lastRun.constraintsProcessed != null ? loopPlugin.lastRun.constraintsProcessed.toSpanned() : ""); - setByPumpView.setText(loopPlugin.lastRun.setByPump != null ? loopPlugin.lastRun.setByPump.toSpanned() : ""); - sourceView.setText(loopPlugin.lastRun.source != null ? loopPlugin.lastRun.source : ""); - lastRunView.setText(loopPlugin.lastRun.lastAPSRun != null && loopPlugin.lastRun.lastAPSRun.getTime() != 0 ? loopPlugin.lastRun.lastAPSRun.toLocaleString() : ""); - lastEnactView.setText(loopPlugin.lastRun.lastEnact != null && loopPlugin.lastRun.lastEnact.getTime() != 0 ? loopPlugin.lastRun.lastEnact.toLocaleString() : ""); + if (getPlugin().lastRun != null) { + requestView.setText(getPlugin().lastRun.request != null ? getPlugin().lastRun.request.toSpanned() : ""); + constraintsProcessedView.setText(getPlugin().lastRun.constraintsProcessed != null ? getPlugin().lastRun.constraintsProcessed.toSpanned() : ""); + setByPumpView.setText(getPlugin().lastRun.setByPump != null ? getPlugin().lastRun.setByPump.toSpanned() : ""); + sourceView.setText(getPlugin().lastRun.source != null ? getPlugin().lastRun.source : ""); + lastRunView.setText(getPlugin().lastRun.lastAPSRun != null && getPlugin().lastRun.lastAPSRun.getTime() != 0 ? getPlugin().lastRun.lastAPSRun.toLocaleString() : ""); + lastEnactView.setText(getPlugin().lastRun.lastEnact != null && getPlugin().lastRun.lastEnact.getTime() != 0 ? getPlugin().lastRun.lastEnact.toLocaleString() : ""); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java index a6b866fc77..2321d8a955 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesFragment.java @@ -29,9 +29,12 @@ import info.nightscout.androidaps.interfaces.FragmentBase; public class ObjectivesFragment extends Fragment implements View.OnClickListener, FragmentBase { private static Logger log = LoggerFactory.getLogger(ObjectivesFragment.class); - private static ObjectivesPlugin objectivesPlugin = new ObjectivesPlugin(); + private static ObjectivesPlugin objectivesPlugin; public static ObjectivesPlugin getPlugin() { + if (objectivesPlugin == null) { + objectivesPlugin = new ObjectivesPlugin(); + } return objectivesPlugin; } @@ -67,7 +70,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener @Override public void onBindViewHolder(ObjectiveViewHolder holder, int position) { ObjectivesPlugin.Objective o = objectives.get(position); - ObjectivesPlugin.RequirementResult requirementsMet = objectivesPlugin.requirementsMet(position); + ObjectivesPlugin.RequirementResult requirementsMet = getPlugin().requirementsMet(position); Context context = MainApp.instance().getApplicationContext(); holder.position.setText(String.valueOf(position + 1)); holder.objective.setText(o.objective); @@ -91,7 +94,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener holder.verifyButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ObjectivesPlugin.Objective o = (ObjectivesPlugin.Objective) v.getTag(); - if (objectivesPlugin.requirementsMet(o.num).done || enableFake.isChecked()) { + if (getPlugin().requirementsMet(o.num).done || enableFake.isChecked()) { o.accomplished = new Date(); updateGUI(); ObjectivesPlugin.saveProgress(); @@ -197,26 +200,26 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener }); reset.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { - objectivesPlugin.initializeData(); - objectivesPlugin.saveProgress(); + getPlugin().initializeData(); + getPlugin().saveProgress(); updateGUI(); } }); // Add correct translations to array after app is initialized - objectivesPlugin.objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective); - objectivesPlugin.objectives.get(1).objective = MainApp.sResources.getString(R.string.objectives_1_objective); - objectivesPlugin.objectives.get(2).objective = MainApp.sResources.getString(R.string.objectives_2_objective); - objectivesPlugin.objectives.get(3).objective = MainApp.sResources.getString(R.string.objectives_3_objective); - objectivesPlugin.objectives.get(4).objective = MainApp.sResources.getString(R.string.objectives_4_objective); - objectivesPlugin.objectives.get(5).objective = MainApp.sResources.getString(R.string.objectives_5_objective); - objectivesPlugin.objectives.get(6).objective = MainApp.sResources.getString(R.string.objectives_6_objective); - objectivesPlugin.objectives.get(0).gate = MainApp.sResources.getString(R.string.objectives_0_gate); - objectivesPlugin.objectives.get(1).gate = MainApp.sResources.getString(R.string.objectives_1_gate); - objectivesPlugin.objectives.get(2).gate = MainApp.sResources.getString(R.string.objectives_2_gate); - objectivesPlugin.objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate); - objectivesPlugin.objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate); - objectivesPlugin.objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate); + getPlugin().objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective); + getPlugin().objectives.get(1).objective = MainApp.sResources.getString(R.string.objectives_1_objective); + getPlugin().objectives.get(2).objective = MainApp.sResources.getString(R.string.objectives_2_objective); + getPlugin().objectives.get(3).objective = MainApp.sResources.getString(R.string.objectives_3_objective); + getPlugin().objectives.get(4).objective = MainApp.sResources.getString(R.string.objectives_4_objective); + getPlugin().objectives.get(5).objective = MainApp.sResources.getString(R.string.objectives_5_objective); + getPlugin().objectives.get(6).objective = MainApp.sResources.getString(R.string.objectives_6_objective); + getPlugin().objectives.get(0).gate = MainApp.sResources.getString(R.string.objectives_0_gate); + getPlugin().objectives.get(1).gate = MainApp.sResources.getString(R.string.objectives_1_gate); + getPlugin().objectives.get(2).gate = MainApp.sResources.getString(R.string.objectives_2_gate); + getPlugin().objectives.get(3).gate = MainApp.sResources.getString(R.string.objectives_3_gate); + getPlugin().objectives.get(4).gate = MainApp.sResources.getString(R.string.objectives_4_gate); + getPlugin().objectives.get(5).gate = MainApp.sResources.getString(R.string.objectives_5_gate); updateGUI(); return view; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java index f124b81a56..a3838d53a6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java @@ -24,9 +24,12 @@ import info.nightscout.utils.JSONFormatter; public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, FragmentBase { private static Logger log = LoggerFactory.getLogger(OpenAPSMAFragment.class); - private static OpenAPSMAPlugin openAPSMAPlugin = new OpenAPSMAPlugin(); + private static OpenAPSMAPlugin openAPSMAPlugin; public static OpenAPSMAPlugin getPlugin() { + if(openAPSMAPlugin==null){ + openAPSMAPlugin = new OpenAPSMAPlugin(); + } return openAPSMAPlugin; } @@ -64,7 +67,7 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, public void onClick(View view) { switch (view.getId()) { case R.id.openapsma_run: - openAPSMAPlugin.invoke(); + getPlugin().invoke(); break; } @@ -98,15 +101,15 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, activity.runOnUiThread(new Runnable() { @Override public void run() { - if (openAPSMAPlugin.lastAPSResult != null) { - glucoseStatusView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getGlucoseStatusParam())); - currentTempView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getCurrentTempParam())); - iobDataView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getIobDataParam())); - profileView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getProfileParam())); - mealDataView.setText(JSONFormatter.format(openAPSMAPlugin.lastDetermineBasalAdapterJS.getMealDataParam())); - resultView.setText(JSONFormatter.format(openAPSMAPlugin.lastAPSResult.json)); - requestView.setText(openAPSMAPlugin.lastAPSResult.toSpanned()); - lastRunView.setText(openAPSMAPlugin.lastAPSRun.toLocaleString()); + if (getPlugin().lastAPSResult != null) { + glucoseStatusView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getGlucoseStatusParam())); + currentTempView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getCurrentTempParam())); + iobDataView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getIobDataParam())); + profileView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getProfileParam())); + mealDataView.setText(JSONFormatter.format(getPlugin().lastDetermineBasalAdapterJS.getMealDataParam())); + resultView.setText(JSONFormatter.format(getPlugin().lastAPSResult.json)); + requestView.setText(getPlugin().lastAPSResult.toSpanned()); + lastRunView.setText(getPlugin().lastAPSRun.toLocaleString()); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java index 93383032d7..852562ddda 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java @@ -29,9 +29,13 @@ import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommuni public class SmsCommunicatorFragment extends Fragment { private static Logger log = LoggerFactory.getLogger(SmsCommunicatorFragment.class); - private static SmsCommunicatorPlugin smsCommunicatorPlugin = new SmsCommunicatorPlugin(); + private static SmsCommunicatorPlugin smsCommunicatorPlugin; public static SmsCommunicatorPlugin getPlugin() { + + if(smsCommunicatorPlugin==null){ + smsCommunicatorPlugin = new SmsCommunicatorPlugin(); + } return smsCommunicatorPlugin; } @@ -81,15 +85,15 @@ public class SmsCommunicatorFragment extends Fragment { return (int) (object1.date.getTime() - object2.date.getTime()); } } - Collections.sort(smsCommunicatorPlugin.messages, new CustomComparator()); + Collections.sort(getPlugin().messages, new CustomComparator()); int messagesToShow = 40; - int start = Math.max(0, smsCommunicatorPlugin.messages.size() - messagesToShow); + int start = Math.max(0, getPlugin().messages.size() - messagesToShow); DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); String logText = ""; - for (int x = start; x < smsCommunicatorPlugin.messages.size(); x++) { - SmsCommunicatorPlugin.Sms sms = smsCommunicatorPlugin.messages.get(x); + for (int x = start; x < getPlugin().messages.size(); x++) { + SmsCommunicatorPlugin.Sms sms = getPlugin().messages.get(x); if (sms.received) { logText += df.format(sms.date) + " <<< " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; } else if (sms.sent) {