AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/SingleClickButton.java
2020-01-13 19:22:28 +01:00

70 lines
2 KiB
Java

package info.nightscout.androidaps.utils;
import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
/**
* Created by mike on 22.12.2017.
*/
public class SingleClickButton extends androidx.appcompat.widget.AppCompatButton implements View.OnClickListener {
private static Logger log = StacktraceLoggerWrapper.getLogger(SingleClickButton.class);
Context context;
OnClickListener listener = null;
public SingleClickButton(Context context) {
super(context);
this.context = context;
super.setOnClickListener(this);
}
public SingleClickButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
super.setOnClickListener(this);
}
public SingleClickButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
super.setOnClickListener(this);
}
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
listener = l;
}
@Override
public void onClick(final View v) {
setEnabled(false);
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(3000);
Activity activity = (Activity) context;
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
setEnabled(true);
log.debug("Button enabled");
}
});
}
}).start();
if (listener != null)
listener.onClick(v);
}
}