Code example for Time

Methods: getActualMaximum, normalize, set, setToNow

0
            if (visible) {
                if (!mWatcherRegistered) {
                    mWatcherRegistered = true;
                    registerReceiver(mWatcher, mFilter, null, mHandler);
                } 
                mCalendar = new Time();
                mCalendar.setToNow();
            } else { 
                if (mWatcherRegistered) {
                    mWatcherRegistered = false;
                    unregisterReceiver(mWatcher);
                } 
                mHandler.removeCallbacks(mDrawClock);
            } 
            drawFrame(); 
        } 
 
        @Override 
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            drawFrame(); 
        } 
 
        @Override 
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
        } 
 
        @Override 
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mHandler.removeCallbacks(mDrawClock);
        } 
 
        @Override 
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xStep, float yStep, int xPixels, int yPixels) {
            if (isPreview()) return;
 
            mOffsetX = xOffset;
            drawFrame(); 
        } 
 
        void drawFrame() { 
            if (mPalette == null) {
                Log.w("PolarClockWallpaper", "no palette?!");
                return; 
            } 
 
            final SurfaceHolder holder = getSurfaceHolder();
            final Rect frame = holder.getSurfaceFrame();
            final int width = frame.width();
            final int height = frame.height();
 
            Canvas c = null;
            try { 
                c = holder.lockCanvas();
                if (c != null) {
                    final Time calendar = mCalendar;
                    final Paint paint = mPaint;
 
                    final long millis = System.currentTimeMillis();
                    calendar.set(millis);
                    calendar.normalize(false);
 
                    int s = width / 2;
                    int t = height / 2;
 
                    c.drawColor(mPalette.getBackgroundColor());
 
                    c.translate(s + MathUtils.lerp(s, -s, mOffsetX), t);
                    c.rotate(-90.0f);
                    if (height < width) {
                        c.scale(0.9f, 0.9f);
                    } 
 
                    float size = Math.min(width, height) * 0.5f - DEFAULT_RING_THICKNESS;
                    final RectF rect = mRect;
                    rect.set(-size, -size, size, size);
                    float angle;
 
                    float lastRingThickness = DEFAULT_RING_THICKNESS;
 
                    if (mShowSeconds) {
                        // Draw seconds   
                        angle = (float) (millis % 60000) / 60000.0f;
                        //Log.d("PolarClock", "millis=" + millis + ", angle=" + angle); 
                        paint.setColor(mPalette.getSecondColor(angle));
 
                        if (mVariableLineWidth) {
                            lastRingThickness = SMALL_RING_THICKNESS;
                            paint.setStrokeWidth(lastRingThickness);
                        } 
                        c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
                    } 
 
                    // Draw minutes 
                    size -= (SMALL_GAP + lastRingThickness);
                    rect.set(-size, -size, size, size);
 
                    angle = ((calendar.minute * 60.0f + calendar.second) % 3600) / 3600.0f;
                    paint.setColor(mPalette.getMinuteColor(angle));
 
                    if (mVariableLineWidth) {
                        lastRingThickness = MEDIUM_RING_THICKNESS;
                        paint.setStrokeWidth(lastRingThickness);
                    } 
                    c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
 
                    // Draw hours 
                    size -= (SMALL_GAP + lastRingThickness);
                    rect.set(-size, -size, size, size);
 
                    angle = ((calendar.hour * 60.0f + calendar.minute) % 1440) / 1440.0f;
                    paint.setColor(mPalette.getHourColor(angle));
 
                    if (mVariableLineWidth) {
                        lastRingThickness = LARGE_RING_THICKNESS;
                        paint.setStrokeWidth(lastRingThickness);
                    } 
                    c.drawArc(rect, 0.0f, angle * 360.0f, false, paint);
 
                    // Draw day 
                    size -= (LARGE_GAP + lastRingThickness);
                    rect.set(-size, -size, size, size);
 
                    angle = (calendar.monthDay - 1) /
                            (float) (calendar.getActualMaximum(Time.MONTH_DAY) - 1);
                    paint.setColor(mPalette.getDayColor(angle));
 
                    if (mVariableLineWidth) {
                        lastRingThickness = MEDIUM_RING_THICKNESS;
                        paint.setStrokeWidth(lastRingThickness);