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