DanaR service

This commit is contained in:
Milos Kozak 2016-07-07 10:34:20 +02:00
parent 0b19cae903
commit ba015f1140
21 changed files with 437 additions and 37 deletions

View file

@ -27,8 +27,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PreferencesActivity"></activity>
<activity android:name=".PreferencesActivity" />
<!-- Receiver from NSClient -->
<receiver
@ -60,7 +59,7 @@
</receiver>
<!-- Receiver keepalive, scheduled every 30 min -->
<receiver android:name=".receivers.KeepAliveReceiver"></receiver>
<receiver android:name=".receivers.KeepAliveReceiver" />
<!-- Service processing incomming data -->
<service
@ -74,6 +73,11 @@
<meta-data
android:name="io.fabric.ApiKey"
android:value="59d462666c664c57b29e1d79ea123e01f8057cfa" />
<service
android:name=".plugins.DanaR.Services.DanaRService"
android:enabled="true"
android:exported="false"></service>
</application>
</manifest>

View file

@ -13,6 +13,8 @@ public class Config {
public static final boolean OBJECTIVESENABLED = APS && true;
public static final boolean CAREPORTALENABLED = APS && true;
public static final boolean DANAR = true;
public static final boolean detailedLog = true;
public static final boolean logFunctionCalls = true;
public static final boolean logIncommingBG = true;

View file

@ -24,10 +24,12 @@ import java.util.ArrayList;
import java.util.Iterator;
import info.nightscout.androidaps.Services.AlertService;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
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.NSProfileViewer.NSProfileViewerFragment;
@ -80,6 +82,7 @@ public class MainActivity extends AppCompatActivity {
pluginsList = new ArrayList<PluginBase>();
// Register all tabs in app here
pluginsList.add(OverviewFragment.newInstance());
if (Config.DANAR) pluginsList.add(DanaRFragment.newInstance());
pluginsList.add(VirtualPumpFragment.newInstance());
if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.newInstance());
if (Config.LOOPENABLED) pluginsList.add(LoopFragment.newInstance());
@ -175,7 +178,7 @@ public class MainActivity extends AppCompatActivity {
log.debug("Exiting");
keepAliveReceiver.cancelAlarm(this);
//MainApp.bus().post(new StopEvent());
MainApp.bus().post(new EventAppExit());
MainApp.closeDbHelper();
finish();
System.runFinalization();

View file

@ -21,10 +21,10 @@ public class MainApp extends Application {
private static Bus sBus;
private static MainApp sInstance;
public static Resources resources;
public static Resources sResources;
private static DatabaseHelper databaseHelper = null;
private static ConfigBuilderFragment configBuilder = null;
private static DatabaseHelper sDatabaseHelper = null;
private static ConfigBuilderFragment sConfigBuilder = null;
@Override
public void onCreate() {
@ -33,7 +33,7 @@ public class MainApp extends Application {
sBus = new Bus(ThreadEnforcer.ANY);
sInstance = this;
resources = getResources();
sResources = getResources();
}
public static Bus bus() {
@ -44,30 +44,30 @@ public class MainApp extends Application {
}
public static DatabaseHelper getDbHelper() {
if (databaseHelper == null) {
databaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class);
if (sDatabaseHelper == null) {
sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class);
}
return databaseHelper;
return sDatabaseHelper;
}
public static void closeDbHelper() {
if (databaseHelper != null) {
databaseHelper.close();
databaseHelper = null;
if (sDatabaseHelper != null) {
sDatabaseHelper.close();
sDatabaseHelper = null;
}
}
public static void setConfigBuilder(ConfigBuilderFragment cb) {
configBuilder = cb;
sConfigBuilder = cb;
}
public static ConfigBuilderFragment getConfigBuilder() {
return configBuilder;
return sConfigBuilder;
}
@Override
public void onTerminate() {
super.onTerminate();
databaseHelper.close();
sDatabaseHelper.close();
}
}

View file

@ -106,7 +106,7 @@ public class AlertMessage {
mButtonDismis = new Button(getApplicationContext());
mButtonDismis.setText(MainApp.resources.getString(R.string.dismiss));
mButtonDismis.setText(MainApp.sResources.getString(R.string.dismiss));
mButtonDismis.setTextSize(20.0F);
mButtonDismis.setTextAlignment(Button.TEXT_ALIGNMENT_CENTER);
mButtonDismis.setGravity(Gravity.CENTER);

View file

@ -162,10 +162,10 @@ public class DataService extends IntentService {
configBuilderFragment.nsClientVersionName = bundles.getString("nsclientversionname");
log.debug("Got versions: NSClient: " + configBuilderFragment.nsClientVersionName + " Nightscout: " + configBuilderFragment.nightscoutVersionName);
if (configBuilderFragment.nsClientVersionCode < 117)
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.resources.getString(R.string.unsupportedclientver));
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.unsupportedclientver));
}
} else {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.resources.getString(R.string.unsupportedclientver));
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.unsupportedclientver));
}
if (bundles.containsKey("status")) {
try {

View file

@ -0,0 +1,7 @@
package info.nightscout.androidaps.events;
/**
* Created by mike on 07.07.2016.
*/
public class EventAppExit {
}

View file

@ -0,0 +1,9 @@
package info.nightscout.androidaps.events;
/**
* Created by mike on 07.07.2016.
*/
public class EventPumpConnecting {
public boolean sConnecting;
public boolean sConnected;
}

View file

@ -97,7 +97,7 @@ public class CareportalFragment extends Fragment implements PluginBase, View.OnC
@Override
public String getName() {
return MainApp.resources.getString(R.string.careportal);
return MainApp.sResources.getString(R.string.careportal);
}
@Override

View file

@ -0,0 +1,21 @@
package info.nightscout.androidaps.plugins.DanaR;
import com.squareup.otto.Bus;
/**
* Created by mike on 07.07.2016.
*/
public class DanaConnection {
public DanaConnection(Bus bus) {
}
public void connectIfNotConnected(String reason) {
}
public void stop() {
}
}

View file

@ -0,0 +1,182 @@
package info.nightscout.androidaps.plugins.DanaR;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.json.JSONObject;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.client.data.NSProfile;
public class DanaRFragment extends Fragment implements PluginBase, PumpInterface {
private static DanaConnection sDanaConnection = null;
boolean fragmentEnabled = true;
boolean fragmentVisible = true;
boolean visibleNow = false;
public static DanaConnection getDanaConnection() {
return sDanaConnection;
}
public static void setDanaConnection(DanaConnection con) {
sDanaConnection = con;
}
public DanaRFragment() {
}
public static DanaRFragment newInstance() {
DanaRFragment fragment = new DanaRFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.danar_fragment, container, false);
return view;
}
// Plugin base interface
@Override
public int getType() {
return PluginBase.PUMP;
}
@Override
public String getName() {
return MainApp.instance().getString(R.string.danarpump);
}
@Override
public boolean isEnabled() {
return fragmentEnabled;
}
@Override
public boolean isVisibleInTabs() {
return fragmentVisible;
}
@Override
public boolean canBeHidden() {
return true;
}
@Override
public void setFragmentEnabled(boolean fragmentEnabled) {
this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(boolean fragmentVisible) {
this.fragmentVisible = fragmentVisible;
}
// Pump interface
@Override
public boolean isTempBasalInProgress() {
return false;
}
@Override
public boolean isExtendedBoluslInProgress() {
return false;
}
@Override
public Integer getBatteryPercent() {
return null;
}
@Override
public Integer getReservoirValue() {
return null;
}
@Override
public void setNewBasalProfile(NSProfile profile) {
}
@Override
public double getBaseBasalRate() {
return 0;
}
@Override
public double getTempBasalAbsoluteRate() {
return 0;
}
@Override
public double getTempBasalRemainingMinutes() {
return 0;
}
@Override
public TempBasal getTempBasal() {
return null;
}
@Override
public TempBasal getExtendedBolus() {
return null;
}
@Override
public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) {
return null;
}
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) {
return null;
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) {
return null;
}
@Override
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
return null;
}
@Override
public PumpEnactResult cancelTempBasal() {
return null;
}
@Override
public PumpEnactResult cancelExtendedBolus() {
return null;
}
@Override
public JSONObject getJSONStatus() {
return null;
}
@Override
public String deviceID() {
return null;
}
}

