From 1e87a365d6d7ade9573c308eed86bc71b06c8743 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Mon, 12 Jun 2017 12:22:10 -0700 Subject: [PATCH] Fix potential race in Theme creation Bug: 38190555 Test: none Change-Id: Id627bd6088dc469baffb1abb8310cd9e992996aa --- core/java/android/app/ContextImpl.java | 28 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 268a105b7c6f6..1b25e65710653 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -256,28 +256,34 @@ class ContextImpl extends Context { @Override public void setTheme(int resId) { - if (mThemeResource != resId) { - mThemeResource = resId; - initializeTheme(); + synchronized (mSync) { + if (mThemeResource != resId) { + mThemeResource = resId; + initializeTheme(); + } } } @Override public int getThemeResId() { - return mThemeResource; + synchronized (mSync) { + return mThemeResource; + } } @Override public Resources.Theme getTheme() { - if (mTheme != null) { + synchronized (mSync) { + if (mTheme != null) { + return mTheme; + } + + mThemeResource = Resources.selectDefaultTheme(mThemeResource, + getOuterContext().getApplicationInfo().targetSdkVersion); + initializeTheme(); + return mTheme; } - - mThemeResource = Resources.selectDefaultTheme(mThemeResource, - getOuterContext().getApplicationInfo().targetSdkVersion); - initializeTheme(); - - return mTheme; } private void initializeTheme() {