Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.systemui.bubbles; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Color; |
| 23 | import android.graphics.drawable.ShapeDrawable; |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame^] | 24 | import android.text.TextUtils; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
| 27 | import android.widget.LinearLayout; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 28 | import android.widget.TextView; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 29 | |
| 30 | import com.android.systemui.R; |
| 31 | import com.android.systemui.recents.TriangleShape; |
| 32 | |
| 33 | /** |
| 34 | * Container for the expanded bubble view, handles rendering the caret and header of the view. |
| 35 | */ |
| 36 | public class BubbleExpandedViewContainer extends LinearLayout { |
| 37 | |
| 38 | // The triangle pointing to the expanded view |
| 39 | private View mPointerView; |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 40 | // The view displayed between the pointer and the expanded view |
| 41 | private TextView mHeaderView; |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 42 | // The view that is being displayed for the expanded state |
| 43 | private View mExpandedView; |
| 44 | |
| 45 | public BubbleExpandedViewContainer(Context context) { |
| 46 | this(context, null); |
| 47 | } |
| 48 | |
| 49 | public BubbleExpandedViewContainer(Context context, AttributeSet attrs) { |
| 50 | this(context, attrs, 0); |
| 51 | } |
| 52 | |
| 53 | public BubbleExpandedViewContainer(Context context, AttributeSet attrs, int defStyleAttr) { |
| 54 | this(context, attrs, defStyleAttr, 0); |
| 55 | } |
| 56 | |
| 57 | public BubbleExpandedViewContainer(Context context, AttributeSet attrs, int defStyleAttr, |
| 58 | int defStyleRes) { |
| 59 | super(context, attrs, defStyleAttr, defStyleRes); |
| 60 | setOrientation(VERTICAL); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void onFinishInflate() { |
| 65 | super.onFinishInflate(); |
| 66 | |
| 67 | Resources res = getResources(); |
| 68 | mPointerView = findViewById(R.id.pointer_view); |
| 69 | int width = res.getDimensionPixelSize(R.dimen.bubble_pointer_width); |
| 70 | int height = res.getDimensionPixelSize(R.dimen.bubble_pointer_height); |
| 71 | ShapeDrawable triangleDrawable = new ShapeDrawable( |
| 72 | TriangleShape.create(width, height, true /* pointUp */)); |
| 73 | triangleDrawable.setTint(Color.WHITE); // TODO: dark mode |
| 74 | mPointerView.setBackground(triangleDrawable); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 75 | mHeaderView = findViewById(R.id.bubble_content_header); |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set the x position that the tip of the triangle should point to. |
| 80 | */ |
| 81 | public void setPointerPosition(int x) { |
| 82 | // Adjust for the pointer size |
| 83 | x -= (mPointerView.getWidth() / 2); |
| 84 | mPointerView.setTranslationX(x); |
| 85 | } |
| 86 | |
| 87 | /** |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 88 | * Set the text displayed within the header. |
| 89 | */ |
| 90 | public void setHeaderText(CharSequence text) { |
| 91 | mHeaderView.setText(text); |
Mady Mellor | 3f2efdb | 2018-11-21 11:30:45 -0800 | [diff] [blame^] | 92 | mHeaderView.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE); |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 96 | * Set the view to display for the expanded state. Passing null will clear the view. |
| 97 | */ |
| 98 | public void setExpandedView(View view) { |
Mark Renouf | 89b1a4a | 2018-12-04 14:59:45 -0500 | [diff] [blame] | 99 | if (mExpandedView == view) { |
| 100 | return; |
| 101 | } |
Mady Mellor | dea7ecf | 2018-12-10 15:47:40 -0800 | [diff] [blame] | 102 | if (mExpandedView != null) { |
| 103 | removeView(mExpandedView); |
| 104 | } |
| 105 | mExpandedView = view; |
| 106 | if (mExpandedView != null) { |
| 107 | addView(mExpandedView); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return the view containing the expanded content, can be null. |
| 113 | */ |
| 114 | @Nullable |
| 115 | public View getExpandedView() { |
| 116 | return mExpandedView; |
| 117 | } |
| 118 | } |