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.quickstep.interaction;
17 
18 import android.content.Context;
19 import android.graphics.drawable.Drawable;
20 import android.util.AttributeSet;
21 import android.util.Log;
22 import android.widget.ImageView;
23 import android.widget.LinearLayout;
24 
25 import androidx.appcompat.content.res.AppCompatResources;
26 
27 import com.android.launcher3.R;
28 import com.android.launcher3.Utilities;
29 import com.android.launcher3.icons.GraphicsUtils;
30 
31 /** Indicator displaying the current progress through the gesture navigation tutorial. */
32 public class TutorialStepIndicator extends LinearLayout {
33 
34     private static final String LOG_TAG = "TutorialStepIndicator";
35 
36     private int mCurrentStep = -1;
37     private int mTotalSteps = -1;
38 
TutorialStepIndicator(Context context)39     public TutorialStepIndicator(Context context) {
40         super(context);
41     }
42 
TutorialStepIndicator(Context context, AttributeSet attrs)43     public TutorialStepIndicator(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
TutorialStepIndicator(Context context, AttributeSet attrs, int defStyleAttr)47     public TutorialStepIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
48         super(context, attrs, defStyleAttr);
49     }
50 
TutorialStepIndicator(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)51     public TutorialStepIndicator(Context context, AttributeSet attrs, int defStyleAttr,
52             int defStyleRes) {
53         super(context, attrs, defStyleAttr, defStyleRes);
54     }
55 
56     /**
57      * Updates this indicator to display totalSteps indicator pills, with the first currentStep
58      * pills highlighted.
59      */
setTutorialProgress(int currentStep, int totalSteps)60     public void setTutorialProgress(int currentStep, int totalSteps) {
61         if (currentStep <= 0) {
62             Log.w(LOG_TAG, "Current step number invalid: " + currentStep + ". Assuming step 1.");
63             currentStep = 1;
64         }
65         if (totalSteps <= 0) {
66             Log.w(LOG_TAG, "Total number of steps invalid: " + totalSteps + ". Assuming 1 step.");
67             totalSteps = 1;
68         }
69         if (currentStep > totalSteps) {
70             Log.w(LOG_TAG, "Current step number greater than the total number of steps. Assuming"
71                     + " final step.");
72             currentStep = totalSteps;
73         }
74         if (totalSteps < 2) {
75             setVisibility(GONE);
76             return;
77         }
78         setVisibility(VISIBLE);
79         mCurrentStep = currentStep;
80         mTotalSteps = totalSteps;
81 
82         initializeStepIndicators();
83     }
84 
initializeStepIndicators()85     private void initializeStepIndicators() {
86         for (int i = mTotalSteps; i < getChildCount(); i++) {
87             removeViewAt(i);
88         }
89         int activeStepIndicatorColor = GraphicsUtils.getAttrColor(
90                 getContext(), android.R.attr.textColorPrimary);
91         int inactiveStepIndicatorColor = GraphicsUtils.getAttrColor(
92                 getContext(), android.R.attr.textColorSecondaryInverse);
93         for (int i = 0; i < mTotalSteps; i++) {
94             Drawable pageIndicatorPillDrawable = AppCompatResources.getDrawable(
95                     getContext(), R.drawable.tutorial_step_indicator_pill);
96 
97             if (i >= getChildCount()) {
98                 ImageView pageIndicatorPill = new ImageView(getContext());
99                 pageIndicatorPill.setImageDrawable(pageIndicatorPillDrawable);
100 
101                 LinearLayout.LayoutParams lp = new LayoutParams(
102                         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
103 
104                 lp.setMarginStart(Utilities.dpToPx(3));
105                 lp.setMarginEnd(Utilities.dpToPx(3));
106 
107                 addView(pageIndicatorPill, lp);
108             }
109             if (pageIndicatorPillDrawable != null) {
110                 if (i < mCurrentStep) {
111                     pageIndicatorPillDrawable.setTint(activeStepIndicatorColor);
112                 } else {
113                     pageIndicatorPillDrawable.setTint(inactiveStepIndicatorColor);
114                 }
115             }
116         }
117     }
118 }
119