diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981032..fbb68289f4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3073b2cc8d..c9f14c62c5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -34,12 +34,12 @@ - + @@ -50,15 +50,9 @@ - - - - - - + + + @@ -66,16 +60,6 @@ - - - - - - - pluginsList = null; - - private static ConfigBuilderFragment configBuilderFragment; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -83,35 +80,13 @@ public class MainActivity extends AppCompatActivity { } toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); + registerBus(); - if (pluginsList == null) { - pluginsList = new ArrayList(); - // Register all tabs in app here - pluginsList.add(OverviewFragment.newInstance()); - if (Config.DANAR) pluginsList.add(DanaRFragment.newInstance()); - if (Config.MM640G) pluginsList.add(MM640gFragment.newInstance()); - pluginsList.add(VirtualPumpFragment.newInstance()); - if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.newInstance()); - if (Config.LOOPENABLED) pluginsList.add(LoopFragment.newInstance()); - if (Config.LOWSUSPEDENABLED) pluginsList.add(LowSuspendFragment.newInstance()); - if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.newInstance()); - pluginsList.add(NSProfileViewerFragment.newInstance()); - pluginsList.add(SimpleProfileFragment.newInstance()); - pluginsList.add(TreatmentsFragment.newInstance()); - pluginsList.add(TempBasalsFragment.newInstance()); - pluginsList.add(SafetyFragment.newInstance()); - if (Config.OBJECTIVESENABLED) pluginsList.add(ObjectivesFragment.newInstance()); - pluginsList.add(SourceXdripFragment.newInstance()); - pluginsList.add(SourceNSClientFragment.newInstance()); - if (Config.SMSCOMMUNICATORENABLED) pluginsList.add(SmsCommunicatorFragment.newInstance()); - pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance()); + if (keepAliveReceiver == null) { - registerBus(); keepAliveReceiver = new KeepAliveReceiver(); + startService(new Intent(this, ExecutionService.class)); keepAliveReceiver.setAlarm(this); - - configBuilderFragment.initialize(); - MainApp.setConfigBuilder(configBuilderFragment); } setUpTabs(false); } @@ -130,7 +105,7 @@ public class MainActivity extends AppCompatActivity { private void setUpTabs(boolean switchToLast) { pageAdapter = new TabPageAdapter(getSupportFragmentManager()); - for (PluginBase f : pluginsList) { + for (PluginBase f : MainApp.getPluginsList()) { pageAdapter.registerNewFragment((Fragment) f); } mPager = (ViewPager) findViewById(R.id.pager); @@ -215,54 +190,4 @@ public class MainActivity extends AppCompatActivity { } } - public static ArrayList getPluginsList() { - return pluginsList; - } - - public static ArrayList getSpecificPluginsList(int type) { - ArrayList newList = new ArrayList(); - - if (pluginsList != null) { - Iterator it = pluginsList.iterator(); - while (it.hasNext()) { - PluginBase p = it.next(); - if (p.getType() == type) - newList.add(p); - } - } else { - log.error("pluginsList=null"); - } - return newList; - } - - public static ArrayList getSpecificPluginsListByInterface(Class interfaceClass) { - ArrayList newList = new ArrayList(); - - if (pluginsList != null) { - Iterator it = pluginsList.iterator(); - while (it.hasNext()) { - PluginBase p = it.next(); - if (p.getClass() != ConfigBuilderFragment.class && interfaceClass.isAssignableFrom(p.getClass())) - newList.add(p); - } - } else { - log.error("pluginsList=null"); - } - return newList; - } - - @Nullable - public static PluginBase getSpecificPlugin(Class pluginClass) { - if (pluginsList != null) { - Iterator it = pluginsList.iterator(); - while (it.hasNext()) { - PluginBase p = it.next(); - if (p.getClass() == pluginClass) - return p; - } - } else { - log.error("pluginsList=null"); - } - return null; - } } diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 2b1a480552..f7d78758b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -2,16 +2,38 @@ package info.nightscout.androidaps; import android.app.Application; import android.content.res.Resources; +import android.support.annotation.Nullable; import com.crashlytics.android.Crashlytics; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.squareup.otto.Bus; import com.squareup.otto.ThreadEnforcer; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; +import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; +import info.nightscout.androidaps.plugins.Loop.LoopFragment; +import info.nightscout.androidaps.plugins.LowSuspend.LowSuspendFragment; +import info.nightscout.androidaps.plugins.MM640g.MM640gFragment; +import info.nightscout.androidaps.plugins.NSProfileViewer.NSProfileViewerFragment; +import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment; +import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAFragment; +import info.nightscout.androidaps.plugins.Overview.OverviewFragment; +import info.nightscout.androidaps.plugins.SafetyFragment.SafetyFragment; +import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfileFragment; +import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment; +import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; +import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment; +import info.nightscout.androidaps.plugins.TempBasals.TempBasalsFragment; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; +import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpFragment; import io.fabric.sdk.android.Fabric; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Iterator; + import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; @@ -26,6 +48,8 @@ public class MainApp extends Application { private static DatabaseHelper sDatabaseHelper = null; private static ConfigBuilderFragment sConfigBuilder = null; + private static ArrayList pluginsList = null; + @Override public void onCreate() { super.onCreate(); @@ -34,6 +58,31 @@ public class MainApp extends Application { sBus = new Bus(ThreadEnforcer.ANY); sInstance = this; sResources = getResources(); + + if (pluginsList == null) { + pluginsList = new ArrayList(); + // Register all tabs in app here + pluginsList.add(OverviewFragment.newInstance()); + if (Config.DANAR) pluginsList.add(DanaRFragment.newInstance()); + if (Config.MM640G) pluginsList.add(MM640gFragment.newInstance()); + pluginsList.add(VirtualPumpFragment.newInstance()); + if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.newInstance()); + if (Config.LOOPENABLED) pluginsList.add(LoopFragment.newInstance()); + if (Config.LOWSUSPEDENABLED) pluginsList.add(LowSuspendFragment.newInstance()); + if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.newInstance()); + pluginsList.add(NSProfileViewerFragment.newInstance()); + pluginsList.add(SimpleProfileFragment.newInstance()); + pluginsList.add(TreatmentsFragment.newInstance()); + pluginsList.add(TempBasalsFragment.newInstance()); + pluginsList.add(SafetyFragment.newInstance()); + if (Config.OBJECTIVESENABLED) pluginsList.add(ObjectivesFragment.newInstance()); + pluginsList.add(SourceXdripFragment.newInstance()); + pluginsList.add(SourceNSClientFragment.newInstance()); + if (Config.SMSCOMMUNICATORENABLED) pluginsList.add(SmsCommunicatorFragment.newInstance()); + pluginsList.add(sConfigBuilder = ConfigBuilderFragment.newInstance()); + + MainApp.getConfigBuilder().initialize(); + } } public static Bus bus() { @@ -65,6 +114,57 @@ public class MainApp extends Application { return sConfigBuilder; } + public static ArrayList getPluginsList() { + return pluginsList; + } + + public static ArrayList getSpecificPluginsList(int type) { + ArrayList newList = new ArrayList(); + + if (pluginsList != null) { + Iterator it = pluginsList.iterator(); + while (it.hasNext()) { + PluginBase p = it.next(); + if (p.getType() == type) + newList.add(p); + } + } else { + log.error("pluginsList=null"); + } + return newList; + } + + public static ArrayList getSpecificPluginsListByInterface(Class interfaceClass) { + ArrayList newList = new ArrayList(); + + if (pluginsList != null) { + Iterator it = pluginsList.iterator(); + while (it.hasNext()) { + PluginBase p = it.next(); + if (p.getClass() != ConfigBuilderFragment.class && interfaceClass.isAssignableFrom(p.getClass())) + newList.add(p); + } + } else { + log.error("pluginsList=null"); + } + return newList; + } + + @Nullable + public static PluginBase getSpecificPlugin(Class pluginClass) { + if (pluginsList != null) { + Iterator it = pluginsList.iterator(); + while (it.hasNext()) { + PluginBase p = it.next(); + if (p.getClass() == pluginClass) + return p; + } + } else { + log.error("pluginsList=null"); + } + return null; + } + @Override public void onTerminate() { super.onTerminate(); diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index 20ba73d57e..d4b06d3ed0 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -89,8 +89,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre addPreferencesFromResource(R.xml.pref_nightscout); if (Config.DANAR) { addPreferencesFromResource(R.xml.pref_danar); - DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); - if (danaRFragment != null && danaRFragment.isEnabled(PluginBase.PROFILE)) { + DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); + if (danaRFragment.isEnabled(PluginBase.PROFILE)) { addPreferencesFromResource(R.xml.pref_danarprofile); } } diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index e7fc4b64f4..fb06d98cd8 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -7,7 +7,6 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.provider.Telephony; import android.support.annotation.Nullable; -import android.telephony.SmsMessage; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; @@ -23,9 +22,12 @@ import org.slf4j.LoggerFactory; import java.sql.SQLException; import java.util.Date; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; @@ -34,7 +36,6 @@ import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment; @@ -43,8 +44,7 @@ import info.nightscout.androidaps.plugins.SmsCommunicator.Events.EventNewSMS; import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment; import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment; -import info.nightscout.androidaps.receivers.NSClientDataReceiver; -import info.nightscout.androidaps.receivers.xDripReceiver; +import info.nightscout.androidaps.receivers.DataReceiver; import info.nightscout.client.data.NSProfile; import info.nightscout.client.data.NSSgv; import info.nightscout.utils.ToastUtils; @@ -57,14 +57,18 @@ public class DataService extends IntentService { boolean nsClientEnabled = true; SmsCommunicatorFragment smsCommunicatorFragment = null; + private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); + private static ScheduledFuture scheduledDisconnection = null; + public DataService() { super("DataService"); + registerBus(); } @Override - protected void onHandleIntent(Intent intent) { + protected void onHandleIntent(final Intent intent) { if (Config.logFunctionCalls) - log.debug("onHandleIntent"); + log.debug("onHandleIntent " + intent); if (MainApp.getConfigBuilder() != null) { if (MainApp.getConfigBuilder().getActiveBgSource().getClass().equals(SourceXdripFragment.class)) { @@ -76,8 +80,8 @@ public class DataService extends IntentService { nsClientEnabled = true; } - if (MainActivity.getSpecificPlugin(SmsCommunicatorFragment.class) != null) { - smsCommunicatorFragment = (SmsCommunicatorFragment) MainActivity.getSpecificPlugin(SmsCommunicatorFragment.class); + if (MainApp.getSpecificPlugin(SmsCommunicatorFragment.class) != null) { + smsCommunicatorFragment = (SmsCommunicatorFragment) MainApp.getSpecificPlugin(SmsCommunicatorFragment.class); } } @@ -86,7 +90,6 @@ public class DataService extends IntentService { if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { if (xDripEnabled) handleNewDataFromXDrip(intent); - xDripReceiver.completeWakefulIntent(intent); } else if (Intents.ACTION_NEW_PROFILE.equals(action) || Intents.ACTION_NEW_TREATMENT.equals(action) || Intents.ACTION_CHANGED_TREATMENT.equals(action) || @@ -98,14 +101,16 @@ public class DataService extends IntentService { Intents.ACTION_NEW_MBG.equals(action) ) { handleNewDataFromNSClient(intent); - NSClientDataReceiver.completeWakefulIntent(intent); } else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) { handleNewSMS(intent); - NSClientDataReceiver.completeWakefulIntent(intent); } + DataReceiver.completeWakefulIntent(intent); } + if (Config.logFunctionCalls) + log.debug("onHandleIntent exit"); } +/* @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); @@ -113,12 +118,13 @@ public class DataService extends IntentService { if (Config.logFunctionCalls) log.debug("onStartCommand"); - registerBus(); return START_STICKY; } +*/ @Override public void onDestroy() { + super.onDestroy(); MainApp.bus().unregister(this); } @@ -163,6 +169,8 @@ public class DataService extends IntentService { private void handleNewDataFromNSClient(Intent intent) { Bundle bundles = intent.getExtras(); if (bundles == null) return; + if (Config.logIncommingData) + log.debug("Got intent: " + intent.getAction()); if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) { @@ -176,7 +184,7 @@ public class DataService extends IntentService { configBuilderFragment.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117 configBuilderFragment.nsClientVersionName = bundles.getString("nsclientversionname"); log.debug("Got versions: NSClient: " + configBuilderFragment.nsClientVersionName + " Nightscout: " + configBuilderFragment.nightscoutVersionName); - if (configBuilderFragment.nsClientVersionCode < 117) + if (configBuilderFragment.nsClientVersionCode < 118) ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.unsupportedclientver)); } } else { @@ -189,7 +197,7 @@ public class DataService extends IntentService { JSONObject settings = statusJson.getJSONObject("settings"); if (settings.has("thresholds")) { JSONObject thresholds = settings.getJSONObject("thresholds"); - OverviewFragment overviewFragment = (OverviewFragment) MainActivity.getSpecificPlugin(OverviewFragment.class); + OverviewFragment overviewFragment = (OverviewFragment) MainApp.getSpecificPlugin(OverviewFragment.class); if (overviewFragment != null && thresholds.has("bgTargetTop")) { overviewFragment.bgTargetHigh = thresholds.getDouble("bgTargetTop"); } @@ -213,7 +221,7 @@ public class DataService extends IntentService { JSONObject devicestatusJson = jsonArray.getJSONObject(0); if (devicestatusJson.has("pump")) { // Objectives 0 - ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainActivity.getSpecificPlugin(ObjectivesFragment.class); + ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainApp.getSpecificPlugin(ObjectivesFragment.class); if (objectivesFragment != null) { objectivesFragment.pumpStatusIsAvailableInNS = true; objectivesFragment.saveProgress(); @@ -232,7 +240,6 @@ public class DataService extends IntentService { String activeProfile = bundles.getString("activeprofile"); String profile = bundles.getString("profile"); NSProfile nsProfile = new NSProfile(new JSONObject(profile), activeProfile); - EventNewBasalProfile event = new EventNewBasalProfile(nsProfile); if (MainApp.getConfigBuilder() == null) { log.error("Config builder not ready on receive profile"); return; @@ -247,61 +254,27 @@ public class DataService extends IntentService { } if (Config.logIncommingData) log.debug("Received profile: " + activeProfile + " " + profile); - MainApp.bus().post(event); + MainApp.bus().post(new EventNewBasalProfile(nsProfile)); } catch (JSONException e) { e.printStackTrace(); } } if (intent.getAction().equals(Intents.ACTION_NEW_TREATMENT)) { try { - String trstring = bundles.getString("treatment"); - JSONObject trJson = new JSONObject(trstring); - if (!trJson.has("insulin") && !trJson.has("carbs")) { - if (Config.logIncommingData) - log.debug("ADD: Uninterested treatment: " + trstring); - return; + if (bundles.containsKey("treatment")) { + String trstring = bundles.getString("treatment"); + handleAddedTreatment(trstring); } - - Treatment stored = null; - trJson = new JSONObject(trstring); - String _id = trJson.getString("_id"); - - if (trJson.has("timeIndex")) { - if (Config.logIncommingData) - log.debug("ADD: timeIndex found: " + trstring); - stored = findByTimeIndex(trJson.getLong("timeIndex")); - } else { - stored = findById(_id); - } - - if (stored != null) { - if (Config.logIncommingData) - log.debug("ADD: Existing treatment: " + trstring); - if (trJson.has("timeIndex")) { - stored._id = _id; - MainApp.getDbHelper().getDaoTreatments().update(stored); + if (bundles.containsKey("treatments")) { + String trstring = bundles.getString("treatments"); + JSONArray jsonArray = new JSONArray(trstring); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject trJson = jsonArray.getJSONObject(i); + String trstr = trJson.toString(); + handleAddedTreatment(trstr); } - MainApp.bus().post(new EventTreatmentChange()); - return; - } else { - if (Config.logIncommingData) - log.debug("ADD: New treatment: " + trstring); - Treatment treatment = new Treatment(); - treatment._id = _id; - treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0; - treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d; - treatment.created_at = new Date(trJson.getLong("mills")); - treatment.setTimeIndex(treatment.getTimeIndex()); - try { - MainApp.getDbHelper().getDaoTreatments().createOrUpdate(treatment); - if (Config.logIncommingData) - log.debug("ADD: Stored treatment: " + treatment.log()); - } catch (SQLException e) { - e.printStackTrace(); - } - MainApp.bus().post(new EventTreatmentChange()); } - + scheduleTreatmentChange(); } catch (Exception e) { e.printStackTrace(); } @@ -310,55 +283,20 @@ public class DataService extends IntentService { if (intent.getAction().equals(Intents.ACTION_CHANGED_TREATMENT)) { try { - String trstring = bundles.getString("treatment"); - JSONObject trJson = new JSONObject(trstring); - if (!trJson.has("insulin") && !trJson.has("carbs")) { - if (Config.logIncommingData) - log.debug("CHANGE: Uninterested treatment: " + trstring); - return; + if (bundles.containsKey("treatment")) { + String trstring = bundles.getString("treatment"); + handleChangedTreatment(trstring); } - trJson = new JSONObject(trstring); - String _id = trJson.getString("_id"); - - Treatment stored; - - if (trJson.has("timeIndex")) { - if (Config.logIncommingData) - log.debug("ADD: timeIndex found: " + trstring); - stored = findByTimeIndex(trJson.getLong("timeIndex")); - } else { - stored = findById(_id); - } - - if (stored != null) { - if (Config.logIncommingData) - log.debug("CHANGE: Existing treatment: " + trstring); - stored._id = _id; - stored.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0; - stored.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d; - stored.created_at = new Date(trJson.getLong("mills")); - MainApp.getDbHelper().getDaoTreatments().update(stored); - MainApp.bus().post(new EventTreatmentChange()); - } else { - if (Config.logIncommingData) - log.debug("CHANGE: New treatment: " + trstring); - Treatment treatment = new Treatment(); - treatment._id = _id; - treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0; - treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d; - //treatment.created_at = DateUtil.fromISODateString(trJson.getString("created_at")); - treatment.created_at = new Date(trJson.getLong("mills")); - treatment.setTimeIndex(treatment.getTimeIndex()); - try { - MainApp.getDbHelper().getDaoTreatments().create(treatment); - if (Config.logIncommingData) - log.debug("CHANGE: Stored treatment: " + treatment.log()); - } catch (SQLException e) { - e.printStackTrace(); + if (bundles.containsKey("treatments")) { + String trstring = bundles.getString("treatments"); + JSONArray jsonArray = new JSONArray(trstring); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject trJson = jsonArray.getJSONObject(i); + String trstr = trJson.toString(); + handleChangedTreatment(trstr); } - MainApp.bus().post(new EventTreatmentChange()); } - + scheduleTreatmentChange(); } catch (Exception e) { e.printStackTrace(); } @@ -382,8 +320,7 @@ public class DataService extends IntentService { removeTreatmentFromDb(_id); } } - MainApp.bus().post(new EventTreatmentChange()); - + scheduleTreatmentChange(); } catch (Exception e) { e.printStackTrace(); } @@ -430,12 +367,110 @@ public class DataService extends IntentService { MainApp.bus().post(new EventNewBG()); } // Objectives 0 - ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainActivity.getSpecificPlugin(ObjectivesFragment.class); + ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainApp.getSpecificPlugin(ObjectivesFragment.class); if (objectivesFragment != null) { objectivesFragment.bgIsAvailableInNS = true; objectivesFragment.saveProgress(); } } + + if (intent.getAction().equals(Intents.ACTION_NEW_MBG)) { + log.error("Not implemented yet"); // TODO implemeng MBGS + } + } + + private void handleAddedTreatment(String trstring) throws JSONException, SQLException { + JSONObject trJson = new JSONObject(trstring); + if (!trJson.has("insulin") && !trJson.has("carbs")) { + if (Config.logIncommingData) + log.debug("ADD: Uninterested treatment: " + trstring); + return; + } + + Treatment stored = null; + trJson = new JSONObject(trstring); + String _id = trJson.getString("_id"); + + if (trJson.has("timeIndex")) { + if (Config.logIncommingData) + log.debug("ADD: timeIndex found: " + trstring); + stored = findByTimeIndex(trJson.getLong("timeIndex")); + } else { + stored = findById(_id); + } + + if (stored != null) { + if (Config.logIncommingData) + log.debug("ADD: Existing treatment: " + trstring); + if (trJson.has("timeIndex")) { + stored._id = _id; + int updated = MainApp.getDbHelper().getDaoTreatments().update(stored); + if (Config.logIncommingData) + log.debug("Records updated: " + updated); + } + return; + } else { + if (Config.logIncommingData) + log.debug("ADD: New treatment: " + trstring); + Treatment treatment = new Treatment(); + treatment._id = _id; + treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0; + treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d; + treatment.created_at = new Date(trJson.getLong("mills")); + treatment.setTimeIndex(treatment.getTimeIndex()); + try { + MainApp.getDbHelper().getDaoTreatments().createOrUpdate(treatment); + if (Config.logIncommingData) + log.debug("ADD: Stored treatment: " + treatment.log()); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + private void handleChangedTreatment(String trstring) throws JSONException, SQLException { + JSONObject trJson = new JSONObject(trstring); + if (!trJson.has("insulin") && !trJson.has("carbs")) { + if (Config.logIncommingData) + log.debug("CHANGE: Uninterested treatment: " + trstring); + return; + } + String _id = trJson.getString("_id"); + + Treatment stored; + + if (trJson.has("timeIndex")) { + if (Config.logIncommingData) + log.debug("ADD: timeIndex found: " + trstring); + stored = findByTimeIndex(trJson.getLong("timeIndex")); + } else { + stored = findById(_id); + } + + if (stored != null) { + if (Config.logIncommingData) + log.debug("CHANGE: Removing old: " + trstring); + removeTreatmentFromDb(_id); + } + + if (Config.logIncommingData) + log.debug("CHANGE: Adding new treatment: " + trstring); + Treatment treatment = new Treatment(); + treatment._id = _id; + treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0; + treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d; + //treatment.created_at = DateUtil.fromISODateString(trJson.getString("created_at")); + treatment.created_at = new Date(trJson.getLong("mills")); + treatment.setTimeIndex(treatment.getTimeIndex()); + try { + Dao.CreateOrUpdateStatus status = MainApp.getDbHelper().getDaoTreatments().createOrUpdate(treatment); + if (Config.logIncommingData) + log.debug("Records updated: " + status.getNumLinesChanged()); + if (Config.logIncommingData) + log.debug("CHANGE: Stored treatment: " + treatment.log()); + } catch (SQLException e) { + e.printStackTrace(); + } } @Nullable @@ -489,8 +524,10 @@ public class DataService extends IntentService { Treatment stored = findById(_id); if (stored != null) { log.debug("REMOVE: Existing treatment (removing): " + _id); - MainApp.getDbHelper().getDaoTreatments().delete(stored); - MainApp.bus().post(new EventTreatmentChange()); + int removed = MainApp.getDbHelper().getDaoTreatments().delete(stored); + if (Config.logIncommingData) + log.debug("Records removed: " + removed); + scheduleTreatmentChange(); } else { log.debug("REMOVE: Not stored treatment (ignoring): " + _id); } @@ -502,4 +539,27 @@ public class DataService extends IntentService { MainApp.bus().post(new EventNewSMS(bundle)); } + public void scheduleTreatmentChange() { +/* + class DisconnectRunnable implements Runnable { + public void run() { + if (Config.logIncommingData) + log.debug("Firing EventTreatmentChange"); + MainApp.bus().post(new EventTreatmentChange()); + scheduledDisconnection = null; + } + } + // prepare task for execution in 5 sec + // cancel waiting task to prevent sending multiple disconnections + if (scheduledDisconnection != null) + scheduledDisconnection.cancel(false); + Runnable task = new DisconnectRunnable(); + final int sec = 5; + scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS); + log.debug("Scheduling EventTreatmentChange"); +*/ + MainApp.bus().post(new EventTreatmentChange()); + } + + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java index 276575d326..93495a93af 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java @@ -113,7 +113,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } public void initialize() { - pluginList = MainActivity.getPluginsList(); + pluginList = MainApp.getPluginsList(); loadSettings(); } @@ -158,35 +158,35 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } void setViews() { - bgsourceDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsListByInterface(BgSourceInterface.class), PluginBase.BGSOURCE); + bgsourceDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class), PluginBase.BGSOURCE); bgsourceListView.setAdapter(bgsourceDataAdapter); setListViewHeightBasedOnChildren(bgsourceListView); - pumpDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsList(PluginBase.PUMP), PluginBase.PUMP); + pumpDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsList(PluginBase.PUMP), PluginBase.PUMP); pumpListView.setAdapter(pumpDataAdapter); setListViewHeightBasedOnChildren(pumpListView); - loopDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsList(PluginBase.LOOP), PluginBase.LOOP); + loopDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsList(PluginBase.LOOP), PluginBase.LOOP); loopListView.setAdapter(loopDataAdapter); setListViewHeightBasedOnChildren(loopListView); - if (MainActivity.getSpecificPluginsList(PluginBase.LOOP).size() == 0) + if (MainApp.getSpecificPluginsList(PluginBase.LOOP).size() == 0) loopLabel.setVisibility(View.GONE); - treatmentsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsList(PluginBase.TREATMENT), PluginBase.TREATMENT); + treatmentsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsList(PluginBase.TREATMENT), PluginBase.TREATMENT); treatmentsListView.setAdapter(treatmentsDataAdapter); setListViewHeightBasedOnChildren(treatmentsListView); - tempsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsList(PluginBase.TEMPBASAL), PluginBase.TEMPBASAL); + tempsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsList(PluginBase.TEMPBASAL), PluginBase.TEMPBASAL); tempsListView.setAdapter(tempsDataAdapter); setListViewHeightBasedOnChildren(tempsListView); - profileDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsListByInterface(ProfileInterface.class), PluginBase.PROFILE); + profileDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsListByInterface(ProfileInterface.class), PluginBase.PROFILE); profileListView.setAdapter(profileDataAdapter); setListViewHeightBasedOnChildren(profileListView); - apsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsList(PluginBase.APS), PluginBase.APS); + apsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsList(PluginBase.APS), PluginBase.APS); apsListView.setAdapter(apsDataAdapter); setListViewHeightBasedOnChildren(apsListView); - if (MainActivity.getSpecificPluginsList(PluginBase.APS).size() == 0) + if (MainApp.getSpecificPluginsList(PluginBase.APS).size() == 0) apsLabel.setVisibility(View.GONE); - constraintsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class), PluginBase.CONSTRAINTS); + constraintsDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class), PluginBase.CONSTRAINTS); constraintsListView.setAdapter(constraintsDataAdapter); setListViewHeightBasedOnChildren(constraintsListView); - generalDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainActivity.getSpecificPluginsList(PluginBase.GENERAL), PluginBase.GENERAL); + generalDataAdapter = new PluginCustomAdapter(getContext(), R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsList(PluginBase.GENERAL), PluginBase.GENERAL); generalListView.setAdapter(generalDataAdapter); setListViewHeightBasedOnChildren(generalListView); @@ -650,15 +650,15 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI break; // Single selection allowed case PluginBase.PROFILE: - pluginsInCategory = MainActivity.getSpecificPluginsListByInterface(ProfileInterface.class); + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class); break; case PluginBase.BGSOURCE: - pluginsInCategory = MainActivity.getSpecificPluginsListByInterface(BgSourceInterface.class); + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class); break; case PluginBase.TEMPBASAL: case PluginBase.TREATMENT: case PluginBase.PUMP: - pluginsInCategory = MainActivity.getSpecificPluginsList(category); + pluginsInCategory = MainApp.getSpecificPluginsList(category); break; } if (pluginsInCategory != null) { @@ -683,7 +683,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI ArrayList pluginsInCategory; // PluginBase.PROFILE - pluginsInCategory = MainActivity.getSpecificPluginsListByInterface(ProfileInterface.class); + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class); activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PROFILE); if (Config.logConfigBuilder) log.debug("Selected profile interface: " + ((PluginBase) activeProfile).getName()); @@ -694,7 +694,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } // PluginBase.BGSOURCE - pluginsInCategory = MainActivity.getSpecificPluginsListByInterface(BgSourceInterface.class); + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class); activeBgSource = (BgSourceInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.BGSOURCE); if (Config.logConfigBuilder) log.debug("Selected bgSource interface: " + ((PluginBase) activeBgSource).getName()); @@ -705,7 +705,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } // PluginBase.PUMP - pluginsInCategory = MainActivity.getSpecificPluginsList(PluginBase.PUMP); + pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.PUMP); activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PUMP); if (Config.logConfigBuilder) log.debug("Selected pump interface: " + ((PluginBase) activePump).getName()); @@ -716,7 +716,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } // PluginBase.LOOP - pluginsInCategory = MainActivity.getSpecificPluginsList(PluginBase.LOOP); + pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.LOOP); activeLoop = (LoopFragment) getTheOneEnabledInArray(pluginsInCategory, PluginBase.LOOP); if (activeLoop != null) { if (Config.logConfigBuilder) @@ -729,7 +729,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } // PluginBase.TEMPBASAL - pluginsInCategory = MainActivity.getSpecificPluginsList(PluginBase.TEMPBASAL); + pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TEMPBASAL); activeTempBasals = (TempBasalsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TEMPBASAL); if (Config.logConfigBuilder) log.debug("Selected tempbasal interface: " + ((PluginBase) activeTempBasals).getName()); @@ -740,7 +740,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } // PluginBase.TREATMENT - pluginsInCategory = MainActivity.getSpecificPluginsList(PluginBase.TREATMENT); + pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TREATMENT); activeTreatments = (TreatmentsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TREATMENT); if (Config.logConfigBuilder) log.debug("Selected treatment interface: " + ((PluginBase) activeTreatments).getName()); @@ -839,7 +839,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI public boolean isLoopEnabled() { boolean result = true; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -852,7 +852,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI public boolean isClosedModeEnabled() { boolean result = true; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -865,7 +865,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI public boolean isAutosensModeEnabled() { boolean result = true; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -878,7 +878,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI public boolean isAMAModeEnabled() { boolean result = true; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -890,7 +890,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI @Override public Double applyBasalConstraints(Double absoluteRate) { Double rateAfterConstrain = absoluteRate; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -902,7 +902,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI @Override public Integer applyBasalConstraints(Integer percentRate) { Integer rateAfterConstrain = percentRate; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -914,7 +914,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI @Override public Double applyBolusConstraints(Double insulin) { Double insulinAfterConstrain = insulin; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -926,7 +926,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI @Override public Integer applyCarbsConstraints(Integer carbs) { Integer carbsAfterConstrain = carbs; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; @@ -938,7 +938,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI @Override public Double applyMaxIOBConstraints(Double maxIob) { Double maxIobAfterConstrain = maxIob; - ArrayList constraintsPlugins = MainActivity.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; 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 44a77ba3f5..f189dc9f4e 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 @@ -195,6 +195,7 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { + log.debug("Service is disconnected"); mBounded = false; mExecutionService = null; } @@ -321,6 +322,10 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface @Override public void setNewBasalProfile(NSProfile profile) { + if (mExecutionService == null) { + log.error("setNewBasalProfile mExecutionService is null"); + return; + } if (!mExecutionService.updateBasalsInPump(profile)) ToastUtils.showToastInUiThread(getContext(), MainApp.sResources.getString(R.string.failedupdatebasalprofile)); } @@ -705,7 +710,7 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface } public static void doConnect(String from) { - mExecutionService.connect(from); + if (mExecutionService != null) mExecutionService.connect(from); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java index ed9980b68f..4ef5654983 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Dialogs/ProfileViewDialog.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import java.util.Date; import info.nightscout.androidaps.MainActivity; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; import info.nightscout.client.data.NSProfile; @@ -48,7 +49,7 @@ public class ProfileViewDialog extends DialogFragment { mHandlerThread.start(); mHandler = new Handler(mHandlerThread.getLooper()); - profile = ((DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class)).getProfile(); + profile = ((DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class)).getProfile(); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java index 6587b0c3e0..8896ffc364 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java @@ -104,11 +104,9 @@ public class ExecutionService extends Service { public ExecutionService() { registerBus(); MainApp.instance().getApplicationContext().registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); - DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); - if (danaRFragment != null) - danaRPump = danaRFragment.getDanaRPump(); - else - log.error("danaRFragment is null"); + DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); + danaRPump = danaRFragment.getDanaRPump(); + PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE); mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DanaConnection"); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBolusExtended.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBolusExtended.java index 2d3221e61a..4ac4676b23 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBolusExtended.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusBolusExtended.java @@ -63,7 +63,7 @@ public class MsgStatusBolusExtended extends MessageBase { } public static void updateExtendedBolusInDB() { - DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); + DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); DanaRPump danaRPump = danaRFragment.getDanaRPump(); Date now = new Date(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusTempBasal.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusTempBasal.java index 4cd69c756a..a166292488 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusTempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgStatusTempBasal.java @@ -54,7 +54,7 @@ public class MsgStatusTempBasal extends MessageBase { } public static void updateTempBasalInDB() { - DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); + DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); DanaRPump danaRPump = danaRFragment.getDanaRPump(); Date now = new Date(); 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 21e53db419..009b10f067 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 @@ -242,7 +242,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug return; APSInterface usedAPS = null; - ArrayList apsPlugins = MainActivity.getSpecificPluginsList(PluginBase.APS); + ArrayList apsPlugins = MainApp.getSpecificPluginsList(PluginBase.APS); for (PluginBase p : apsPlugins) { APSInterface aps = (APSInterface) p; if (!p.isEnabled(PluginBase.APS)) continue; 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 6d23956181..a663d4f3a0 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 @@ -62,7 +62,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener @Override public boolean isVisibleInTabs(int type) { - LoopFragment loopFragment = (LoopFragment) MainActivity.getSpecificPlugin(LoopFragment.class); + LoopFragment loopFragment = (LoopFragment) MainApp.getSpecificPlugin(LoopFragment.class); return fragmentVisible && loopFragment != null && loopFragment.isVisibleInTabs(type); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 7b94253f90..7984eeff2c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -263,7 +263,7 @@ public class OverviewFragment extends Fragment implements PluginBase { finalLastRun.lastEnact = new Date(); finalLastRun.lastOpenModeAccept = new Date(); MainApp.getConfigBuilder().uploadDeviceStatus(); - ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainActivity.getSpecificPlugin(ObjectivesFragment.class); + ObjectivesFragment objectivesFragment = (ObjectivesFragment) MainApp.getSpecificPlugin(ObjectivesFragment.class); if (objectivesFragment != null) { objectivesFragment.manualEnacts++; objectivesFragment.saveProgress(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Receivers/SmsReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Receivers/SmsReceiver.java deleted file mode 100644 index 7a416afe7d..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Receivers/SmsReceiver.java +++ /dev/null @@ -1,29 +0,0 @@ -package info.nightscout.androidaps.plugins.SmsCommunicator.Receivers; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.support.v4.content.WakefulBroadcastReceiver; -import android.telephony.SmsMessage; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.MainActivity; -import info.nightscout.androidaps.Services.DataService; -import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment; - -public class SmsReceiver extends WakefulBroadcastReceiver { - private static Logger log = LoggerFactory.getLogger(SmsReceiver.class); - - @Override - public void onReceive(Context context, Intent intent) { - if (Config.logFunctionCalls) - log.debug("onReceive " + intent); - startWakefulService(context, new Intent(context, DataService.class) - .setAction(intent.getAction()) - .putExtras(intent)); - } -} \ No newline at end of file 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 fd436d9dab..7f007f1e41 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 @@ -194,7 +194,7 @@ public class SmsCommunicatorFragment extends Fragment implements PluginBase { receivedSms.processed = true; break; case "DANAR": - DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); + DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); if (danaRFragment != null) reply = danaRFragment.shortStatus(); receivedSms.processed = true; break; @@ -225,7 +225,7 @@ public class SmsCommunicatorFragment extends Fragment implements PluginBase { bolusWaitingForConfirmation.processed = true; PumpInterface pumpInterface = MainApp.getConfigBuilder().getActivePump(); if (pumpInterface != null) { - danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); + danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); PumpEnactResult result = pumpInterface.deliverTreatment(bolusWaitingForConfirmation.bolusRequested, 0); if (result.success) { reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), bolusWaitingForConfirmation.bolusRequested); diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/xDripReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java similarity index 71% rename from app/src/main/java/info/nightscout/androidaps/receivers/xDripReceiver.java rename to app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java index 2c6a9b9237..aa767bb154 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/xDripReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.java @@ -7,14 +7,11 @@ import android.support.v4.content.WakefulBroadcastReceiver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.Services.DataService; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment; - -public class xDripReceiver extends WakefulBroadcastReceiver { - private static Logger log = LoggerFactory.getLogger(xDripReceiver.class); +public class DataReceiver extends WakefulBroadcastReceiver { + private static Logger log = LoggerFactory.getLogger(DataReceiver.class); @Override public void onReceive(Context context, Intent intent) { diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 65c07aec04..bd6a58c8e5 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -29,26 +29,6 @@ import info.nightscout.utils.ToastUtils; public class KeepAliveReceiver extends BroadcastReceiver { private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class); - private boolean mBounded; - private static ExecutionService mExecutionService; - - ServiceConnection mConnection = new ServiceConnection() { - - public void onServiceDisconnected(ComponentName name) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "ExecutionService is disconnected"); // TODO: remove - mBounded = false; - mExecutionService = null; - } - - public void onServiceConnected(ComponentName name, IBinder service) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "ExecutionService is connected"); // TODO: remove - log.debug("Service is connected"); - mBounded = true; - ExecutionService.LocalBinder mLocalBinder = (ExecutionService.LocalBinder) service; - mExecutionService = mLocalBinder.getServiceInstance(); - } - }; - @Override public void onReceive(Context context, Intent rIntent) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); @@ -56,12 +36,12 @@ public class KeepAliveReceiver extends BroadcastReceiver { wl.acquire(); log.debug("KeepAlive received"); - DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); - if (Config.DANAR && danaRFragment != null && danaRFragment.isEnabled(PluginBase.PUMP)) { + final DanaRFragment danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); + if (Config.DANAR && danaRFragment.isEnabled(PluginBase.PUMP)) { Thread t = new Thread(new Runnable() { @Override public void run() { - mExecutionService.connect("KeepAlive"); + danaRFragment.doConnect("KeepAlive"); } }); t.start(); @@ -80,10 +60,6 @@ public class KeepAliveReceiver extends BroadcastReceiver { } am.cancel(pi); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi); - - // DanaR bind - Intent intent = new Intent(context, ExecutionService.class); - context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } public void cancelAlarm(Context context) { @@ -91,11 +67,5 @@ public class KeepAliveReceiver extends BroadcastReceiver { PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(sender); - - // DanaR bind - if (mBounded) { - context.unbindService(mConnection); - mBounded = false; - } } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/NSClientDataReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/NSClientDataReceiver.java deleted file mode 100644 index 25b2b8a270..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/receivers/NSClientDataReceiver.java +++ /dev/null @@ -1,27 +0,0 @@ -package info.nightscout.androidaps.receivers; - -import android.content.Context; -import android.content.Intent; -import android.support.v4.content.WakefulBroadcastReceiver; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.MainActivity; -import info.nightscout.androidaps.Services.DataService; -import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; -import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment; - -public class NSClientDataReceiver extends WakefulBroadcastReceiver { - private static Logger log = LoggerFactory.getLogger(NSClientDataReceiver.class); - - @Override - public void onReceive(Context context, Intent intent) { - if (Config.logFunctionCalls) - log.debug("onReceive " + intent); - startWakefulService(context, new Intent(context, DataService.class) - .setAction(intent.getAction()) - .putExtras(intent)); - } -}