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