View file

@ -0,0 +1,152 @@
package info.nightscout.androidaps.plugins.DanaR.Services;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.support.v7.app.NotificationCompat;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventPumpConnecting;
import info.nightscout.androidaps.plugins.DanaR.DanaConnection;
import info.nightscout.androidaps.plugins.DanaR.DanaRFragment;
public class DanaRService extends Service {
private static Logger log = LoggerFactory.getLogger(DanaRService.class);
Handler mHandler;
private HandlerThread mHandlerThread;
private Notification mNotification;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mNotificationCompatBuilder;
private DanaConnection mDanaConnection;
private static final int notifyId = 130;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Config.logFunctionCalls)
log.info("onStartCommand");
if (mHandlerThread == null) {
enableForeground();
if (Config.logFunctionCalls)
log.debug("Creating handler thread");
this.mHandlerThread = new HandlerThread(DanaRService.class.getSimpleName() + "Handler");
mHandlerThread.start();
this.mHandler = new Handler(mHandlerThread.getLooper());
DanaRFragment danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class);
mDanaConnection = danaRFragment.getDanaConnection();
registerBus();
if (mDanaConnection == null) {
mDanaConnection = new DanaConnection(MainApp.bus());
danaRFragment.setDanaConnection(mDanaConnection);
}
}
if (Config.DANAR)
mHandler.post(new Runnable() {
@Override
public void run() {
mDanaConnection.connectIfNotConnected("onStartCommand connectionCheck");
}
});
if (Config.logFunctionCalls)
log.info("onStartCommand end");
return START_STICKY;
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
} catch (RuntimeException x) {
// Ignore
}
MainApp.bus().register(this);
}
private void enableForeground() {
mNotificationCompatBuilder = new NotificationCompat.Builder(getApplicationContext());
mNotificationCompatBuilder.setContentTitle(MainApp.sResources.getString(R.string.app_name))
.setSmallIcon(R.drawable.notification_icon)
.setAutoCancel(false)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MIN)
.setOnlyAlertOnce(true)
.setWhen(System.currentTimeMillis())
.setLocalOnly(true);
mNotification = mNotificationCompatBuilder.build();
nortifManagerNotify();
startForeground(notifyId, mNotification);
}
private void nortifManagerNotify() {
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyId, mNotification);
}
@Subscribe
public void onStatusEvent(final EventPumpConnecting c) {
if (Config.DANAR) {
String connectionText;
if (c.sConnecting) {
connectionText = MainApp.sResources.getString(R.string.connecting);
} else {
if (c.sConnected) {
connectionText = MainApp.sResources.getString(R.string.connected);
} else {
connectionText = MainApp.sResources.getString(R.string.disconnected);
}
}
mNotificationCompatBuilder.setWhen(System.currentTimeMillis())
.setContentText(connectionText);
mNotification = mNotificationCompatBuilder.build();
nortifManagerNotify();
}
}
@Subscribe
public void onStopEvent(EventAppExit event) {
if (Config.logFunctionCalls)
log.debug("onStopEvent received");
mDanaConnection.stop();
stopForeground(true);
stopSelf();
if (Config.logFunctionCalls)
log.debug("onStopEvent finished");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
if (Config.logFunctionCalls)
log.info("onCreate");
mHandler = new Handler(); // TODO: not needed???
}
}

