summaryrefslogtreecommitdiff
path: root/library/main/src/com/android/setupwizardlib/util
diff options
context:
space:
mode:
authorMaurice Lam <yukl@google.com>2017-02-16 16:48:57 -0800
committerMaurice Lam <yukl@google.com>2017-02-17 15:57:24 -0800
commite2a8d27c2a43cf0b48b80e5b05f893222c541e04 (patch)
tree0bef6061f6a126fb7ae43f5c35b41b363acb18a2 /library/main/src/com/android/setupwizardlib/util
parent74acd46e273b62bb043bd81e0db51d7b686516be (diff)
downloadsetupwizard-e2a8d27c2a43cf0b48b80e5b05f893222c541e04.tar.gz
Add theme fallback
- Add FallbackThemeWrapper, which behaves like ContextThemeWrapper, except that the base context's theme attributes takes precedence over the wrapper context's. - Use the FallbackThemeWrapper in TemplateLayout (SetupWizardLayout and GlifLayout), so that inflating them with the wrong theme will not crash, but rather look wrong. This allows SuwLib to add required attributes without risking crashing clients that for some reason doesn't use the right themes. Test: ./gradlew connectedAndroidTest Change-Id: I694ad7279ce733659f6aa5d72e2087a09d3f1e22
Diffstat (limited to 'library/main/src/com/android/setupwizardlib/util')
-rw-r--r--library/main/src/com/android/setupwizardlib/util/FallbackThemeWrapper.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/library/main/src/com/android/setupwizardlib/util/FallbackThemeWrapper.java b/library/main/src/com/android/setupwizardlib/util/FallbackThemeWrapper.java
new file mode 100644
index 0000000..f3312c6
--- /dev/null
+++ b/library/main/src/com/android/setupwizardlib/util/FallbackThemeWrapper.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.setupwizardlib.util;
+
+import android.content.Context;
+import android.content.res.Resources.Theme;
+import android.support.annotation.StyleRes;
+import android.view.ContextThemeWrapper;
+
+/**
+ * Same as {@link ContextThemeWrapper}, but the base context's theme attributes take precendence
+ * over the wrapper context's. This is used to provide default values for theme attributes
+ * referenced in layouts, to remove the risk of crashing the client because of using the wrong
+ * theme.
+ */
+public class FallbackThemeWrapper extends ContextThemeWrapper {
+
+ /**
+ * Creates a new context wrapper with the specified theme.
+ *
+ * The specified theme will be applied as fallbacks to the base context's theme. Any attributes
+ * defined in the base context's theme will retain their original values. Otherwise values in
+ * {@code themeResId} will be used.
+ *
+ * @param base The base context.
+ * @param themeResId The theme to use as fallback.
+ */
+ public FallbackThemeWrapper(Context base, @StyleRes int themeResId) {
+ super(base, themeResId);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void onApplyThemeResource(Theme theme, int resId, boolean first) {
+ theme.applyStyle(resId, false /* force */);
+ }
+}