From 442d48b4c2927e8c7512c8b73e73fe7cb7a353ec Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 2 Aug 2019 16:31:57 +0200 Subject: [PATCH 1/5] fix OPAS fragments --- .../aps/openAPSAMA/OpenAPSAMAFragment.java | 3 +- .../aps/openAPSSMB/OpenAPSSMBFragment.java | 18 ++++++------ .../androidaps/utils/JSONFormatter.java | 28 +++++++++++++------ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java index 0aadf52cbd..6d465c9a06 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.aps.openAPSAMA; import android.app.Activity; import android.os.Bundle; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -97,7 +98,7 @@ public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnCli currentTempView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getCurrentTempParam())); try { JSONArray iobArray = new JSONArray(determineBasalAdapterAMAJS.getIobDataParam()); - iobDataView.setText(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0))); + iobDataView.setText(TextUtils.concat(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n", JSONFormatter.format(iobArray.getString(0)))); } catch (JSONException e) { log.error("Unhandled exception", e); iobDataView.setText("JSONException"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java index 18828e840e..028815df34 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java @@ -2,6 +2,8 @@ package info.nightscout.androidaps.plugins.aps.openAPSSMB; import android.app.Activity; import android.os.Bundle; +import android.text.Html; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -94,26 +96,26 @@ public class OpenAPSSMBFragment extends SubscriberFragment { } DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = plugin.lastDetermineBasalAdapterSMBJS; if (determineBasalAdapterSMBJS != null) { - glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getGlucoseStatusParam()).toString().trim()); - currentTempView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getCurrentTempParam()).toString().trim()); + glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getGlucoseStatusParam())); + currentTempView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getCurrentTempParam())); try { JSONArray iobArray = new JSONArray(determineBasalAdapterSMBJS.getIobDataParam()); - iobDataView.setText((String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0))).trim()); + iobDataView.setText(TextUtils.concat(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n", JSONFormatter.format(iobArray.getString(0)))); } catch (JSONException e) { log.error("Unhandled exception", e); iobDataView.setText("JSONException see log for details"); } - profileView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getProfileParam()).toString().trim()); - mealDataView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getMealDataParam()).toString().trim()); - scriptdebugView.setText(determineBasalAdapterSMBJS.getScriptDebug().trim()); + profileView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getProfileParam())); + mealDataView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getMealDataParam())); + scriptdebugView.setText(determineBasalAdapterSMBJS.getScriptDebug()); if (lastAPSResult != null && lastAPSResult.inputConstraints != null) - constraintsView.setText(lastAPSResult.inputConstraints.getReasons().trim()); + constraintsView.setText(lastAPSResult.inputConstraints.getReasons()); } if (plugin.lastAPSRun != 0) { lastRunView.setText(DateUtil.dateAndTimeFullString(plugin.lastAPSRun)); } if (plugin.lastAutosensResult != null) { - autosensDataView.setText(JSONFormatter.format(plugin.lastAutosensResult.json()).toString().trim()); + autosensDataView.setText(JSONFormatter.format(plugin.lastAutosensResult.json())); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java b/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java index 159e90fda7..6205beee21 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.utils; +import android.os.Build; import android.text.Html; import android.text.Spanned; @@ -18,14 +19,23 @@ public class JSONFormatter { private static Logger log = LoggerFactory.getLogger(JSONFormatter.class); public static Spanned format(final String jsonString) { - final JsonVisitor visitor = new JsonVisitor(4, ' '); + final JsonVisitor visitor = new JsonVisitor(1, '\t'); try { if (jsonString.equals("undefined")) return Html.fromHtml("undefined"); else if (jsonString.getBytes()[0] == '[') - return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0)); - else - return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0), Html.FROM_HTML_MODE_COMPACT); + } else { + return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0)); + } + else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0), Html.FROM_HTML_MODE_COMPACT); + } else { + return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0)); + } + } } catch (JSONException e) { log.error("Unhandled exception", e); return Html.fromHtml(""); @@ -33,7 +43,7 @@ public class JSONFormatter { } public static Spanned format(final JSONObject object) { - final JsonVisitor visitor = new JsonVisitor(4, ' '); + final JsonVisitor visitor = new JsonVisitor(1, '\t'); try { return Html.fromHtml(visitor.visit(object, 0)); } catch (JSONException e) { @@ -58,7 +68,7 @@ public class JSONFormatter { } else { ret += write("[", indent); for (int i = 0; i < length; i++) { - ret += visit(array.get(i), indent + 1); + ret += visit(array.get(i), indent); } ret += write("]", indent); } @@ -73,8 +83,8 @@ public class JSONFormatter { final Iterator keys = obj.keys(); while (keys.hasNext()) { final String key = keys.next(); - ret += write("" + key + ": ", indent + 1); - ret += visit(obj.get(key), 0); + ret += write("" + key + ": ", indent); + ret += visit(obj.get(key), indent + 1); ret += "
"; } } @@ -86,7 +96,7 @@ public class JSONFormatter { if (object instanceof JSONArray) { ret += visit((JSONArray) object, indent); } else if (object instanceof JSONObject) { - ret += visit((JSONObject) object, indent); + ret += "
" + visit((JSONObject) object, indent); } else { if (object instanceof String) { ret += write("\"" + ((String) object).replace("<", "<").replace(">", ">") + "\"", indent); From b572d110d82fc7aeba94f6cfafc0dfe7bdfdb756 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 2 Aug 2019 17:15:57 +0200 Subject: [PATCH 2/5] remove splash on other activities --- .../androidaps/activities/AgreementActivity.java | 4 ++-- .../activities/HistoryBrowseActivity.java | 4 ++-- .../androidaps/activities/NoSplashActivity.kt | 13 +++++++++++++ .../androidaps/activities/PreferencesActivity.java | 1 + .../androidaps/activities/TDDStatsActivity.java | 4 ++-- 5 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/activities/NoSplashActivity.kt diff --git a/app/src/main/java/info/nightscout/androidaps/activities/AgreementActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/AgreementActivity.java index 32352e1371..d3792bbad5 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/AgreementActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/AgreementActivity.java @@ -11,13 +11,13 @@ import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.R; import info.nightscout.androidaps.utils.SP; -public class AgreementActivity extends Activity { +public class AgreementActivity extends NoSplashActivity { boolean IUnderstand; CheckBox agreeCheckBox; Button saveButton; @Override - protected void onCreate(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_agreement); IUnderstand = SP.getBoolean(R.string.key_i_understand, false); diff --git a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java index a076608ed2..7914c0508d 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java @@ -45,7 +45,7 @@ import info.nightscout.androidaps.plugins.general.overview.graphData.GraphData; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.T; -public class HistoryBrowseActivity extends AppCompatActivity { +public class HistoryBrowseActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(HistoryBrowseActivity.class); @@ -82,7 +82,7 @@ public class HistoryBrowseActivity extends AppCompatActivity { } @Override - protected void onCreate(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_historybrowse); diff --git a/app/src/main/java/info/nightscout/androidaps/activities/NoSplashActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/NoSplashActivity.kt new file mode 100644 index 0000000000..0b7af2bd1d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/activities/NoSplashActivity.kt @@ -0,0 +1,13 @@ +package info.nightscout.androidaps.activities + +import android.app.Activity +import android.os.Bundle + +import info.nightscout.androidaps.R + +open class NoSplashActivity : Activity() { + public override fun onCreate(savedInstanceState: Bundle?) { + setTheme(R.style.AppTheme_NoActionBar) + super.onCreate(savedInstanceState) + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java index 7b8a01c843..c5539f29d2 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java @@ -56,6 +56,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre @Override protected void onCreate(Bundle savedInstanceState) { + setTheme(R.style.AppTheme_NoActionBar); super.onCreate(savedInstanceState); myPreferenceFragment = new MyPreferenceFragment(); Bundle args = new Bundle(); diff --git a/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java index 832ca2ad85..487def0241 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java @@ -52,7 +52,7 @@ import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SafeParse; -public class TDDStatsActivity extends Activity { +public class TDDStatsActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(TDDStatsActivity.class); TextView statusView, statsMessage, totalBaseBasal2; @@ -99,7 +99,7 @@ public class TDDStatsActivity extends Activity { } @Override - protected void onCreate(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.danar_statsactivity); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); From 628adf25ba5390e851e02376f04588750a77fe2c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 2 Aug 2019 21:55:34 +0200 Subject: [PATCH 3/5] better check soundid passed to AlarmSoundService --- .../general/overview/dialogs/ErrorDialog.java | 14 ++++++----- .../overview/dialogs/ErrorHelperActivity.java | 2 +- .../notifications/NotificationStore.java | 11 +++++---- .../pump/medtronic/MedtronicPumpPlugin.java | 1 + .../services/AlarmSoundService.java | 24 +++++++------------ 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java index ea8dd8e4bb..f6b654567e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java @@ -97,12 +97,14 @@ public class ErrorDialog extends DialogFragment implements View.OnClickListener } private void startAlarm() { - Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); - alarm.putExtra("soundid", soundId); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - MainApp.instance().startForegroundService(alarm); - else - MainApp.instance().startService(alarm); + if (soundId != 0) { + Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); + alarm.putExtra("soundid", soundId); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + MainApp.instance().startForegroundService(alarm); + else + MainApp.instance().startService(alarm); + } } private void stopAlarm() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java index 86e7ac5461..37b831e34f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java @@ -18,7 +18,7 @@ public class ErrorHelperActivity extends AppCompatActivity { ErrorDialog errorDialog = new ErrorDialog(); errorDialog.setHelperActivity(this); errorDialog.setStatus(getIntent().getStringExtra("status")); - errorDialog.setSound(getIntent().getIntExtra("soundid", 0)); + errorDialog.setSound(getIntent().getIntExtra("soundid", R.raw.error)); errorDialog.setTitle(getIntent().getStringExtra("title")); errorDialog.show(this.getSupportFragmentManager(), "Error"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java index 2a94d2c859..f46e5184cd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java @@ -7,13 +7,14 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.media.AudioAttributes; +import android.media.AudioManager; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; +import android.view.View; + import androidx.core.app.NotificationCompat; import androidx.recyclerview.widget.RecyclerView; -import android.view.View; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,7 +67,7 @@ public class NotificationStore { if (SP.getBoolean(MainApp.gs(R.string.key_raise_notifications_as_android_notifications), false) && !(n instanceof NotificationWithAction)) { raiseSystemNotification(n); - if (usesChannels && n.soundId != null) { + if (usesChannels && n.soundId != null && n.soundId != 0) { Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); alarm.putExtra("soundid", n.soundId); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) @@ -76,7 +77,7 @@ public class NotificationStore { } } else { - if (n.soundId != null) { + if (n.soundId != null && n.soundId != 0) { Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); alarm.putExtra("soundid", n.soundId); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) @@ -146,7 +147,7 @@ public class NotificationStore { if (n.level == Notification.URGENT) { notificationBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000}) .setContentTitle(MainApp.gs(R.string.urgent_alarm)) - .setSound(sound, AudioAttributes.USAGE_ALARM); + .setSound(sound, AudioManager.STREAM_ALARM); } else { notificationBuilder.setVibrate(new long[]{0, 100, 50, 100, 50}) .setContentTitle(MainApp.gs(R.string.info)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index b6258b7de8..d197431190 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -1529,6 +1529,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter ServiceTaskExecutor.startTask(new WakeAndTuneTask()); } else { Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); i.putExtra("status", MainApp.gs(R.string.medtronic_error_operation_not_possible_no_configuration)); i.putExtra("title", MainApp.gs(R.string.combo_warning)); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); diff --git a/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java b/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java index a62f2c6fb7..7d7a5142e5 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java @@ -51,28 +51,22 @@ public class AlarmSoundService extends Service { resourceId = intent.getIntExtra("soundid", R.raw.error); player = new MediaPlayer(); - AssetFileDescriptor afd = MainApp.sResources.openRawResourceFd(resourceId); - if (afd == null) - return START_STICKY; try { + AssetFileDescriptor afd = MainApp.sResources.openRawResourceFd(resourceId); + if (afd == null) + return START_STICKY; player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); - } catch (IOException e) { - log.error("Unhandled exception", e); - } - player.setLooping(true); // Set looping - AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); - if (manager == null || !manager.isMusicActive()) { - player.setVolume(100, 100); - } - - try { + player.setLooping(true); // Set looping + AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); + if (manager == null || !manager.isMusicActive()) { + player.setVolume(100, 100); + } player.prepare(); player.start(); - } catch (IOException e) { + } catch (Exception e) { log.error("Unhandled exception", e); } - return START_STICKY; } From e4a3b0656e264b5161d919fb40328ca4ac877740 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 2 Aug 2019 22:33:35 +0200 Subject: [PATCH 4/5] ErrorHelperActivity, ErrorDialog -> kotlin --- .../general/overview/dialogs/ErrorDialog.java | 114 ------------------ .../general/overview/dialogs/ErrorDialog.kt | 85 +++++++++++++ .../overview/dialogs/ErrorHelperActivity.java | 29 ----- .../overview/dialogs/ErrorHelperActivity.kt | 25 ++++ 4 files changed, 110 insertions(+), 143 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java deleted file mode 100644 index f6b654567e..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.java +++ /dev/null @@ -1,114 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - - -import android.content.Intent; -import android.os.Build; -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.TextView; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.services.AlarmSoundService; - -public class ErrorDialog extends DialogFragment implements View.OnClickListener { - private static Logger log = LoggerFactory.getLogger(ErrorDialog.class); - Button muteButton; - Button okButton; - TextView statusView; - ErrorHelperActivity helperActivity; - - static String status; - static String title; - static int soundId; - - public ErrorDialog() { - super(); - } - - public void setStatus(String status) { - this.status = status; - } - - public void setTitle(String title) { - this.title = title; - } - - public void setSound(int soundId) { - this.soundId = soundId; - } - - public void setHelperActivity(ErrorHelperActivity activity) { - this.helperActivity = activity; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - getDialog().setTitle(title); - View view = inflater.inflate(R.layout.overview_error_dialog, container, false); - muteButton = (Button) view.findViewById(R.id.overview_error_mute); - okButton = (Button) view.findViewById(R.id.overview_error_ok); - statusView = (TextView) view.findViewById(R.id.overview_error_status); - muteButton.setOnClickListener(this); - okButton.setOnClickListener(this); - setCancelable(false); - - startAlarm(); - return view; - } - - @Override - public void onResume() { - super.onResume(); - if (getDialog() != null) - getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - statusView.setText(status); - } - - @Override - public void dismiss() { - super.dismissAllowingStateLoss(); - if (helperActivity != null) { - helperActivity.finish(); - } - stopAlarm(); - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.overview_error_mute: - log.debug("Error dialog mute button pressed"); - stopAlarm(); - break; - case R.id.overview_error_ok: - log.debug("Error dialog ok button pressed"); - dismiss(); - break; - } - } - - private void startAlarm() { - if (soundId != 0) { - Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); - alarm.putExtra("soundid", soundId); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - MainApp.instance().startForegroundService(alarm); - else - MainApp.instance().startService(alarm); - } - } - - private void stopAlarm() { - Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); - MainApp.instance().stopService(alarm); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.kt new file mode 100644 index 0000000000..d9d7c74542 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.kt @@ -0,0 +1,85 @@ +package info.nightscout.androidaps.plugins.general.overview.dialogs + + +import android.content.Intent +import android.os.Build +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.DialogFragment +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.services.AlarmSoundService +import kotlinx.android.synthetic.main.overview_error_dialog.* +import org.slf4j.LoggerFactory + +class ErrorDialog : DialogFragment() { + private val log = LoggerFactory.getLogger(ErrorDialog::class.java) + + var helperActivity: ErrorHelperActivity? = null + var status: String = "" + var title: String = "" + var sound: Int = 0 + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + dialog.setTitle(title) + isCancelable = false + + savedInstanceState?.let { bundle -> + bundle.getString("status")?.let { status = it } + bundle.getString("title")?.let { title = it } + sound = bundle.getInt("sound", R.raw.error) + } + return inflater.inflate(R.layout.overview_error_dialog, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + overview_error_ok.setOnClickListener { + log.debug("Error dialog ok button pressed") + dismiss() + } + overview_error_mute.setOnClickListener { + log.debug("Error dialog mute button pressed") + stopAlarm() + } + startAlarm() + } + + override fun onSaveInstanceState(bundle: Bundle) { + super.onSaveInstanceState(bundle) + bundle.putString("status", status) + bundle.putString("title", title) + bundle.putInt("sound", sound) + } + + override fun onResume() { + super.onResume() + dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + overview_error_status.text = status + } + + override fun dismiss() { + super.dismissAllowingStateLoss() + helperActivity?.finish() + stopAlarm() + } + + private fun startAlarm() { + if (sound != 0) { + val alarm = Intent(MainApp.instance().applicationContext, AlarmSoundService::class.java) + alarm.putExtra("soundid", sound) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + MainApp.instance().startForegroundService(alarm) + } else { + MainApp.instance().startService(alarm) + } + } + } + + private fun stopAlarm() = + MainApp.instance().stopService(Intent(MainApp.instance().applicationContext, AlarmSoundService::class.java)) +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java deleted file mode 100644 index 37b831e34f..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.java +++ /dev/null @@ -1,29 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - -import android.os.Bundle; -import androidx.appcompat.app.AppCompatActivity; - -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; -import info.nightscout.androidaps.utils.SP; - -public class ErrorHelperActivity extends AppCompatActivity { - public ErrorHelperActivity() { - super(); - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - ErrorDialog errorDialog = new ErrorDialog(); - errorDialog.setHelperActivity(this); - errorDialog.setStatus(getIntent().getStringExtra("status")); - errorDialog.setSound(getIntent().getIntExtra("soundid", R.raw.error)); - errorDialog.setTitle(getIntent().getStringExtra("title")); - errorDialog.show(this.getSupportFragmentManager(), "Error"); - - if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) { - NSUpload.uploadError(getIntent().getStringExtra("status")); - } - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.kt new file mode 100644 index 0000000000..8e4c7f1b57 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.kt @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.plugins.general.overview.dialogs + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity + +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload +import info.nightscout.androidaps.utils.SP + +class ErrorHelperActivity : AppCompatActivity() { + + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val errorDialog = ErrorDialog() + errorDialog.helperActivity = this + errorDialog.status = intent.getStringExtra("status") + errorDialog.sound = intent.getIntExtra("soundid", R.raw.error) + errorDialog.title = intent.getStringExtra("title") + errorDialog.show(supportFragmentManager, "Error") + + if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) { + NSUpload.uploadError(intent.getStringExtra("status")) + } + } +} From 43c0ce25bab06fe3dca5940d9b73db0c1911ac90 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 3 Aug 2019 18:19:38 +0200 Subject: [PATCH 5/5] startForeground in onCreate --- .../info/nightscout/androidaps/services/AlarmSoundService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java b/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java index 7d7a5142e5..2e9130f7db 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/AlarmSoundService.java @@ -38,6 +38,8 @@ public class AlarmSoundService extends Service { super.onCreate(); if (L.isEnabled(L.CORE)) log.debug("onCreate"); + Notification notification = PersistentNotificationPlugin.getPlugin().getLastNotification(); + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, notification); } public int onStartCommand(Intent intent, int flags, int startId) {