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