summaryrefslogtreecommitdiff
path: root/main/src/com/google/android/setupdesign/util/ThemeHelper.java
blob: 7f29c5dc02333ea305c37c7074d5f9c7785f4e98 (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
/*
 * 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 androidx.annotation.NonNull;
import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
import com.google.android.setupcompat.util.WizardManagerHelper;

/** The helper class holds the constant names of themes and util functions */
public final class 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";

  /**
   * Placeholder, not avirailed yet.
   */
  public static final String THEME_GLIF_V4 = "glif_v4";

  /**
   * Placeholder, not avirailed yet.
   */
  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);
  }

  private ThemeHelper() {}
}