1/*
2 * Copyright (C) 2014 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 android.support.v7.view;
18
19import android.content.Context;
20import android.content.ContextWrapper;
21import android.content.res.Resources;
22import android.support.annotation.RestrictTo;
23import android.support.annotation.StyleRes;
24import android.support.v7.appcompat.R;
25import android.view.LayoutInflater;
26
27import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
28
29/**
30 * A ContextWrapper that allows you to modify the theme from what is in the
31 * wrapped context.
32 *
33 * @hide
34 */
35@RestrictTo(GROUP_ID)
36public class ContextThemeWrapper extends ContextWrapper {
37    private int mThemeResource;
38    private Resources.Theme mTheme;
39    private LayoutInflater mInflater;
40
41    public ContextThemeWrapper(Context base, @StyleRes int themeResId) {
42        super(base);
43        mThemeResource = themeResId;
44    }
45
46    public ContextThemeWrapper(Context base, Resources.Theme theme) {
47        super(base);
48        mTheme = theme;
49    }
50
51    @Override
52    public void setTheme(int resid) {
53        if (mThemeResource != resid) {
54            mThemeResource = resid;
55            initializeTheme();
56        }
57    }
58
59    public int getThemeResId() {
60        return mThemeResource;
61    }
62
63    @Override
64    public Resources.Theme getTheme() {
65        if (mTheme != null) {
66            return mTheme;
67        }
68
69        if (mThemeResource == 0) {
70            mThemeResource = R.style.Theme_AppCompat_Light;
71        }
72        initializeTheme();
73
74        return mTheme;
75    }
76
77    @Override
78    public Object getSystemService(String name) {
79        if (LAYOUT_INFLATER_SERVICE.equals(name)) {
80            if (mInflater == null) {
81                mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
82            }
83            return mInflater;
84        }
85        return getBaseContext().getSystemService(name);
86    }
87
88    /**
89     * Called by {@link #setTheme} and {@link #getTheme} to apply a theme
90     * resource to the current Theme object.  Can override to change the
91     * default (simple) behavior.  This method will not be called in multiple
92     * threads simultaneously.
93     *
94     * @param theme The Theme object being modified.
95     * @param resid The theme style resource being applied to <var>theme</var>.
96     * @param first Set to true if this is the first time a style is being
97     *              applied to <var>theme</var>.
98     */
99    protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
100        theme.applyStyle(resid, true);
101    }
102
103    private void initializeTheme() {
104        final boolean first = mTheme == null;
105        if (first) {
106            mTheme = getResources().newTheme();
107            Resources.Theme theme = getBaseContext().getTheme();
108            if (theme != null) {
109                mTheme.setTo(theme);
110            }
111        }
112        onApplyThemeResource(mTheme, mThemeResource, first);
113    }
114}
115
116