blob: 59ee2679ca3d9404953577263382c28a750bcfd5 [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;
23import android.view.View;
24import android.widget.FrameLayout;
Robert Snoebergerac7b903392019-02-11 17:41:05 -050025import android.widget.FrameLayout.LayoutParams;
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050026
27import com.android.keyguard.R;
28
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
35 /**
36 * Clock face views.
37 */
38 private View mDigitalClock;
39 private View mAnalogClock;
Robert Snoebergerce8c2042019-01-17 10:36:02 -050040 private View mTypeClock;
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050041
42 /**
43 * Pixel shifting amplitidues used to prevent screen burn-in.
44 */
45 private int mBurnInPreventionOffsetX;
46 private int mBurnInPreventionOffsetY;
47
48 public ClockLayout(Context context) {
49 this(context, null);
50 }
51
52 public ClockLayout(Context context, AttributeSet attrs) {
53 this(context, attrs, 0);
54 }
55
56 public ClockLayout(Context context, AttributeSet attrs, int defStyleAttr) {
57 super(context, attrs, defStyleAttr);
58 }
59
60 @Override
61 protected void onFinishInflate() {
62 super.onFinishInflate();
63 mDigitalClock = findViewById(R.id.digital_clock);
64 mAnalogClock = findViewById(R.id.analog_clock);
Robert Snoebergerce8c2042019-01-17 10:36:02 -050065 mTypeClock = findViewById(R.id.type_clock);
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050066
67 // Get pixel shifting X, Y amplitudes from resources.
68 Resources resources = getResources();
69 mBurnInPreventionOffsetX = resources.getDimensionPixelSize(
70 R.dimen.burn_in_prevention_offset_x);
71 mBurnInPreventionOffsetY = resources.getDimensionPixelSize(
72 R.dimen.burn_in_prevention_offset_y);
73 }
74
75 @Override
76 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
77 super.onLayout(changed, left, top, right, bottom);
78
Robert Snoeberger19e3e2a2019-03-08 16:27:04 -050079 final float offsetX = getBurnInOffset(mBurnInPreventionOffsetX * 2, true)
80 - mBurnInPreventionOffsetX;
81 final float offsetY = getBurnInOffset(mBurnInPreventionOffsetY * 2, false)
82 - mBurnInPreventionOffsetY;
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050083
84 // Put digital clock in two left corner of the screen.
85 if (mDigitalClock != null) {
Robert Snoebergerac7b903392019-02-11 17:41:05 -050086 LayoutParams params = (LayoutParams) mDigitalClock.getLayoutParams();
87 mDigitalClock.setX(offsetX + params.leftMargin);
88 mDigitalClock.setY(offsetY + params.topMargin);
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050089 }
90
91 // Put the analog clock in the middle of the screen.
92 if (mAnalogClock != null) {
93 mAnalogClock.setX(Math.max(0f, 0.5f * (getWidth() - mAnalogClock.getWidth()))
94 + offsetX);
95 mAnalogClock.setY(Math.max(0f, 0.5f * (getHeight() - mAnalogClock.getHeight()))
96 + offsetY);
97 }
Robert Snoebergerce8c2042019-01-17 10:36:02 -050098
99 // Put the typographic clock part way down the screen.
100 if (mTypeClock != null) {
101 mTypeClock.setX(offsetX);
102 mTypeClock.setY(0.2f * getHeight() + offsetY);
103 }
Robert Snoeberger046ee9c2019-01-10 18:29:38 -0500104 }
105}