From 88ab69780f58e4b32d497266b2ad646a4d74827b Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Thu, 28 Jul 2011 11:25:01 -0700 Subject: Fix bug 5087752 - Maintain correct contrast against action bars in inverse-bar themes Add the actionBarWidgetTheme theme attribute. This lets a theme specify a wrapper theme that can be used to create views that will end up in the action bar so that the rest of the code can ignore differences in contrast. (e.g. the inverse action bar themes.) Apps can use ActionBar#getThemedContext() to obtain a Context with a proper theme for views that will end up in the action bar. MenuInflaters generated by Activities will automatically use this to properly theme inflated action views. Change-Id: Ib28c82bf47c25d446cca2a63f617b8a4a0afa6b2 --- core/java/android/app/ActionBar.java | 13 ++++++++++++- core/java/android/app/Activity.java | 14 +++++++++++++- .../com/android/internal/app/ActionBarImpl.java | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) (limited to 'core/java') diff --git a/core/java/android/app/ActionBar.java b/core/java/android/app/ActionBar.java index a217867..7acaec8 100644 --- a/core/java/android/app/ActionBar.java +++ b/core/java/android/app/ActionBar.java @@ -627,7 +627,18 @@ public abstract class ActionBar { * * @param enabled true to enable the home button, false to disable the home button. */ - public abstract void setHomeButtonEnabled(boolean enabled); + public void setHomeButtonEnabled(boolean enabled) { } + + /** + * Returns a {@link Context} with an appropriate theme for creating views that + * will appear in the action bar. If you are inflating or instantiating custom views + * that will appear in an action bar, you should use the Context returned by this method. + * (This includes adapters used for list navigation mode.) + * This will ensure that views contrast properly against the action bar. + * + * @return A themed Context for creating views + */ + public Context getThemedContext() { return null; } /** * Listener interface for ActionBar navigation events. diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 8d03ac7..c6c4025 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -32,6 +32,7 @@ import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.TypedArray; +import android.content.res.Resources.Theme; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -54,6 +55,7 @@ import android.util.AttributeSet; import android.util.EventLog; import android.util.Log; import android.util.SparseArray; +import android.util.TypedValue; import android.view.ActionMode; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; @@ -672,6 +674,7 @@ public class Activity extends ContextThemeWrapper /*package*/ int mConfigChangeFlags; /*package*/ Configuration mCurrentConfig; private SearchManager mSearchManager; + private MenuInflater mMenuInflater; static final class NonConfigurationInstances { Object activity; @@ -3083,7 +3086,16 @@ public class Activity extends ContextThemeWrapper * Returns a {@link MenuInflater} with this context. */ public MenuInflater getMenuInflater() { - return new MenuInflater(this); + // Make sure that action views can get an appropriate theme. + if (mMenuInflater == null) { + initActionBar(); + if (mActionBar != null) { + mMenuInflater = new MenuInflater(mActionBar.getThemedContext()); + } else { + mMenuInflater = new MenuInflater(this); + } + } + return mMenuInflater; } @Override diff --git a/core/java/com/android/internal/app/ActionBarImpl.java b/core/java/com/android/internal/app/ActionBarImpl.java index 95f1f98..7684bc5 100644 --- a/core/java/com/android/internal/app/ActionBarImpl.java +++ b/core/java/com/android/internal/app/ActionBarImpl.java @@ -35,10 +35,13 @@ import android.app.Dialog; import android.app.FragmentTransaction; import android.content.Context; import android.content.res.Configuration; +import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Handler; +import android.util.TypedValue; import android.view.ActionMode; +import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -61,6 +64,7 @@ public class ActionBarImpl extends ActionBar { private static final String TAG = "ActionBarImpl"; private Context mContext; + private Context mThemedContext; private Activity mActivity; private Dialog mDialog; @@ -605,6 +609,23 @@ public class ActionBarImpl extends ActionBar { } } + public Context getThemedContext() { + if (mThemedContext == null) { + TypedValue outValue = new TypedValue(); + Resources.Theme currentTheme = mContext.getTheme(); + currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme, + outValue, true); + final int targetThemeRes = outValue.resourceId; + + if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) { + mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes); + } else { + mThemedContext = mContext; + } + } + return mThemedContext; + } + /** * @hide */ -- cgit v1.1