blob: c18f9b61c044786fe300e183dcc4da2914a1d1f9 [file] [log] [blame]
Lucas Dupin957e50c2017-10-10 11:23:27 -07001/*
2 * Copyright (C) 2017 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
17package com.android.keyguard;
18
19import android.app.PendingIntent;
20import android.app.slice.Slice;
21import android.app.slice.SliceItem;
22import android.app.slice.SliceQuery;
23import android.content.Context;
24import android.database.ContentObserver;
25import android.graphics.Color;
26import android.net.Uri;
27import android.os.Handler;
28import android.util.AttributeSet;
29import android.widget.LinearLayout;
30import android.widget.TextView;
31
32import com.android.internal.graphics.ColorUtils;
33import com.android.systemui.R;
34import com.android.systemui.keyguard.KeyguardSliceProvider;
35
36/**
37 * View visible under the clock on the lock screen and AoD.
38 */
39public class KeyguardSliceView extends LinearLayout {
40
41 private final Uri mKeyguardSliceUri;
42 private TextView mTitle;
43 private TextView mText;
44 private Slice mSlice;
45 private PendingIntent mSliceAction;
46 private int mTextColor;
47 private float mDarkAmount = 0;
48
49 private final ContentObserver mObserver;
50
51 public KeyguardSliceView(Context context) {
52 this(context, null, 0);
53 }
54
55 public KeyguardSliceView(Context context, AttributeSet attrs) {
56 this(context, attrs, 0);
57 }
58
59 public KeyguardSliceView(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
61 mObserver = new KeyguardSliceObserver(new Handler());
62 mKeyguardSliceUri = Uri.parse(KeyguardSliceProvider.KEYGUARD_SLICE_URI);;
63 }
64
65 @Override
66 protected void onFinishInflate() {
67 super.onFinishInflate();
68 mTitle = findViewById(R.id.title);
69 mText = findViewById(R.id.text);
70 mTextColor = mTitle.getCurrentTextColor();
71 }
72
73 @Override
74 protected void onAttachedToWindow() {
75 super.onAttachedToWindow();
76
77 // Set initial content
78 showSlice(Slice.bindSlice(getContext().getContentResolver(), mKeyguardSliceUri));
79
80 // Make sure we always have the most current slice
81 getContext().getContentResolver().registerContentObserver(mKeyguardSliceUri,
82 false /* notifyDescendants */, mObserver);
83 }
84
85 @Override
86 protected void onDetachedFromWindow() {
87 super.onDetachedFromWindow();
88
89 getContext().getContentResolver().unregisterContentObserver(mObserver);
90 }
91
92 private void showSlice(Slice slice) {
93 // Items will be wrapped into an action when they have tap targets.
94 SliceItem actionSlice = SliceQuery.find(slice, SliceItem.TYPE_ACTION);
95 if (actionSlice != null) {
96 mSlice = actionSlice.getSlice();
97 mSliceAction = actionSlice.getAction();
98 } else {
99 mSlice = slice;
100 mSliceAction = null;
101 }
102
103 if (mSlice == null) {
104 setVisibility(GONE);
105 return;
106 }
107
108 SliceItem title = SliceQuery.find(mSlice, SliceItem.TYPE_TEXT, Slice.HINT_TITLE, null);
109 if (title == null) {
110 mTitle.setVisibility(GONE);
111 } else {
112 mTitle.setVisibility(VISIBLE);
113 mTitle.setText(title.getText());
114 }
115
116 SliceItem text = SliceQuery.find(mSlice, SliceItem.TYPE_TEXT, null, Slice.HINT_TITLE);
117 if (text == null) {
118 mText.setVisibility(GONE);
119 } else {
120 mText.setVisibility(VISIBLE);
121 mText.setText(text.getText());
122 }
123
124 final int visibility = title == null && text == null ? GONE : VISIBLE;
125 if (visibility != getVisibility()) {
126 setVisibility(visibility);
127 }
128 }
129
130 public void setDark(float darkAmount) {
131 mDarkAmount = darkAmount;
132 updateTextColors();
133 }
134
135 public void setTextColor(int textColor) {
136 mTextColor = textColor;
137 }
138
139 private void updateTextColors() {
140 final int blendedColor = ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount);
141 mTitle.setTextColor(blendedColor);
142 mText.setTextColor(blendedColor);
143 }
144
145 private class KeyguardSliceObserver extends ContentObserver {
146 KeyguardSliceObserver(Handler handler) {
147 super(handler);
148 }
149
150 @Override
151 public void onChange(boolean selfChange) {
152 this.onChange(selfChange, null);
153 }
154
155 @Override
156 public void onChange(boolean selfChange, Uri uri) {
157 showSlice(Slice.bindSlice(getContext().getContentResolver(), mKeyguardSliceUri));
158 }
159 }
160}