FileDocCategorySizeDatePackage
DigitalClock.javaAPI DocAndroid 1.5 API6474Wed May 06 22:42:42 BST 2009com.android.alarmclock

DigitalClock

public class DigitalClock extends android.widget.LinearLayout
Displays the time

Fields Summary
private static final String
M12
private static final String
M24
private Calendar
mCalendar
private String
mFormat
private android.widget.TextView
mTimeDisplay
private AmPm
mAmPm
private boolean
mAnimate
private android.database.ContentObserver
mFormatChangeObserver
private boolean
mLive
private boolean
mAttached
private final android.os.Handler
mHandler
private final android.content.BroadcastReceiver
mIntentReceiver
Constructors Summary
public DigitalClock(android.content.Context context)

        this(context, null);
    
public DigitalClock(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
    
Methods Summary
protected voidonAttachedToWindow()

        super.onAttachedToWindow();

        if (Log.LOGV) Log.v("onAttachedToWindow " + this);

        if (mAttached) return;
        mAttached = true;

        if (mAnimate) {
            AnimationDrawable frameAnimation =
                    (AnimationDrawable) mContext.getResources().getDrawable(
                            R.drawable.animate_circle);
            View digitalClock = findViewById(R.id.digitalClock);
            digitalClock.setBackgroundDrawable(frameAnimation);
            /* Start the animation (looped playback by default). */
            ((AnimationDrawable) digitalClock.getBackground()).start();
            /* No luck wrapping animation or individual bitmaps in a
               ScaleDrawable */
            digitalClock.requestLayout();
        }

        if (mLive) {
            /* monitor time ticks, time changed, timezone */
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_TIME_CHANGED);
            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
            mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
        }

        /* monitor 12/24-hour display preference */
        mFormatChangeObserver = new FormatChangeObserver();
        mContext.getContentResolver().registerContentObserver(
                Settings.System.CONTENT_URI, true, mFormatChangeObserver);

        updateTime();
    
protected voidonDetachedFromWindow()

        super.onDetachedFromWindow();

        if (!mAttached) return;
        mAttached = false;

        Drawable background = getBackground();
        if (background instanceof AnimationDrawable) {
            ((AnimationDrawable) background).stop();
        }

        if (mLive) {
            mContext.unregisterReceiver(mIntentReceiver);
        }
        mContext.getContentResolver().unregisterContentObserver(
                mFormatChangeObserver);
    
protected voidonFinishInflate()

        super.onFinishInflate();

        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mAmPm = new AmPm(this);
        mCalendar = Calendar.getInstance();

        setDateFormat();
    
voidsetAnimate()

        mAnimate = true;
    
private voidsetDateFormat()

        mFormat = Alarms.get24HourMode(mContext) ? M24 : M12;
        mAmPm.setShowAmPm(mFormat == M12);
    
voidsetLive(boolean live)

        mLive = live;
    
voidupdateTime(java.util.Calendar c)

        mCalendar = c;
        updateTime();
    
private voidupdateTime()

        if (mLive) {
            mCalendar.setTimeInMillis(System.currentTimeMillis());
        }

        CharSequence newTime = DateFormat.format(mFormat, mCalendar);
        mTimeDisplay.setText(newTime);
        mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);