From e851d15aff741a0fa8db78824bb8702eddbafd44 Mon Sep 17 00:00:00 2001 From: viktoria Date: Mon, 2 Jan 2017 18:04:45 +0100 Subject: [PATCH 01/30] DanaR Stats Activity --- app/src/main/AndroidManifest.xml | 1 + .../plugins/DanaR/DanaRFragment.java | 12 + .../DanaR/History/DanaRStatsActivity.java | 282 ++++++++++++++++++ .../DanaRKorean/DanaRKoreanFragment.java | 12 + app/src/main/res/layout/danar_fragment.xml | 6 + .../main/res/layout/danar_statsactivity.xml | 73 +++++ app/src/main/res/values/strings.xml | 6 + 7 files changed, 392 insertions(+) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRStatsActivity.java create mode 100644 app/src/main/res/layout/danar_statsactivity.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d49ee22092..e46fae2c92 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -36,6 +36,7 @@ + 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 b7fae2a125..9fa77e0e17 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 @@ -29,6 +29,7 @@ import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog; import info.nightscout.androidaps.plugins.DanaR.History.DanaRHistoryActivity; +import info.nightscout.androidaps.plugins.DanaR.History.DanaRStatsActivity; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus; import info.nightscout.utils.DateUtil; @@ -65,6 +66,8 @@ public class DanaRFragment extends Fragment implements FragmentBase { TextView iobView; Button viewProfileButton; Button historyButton; + Button statsButton; + public DanaRFragment() { if (sHandlerThread == null) { @@ -105,6 +108,8 @@ public class DanaRFragment extends Fragment implements FragmentBase { iobView = (TextView) view.findViewById(R.id.danar_iob); viewProfileButton = (Button) view.findViewById(R.id.danar_viewprofile); historyButton = (Button) view.findViewById(R.id.danar_history); + statsButton = (Button) view.findViewById(R.id.danar_stats); + viewProfileButton.setOnClickListener(new View.OnClickListener() { @Override @@ -122,6 +127,13 @@ public class DanaRFragment extends Fragment implements FragmentBase { } }); + statsButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(new Intent(getContext(), DanaRStatsActivity.class)); + } + }); + btConnectionView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRStatsActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRStatsActivity.java new file mode 100644 index 0000000000..7a41a9e049 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRStatsActivity.java @@ -0,0 +1,282 @@ +package info.nightscout.androidaps.plugins.DanaR.History; + +import android.app.Activity; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.graphics.Color; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.support.v7.widget.LinearLayoutManager; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.stmt.PreparedQuery; +import com.j256.ormlite.stmt.QueryBuilder; +import com.j256.ormlite.stmt.Where; +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.SQLException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.DanaRHistoryRecord; +import info.nightscout.androidaps.interfaces.ProfileInterface; +import info.nightscout.androidaps.plugins.CircadianPercentageProfile.CircadianPercentageProfilePlugin; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; +import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRSyncStatus; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.ToastUtils; + +public class DanaRStatsActivity extends Activity { + private static Logger log = LoggerFactory.getLogger(DanaRStatsActivity.class); + + private boolean mBounded; + private static ExecutionService mExecutionService; + + private Handler mHandler; + private static HandlerThread mHandlerThread; + + TextView statusView; + TextView mainView; + Button reloadButton; + LinearLayoutManager llm; + + List historyList = new ArrayList<>(); + + + public DanaRStatsActivity() { + super(); + mHandlerThread = new HandlerThread(DanaRStatsActivity.class.getSimpleName()); + mHandlerThread.start(); + this.mHandler = new Handler(mHandlerThread.getLooper()); + } + + + @Override + public void onStart() { + super.onStart(); + Intent intent = new Intent(this, ExecutionService.class); + bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + } + + @Override + protected void onResume() { + super.onResume(); + MainApp.bus().register(this); + } + + @Override + protected void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onStop() { + super.onStop(); + if (mBounded) { + unbindService(mConnection); + mBounded = false; + } + } + + ServiceConnection mConnection = new ServiceConnection() { + + public void onServiceDisconnected(ComponentName name) { + log.debug("Service is disconnected"); + mBounded = false; + mExecutionService = null; + } + + public void onServiceConnected(ComponentName name, IBinder service) { + log.debug("Service is connected"); + mBounded = true; + ExecutionService.LocalBinder mLocalBinder = (ExecutionService.LocalBinder) service; + mExecutionService = mLocalBinder.getServiceInstance(); + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.danar_statsactivity); + + statusView = (TextView) findViewById(R.id.danar_historystatus); + mainView = (TextView) findViewById(R.id.danar_stats_textview); + reloadButton = (Button) findViewById(R.id.danar_historyreload); + llm = new LinearLayoutManager(this); + statusView.setVisibility(View.GONE); + + + reloadButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mExecutionService.isConnected() || mExecutionService.isConnecting()) { + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.pumpbusy)); + return; + } + mHandler.post(new Runnable() { + @Override + public void run() { + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadButton.setVisibility(View.GONE); + statusView.setVisibility(View.VISIBLE); + } + }); + mExecutionService.loadHistory(RecordTypes.RECORD_TYPE_DAILY); + loadDataFromDB(RecordTypes.RECORD_TYPE_DAILY); + runOnUiThread(new Runnable() { + @Override + public void run() { + reloadButton.setVisibility(View.VISIBLE); + statusView.setVisibility(View.GONE); + } + }); + } + }); + } + }); + loadDataFromDB(RecordTypes.RECORD_TYPE_DAILY); + } + + + private void loadDataFromDB(byte type) { + try { + Dao dao = MainApp.getDbHelper().getDaoDanaRHistory(); + QueryBuilder queryBuilder = dao.queryBuilder(); + queryBuilder.orderBy("recordDate", false); + Where where = queryBuilder.where(); + where.eq("recordCode", type); + queryBuilder.limit(10L); + PreparedQuery preparedQuery = queryBuilder.prepare(); + historyList = dao.query(preparedQuery); + } catch (SQLException e) { + e.printStackTrace(); + historyList = new ArrayList<>(); + } + runOnUiThread(new Runnable() { + @Override + public void run() { + mainView.setText(""); + DateFormat df = new SimpleDateFormat("dd.MM. - "); + + double magicNumber = 18d; + + ProfileInterface pi = ConfigBuilderPlugin.getActiveProfile(); + if (pi instanceof CircadianPercentageProfilePlugin){ + magicNumber = ((CircadianPercentageProfilePlugin)pi).baseBasalSum(); + } + + magicNumber *=2; + + int i = 0; + double sum = 0d; + double weighted03 = 0d; + double weighted05 = 0d; + double weighted07 = 0d; + String avg_string = "\n" + getString(R.string.danar_stats_avg); + String weighted_string = "\n\n" + getString(R.string.danar_stats_expweight); + + for (DanaRHistoryRecord record: historyList) { + double tdd= record.getRecordDailyBolus() + record.getRecordDailyBasal(); + mainView.append(df.format(new Date(record.getRecordDate()))); + mainView.append(getString(R.string.danar_stats_basalrate) + DecimalFormatter.to2Decimal(record.getRecordDailyBasal())); + mainView.append(getString(R.string.danar_stats_bolus) + DecimalFormatter.to2Decimal(record.getRecordDailyBolus())); + mainView.append(getString(R.string.danar_stats_tdi) + DecimalFormatter.to2Decimal(tdd)); + mainView.append(" " + Math.round(100*tdd/magicNumber) +"%"); + mainView.append("\n"); + sum = sum + tdd; + i++; + avg_string = avg_string + "\n " + i +": " + DecimalFormatter.to2Decimal(sum/i) + " " + Math.round(100*sum/i/magicNumber) +"%"; + } + + if (historyList.size()<3 || !(df.format(new Date(historyList.get(0).getRecordDate())).equals(df.format(new Date(System.currentTimeMillis() - 1000*60*60*24))))){ + mainView.setBackgroundColor(Color.RED); + } else { + mainView.setBackgroundColor(Color.TRANSPARENT); + } + + Collections.reverse(historyList); + + i = 0; + for (DanaRHistoryRecord record: historyList) { + double tdd= record.getRecordDailyBolus() + record.getRecordDailyBasal(); + if(i == 0 ) { + weighted03 = tdd; + weighted05 = tdd; + weighted07 = tdd; + + } else { + weighted07 = (weighted07*0.3 + tdd*0.7); + weighted05 = (weighted05*0.5 + tdd*0.5); + weighted03 = (weighted03*0.7 + tdd*0.3); + } + i++; + } + weighted_string = weighted_string + "\n 0.3 " + DecimalFormatter.to2Decimal(weighted03) + " " + Math.round(100*weighted03/magicNumber) +"%"; + weighted_string = weighted_string + "\n 0.5 " + DecimalFormatter.to2Decimal(weighted05) + " " + Math.round(100*weighted05/magicNumber) +"%"; + weighted_string = weighted_string + "\n 0.7 " + DecimalFormatter.to2Decimal(weighted07) + " " + Math.round(100*weighted07/magicNumber) +"%"; + + + mainView.append(avg_string); + mainView.append(weighted_string); + } + }); + } + + + @Subscribe + public void onStatusEvent(final EventDanaRSyncStatus s) { + log.debug("EventDanaRSyncStatus: " + s.message); + runOnUiThread( + new Runnable() { + @Override + public void run() { + statusView.setText(s.message); + } + }); + } + + @Subscribe + public void onStatusEvent(final EventDanaRConnectionStatus c) { + runOnUiThread( + new Runnable() { + @Override + public void run() { + if (c.sStatus == EventDanaRConnectionStatus.CONNECTING) { + statusView.setText(String.format(getString(R.string.danar_history_connectingfor), c.sSecondsElapsed)); + log.debug("EventDanaRConnectionStatus: " + "Connecting for " + c.sSecondsElapsed + "s"); + } else if (c.sStatus == EventDanaRConnectionStatus.CONNECTED) { + statusView.setText(MainApp.sResources.getString(R.string.connected)); + log.debug("EventDanaRConnectionStatus: Connected"); + } else { + statusView.setText(MainApp.sResources.getString(R.string.disconnected)); + log.debug("EventDanaRConnectionStatus: Disconnected"); + } + } + } + ); + } + + +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java index 907718f636..d645f252d3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java @@ -28,6 +28,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog; +import info.nightscout.androidaps.plugins.DanaR.History.DanaRStatsActivity; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus; import info.nightscout.androidaps.plugins.DanaRKorean.History.DanaRHistoryActivity; @@ -62,6 +63,8 @@ public class DanaRKoreanFragment extends Fragment implements FragmentBase { TextView iobView; Button viewProfileButton; Button historyButton; + Button statsButton; + public DanaRKoreanFragment() { if (sHandlerThread == null) { @@ -102,6 +105,8 @@ public class DanaRKoreanFragment extends Fragment implements FragmentBase { iobView = (TextView) view.findViewById(R.id.danar_iob); viewProfileButton = (Button) view.findViewById(R.id.danar_viewprofile); historyButton = (Button) view.findViewById(R.id.danar_history); + statsButton = (Button) view.findViewById(R.id.danar_stats); + viewProfileButton.setOnClickListener(new View.OnClickListener() { @Override @@ -119,6 +124,13 @@ public class DanaRKoreanFragment extends Fragment implements FragmentBase { } }); + statsButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(new Intent(getContext(), DanaRStatsActivity.class)); + } + }); + btConnectionView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/app/src/main/res/layout/danar_fragment.xml b/app/src/main/res/layout/danar_fragment.xml index 00463f5dd0..82a480ceac 100644 --- a/app/src/main/res/layout/danar_fragment.xml +++ b/app/src/main/res/layout/danar_fragment.xml @@ -269,6 +269,12 @@ android:text="@string/danar_history" android:id="@+id/danar_history" android:layout_weight="1" /> +