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.app.WallpaperManager;
     19 import android.content.res.Resources;
     20 import android.graphics.Bitmap;
     21 import android.graphics.BitmapFactory;
     22 import android.graphics.Color;
     23 import android.graphics.Paint.Style;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.widget.TextView;
     27 
     28 import com.android.internal.colorextraction.ColorExtractor;
     29 import com.android.keyguard.R;
     30 import com.android.systemui.colorextraction.SysuiColorExtractor;
     31 import com.android.systemui.plugins.ClockPlugin;
     32 
     33 import java.util.TimeZone;
     34 
     35 /**
     36  * Plugin for the default clock face used only to provide a preview.
     37  */
     38 public class DefaultClockController implements ClockPlugin {
     39 
     40     /**
     41      * Resources used to get title and thumbnail.
     42      */
     43     private final Resources mResources;
     44 
     45     /**
     46      * LayoutInflater used to inflate custom clock views.
     47      */
     48     private final LayoutInflater mLayoutInflater;
     49 
     50     /**
     51      * Extracts accent color from wallpaper.
     52      */
     53     private final SysuiColorExtractor mColorExtractor;
     54 
     55     /**
     56      * Renders preview from clock view.
     57      */
     58     private final ViewPreviewer mRenderer = new ViewPreviewer();
     59 
     60     /**
     61      * Root view of preview.
     62      */
     63     private View mView;
     64 
     65     /**
     66      * Text clock in preview view hierarchy.
     67      */
     68     private TextView mTextTime;
     69 
     70     /**
     71      * Date showing below time in preview view hierarchy.
     72      */
     73     private TextView mTextDate;
     74 
     75     /**
     76      * Create a DefaultClockController instance.
     77      *
     78      * @param res Resources contains title and thumbnail.
     79      * @param inflater Inflater used to inflate custom clock views.
     80      * @param colorExtractor Extracts accent color from wallpaper.
     81      */
     82     public DefaultClockController(Resources res, LayoutInflater inflater,
     83             SysuiColorExtractor colorExtractor) {
     84         mResources = res;
     85         mLayoutInflater = inflater;
     86         mColorExtractor = colorExtractor;
     87     }
     88 
     89     private void createViews() {
     90         mView = mLayoutInflater.inflate(R.layout.default_clock_preview, null);
     91         mTextTime = mView.findViewById(R.id.time);
     92         mTextDate = mView.findViewById(R.id.date);
     93     }
     94 
     95     @Override
     96     public void onDestroyView() {
     97         mView = null;
     98         mTextTime = null;
     99         mTextDate = null;
    100     }
    101 
    102     @Override
    103     public String getName() {
    104         return "default";
    105     }
    106 
    107     @Override
    108     public String getTitle() {
    109         return mResources.getString(R.string.clock_title_default);
    110     }
    111 
    112     @Override
    113     public Bitmap getThumbnail() {
    114         return BitmapFactory.decodeResource(mResources, R.drawable.default_thumbnail);
    115     }
    116 
    117     @Override
    118     public Bitmap getPreview(int width, int height) {
    119 
    120         // Use the big clock view for the preview
    121         View view = getBigClockView();
    122 
    123         // Initialize state of plugin before generating preview.
    124         setDarkAmount(1f);
    125         setTextColor(Color.WHITE);
    126         ColorExtractor.GradientColors colors = mColorExtractor.getColors(
    127                 WallpaperManager.FLAG_LOCK);
    128         setColorPalette(colors.supportsDarkText(), colors.getColorPalette());
    129         onTimeTick();
    130 
    131         return mRenderer.createPreview(view, width, height);
    132     }
    133 
    134     @Override
    135     public View getView() {
    136         return null;
    137     }
    138 
    139     @Override
    140     public View getBigClockView() {
    141         if (mView == null) {
    142             createViews();
    143         }
    144         return mView;
    145     }
    146 
    147     @Override
    148     public int getPreferredY(int totalHeight) {
    149         return totalHeight / 2;
    150     }
    151 
    152     @Override
    153     public void setStyle(Style style) {}
    154 
    155     @Override
    156     public void setTextColor(int color) {
    157         mTextTime.setTextColor(color);
    158         mTextDate.setTextColor(color);
    159     }
    160 
    161     @Override
    162     public void setColorPalette(boolean supportsDarkText, int[] colorPalette) {}
    163 
    164     @Override
    165     public void onTimeTick() {
    166     }
    167 
    168     @Override
    169     public void setDarkAmount(float darkAmount) {}
    170 
    171     @Override
    172     public void onTimeZoneChanged(TimeZone timeZone) {}
    173 
    174     @Override
    175     public boolean shouldShowStatusArea() {
    176         return true;
    177     }
    178 }
    179