ErrorHelperActivity, ErrorDialog -> kotlin

This commit is contained in:
Milos Kozak 2019-08-02 22:33:35 +02:00
parent 628adf25ba
commit e4a3b0656e
4 changed files with 110 additions and 143 deletions

View file

@ -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);
}
}

View file

@ -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))
}

View file

@ -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"));
}
}
}

View file

@ -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"))
}
}
}