summaryrefslogtreecommitdiff
path: root/main/src/com/google/android/setupdesign/util/ThemeHelper.java
blob: 4c349e4b0c71ff56a40217d17ecf6280118e635d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.setupdesign.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.google.android.setupcompat.PartnerCustomizationLayout;
import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
import com.google.android.setupcompat.util.BuildCompatUtils;
import com.google.android.setupcompat.util.Logger;
import com.google.android.setupcompat.util.WizardManagerHelper;
import com.google.android.setupdesign.R;
import java.util.Objects;

/** The helper class holds the constant names of themes and util functions */
public final class ThemeHelper {

  private static final Logger LOG = new Logger("ThemeHelper");

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the dark
   * variant of the theme used in setup wizard for Nougat MR1.
   */
  public static final String THEME_GLIF = "glif";

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the default
   * theme used in setup wizard for Nougat MR1.
   */
  public static final String THEME_GLIF_LIGHT = "glif_light";

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the dark
   * variant of the theme used in setup wizard for O DR.
   */
  public static final String THEME_GLIF_V2 = "glif_v2";

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the default
   * theme used in setup wizard for O DR.
   */
  public static final String THEME_GLIF_V2_LIGHT = "glif_v2_light";

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the dark
   * variant of the theme used in setup wizard for P.
   */
  public static final String THEME_GLIF_V3 = "glif_v3";

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the default
   * theme used in setup wizard for P.
   */
  public static final String THEME_GLIF_V3_LIGHT = "glif_v3_light";

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the dark
   * variant of the theme used in setup wizard for T.
   */
  public static final String THEME_GLIF_V4 = "glif_v4";

  /**
   * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the default
   * theme used in setup wizard for T.
   */
  public static final String THEME_GLIF_V4_LIGHT = "glif_v4_light";

  public static final String THEME_HOLO = "holo";
  public static final String THEME_HOLO_LIGHT = "holo_light";
  public static final String THEME_MATERIAL = "material";
  public static final String THEME_MATERIAL_LIGHT = "material_light";

  /**
   * Checks the intent whether the extra indicates that the light theme should be used or not. If
   * the theme is not specified in the intent, or the theme specified is unknown, the value def will
   * be returned. Note that day-night themes are not taken into account by this method.
   *
   * @param intent The intent used to start the activity, which the theme extra will be read from.
   * @param def The default value if the theme is not specified.
   * @return True if the activity started by the given intent should use light theme.
   */
  public static boolean isLightTheme(Intent intent, boolean def) {
    final String theme = intent.getStringExtra(WizardManagerHelper.EXTRA_THEME);
    return isLightTheme(theme, def);
  }

  /**
   * Checks whether {@code theme} represents a light or dark theme. If the theme specified is
   * unknown, the value def will be returned. Note that day-night themes are not taken into account
   * by this method.
   *
   * @param theme The theme as specified from an intent sent from setup wizard.
   * @param def The default value if the theme is not known.
   * @return True if {@code theme} represents a light theme.
   */
  public static boolean isLightTheme(String theme, boolean def) {
    if (THEME_HOLO_LIGHT.equals(theme)
        || THEME_MATERIAL_LIGHT.equals(theme)
        || THEME_GLIF_LIGHT.equals(theme)
        || THEME_GLIF_V2_LIGHT.equals(theme)
        || THEME_GLIF_V3_LIGHT.equals(theme)
        || THEME_GLIF_V4_LIGHT.equals(theme)) {
      return true;
    } else if (THEME_HOLO.equals(theme)
        || THEME_MATERIAL.equals(theme)
        || THEME_GLIF.equals(theme)
        || THEME_GLIF_V2.equals(theme)
        || THEME_GLIF_V3.equals(theme)
        || THEME_GLIF_V4.equals(theme)) {
      return false;
    } else {
      return def;
    }
  }

  /**
   * Reads the theme from the intent, and applies the theme to the activity as resolved by {@link
   * ThemeResolver#getDefault()}.
   *
   * <p>If you require extra theme attributes, consider overriding {@link
   * android.app.Activity#onApplyThemeResource} in your activity and call {@link
   * android.content.res.Resources.Theme#applyStyle(int, boolean)} using your theme overlay.
   *
   * <pre>{@code
   * protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
   *     super.onApplyThemeResource(theme, resid, first);
   *     theme.applyStyle(R.style.MyThemeOverlay, true);
   * }
   * }</pre>
   *
   * @param activity the activity to get the intent from and apply the resulting theme to.
   */
  public static void applyTheme(Activity activity) {
    ThemeResolver.getDefault().applyTheme(activity);
  }

  /**
   * Checks whether SetupWizard supports the DayNight theme during setup flow; if it returns false,
   * setup flow is always light theme.
   *
   * @return true if the SetupWizard is listening to system DayNight theme setting.
   */
  public static boolean isSetupWizardDayNightEnabled(@NonNull Context context) {
    return PartnerConfigHelper.isSetupWizardDayNightEnabled(context);
  }

  /**
   * Returns true if the partner provider of SetupWizard is ready to support more partner configs.
   */
  public static boolean shouldApplyExtendedPartnerConfig(@NonNull Context context) {
    return PartnerConfigHelper.shouldApplyExtendedPartnerConfig(context);
  }

