• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 android.graphics.drawable.Drawable;
20 import android.util.AttributeSet;
21 import android.util.Log;
22 import android.view.View;
23 import android.widget.ImageView;
24 import android.widget.LinearLayout;
25 import android.widget.TextView;
26 import androidx.annotation.DrawableRes;
27 import com.google.android.setupcompat.internal.TemplateLayout;
28 import com.google.android.setupcompat.template.Mixin;
29 import com.google.android.setupdesign.R;
30 import com.google.android.setupdesign.util.HeaderAreaStyler;
31 import com.google.android.setupdesign.util.LayoutStyler;
32 import com.google.android.setupdesign.util.PartnerStyleHelper;
33 import com.google.errorprone.annotations.CheckReturnValue;
34 
35 /** A {@link Mixin} for setting an account name and account avatar on the template layout. */
36 @CheckReturnValue // see go/why-crv
37 public class ProfileMixin implements Mixin {
38 
39   private final TemplateLayout templateLayout;
40   private static final String TAG = "ProfileMixin";
41   public static final int RIGHT = 5;
42   public static final int CENTER = 17;
43   public static final int LEFT = 3;
44 
45   /**
46    * A {@link Mixin} for setting and getting the Account.
47    *
48    * @param layout The template layout that this Mixin is a part of
49    * @param attrs XML attributes given to the layout
50    * @param defStyleAttr The default style attribute as given to the constructor of the layout
51    */
ProfileMixin(TemplateLayout layout, AttributeSet attrs, int defStyleAttr)52   public ProfileMixin(TemplateLayout layout, AttributeSet attrs, int defStyleAttr) {
53     templateLayout = layout;
54   }
55 
56   /**
57    * Sets the Account name.
58    *
59    * @param accountName The text to be set as account name
60    */
setAccountName(CharSequence accountName)61   public void setAccountName(CharSequence accountName) {
62     final TextView accountView = getAccountNameView();
63     final ImageView iconView = getAccountAvatarView();
64     final LinearLayout container = getContainerView();
65     if (accountView != null && accountName != null) {
66       accountView.setText(accountName);
67       container.setVisibility(View.VISIBLE);
68       if (iconView != null && getAccountAvatar() == null) {
69         iconView.setVisibility(View.GONE);
70       }
71     } else {
72       Log.w(TAG, "Didn't get the account name");
73     }
74   }
75 
76   /**
77    * Sets the icon on this layout.
78    *
79    * @param icon A drawable icon to set, or {@code null} to hide the icon
80    */
setAccountAvatar(Drawable icon)81   public void setAccountAvatar(Drawable icon) {
82     final ImageView iconView = getAccountAvatarView();
83     final LinearLayout container = getContainerView();
84     if (iconView != null && icon != null) {
85       iconView.setImageDrawable(icon);
86       container.setVisibility(View.VISIBLE);
87       iconView.setVisibility(View.VISIBLE);
88     } else if (iconView != null) {
89       iconView.setVisibility(View.GONE);
90       Log.w(TAG, "Didn't get the account avatar");
91     }
92   }
93 
94   /**
95    * Sets the icon on this layout.
96    *
97    * @param icon A drawable icon resource to set, or {@code null} to hide the icon
98    */
setAccountAvatar(@rawableRes int icon)99   public void setAccountAvatar(@DrawableRes int icon) {
100     final ImageView iconView = getAccountAvatarView();
101     final LinearLayout container = getContainerView();
102     if (iconView != null && icon != 0) {
103       // Note: setImageResource on the ImageView is overridden in AppCompatImageView for
104       // support lib users, which enables vector drawable compat to work on versions pre-L.
105       iconView.setImageResource(icon);
106       container.setVisibility(View.VISIBLE);
107       iconView.setVisibility(View.VISIBLE);
108     } else if (iconView != null) {
109       iconView.setVisibility(View.GONE);
110       Log.w(TAG, "Didn't get the account avatar");
111     }
112   }
113 
114   /**
115    * Sets the account name icon on this layout.
116    *
117    * @param accountName The text to be set as account name
118    * @param icon A drawable icon
119    */
setAccount(CharSequence accountName, Drawable icon)120   public void setAccount(CharSequence accountName, Drawable icon) {
121     setAccountName(accountName);
122     setAccountAvatar(icon);
123   }
124 
125   /**
126    * Sets the account name icon on this layout.
127    *
128    * @param accountName The text to be set as account name
129    * @param icon A drawable icon resource
130    */
setAccount(CharSequence accountName, @DrawableRes int icon)131   public void setAccount(CharSequence accountName, @DrawableRes int icon) {
132     setAccountName(accountName);
133     setAccountAvatar(icon);
134   }
135 
136   /**
137    * Sets the visibility of the account. gone map to 8 invisible map to 4 visible map to 0
138    *
139    * @param visibility Set it visible or not
140    */
setVisibility(int visibility)141   public void setVisibility(int visibility) {
142     getContainerView().setVisibility(visibility);
143   }
144 
145   /**
146    * Makes account align to left, center or right.
147    *
148    * @param gravity the number or the gravity
149    */
setAccountAlignment(int gravity)150   public void setAccountAlignment(int gravity) {
151     final LinearLayout container = getContainerView();
152     if (gravity == RIGHT || gravity == CENTER || gravity == LEFT) {
153       container.setGravity(gravity);
154     } else {
155       Log.w(TAG, "Unsupported alignment");
156     }
157   }
158 
159   /** Tries to apply the partner customization to the account photo. */
tryApplyPartnerCustomizationStyle()160   public void tryApplyPartnerCustomizationStyle() {
161     if (PartnerStyleHelper.shouldApplyPartnerResource(templateLayout)) {
162       final ImageView iconView = getAccountAvatarView();
163       final TextView accountView = getAccountNameView();
164       final LinearLayout container = getContainerView();
165       View iconAreaView = templateLayout.findManagedViewById(R.id.sud_layout_header);
166       LayoutStyler.applyPartnerCustomizationExtraPaddingStyle(iconAreaView);
167       HeaderAreaStyler.applyPartnerCustomizationAccountStyle(iconView, accountView, container);
168     }
169   }
170 
171   /** Returns the current account name. */
getAccountName()172   public CharSequence getAccountName() {
173     final TextView accountView = getAccountNameView();
174     return accountView.getText();
175   }
176 
177   /** Returns the current account avatar. */
getAccountAvatar()178   public Drawable getAccountAvatar() {
179     final ImageView iconView = getAccountAvatarView();
180     return iconView.getDrawable();
181   }
182 
183   /** Returns the current account name text. */
getAccountNameView()184   private TextView getAccountNameView() {
185     return (TextView) templateLayout.findManagedViewById(R.id.sud_account_name);
186   }
187 
188   /** Returns the current account icon image. */
getAccountAvatarView()189   private ImageView getAccountAvatarView() {
190     return (ImageView) templateLayout.findManagedViewById(R.id.sud_account_avatar);
191   }
192 
getContainerView()193   private LinearLayout getContainerView() {
194     return (LinearLayout) templateLayout.findManagedViewById(R.id.sud_layout_profile);
195   }
196 
197   /** Returns the current account visibility. */
getVisibility()198   public int getVisibility() {
199     return getContainerView().getVisibility();
200   }
201 }
202