The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Tor Norbye | 417ee5b | 2015-03-10 20:57:37 -0700 | [diff] [blame] | 19 | import android.annotation.StyleRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.ContextWrapper; |
Alan Viverette | 99bf63f | 2015-08-25 13:45:38 -0400 | [diff] [blame] | 22 | import android.content.res.AssetManager; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 23 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.res.Resources; |
| 25 | |
| 26 | /** |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 27 | * A context wrapper that allows you to modify or replace the theme of the |
| 28 | * wrapped context. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | */ |
| 30 | public class ContextThemeWrapper extends ContextWrapper { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | private int mThemeResource; |
| 32 | private Resources.Theme mTheme; |
| 33 | private LayoutInflater mInflater; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 34 | private Configuration mOverrideConfiguration; |
| 35 | private Resources mResources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 37 | /** |
| 38 | * Creates a new context wrapper with no theme and no base context. |
| 39 | * <p> |
| 40 | * <stong>Note:</strong> A base context <strong>must</strong> be attached |
| 41 | * using {@link #attachBaseContext(Context)} before calling any other |
| 42 | * method on the newly constructed context wrapper. |
| 43 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | public ContextThemeWrapper() { |
| 45 | super(null); |
| 46 | } |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 47 | |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 48 | /** |
| 49 | * Creates a new context wrapper with the specified theme. |
| 50 | * <p> |
| 51 | * The specified theme will be applied on top of the base context's theme. |
| 52 | * Any attributes not explicitly defined in the theme identified by |
| 53 | * <var>themeResId</var> will retain their original values. |
| 54 | * |
| 55 | * @param base the base context |
| 56 | * @param themeResId the resource ID of the theme to be applied on top of |
| 57 | * the base context's theme |
| 58 | */ |
Tor Norbye | 417ee5b | 2015-03-10 20:57:37 -0700 | [diff] [blame] | 59 | public ContextThemeWrapper(Context base, @StyleRes int themeResId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | super(base); |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 61 | mThemeResource = themeResId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 64 | /** |
| 65 | * Creates a new context wrapper with the specified theme. |
| 66 | * <p> |
| 67 | * Unlike {@link #ContextThemeWrapper(Context, int)}, the theme passed to |
| 68 | * this constructor will completely replace the base context's theme. |
| 69 | * |
| 70 | * @param base the base context |
| 71 | * @param theme the theme against which resources should be inflated |
| 72 | */ |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 73 | public ContextThemeWrapper(Context base, Resources.Theme theme) { |
| 74 | super(base); |
| 75 | mTheme = theme; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void attachBaseContext(Context newBase) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | super.attachBaseContext(newBase); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Call to set an "override configuration" on this context -- this is |
| 85 | * a configuration that replies one or more values of the standard |
| 86 | * configuration that is applied to the context. See |
| 87 | * {@link Context#createConfigurationContext(Configuration)} for more |
| 88 | * information. |
| 89 | * |
| 90 | * <p>This method can only be called once, and must be called before any |
Alan Viverette | 99bf63f | 2015-08-25 13:45:38 -0400 | [diff] [blame] | 91 | * calls to {@link #getResources()} or {@link #getAssets()} are made. |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 92 | */ |
| 93 | public void applyOverrideConfiguration(Configuration overrideConfiguration) { |
| 94 | if (mResources != null) { |
Alan Viverette | 99bf63f | 2015-08-25 13:45:38 -0400 | [diff] [blame] | 95 | throw new IllegalStateException( |
| 96 | "getResources() or getAssets() has already been called"); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 97 | } |
| 98 | if (mOverrideConfiguration != null) { |
| 99 | throw new IllegalStateException("Override configuration has already been set"); |
| 100 | } |
| 101 | mOverrideConfiguration = new Configuration(overrideConfiguration); |
| 102 | } |
| 103 | |
| 104 | @Override |
Alan Viverette | 99bf63f | 2015-08-25 13:45:38 -0400 | [diff] [blame] | 105 | public AssetManager getAssets() { |
| 106 | // Ensure we're returning assets with the correct configuration. |
Alan Viverette | 713a5cd | 2015-12-16 15:46:32 -0500 | [diff] [blame] | 107 | return getResourcesInternal().getAssets(); |
Alan Viverette | 99bf63f | 2015-08-25 13:45:38 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 111 | public Resources getResources() { |
Alan Viverette | 713a5cd | 2015-12-16 15:46:32 -0500 | [diff] [blame] | 112 | return getResourcesInternal(); |
| 113 | } |
| 114 | |
| 115 | private Resources getResourcesInternal() { |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 116 | if (mResources == null) { |
| 117 | if (mOverrideConfiguration == null) { |
| 118 | mResources = super.getResources(); |
| 119 | } else { |
| 120 | final Context resContext = createConfigurationContext(mOverrideConfiguration); |
| 121 | mResources = resContext.getResources(); |
| 122 | } |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 123 | } |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 124 | return mResources; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 125 | } |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 126 | |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void setTheme(int resid) { |
| 129 | if (mThemeResource != resid) { |
| 130 | mThemeResource = resid; |
| 131 | initializeTheme(); |
| 132 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 134 | |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 135 | /** @hide */ |
| 136 | @Override |
| 137 | public int getThemeResId() { |
| 138 | return mThemeResource; |
| 139 | } |
| 140 | |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 141 | @Override |
| 142 | public Resources.Theme getTheme() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | if (mTheme != null) { |
| 144 | return mTheme; |
| 145 | } |
| 146 | |
Alan Viverette | 5effd7e | 2014-05-05 12:25:33 -0700 | [diff] [blame] | 147 | mThemeResource = Resources.selectDefaultTheme(mThemeResource, |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 148 | getApplicationInfo().targetSdkVersion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | initializeTheme(); |
| 150 | |
| 151 | return mTheme; |
| 152 | } |
| 153 | |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 154 | @Override |
| 155 | public Object getSystemService(String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | if (LAYOUT_INFLATER_SERVICE.equals(name)) { |
| 157 | if (mInflater == null) { |
Fabrice Di Meglio | ebb9f3a | 2014-03-07 17:04:52 -0800 | [diff] [blame] | 158 | mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
| 160 | return mInflater; |
| 161 | } |
Fabrice Di Meglio | ebb9f3a | 2014-03-07 17:04:52 -0800 | [diff] [blame] | 162 | return getBaseContext().getSystemService(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 164 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Called by {@link #setTheme} and {@link #getTheme} to apply a theme |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 167 | * resource to the current Theme object. May be overridden to change the |
| 168 | * default (simple) behavior. This method will not be called in multiple |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | * threads simultaneously. |
| 170 | * |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 171 | * @param theme the theme being modified |
| 172 | * @param resId the style resource being applied to <var>theme</var> |
| 173 | * @param first {@code true} if this is the first time a style is being |
| 174 | * applied to <var>theme</var> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | */ |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 176 | protected void onApplyThemeResource(Resources.Theme theme, int resId, boolean first) { |
| 177 | theme.applyStyle(resId, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private void initializeTheme() { |
| 181 | final boolean first = mTheme == null; |
| 182 | if (first) { |
| 183 | mTheme = getResources().newTheme(); |
Alan Viverette | 04067f3 | 2015-08-25 14:07:02 -0400 | [diff] [blame] | 184 | final Resources.Theme theme = getBaseContext().getTheme(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | if (theme != null) { |
| 186 | mTheme.setTo(theme); |
| 187 | } |
| 188 | } |
| 189 | onApplyThemeResource(mTheme, mThemeResource, first); |
| 190 | } |
| 191 | } |
| 192 | |