AndroidAPS/app/src/main/java/info/nightscout/utils/XdripCalibrations.java

68 lines
2.8 KiB
Java
Raw Normal View History

2017-02-10 17:25:32 +01:00
package info.nightscout.utils;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
2017-06-02 10:25:49 +02:00
import info.nightscout.androidaps.data.Profile;
2017-02-10 17:25:32 +01:00
/**
* Created by mike on 10.02.2017.
*/
2017-02-10 21:20:55 +01:00
public class XdripCalibrations {
private static Logger log = LoggerFactory.getLogger(XdripCalibrations.class);
2017-02-10 17:25:32 +01:00
public static void confirmAndSendCalibration(final Double bg, Context parentContext) {
if (parentContext != null) {
String confirmMessage = String.format(MainApp.sResources.getString(R.string.send_calibration), bg);
AlertDialog.Builder builder = new AlertDialog.Builder(parentContext);
builder.setTitle(MainApp.sResources.getString(R.string.confirmation));
builder.setMessage(confirmMessage);
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
sendIntent(bg);
}
});
builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null);
builder.show();
}
}
2017-02-10 21:20:55 +01:00
public static boolean sendIntent(Double bg) {
2017-02-10 17:25:32 +01:00
Context context = MainApp.instance().getApplicationContext();
Bundle bundle = new Bundle();
bundle.putDouble("glucose_number", bg);
bundle.putString("units", MainApp.getConfigBuilder().getProfileUnits().equals(Constants.MGDL) ? "mgdl" : "mmol");
2017-06-15 23:12:12 +02:00
bundle.putLong("timestamp", System.currentTimeMillis());
2017-02-10 17:25:32 +01:00
Intent intent = new Intent(Intents.ACTION_REMOTE_CALIBRATION);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(intent, 0);
if (q.size() < 1) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.xdripnotinstalled));
log.debug(MainApp.sResources.getString(R.string.xdripnotinstalled));
return false;
} else {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.calibrationsent));
2017-02-10 21:20:55 +01:00
log.debug(MainApp.sResources.getString(R.string.calibrationsent));
2017-02-10 17:25:32 +01:00
return true;
}
}
}