blob: d44d89e63e8f6599022672a31de05ce6576f8e17 [file] [log] [blame]
Robert Snoeberger046ee9c2019-01-10 18:29:38 -05001/*
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 */
16package com.android.keyguard.clock;
17
18import static com.android.systemui.doze.util.BurnInHelperKt.getBurnInOffset;
19
20import android.content.Context;
21import android.content.res.Resources;
22import android.util.AttributeSet;
Robert Snoebergere9aa87b2019-04-29 16:37:14 -040023import android.util.MathUtils;
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050024import android.view.View;
25import android.widget.FrameLayout;
26
Sunny Goyal87fccf02019-08-13 17:39:10 -070027import com.android.systemui.R;
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050028
29/**
30 * Positions clock faces (analog, digital, typographic) and handles pixel shifting
31 * to prevent screen burn-in.
32 */
33public class ClockLayout extends FrameLayout {
34
Robert Snoeberger7fd7f512019-04-12 13:46:16 -040035 private static final int ANALOG_CLOCK_SHIFT_FACTOR = 3;
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050036 /**
37 * Clock face views.
38 */
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050039 private View mAnalogClock;
40
41 /**
Robert Snoeberger69956802019-04-16 16:55:21 -040042 * Pixel shifting amplitudes used to prevent screen burn-in.
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050043 */
44 private int mBurnInPreventionOffsetX;
45 private int mBurnInPreventionOffsetY;
46
Robert Snoebergere9aa87b2019-04-29 16:37:14 -040047 private float mDarkAmount;
48
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050049 public ClockLayout(Context context) {
50 this(context, null);
51 }
52
53 public ClockLayout(Context context, AttributeSet attrs) {
54 this(context, attrs, 0);
55 }
56
57 public ClockLayout(Context context, AttributeSet attrs, int defStyleAttr) {
58 super(context, attrs, defStyleAttr);
59 }
60
61 @Override
62 protected void onFinishInflate() {
63 super.onFinishInflate();
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050064 mAnalogClock = findViewById(R.id.analog_clock);
65
66 // Get pixel shifting X, Y amplitudes from resources.
67 Resources resources = getResources();
68 mBurnInPreventionOffsetX = resources.getDimensionPixelSize(
69 R.dimen.burn_in_prevention_offset_x);
70 mBurnInPreventionOffsetY = resources.getDimensionPixelSize(
71 R.dimen.burn_in_prevention_offset_y);
72 }
73
74 @Override
75 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
76 super.onLayout(changed, left, top, right, bottom);
Robert Snoeberger7fd7f512019-04-12 13:46:16 -040077 positionChildren();
78 }
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050079
Robert Snoeberger7fd7f512019-04-12 13:46:16 -040080 void onTimeChanged() {
81 positionChildren();
82 }
83
Robert Snoebergere9aa87b2019-04-29 16:37:14 -040084 /**
85 * See {@link com.android.systemui.plugins.ClockPlugin#setDarkAmount(float)}.
86 */
87 void setDarkAmount(float darkAmount) {
88 mDarkAmount = darkAmount;
89 positionChildren();
90 }
91
Robert Snoeberger7fd7f512019-04-12 13:46:16 -040092 private void positionChildren() {
Robert Snoebergere9aa87b2019-04-29 16:37:14 -040093 final float offsetX = MathUtils.lerp(0f,
94 getBurnInOffset(mBurnInPreventionOffsetX * 2, true) - mBurnInPreventionOffsetX,
95 mDarkAmount);
96 final float offsetY = MathUtils.lerp(0f,
Robert Snoeberger0877aab2019-06-11 16:48:41 -040097 getBurnInOffset(mBurnInPreventionOffsetY * 2, false)
98 - 0.5f * mBurnInPreventionOffsetY,
Robert Snoebergere9aa87b2019-04-29 16:37:14 -040099 mDarkAmount);
Robert Snoeberger046ee9c2019-01-10 18:29:38 -0500100
Robert Snoeberger046ee9c2019-01-10 18:29:38 -0500101 // Put the analog clock in the middle of the screen.
102 if (mAnalogClock != null) {
103 mAnalogClock.setX(Math.max(0f, 0.5f * (getWidth() - mAnalogClock.getWidth()))
Robert Snoeberger7fd7f512019-04-12 13:46:16 -0400104 + ANALOG_CLOCK_SHIFT_FACTOR * offsetX);
Robert Snoeberger046ee9c2019-01-10 18:29:38 -0500105 mAnalogClock.setY(Math.max(0f, 0.5f * (getHeight() - mAnalogClock.getHeight()))
Robert Snoeberger7fd7f512019-04-12 13:46:16 -0400106 + ANALOG_CLOCK_SHIFT_FACTOR * offsetY);
Robert Snoeberger046ee9c2019-01-10 18:29:38 -0500107 }
Robert Snoeberger046ee9c2019-01-10 18:29:38 -0500108 }
109}