Home | History | Annotate | Download | only in clock
      1 /*
      2  * Copyright (C) 2019 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.keyguard.clock;
     17 
     18 import android.content.Context;
     19 import android.text.format.DateFormat;
     20 import android.util.AttributeSet;
     21 import android.widget.FrameLayout;
     22 import android.widget.ImageView;
     23 
     24 import com.android.keyguard.R;
     25 
     26 import java.text.SimpleDateFormat;
     27 import java.util.Calendar;
     28 import java.util.TimeZone;
     29 
     30 /**
     31  * Clock composed of two images that rotate with the time.
     32  *
     33  * The images are the clock hands. ImageClock expects two child ImageViews
     34  * with ids hour_hand and minute_hand.
     35  */
     36 public class ImageClock extends FrameLayout {
     37 
     38     private ImageView mHourHand;
     39     private ImageView mMinuteHand;
     40     private final Calendar mTime = Calendar.getInstance(TimeZone.getDefault());
     41     private String mDescFormat;
     42     private TimeZone mTimeZone;
     43 
     44     public ImageClock(Context context) {
     45         this(context, null);
     46     }
     47 
     48     public ImageClock(Context context, AttributeSet attrs) {
     49         this(context, attrs, 0);
     50     }
     51 
     52     public ImageClock(Context context, AttributeSet attrs, int defStyleAttr) {
     53         super(context, attrs, defStyleAttr);
     54         mDescFormat = ((SimpleDateFormat) DateFormat.getTimeFormat(context)).toLocalizedPattern();
     55     }
     56 
     57     /**
     58      * Call when the time changes to update the rotation of the clock hands.
     59      */
     60     public void onTimeChanged() {
     61         mTime.setTimeInMillis(System.currentTimeMillis());
     62         final float hourAngle = mTime.get(Calendar.HOUR) * 30f + mTime.get(Calendar.MINUTE) * 0.5f;
     63         mHourHand.setRotation(hourAngle);
     64         final float minuteAngle = mTime.get(Calendar.MINUTE) * 6f;
     65         mMinuteHand.setRotation(minuteAngle);
     66         setContentDescription(DateFormat.format(mDescFormat, mTime));
     67         invalidate();
     68     }
     69 
     70     /**
     71      * Call when the time zone has changed to update clock hands.
     72      *
     73      * @param timeZone The updated time zone that will be used.
     74      */
     75     public void onTimeZoneChanged(TimeZone timeZone) {
     76         mTimeZone = timeZone;
     77         mTime.setTimeZone(timeZone);
     78     }
     79 
     80     /**
     81      * Sets the colors to use on the clock face.
     82      * @param dark Darker color obtained from color palette.
     83      * @param light Lighter color obtained from color palette.
     84      */
     85     public void setClockColors(int dark, int light) {
     86         mHourHand.setColorFilter(dark);
     87         mMinuteHand.setColorFilter(light);
     88     }
     89 
     90     @Override
     91     protected void onFinishInflate() {
     92         super.onFinishInflate();
     93         mHourHand = findViewById(R.id.hour_hand);
     94         mMinuteHand = findViewById(R.id.minute_hand);
     95     }
     96 
     97     @Override
     98     protected void onAttachedToWindow() {
     99         super.onAttachedToWindow();
    100         mTime.setTimeZone(mTimeZone != null ? mTimeZone : TimeZone.getDefault());
    101         onTimeChanged();
    102     }
    103 }
    104