Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 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.Intent;
     21 import com.google.android.setupcompat.util.WizardManagerHelper;
     22 
     23 /** The helper class holds the constant names of themes and util functions */
     24 public class ThemeHelper {
     25 
     26   /**
     27    * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the dark
     28    * variant of the theme used in setup wizard for Nougat MR1.
     29    */
     30   public static final String THEME_GLIF = "glif";
     31 
     32   /**
     33    * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the default
     34    * theme used in setup wizard for Nougat MR1.
     35    */
     36   public static final String THEME_GLIF_LIGHT = "glif_light";
     37 
     38   /**
     39    * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the dark
     40    * variant of the theme used in setup wizard for O DR.
     41    */
     42   public static final String THEME_GLIF_V2 = "glif_v2";
     43 
     44   /**
     45    * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the default
     46    * theme used in setup wizard for O DR.
     47    */
     48   public static final String THEME_GLIF_V2_LIGHT = "glif_v2_light";
     49 
     50   /**
     51    * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the dark
     52    * variant of the theme used in setup wizard for P.
     53    */
     54   public static final String THEME_GLIF_V3 = "glif_v3";
     55 
     56   /**
     57    * Passed in a setup wizard intent as {@link WizardManagerHelper#EXTRA_THEME}. This is the default
     58    * theme used in setup wizard for P.
     59    */
     60   public static final String THEME_GLIF_V3_LIGHT = "glif_v3_light";
     61 
     62   public static final String THEME_HOLO = "holo";
     63   public static final String THEME_HOLO_LIGHT = "holo_light";
     64   public static final String THEME_MATERIAL = "material";
     65   public static final String THEME_MATERIAL_LIGHT = "material_light";
     66 
     67   /**
     68    * Checks the intent whether the extra indicates that the light theme should be used or not. If
     69    * the theme is not specified in the intent, or the theme specified is unknown, the value def will
     70    * be returned. Note that day-night themes are not taken into account by this method.
     71    *
     72    * @param intent The intent used to start the activity, which the theme extra will be read from.
     73    * @param def The default value if the theme is not specified.
     74    * @return True if the activity started by the given intent should use light theme.
     75    */
     76   public static boolean isLightTheme(Intent intent, boolean def) {
     77     final String theme = intent.getStringExtra(WizardManagerHelper.EXTRA_THEME);
     78     return isLightTheme(theme, def);
     79   }
     80 
     81   /**
     82    * Checks whether {@code theme} represents a light or dark theme. If the theme specified is
     83    * unknown, the value def will be returned. Note that day-night themes are not taken into account
     84    * by this method.
     85    *
     86    * @param theme The theme as specified from an intent sent from setup wizard.
     87    * @param def The default value if the theme is not known.
     88    * @return True if {@code theme} represents a light theme.
     89    */
     90   public static boolean isLightTheme(String theme, boolean def) {
     91     if (THEME_HOLO_LIGHT.equals(theme)
     92         || THEME_MATERIAL_LIGHT.equals(theme)
     93         || THEME_GLIF_LIGHT.equals(theme)
     94         || THEME_GLIF_V2_LIGHT.equals(theme)
     95         || THEME_GLIF_V3_LIGHT.equals(theme)) {
     96       return true;
     97     } else if (THEME_HOLO.equals(theme)
     98         || THEME_MATERIAL.equals(theme)
     99         || THEME_GLIF.equals(theme)
    100         || THEME_GLIF_V2.equals(theme)
    101         || THEME_GLIF_V3.equals(theme)) {
    102       return false;
    103     } else {
    104       return def;
    105     }
    106   }
    107 
    108   /**
    109    * Reads the theme from the intent, and applies the theme to the activity as resolved by {@link
    110    * ThemeResolver#getDefault()}.
    111    *
    112    * <p>If you require extra theme attributes, consider overriding {@link
    113    * android.app.Activity#onApplyThemeResource} in your activity and call {@link
    114    * android.content.res.Resources.Theme#applyStyle(int, boolean)} using your theme overlay.
    115    *
    116    * <pre>{@code
    117    * protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
    118    *     super.onApplyThemeResource(theme, resid, first);
    119    *     theme.applyStyle(R.style.MyThemeOverlay, true);
    120    * }
    121    * }</pre>
    122    *
    123    * @param activity the activity to get the intent from and apply the resulting theme to.
    124    */
    125   public static void applyTheme(Activity activity) {
    126     ThemeResolver.getDefault().applyTheme(activity);
    127   }
    128 }
    129