blob: 71ae1f8620f604e3acbee2528556bcbc0ee44f84 [file] [log] [blame]
Mady Mellordea7ecf2018-12-10 15:47:40 -08001/*
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
17package com.android.systemui.bubbles;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.Color;
23import android.graphics.drawable.ShapeDrawable;
Mady Mellor3f2efdb2018-11-21 11:30:45 -080024import android.text.TextUtils;
Mady Mellordea7ecf2018-12-10 15:47:40 -080025import android.util.AttributeSet;
26import android.view.View;
27import android.widget.LinearLayout;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050028import android.widget.TextView;
Mady Mellordea7ecf2018-12-10 15:47:40 -080029
30import com.android.systemui.R;
31import com.android.systemui.recents.TriangleShape;
32
33/**
34 * Container for the expanded bubble view, handles rendering the caret and header of the view.
35 */
36public class BubbleExpandedViewContainer extends LinearLayout {
37
38 // The triangle pointing to the expanded view
39 private View mPointerView;
Mark Renouf89b1a4a2018-12-04 14:59:45 -050040 // The view displayed between the pointer and the expanded view
41 private TextView mHeaderView;
Mady Mellordea7ecf2018-12-10 15:47:40 -080042 // 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 Renouf89b1a4a2018-12-04 14:59:45 -050075 mHeaderView = findViewById(R.id.bubble_content_header);
Mady Mellordea7ecf2018-12-10 15:47:40 -080076 }
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 Renouf89b1a4a2018-12-04 14:59:45 -050088 * Set the text displayed within the header.
89 */
90 public void setHeaderText(CharSequence text) {
91 mHeaderView.setText(text);
Mady Mellor3f2efdb2018-11-21 11:30:45 -080092 mHeaderView.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE);
Mark Renouf89b1a4a2018-12-04 14:59:45 -050093 }
94
95 /**
Mady Mellordea7ecf2018-12-10 15:47:40 -080096 * Set the view to display for the expanded state. Passing null will clear the view.
97 */
98 public void setExpandedView(View view) {
Mark Renouf89b1a4a2018-12-04 14:59:45 -050099 if (mExpandedView == view) {
100 return;
101 }
Mady Mellordea7ecf2018-12-10 15:47:40 -0800102 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}