  /** Returns true if the SetupWizard is flow enabled "Material You(Glifv4)" style. */
  public static boolean shouldApplyMaterialYouStyle(@NonNull Context context) {
    return PartnerConfigHelper.shouldApplyMaterialYouStyle(context);
  }

  /** Returns {@code true} if this {@code context} should apply dynamic color. */
  public static boolean shouldApplyDynamicColor(@NonNull Context context) {
    return PartnerConfigHelper.isSetupWizardDynamicColorEnabled(context);
  }

  /**
   * Returns a theme resource id if the {@link com.google.android.setupdesign.GlifLayout} should
   * apply dynamic color.
   *
   * <p>Otherwise returns {@code 0}.
   */
  @StyleRes
  public static int getDynamicColorTheme(@NonNull Context context) {
    @StyleRes int resId = 0;

    Activity activity;
    try {
      activity = PartnerCustomizationLayout.lookupActivityFromContext(context);
    } catch (IllegalArgumentException ex) {
      LOG.e(Objects.requireNonNull(ex.getMessage()));
      return resId;
    }

    boolean isSetupFlow = WizardManagerHelper.isAnySetupWizard(activity.getIntent());
    boolean isDayNightEnabled = isSetupWizardDayNightEnabled(context);

    if (isSetupFlow && !BuildCompatUtils.isAtLeastU()) {
      // return theme for inside setup flow
      resId =
          isDayNightEnabled
              ? R.style.SudDynamicColorTheme_DayNight
              : R.style.SudDynamicColorTheme_Light;
    } else {
      // return theme for outside setup flow
      resId =
          isDayNightEnabled
              ? R.style.SudFullDynamicColorTheme_DayNight
              : R.style.SudFullDynamicColorTheme_Light;
      LOG.atInfo(
          "Return "
              + (isDayNightEnabled
                  ? "SudFullDynamicColorTheme_DayNight"
                  : "SudFullDynamicColorTheme_Light"));
    }

    LOG.atDebug(
        "Gets the dynamic accentColor: [Light] "
            + colorIntToHex(context, R.color.sud_dynamic_color_accent_glif_v3_light)
            + ", "
            + (BuildCompatUtils.isAtLeastS()
                ? colorIntToHex(context, android.R.color.system_accent1_600)
                : "n/a")
            + ", [Dark] "
            + colorIntToHex(context, R.color.sud_dynamic_color_accent_glif_v3_dark)
            + ", "
            + (BuildCompatUtils.isAtLeastS()
                ? colorIntToHex(context, android.R.color.system_accent1_100)
                : "n/a"));

    return resId;
  }

  /** Returns a default theme resource id which provides by setup wizard. */
  @StyleRes
  public static int getSuwDefaultTheme(@NonNull Context context) {
    String themeName = PartnerConfigHelper.getSuwDefaultThemeString(context);
    @StyleRes int defaultTheme;
    if (VERSION.SDK_INT < VERSION_CODES.O) {
      defaultTheme =
          ThemeHelper.isSetupWizardDayNightEnabled(context)
              ? R.style.SudThemeGlif_DayNight
              : R.style.SudThemeGlif_Light;
    } else if (VERSION.SDK_INT < VERSION_CODES.P) {
      defaultTheme =
          ThemeHelper.isSetupWizardDayNightEnabled(context)
              ? R.style.SudThemeGlifV2_DayNight
              : R.style.SudThemeGlifV2_Light;
    } else if (VERSION.SDK_INT < VERSION_CODES.TIRAMISU) {
      defaultTheme =
          ThemeHelper.isSetupWizardDayNightEnabled(context)
              ? R.style.SudThemeGlifV3_DayNight
              : R.style.SudThemeGlifV3_Light;
    } else {
      defaultTheme =
          ThemeHelper.isSetupWizardDayNightEnabled(context)
              ? R.style.SudThemeGlifV4_DayNight
              : R.style.SudThemeGlifV4_Light;
    }
    return new ThemeResolver.Builder()
        .setDefaultTheme(defaultTheme)
        .setUseDayNight(isSetupWizardDayNightEnabled(context))
        .build()
        .resolve(themeName, /* suppressDayNight= */ !isSetupWizardDayNightEnabled(context));
  }

  /** Returns {@code true} if the dynamic color is set. */
  public static boolean trySetDynamicColor(@NonNull Context context) {
    if (!BuildCompatUtils.isAtLeastS()) {
      LOG.w("Dynamic color require platform version at least S.");
      return false;
    }

    if (!shouldApplyDynamicColor(context)) {
      LOG.w("SetupWizard does not support the dynamic color or supporting status unknown.");
      return false;
    }

    Activity activity;
    try {
      activity = PartnerCustomizationLayout.lookupActivityFromContext(context);
    } catch (IllegalArgumentException ex) {
      LOG.e(Objects.requireNonNull(ex.getMessage()));
      return false;
    }

    @StyleRes int resId = getDynamicColorTheme(context);
    if (resId != 0) {
      activity.setTheme(resId);
    } else {
      LOG.w("Error occurred on getting dynamic color theme.");
      return false;
    }

    return true;
  }

  private static String colorIntToHex(Context context, int colorInt) {
    return String.format("#%06X", (0xFFFFFF & context.getResources().getColor(colorInt)));
  }

  private ThemeHelper() {}
}