package io.straas.android.sdk.circall.demo.ui;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.lang.ref.WeakReference;

import static io.straas.android.sdk.circall.demo.ui.FlashHint.FlashHandler.FLASH_INTERVAL;
import static io.straas.android.sdk.circall.demo.ui.FlashHint.FlashHandler.MESSAGE_FLASH;

public class FlashHint extends android.support.v7.widget.AppCompatImageView {

    private FlashHandler mHandler;
    public boolean mFlashEnabled;

    static class FlashHandler extends Handler {
        static final long FLASH_INTERVAL = 1000;

        static final int MESSAGE_FLASH = 0;

        private final WeakReference<FlashHint> mRecordingFlashHint;

        FlashHandler(FlashHint recordingFlashHint) {
            super();
            mRecordingFlashHint = new WeakReference<>(recordingFlashHint);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            FlashHint flashHint = mRecordingFlashHint.get();
            if (flashHint == null) {
                return;
            }
            switch (msg.what) {
                case MESSAGE_FLASH:
                    flashHint.setVisibility(
                            flashHint.getVisibility() == VISIBLE
                            ? INVISIBLE
                            : VISIBLE);
                    removeMessages(MESSAGE_FLASH);
                    sendEmptyMessageDelayed(MESSAGE_FLASH, FLASH_INTERVAL);
                    break;
                default:
                    break;
            }
        }
    }

    public FlashHint(Context context) {
        this(context, null);
    }

    public FlashHint(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlashHint(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setVisibility(View.GONE);
        mHandler = new FlashHandler(this);
    }

    public void setFlash(boolean enabled) {
        mFlashEnabled = enabled;
        if (mFlashEnabled) {
            bringToFront();
            mHandler.removeMessages(MESSAGE_FLASH);
            mHandler.sendEmptyMessageDelayed(MESSAGE_FLASH, FLASH_INTERVAL);
        } else {
            setVisibility(View.INVISIBLE);
            mHandler.removeMessages(MESSAGE_FLASH);
        }
    }

    @BindingAdapter("app:flash")
    public static void setFlash(FlashHint flashHint, boolean enabled) {
        flashHint.setFlash(enabled);
    }
}

@@ -120,62 +120,8 @@
 er;%0A
-import android.os.Handler;%0Aimport android.os.Message;%0A
 impo
@@ -225,911 +225,288 @@
 w;%0A%0A
-import java.lang.ref.WeakReference;%0A%0Aimport static io.straas.android.sdk.circall.demo.ui.FlashHint.FlashHandler.FLASH_INTERVAL;%0Aimport static io.straas.android.sdk.circall.demo.ui.FlashHint.FlashHandler.MESSAGE_FLASH;%0A%0Apublic class FlashHint extends android.support.v7.widget.AppCompatImageView %7B%0A%0A    private FlashHandler mHandler;%0A    public boolean mFlashEnabled;%0A%0A    static class FlashHandler extends Handler %7B%0A        static final long FLASH_INTERVAL = 1000;%0A%0A        static final int MESSAGE_FLASH = 0;%0A%0A        private final WeakReference%3CFlashHint%3E mRecordingFlashHint;%0A%0A        FlashHandler(FlashHint recordingFlashHint) %7B%0A            super();%0A            mRecording
+public class FlashHint extends android.support.v7.widget.AppCompatImageView %7B%0A%0A    static final long FLASH_INTERVAL = 1000;%0A%0A    public boolean mFlashEnabled;%0A%0A    Runnable mRun
 Flash
-Hint
  = new 
-WeakReference%3C%3E(recordingFlashHint);%0A        %7D%0A%0A        @Override%0A        public void handleMessage(Message msg) %7B%0A            super.handleMessage(msg);%0A            FlashHint flashHint = mRecordingFlashHint.get(
+Runnable() %7B%0A        @Override%0A        public void run() %7B%0A            removeCallbacks(this
 );%0A 
@@ -524,25 +524,22 @@
 if (
-f
+!mF
 lash
-Hint == null
+Enabled
 ) %7B%0A
@@ -592,85 +592,9 @@
     
-switch (msg.what) %7B%0A                case MESSAGE_FLASH:%0A                    f
+F
 lash
@@ -598,16 +598,21 @@
 ashHint.
+this.
 setVisib
@@ -638,25 +638,17 @@
         
-        f
+F
 lashHint
@@ -648,16 +648,21 @@
 ashHint.
+this.
 getVisib
@@ -776,215 +776,42 @@
     
-        removeMessages(MESSAGE_FLASH);%0A                    sendEmptyMessageDelayed(MESSAGE_FLASH, FLASH_INTERVAL);%0A                    break;%0A                default:%0A                    break;%0A            %7D
+postDelayed(this, FLASH_INTERVAL);
 %0A   
@@ -818,24 +818,25 @@
      %7D%0A    %7D
+;
 %0A%0A    public
@@ -1231,51 +1231,8 @@
 E);%0A
-        mHandler = new FlashHandler(this);%0A
     
@@ -1384,106 +1384,69 @@
     
-mHandler.removeMessages(MESSAGE_FLASH);%0A            mHandler.sendEmptyMessageDelayed(MESSAGE_FLASH
+removeCallbacks(mRunFlash);%0A            postDelayed(mRunFlash
 , FL
@@ -1536,45 +1536,33 @@
     
-mHandler.removeMessages(MESSAGE_FLASH
+removeCallbacks(mRunFlash
 );%0A 

Use built-in Handler in FlashHint.