View file

@ -38,7 +38,6 @@ import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
public class LoopFragment extends Fragment implements View.OnClickListener, PluginBase {
@ -289,7 +288,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainApp.instance().getApplicationContext());
builder.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(MainApp.resources.getString(R.string.openloop_newsuggestion))
.setContentTitle(MainApp.sResources.getString(R.string.openloop_newsuggestion))
.setContentText(resultAfterConstraints.toString())
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)

View file

@ -29,6 +29,7 @@ import info.nightscout.utils.*;
// TODO: wizard upload top NS calculation
// TODO: add carbtime
// TODO: add profile selection
public class WizardDialog extends DialogFragment implements OnClickListener {
Button wizardDialogDeliverButton;
@ -181,7 +182,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.resources.getString(R.string.noprofile));
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.noprofile));
return;
}

View file

@ -387,8 +387,8 @@ public class OverviewFragment extends Fragment implements PluginBase {
if (Config.APS) {
apsModeView.setVisibility(View.VISIBLE);
if (MainApp.getConfigBuilder().isClosedModeEnabled())
apsModeView.setText(MainApp.resources.getString(R.string.closedloop));
else apsModeView.setText(MainApp.resources.getString(R.string.openloop));
apsModeView.setText(MainApp.sResources.getString(R.string.closedloop));
else apsModeView.setText(MainApp.sResources.getString(R.string.openloop));
} else {
apsModeView.setVisibility(View.GONE);
}

View file

@ -14,19 +14,24 @@ import android.os.PowerManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.plugins.DanaR.Services.DanaRService;
public class KeepAliveReceiver extends BroadcastReceiver {
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
public void onReceive(Context context, Intent rIntent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
log.debug("KeepAlive received");
// TODO: Start service here
if (Config.DANAR) {
Intent intent = new Intent(context, DanaRService.class);
context.startService(intent);
}
wl.release();
}

View file

@ -3,7 +3,6 @@ package info.nightscout.client.data;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -22,7 +21,7 @@ public class DbLogger {
Logger log = LoggerFactory.getLogger(sender);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(intent, 0);
if (q.size() < 1) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(),MainApp.resources.getString(R.string.nsclientnotinstalled));
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(),MainApp.sResources.getString(R.string.nsclientnotinstalled));
log.error("DBADD No receivers");
} else if (Config.logNSUpload)
log.debug("DBADD dbAdd " + q.size() + " receivers " + data);

