• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.util;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.os.Build;
23 import android.view.Gravity;
24 import android.view.View;
25 import com.google.android.setupcompat.PartnerCustomizationLayout;
26 import com.google.android.setupcompat.internal.TemplateLayout;
27 import com.google.android.setupcompat.partnerconfig.PartnerConfig;
28 import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
29 import com.google.android.setupcompat.util.WizardManagerHelper;
30 import com.google.android.setupdesign.GlifLayout;
31 import com.google.android.setupdesign.R;
32 import java.util.Locale;
33 
34 /** The helper reads styles from the partner configurations. */
35 public final class PartnerStyleHelper {
36 
37   private static final String TAG = "PartnerStyleHelper";
38   /**
39    * Returns the partner configuration of layout gravity, usually apply to widgets in header area.
40    */
getLayoutGravity(Context context)41   public static int getLayoutGravity(Context context) {
42     String gravity =
43         PartnerConfigHelper.get(context).getString(context, PartnerConfig.CONFIG_LAYOUT_GRAVITY);
44     if (gravity == null) {
45       return 0;
46     }
47     switch (gravity.toLowerCase(Locale.ROOT)) {
48       case "center":
49         return Gravity.CENTER;
50       case "start":
51         return Gravity.START;
52       default:
53         return 0;
54     }
55   }
56 
57   /** Returns the given layout if apply partner heavy theme. */
isPartnerHeavyThemeLayout(TemplateLayout layout)58   public static boolean isPartnerHeavyThemeLayout(TemplateLayout layout) {
59     if (!(layout instanceof GlifLayout)) {
60       return false;
61     }
62     return ((GlifLayout) layout).shouldApplyPartnerHeavyThemeResource();
63   }
64 
65   /** Returns the given layout if apply partner light theme. */
isPartnerLightThemeLayout(TemplateLayout layout)66   public static boolean isPartnerLightThemeLayout(TemplateLayout layout) {
67     if (!(layout instanceof PartnerCustomizationLayout)) {
68       return false;
69     }
70     return ((PartnerCustomizationLayout) layout).shouldApplyPartnerResource();
71   }
72 
73   /**
74    * Returns if the current layout/activity of the given {@code view} applies partner customized
75    * configurations or not.
76    *
77    * @param view A PartnerCustomizationLayout view, would be used to get the activity and context.
78    */
shouldApplyPartnerResource(View view)79   public static boolean shouldApplyPartnerResource(View view) {
80     if (view == null) {
81       return false;
82     }
83     if (view instanceof PartnerCustomizationLayout) {
84       return isPartnerLightThemeLayout((PartnerCustomizationLayout) view);
85     }
86     return shouldApplyPartnerResource(view.getContext());
87   }
88 
shouldApplyPartnerResource(Context context)89   private static boolean shouldApplyPartnerResource(Context context) {
90     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
91       return false;
92     }
93 
94     if (!PartnerConfigHelper.get(context).isAvailable()) {
95       return false;
96     }
97 
98     Activity activity = null;
99     try {
100       activity = PartnerCustomizationLayout.lookupActivityFromContext(context);
101       if (activity != null) {
102         TemplateLayout layout = findLayoutFromActivity(activity);
103         if (layout instanceof PartnerCustomizationLayout) {
104           return ((PartnerCustomizationLayout) layout).shouldApplyPartnerResource();
105         }
106       }
107     } catch (IllegalArgumentException | ClassCastException ex) {
108       // fall through
109     }
110 
111     // try best to get partner resource settings from attrs
112     boolean isSetupFlow = false;
113     if (activity != null) {
114       isSetupFlow = WizardManagerHelper.isAnySetupWizard(activity.getIntent());
115     }
116     TypedArray a =
117         context.obtainStyledAttributes(
118             new int[] {com.google.android.setupcompat.R.attr.sucUsePartnerResource});
119     boolean usePartnerResource = a.getBoolean(0, true);
120     a.recycle();
121 
122     return isSetupFlow || usePartnerResource;
123   }
124 
125   /**
126    * Returns if the current layout/activity applies heavy partner customized configurations or not.
127    *
128    * @param view A view would be used to get the activity and context.
129    */
shouldApplyPartnerHeavyThemeResource(View view)130   public static boolean shouldApplyPartnerHeavyThemeResource(View view) {
131     if (view == null) {
132       return false;
133     }
134     if (view instanceof GlifLayout) {
135       return isPartnerHeavyThemeLayout((GlifLayout) view);
136     }
137     return shouldApplyPartnerHeavyThemeResource(view.getContext());
138   }
139 
shouldApplyPartnerHeavyThemeResource(Context context)140   static boolean shouldApplyPartnerHeavyThemeResource(Context context) {
141     try {
142       Activity activity = PartnerCustomizationLayout.lookupActivityFromContext(context);
143       TemplateLayout layout = findLayoutFromActivity(activity);
144       if (layout instanceof GlifLayout) {
145         return ((GlifLayout) layout).shouldApplyPartnerHeavyThemeResource();
146       }
147     } catch (IllegalArgumentException | ClassCastException ex) {
148       // fall through
149     }
150 
151     // try best to get partner resource settings from attr
152     TypedArray a = context.obtainStyledAttributes(new int[] {R.attr.sudUsePartnerHeavyTheme});
153     boolean usePartnerHeavyTheme = a.getBoolean(0, false);
154     a.recycle();
155     usePartnerHeavyTheme =
156         usePartnerHeavyTheme || PartnerConfigHelper.shouldApplyExtendedPartnerConfig(context);
157 
158     return shouldApplyPartnerResource(context) && usePartnerHeavyTheme;
159   }
160 
161   /**
162    * Returns if the current layout/activity applies dynamic color configurations or not.
163    *
164    * @param view A GlifLayout view would be used to get the activity and context.
165    */
useDynamicColor(View view)166   public static boolean useDynamicColor(View view) {
167     if (view == null) {
168       return false;
169     }
170     return getDynamicColorPatnerConfig(view.getContext());
171   }
172 
getDynamicColorPatnerConfig(Context context)173   static boolean getDynamicColorPatnerConfig(Context context) {
174     try {
175       Activity activity = PartnerCustomizationLayout.lookupActivityFromContext(context);
176       TemplateLayout layout = findLayoutFromActivity(activity);
177       if (layout instanceof GlifLayout) {
178         return ((GlifLayout) layout).shouldApplyDynamicColor();
179       }
180       return PartnerConfigHelper.isSetupWizardFullDynamicColorEnabled(activity);
181     } catch (IllegalArgumentException | ClassCastException ex) {
182       // fall through
183     }
184 
185     return false;
186   }
187 
findLayoutFromActivity(Activity activity)188   private static TemplateLayout findLayoutFromActivity(Activity activity) {
189     if (activity == null) {
190       return null;
191     }
192     // This only worked after activity setContentView, otherwise it will return null
193     View rootView = activity.findViewById(R.id.suc_layout_status);
194     return rootView != null ? (TemplateLayout) rootView.getParent() : null;
195   }
196 
PartnerStyleHelper()197   private PartnerStyleHelper() {}
198 }
199