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