1 /*
2  * Copyright (C) 2020 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.google.android.setupdesign.template;
18 
19 import static android.view.View.GONE;
20 import static android.view.View.INVISIBLE;
21 import static android.view.View.VISIBLE;
22 
23 import android.annotation.TargetApi;
24 import android.content.Context;
25 import android.os.Build.VERSION_CODES;
26 import android.view.View;
27 import android.view.ViewStub;
28 import android.widget.ProgressBar;
29 import android.widget.TextView;
30 import androidx.annotation.Nullable;
31 import com.google.android.setupcompat.partnerconfig.PartnerConfig;
32 import com.google.android.setupcompat.partnerconfig.PartnerConfig.ResourceType;
33 import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
34 import com.google.android.setupcompat.partnerconfig.ResourceEntry;
35 import com.google.android.setupcompat.template.Mixin;
36 import com.google.android.setupdesign.GlifLayout;
37 import com.google.android.setupdesign.R;
38 import com.google.android.setupdesign.view.IllustrationVideoView;
39 
40 // TODO: remove this mixin after migrate to new GlifLoadingLayout
41 /**
42  * A {@link Mixin} for showing a progress illustration.
43  *
44  * @deprecated Will be replaced by GlifLoadingLayout.
45  */
46 @TargetApi(VERSION_CODES.ICE_CREAM_SANDWICH)
47 @Deprecated
48 public class IllustrationProgressMixin implements Mixin {
49 
50   private final GlifLayout glifLayout;
51   private final Context context;
52 
53   private ProgressConfig progressConfig = ProgressConfig.CONFIG_DEFAULT;
54   private String progressDescription;
55 
IllustrationProgressMixin(GlifLayout layout)56   public IllustrationProgressMixin(GlifLayout layout) {
57     this.glifLayout = layout;
58     context = layout.getContext();
59   }
60 
61   /**
62    * Sets whether the progress layout is shown. If the progress layout has not been inflated from
63    * the stub, this method will inflate the progress layout.
64    *
65    * @param shown True to show the progress layout, false to set the layout visibiltiy to {@code
66    *     GONE}
67    */
setShown(boolean shown)68   public void setShown(boolean shown) {
69     if (!shown) {
70       View view = peekProgressIllustrationLayout();
71       if (view != null) {
72         view.setVisibility(GONE);
73       }
74     } else {
75       View view = getProgressIllustrationLayout();
76       if (view != null) {
77         view.setVisibility(VISIBLE);
78 
79         if (progressDescription != null) {
80           TextView descriptionView = view.findViewById(R.id.sud_layout_description);
81           if (descriptionView != null) {
82             descriptionView.setVisibility(VISIBLE);
83             descriptionView.setText(progressDescription);
84           }
85         }
86       }
87     }
88   }
89 
90   /** Returns true if the progress layout is currently shown. */
isShown()91   public boolean isShown() {
92     View view = peekProgressIllustrationLayout();
93     return view != null && view.getVisibility() == VISIBLE;
94   }
95 
96   /**
97    * Sets the type of progress illustration.
98    *
99    * @param config {@link ProgressConfig}
100    */
setProgressConfig(ProgressConfig config)101   public void setProgressConfig(ProgressConfig config) {
102     this.progressConfig = config;
103 
104     // When ViewStub not inflated, do nothing. It will set illustration resource when inflate
105     // layout.
106     if (peekProgressIllustrationLayout() != null) {
107       setIllustrationResource();
108     }
109   }
110 
111   /**
112    * Sets the description text of progress
113    * @param description the description text.
114    */
setProgressIllustrationDescription(String description)115   public void setProgressIllustrationDescription(String description) {
116     progressDescription = description;
117 
118     if (isShown()) {
119       final View progressLayout = getProgressIllustrationLayout();
120       if (progressLayout != null) {
121         TextView descriptionView = progressLayout.findViewById(R.id.sud_layout_description);
122         if (description != null) {
123           descriptionView.setVisibility(VISIBLE);
124           descriptionView.setText(description);
125         } else {
126           descriptionView.setVisibility(INVISIBLE);
127           descriptionView.setText(description);
128         }
129       }
130     }
131   }
132 
getProgressIllustrationLayout()133   @Nullable private View getProgressIllustrationLayout() {
134     final View progressLayout = peekProgressIllustrationLayout();
135     if (progressLayout == null) {
136       final ViewStub viewStub =
137           glifLayout.findManagedViewById(R.id.sud_layout_illustration_progress_stub);
138 
139       if (viewStub != null) {
140         viewStub.inflate();
141         setIllustrationResource();
142       }
143     }
144 
145     return peekProgressIllustrationLayout();
146   }
147 
setIllustrationResource()148   private void setIllustrationResource() {
149     IllustrationVideoView illustrationVideoView =
150         glifLayout.findManagedViewById(R.id.sud_progress_illustration);
151     ProgressBar progressBar = glifLayout.findManagedViewById(R.id.sud_progress_bar);
152 
153     PartnerConfigHelper partnerConfigHelper = PartnerConfigHelper.get(context);
154     ResourceEntry resourceEntry =
155         partnerConfigHelper.getIllustrationResourceEntry(
156             context, progressConfig.getPartnerConfig());
157 
158     if (resourceEntry != null) {
159       progressBar.setVisibility(GONE);
160       illustrationVideoView.setVisibility(VISIBLE);
161       illustrationVideoView.setVideoResourceEntry(resourceEntry);
162     } else {
163       progressBar.setVisibility(VISIBLE);
164       illustrationVideoView.setVisibility(GONE);
165     }
166   }
167 
peekProgressIllustrationLayout()168   @Nullable private View peekProgressIllustrationLayout() {
169     return glifLayout.findViewById(R.id.sud_layout_progress_illustration);
170   }
171 
172   /** The progress config used to maps to different animation */
173   public enum ProgressConfig {
174     CONFIG_DEFAULT(PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_DEFAULT),
175     CONFIG_ACCOUNT(PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_ACCOUNT),
176     CONFIG_CONNECTION(PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_CONNECTION),
177     CONFIG_UPDATE(PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_UPDATE);
178 
179     private final PartnerConfig config;
180 
ProgressConfig(PartnerConfig config)181     ProgressConfig(PartnerConfig config) {
182       if (config.getResourceType() != ResourceType.ILLUSTRATION) {
183         throw new IllegalArgumentException(
184             "Illustration progress only allow illustration resource");
185       }
186       this.config = config;
187     }
188 
getPartnerConfig()189     PartnerConfig getPartnerConfig() {
190       return config;
191     }
192   }
193 }
194