androidconcurrencyandroid-widgetjava.util.concurrentatomicboolean

How to make android digital clock flash?


I need to implement Digital clock for my layout. and I want the dots between hours and minutes to disappear and reappear every half of the second or so. I copied android clock code and changed it a bit. The idea was to change time formatting based on some boolean. But for some reason this pproach didn't work... If you are a concurrency master, please help me! Here's the code.

public class CustomDigitalClock extends TextView {

    Calendar mCalendar;
    private final static String m12 = "h:mm aa";
    private final static String m24 = "k:mm";
    private final static String m24space = "k mm";
    private static final String LOG_TAG = "Clock";
    private FormatChangeObserver mFormatChangeObserver;

    private Runnable mTicker;
    private Runnable dotsRunner;
    private Handler mHandler;
    private AtomicBoolean dotsVisible;

    private volatile boolean mTickerStopped = false;

    String mFormat;

    public CustomDigitalClock(Context context) {
        super(context);
        initClock(context);
    }

    public CustomDigitalClock(Context context, AttributeSet attrs) {
        super(context, attrs);
        initClock(context);
    }

    private void initClock(Context context) {
        Resources r = context.getResources();
        dotsVisible = new AtomicBoolean();


        if (mCalendar == null) {
            mCalendar = Calendar.getInstance();
        }

        mFormatChangeObserver = new FormatChangeObserver();
        getContext().getContentResolver().registerContentObserver(
                Settings.System.CONTENT_URI, true, mFormatChangeObserver);

        setFormat();
    }

    @Override
    protected void onAttachedToWindow() {
        mTickerStopped = false;
        super.onAttachedToWindow();
        mHandler = new Handler();

        /**
         * requests a tick on the next hard-second boundary
         */
        mTicker = new Runnable() {
            public void run() {
                if (mTickerStopped) return;
                mCalendar.setTimeInMillis(System.currentTimeMillis());

                if (dotsVisible.get()) {
                    setText(DateFormat.format(m24, mCalendar));
                } else {
                    setText(DateFormat.format(m24space, mCalendar));
                }

                invalidate();
                long now = SystemClock.uptimeMillis();
                long next = now + (1000 - now % 1000);
                mHandler.postAtTime(mTicker, next);
            }
        };
        dotsRunner = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    dotsVisible.compareAndSet(true, false);
                    dotsVisible.compareAndSet(false, true);
                } catch (InterruptedException ex) {
                    Log.e(LOG_TAG, ex.getMessage());
                }
            }
        };
        dotsRunner.run();
        mTicker.run();

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mTickerStopped = true;
    }

    /**
     * Pulls 12/24 mode from system settings
     */
    private boolean get24HourMode() {
        return android.text.format.DateFormat.is24HourFormat(getContext());
    }

    private void setFormat() {
        if (get24HourMode()) {
            mFormat = m24;
        } else {
            mFormat = m12;
        }
    }

    private class FormatChangeObserver extends ContentObserver {
        public FormatChangeObserver() {
            super(new Handler());
        }

        @Override
        public void onChange(boolean selfChange) {
            setFormat();
        }
    }

}

Solution

  • So I solved the problem myself. If you ever need to have a clock with blinking dots, you can take mine.

      public class CustomDigitalClock extends TextView {
    
        Calendar mCalendar;
        private final static String m12 = "h:mm aa";
        private final static String m24 = "k:mm";
        private final static String m24space = "k mm";
        private static final String LOG_TAG = "Clock";
        private FormatChangeObserver mFormatChangeObserver;
    
        private Runnable mTicker;
        private Runnable dotsRunner;
        private Handler mHandler;
        private AtomicBoolean dotsVisible;
    
        private volatile boolean mTickerStopped = false;
        private volatile boolean dotsRunnerStopped = false;
    
        String mFormat;
    
        public CustomDigitalClock(Context context) {
            super(context);
            initClock(context);
        }
    
        public CustomDigitalClock(Context context, AttributeSet attrs) {
            super(context, attrs);
            initClock(context);
        }
    
        private void initClock(Context context) {
            Resources r = context.getResources();
            dotsVisible = new AtomicBoolean(true);
    
    
            if (mCalendar == null) {
                mCalendar = Calendar.getInstance();
            }
    
            mFormatChangeObserver = new FormatChangeObserver();
            getContext().getContentResolver().registerContentObserver(
                    Settings.System.CONTENT_URI, true, mFormatChangeObserver);
    
            setFormat();
        }
    
        @Override
        protected void onAttachedToWindow() {
            mTickerStopped = false;
            dotsRunnerStopped = false;
            super.onAttachedToWindow();
            mHandler = new Handler();
    
            /**
             * requests a tick on the next hard-second boundary
             */
            mTicker = new Runnable() {
                public void run() {
                    if (mTickerStopped) return;
                    mCalendar.setTimeInMillis(System.currentTimeMillis());
    
                    if (dotsVisible.get()) {
                        setText(DateFormat.format(m24, mCalendar));
                    } else {
                        setText(DateFormat.format(m24space, mCalendar));
                    }
    
                    invalidate();
                    long now = SystemClock.uptimeMillis();
                    //long next = now + (1000 - now % 1000);
                    long next = now + 800;
    
                    mHandler.postAtTime(mTicker, next);
                }
            };
            dotsRunner = new Runnable() {
                @Override
                public void run() {
                    while(!dotsRunnerStopped) {
                        try {
                            Thread.sleep(800);
                            Log.i(LOG_TAG, "slept for 1 sec");
                            if (dotsVisible.compareAndSet(true, false)) {
                                Log.i(LOG_TAG, "set visible to " + dotsVisible.get());
                            } else {
                                dotsVisible.compareAndSet(false, true);
                                Log.i(LOG_TAG, "Set visible to " + dotsVisible.get());
                            }
                        } catch (InterruptedException ex) {
                            Log.e(LOG_TAG, ex.getMessage());
                        }
                    }
                }
            };
            mTicker.run();
            new Thread(dotsRunner).start();
    
        }
    
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            mTickerStopped = true;
            dotsRunnerStopped = true;
        }
    
        /**
         * Pulls 12/24 mode from system settings
         */
        private boolean get24HourMode() {
            return android.text.format.DateFormat.is24HourFormat(getContext());
        }
    
        private void setFormat() {
            if (get24HourMode()) {
                mFormat = m24;
            } else {
                mFormat = m12;
            }
        }
    
        private class FormatChangeObserver extends ContentObserver {
            public FormatChangeObserver() {
                super(new Handler());
            }
    
            @Override
            public void onChange(boolean selfChange) {
                setFormat();
            }
        }
    
    }