1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.taskbar;
17 
18 import static com.android.launcher3.anim.Interpolators.AGGRESSIVE_EASE;
19 
20 import android.animation.PropertyValuesHolder;
21 import android.content.Context;
22 import android.graphics.Rect;
23 import android.util.AttributeSet;
24 import android.util.Pair;
25 import android.view.View;
26 import android.widget.Button;
27 
28 import com.android.launcher3.Insettable;
29 import com.android.launcher3.R;
30 import com.android.launcher3.views.AbstractSlideInView;
31 
32 /** Education view about the Taskbar. */
33 public class TaskbarEduView extends AbstractSlideInView<TaskbarActivityContext>
34         implements Insettable {
35 
36     private static final int DEFAULT_OPEN_DURATION = 500;
37     private static final int DEFAULT_CLOSE_DURATION = 200;
38 
39     private final Rect mInsets = new Rect();
40 
41     private Button mStartButton;
42     private Button mEndButton;
43     private TaskbarEduPagedView mPagedView;
44 
TaskbarEduView(Context context, AttributeSet attr)45     public TaskbarEduView(Context context, AttributeSet attr) {
46         this(context, attr, 0);
47     }
48 
TaskbarEduView(Context context, AttributeSet attrs, int defStyleAttr)49     public TaskbarEduView(Context context, AttributeSet attrs,
50             int defStyleAttr) {
51         super(context, attrs, defStyleAttr);
52     }
53 
init(TaskbarEduController.TaskbarEduCallbacks callbacks)54     protected void init(TaskbarEduController.TaskbarEduCallbacks callbacks) {
55         if (mPagedView != null) {
56             mPagedView.setControllerCallbacks(callbacks);
57         }
58     }
59 
60     @Override
handleClose(boolean animate)61     protected void handleClose(boolean animate) {
62         handleClose(animate, DEFAULT_CLOSE_DURATION);
63     }
64 
65     @Override
isOfType(int type)66     protected boolean isOfType(int type) {
67         return (type & TYPE_TASKBAR_EDUCATION_DIALOG) != 0;
68     }
69 
70     @Override
onFinishInflate()71     protected void onFinishInflate() {
72         super.onFinishInflate();
73         mContent = findViewById(R.id.edu_view);
74         mStartButton = findViewById(R.id.edu_start_button);
75         mEndButton = findViewById(R.id.edu_end_button);
76         mPagedView = findViewById(R.id.content);
77         mPagedView.setTaskbarEduView(this);
78     }
79 
80     @Override
setInsets(Rect insets)81     public void setInsets(Rect insets) {
82         mInsets.set(insets);
83         mContent.setPadding(mContent.getPaddingStart(),
84                 mContent.getPaddingTop(), mContent.getPaddingEnd(), insets.bottom);
85     }
86 
87     @Override
attachToContainer()88     protected void attachToContainer() {
89         if (mColorScrim != null) {
90             getPopupContainer().addView(mColorScrim, 0);
91         }
92         getPopupContainer().addView(this, 1);
93     }
94 
95     /** Show the Education flow. */
show()96     public void show() {
97         attachToContainer();
98         animateOpen();
99     }
100 
101     @Override
getAccessibilityTarget()102     protected Pair<View, String> getAccessibilityTarget() {
103         return Pair.create(mContent, mIsOpen ? getContext().getString(R.string.taskbar_edu_opened)
104                 : getContext().getString(R.string.taskbar_edu_closed));
105     }
106 
107     @Override
getScrimColor(Context context)108     protected int getScrimColor(Context context) {
109         return context.getResources().getColor(R.color.widgets_picker_scrim);
110     }
111 
112     @Override
onLayout(boolean changed, int l, int t, int r, int b)113     protected void onLayout(boolean changed, int l, int t, int r, int b) {
114         int width = r - l;
115         int height = b - t;
116 
117         // Lay out the content as center bottom aligned.
118         int contentWidth = mContent.getMeasuredWidth();
119         int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left;
120         mContent.layout(contentLeft, height - mContent.getMeasuredHeight(),
121                 contentLeft + contentWidth, height);
122 
123         setTranslationShift(mTranslationShift);
124     }
125 
animateOpen()126     private void animateOpen() {
127         if (mIsOpen || mOpenCloseAnimator.isRunning()) {
128             return;
129         }
130         mIsOpen = true;
131         mOpenCloseAnimator.setValues(
132                 PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED));
133         mOpenCloseAnimator.setInterpolator(AGGRESSIVE_EASE);
134         mOpenCloseAnimator.setDuration(DEFAULT_OPEN_DURATION).start();
135     }
136 
snapToPage(int page)137     void snapToPage(int page) {
138         mPagedView.snapToPage(page);
139     }
140 
updateStartButton(int textResId, OnClickListener onClickListener)141     void updateStartButton(int textResId, OnClickListener onClickListener) {
142         mStartButton.setText(textResId);
143         mStartButton.setOnClickListener(onClickListener);
144     }
145 
updateEndButton(int textResId, OnClickListener onClickListener)146     void updateEndButton(int textResId, OnClickListener onClickListener) {
147         mEndButton.setText(textResId);
148         mEndButton.setOnClickListener(onClickListener);
149     }
150 }
151