FallbackThemeWrapper.java revision d3ffc713be68823409d5dc6d2d44bb21bde5bfd0
1/* 2 * Copyright (C) 2017 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 17package com.android.setupwizardlib.util; 18 19import android.content.Context; 20import android.content.res.Resources.Theme; 21import android.support.annotation.StyleRes; 22import android.view.ContextThemeWrapper; 23 24/** 25 * Same as {@link ContextThemeWrapper}, but the base context's theme attributes take precedence 26 * over the wrapper context's. This is used to provide default values for theme attributes 27 * referenced in layouts, to remove the risk of crashing the client because of using the wrong 28 * theme. 29 */ 30public class FallbackThemeWrapper extends ContextThemeWrapper { 31 32 /** 33 * Creates a new context wrapper with the specified theme. 34 * 35 * The specified theme will be applied as fallbacks to the base context's theme. Any attributes 36 * defined in the base context's theme will retain their original values. Otherwise values in 37 * {@code themeResId} will be used. 38 * 39 * @param base The base context. 40 * @param themeResId The theme to use as fallback. 41 */ 42 public FallbackThemeWrapper(Context base, @StyleRes int themeResId) { 43 super(base, themeResId); 44 } 45 46 /** 47 * {@inheritDoc} 48 */ 49 @Override 50 protected void onApplyThemeResource(Theme theme, int resId, boolean first) { 51 theme.applyStyle(resId, false /* force */); 52 } 53} 54