View file

@ -29,7 +29,7 @@ import info.nightscout.androidaps.R;
public class ImportExportPrefs {
static File path = new File(Environment.getExternalStorageDirectory().toString());
static final File file = new File(path, MainApp.resources.getString(R.string.app_name) + "Preferences");
static final File file = new File(path, MainApp.sResources.getString(R.string.app_name) + "Preferences");
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
@ -55,7 +55,7 @@ public class ImportExportPrefs {
public static void exportSharedPreferences(final Context c) {
new AlertDialog.Builder(c)
.setMessage(MainApp.resources.getString(R.string.export_to) + " " + file + " ?")
.setMessage(MainApp.sResources.getString(R.string.export_to) + " " + file + " ?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
@ -69,9 +69,9 @@ public class ImportExportPrefs {
}
pw.close();
fw.close();
ToastUtils.showToastInUiThread(c, MainApp.resources.getString(R.string.exported));
ToastUtils.showToastInUiThread(c, MainApp.sResources.getString(R.string.exported));
} catch (FileNotFoundException e) {
ToastUtils.showToastInUiThread(c, MainApp.resources.getString(R.string.filenotfound) + " " + file);
ToastUtils.showToastInUiThread(c, MainApp.sResources.getString(R.string.filenotfound) + " " + file);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
@ -84,7 +84,7 @@ public class ImportExportPrefs {
public static void importSharedPreferences(final Context c) {
new AlertDialog.Builder(c)
.setMessage(MainApp.resources.getString(R.string.import_from) + " " + file + " ?")
.setMessage(MainApp.sResources.getString(R.string.import_from) + " " + file + " ?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
@ -109,9 +109,9 @@ public class ImportExportPrefs {
}
reader.close();
editor.commit();
ToastUtils.showToastInUiThread(c, MainApp.resources.getString(R.string.setting_imported));
ToastUtils.showToastInUiThread(c, MainApp.sResources.getString(R.string.setting_imported));
} catch (FileNotFoundException e) {
ToastUtils.showToastInUiThread(c, MainApp.resources.getString(R.string.filenotfound) + " " + file);
ToastUtils.showToastInUiThread(c, MainApp.sResources.getString(R.string.filenotfound) + " " + file);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

View file

@ -0,0 +1,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.nightscout.androidaps.plugins.DanaR.DanaRFragment">
</FrameLayout>

View file

@ -220,4 +220,8 @@
<string name="bg_lang">Bulgarian</string>
<string name="dismiss">POTVRDIT</string>
<string name="language">Jazyk</string>
<string name="connected">Připojeno</string>
<string name="connecting">Připojuji</string>
<string name="danarpump">DanaR</string>
<string name="disconnected">Odpojeno</string>
</resources>

View file

@ -229,4 +229,9 @@
<string name="dismiss">DISMISS</string>
<string name="language">Language</string>
<string name="danarpump">DanaR</string>
<string name="connecting">Connecting</string>
<string name="connected">Connected</string>
<string name="disconnected">Disconnected</string>
</resources>