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