OSDN Git Service

Use right colors for status bar and nav bar.
authorDeepanshu Gupta <deepanshu@google.com>
Wed, 18 Mar 2015 22:47:54 +0000 (15:47 -0700)
committerDeepanshu Gupta <deepanshu@google.com>
Sun, 22 Mar 2015 18:03:44 +0000 (11:03 -0700)
Bug: http://b.android.com/81862
Change-Id: Ia10297e83e54c01a50de6cbcac2b7fef48e8d238

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/Config.java
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/CustomBar.java
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/NavigationBar.java
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/StatusBar.java
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/bars/TitleBar.java

index 82a5130..a0bc5e3 100644 (file)
@@ -16,6 +16,8 @@
 
 package com.android.layoutlib.bridge.bars;
 
+import android.os.Build.VERSION_CODES;
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -40,12 +42,12 @@ public class Config {
     private static final int BLACK = 0xFF000000;
 
     public static boolean showOnScreenNavBar(int platformVersion) {
-        return platformVersion == 0 || platformVersion >= ICE_CREAM_SANDWICH;
+        return isGreaterOrEqual(platformVersion, ICE_CREAM_SANDWICH);
     }
 
     public static int getStatusBarColor(int platformVersion) {
         // return white for froyo and earlier; black otherwise.
-        return platformVersion == 0 || platformVersion >= GINGERBREAD ? BLACK : WHITE;
+        return isGreaterOrEqual(platformVersion, GINGERBREAD) ? BLACK : WHITE;
     }
 
     public static List<String> getResourceDirs(int platformVersion) {
@@ -95,7 +97,7 @@ public class Config {
     }
 
     public static int getTimeColor(int platformVersion) {
-        if (platformVersion == 0 || platformVersion >= KITKAT ||
+        if (isGreaterOrEqual(platformVersion, KITKAT) ||
                 platformVersion > FROYO && platformVersion < HONEYCOMB) {
             // Gingerbread and KitKat onwards.
             return WHITE;
@@ -114,4 +116,13 @@ public class Config {
     public static String getWifiIconType(int platformVersion) {
         return platformVersion == 0 ? "xml" : "png";
     }
+
+    /**
+     * Compare simulated platform version and code from {@link VERSION_CODES} to check if
+     * the simulated platform is greater than or equal to the version code.
+     */
+    public static boolean isGreaterOrEqual(int platformVersion, int code) {
+        // simulated platform version = 0 means that we use the latest.
+        return platformVersion == 0 || platformVersion >= code;
+    }
 }
index 13ddf07..bc1a41d 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.android.layoutlib.bridge.bars;
 
+import com.android.annotations.NonNull;
 import com.android.ide.common.rendering.api.RenderResources;
 import com.android.ide.common.rendering.api.ResourceValue;
 import com.android.ide.common.rendering.api.StyleResourceValue;
@@ -26,6 +27,7 @@ import com.android.layoutlib.bridge.impl.ParserFactory;
 import com.android.layoutlib.bridge.impl.ResourceHelper;
 import com.android.resources.Density;
 import com.android.resources.LayoutDirection;
+import com.android.resources.ResourceType;
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
@@ -47,6 +49,8 @@ import android.widget.TextView;
 import java.io.IOException;
 import java.io.InputStream;
 
+import static android.os.Build.VERSION_CODES.LOLLIPOP;
+
 /**
  * Base "bar" class for the window decor around the the edited layout.
  * This is basically an horizontal layout that loads a given layout on creation (it is read
@@ -63,7 +67,7 @@ abstract class CustomBar extends LinearLayout {
 
     protected abstract TextView getStyleableTextView();
 
-    protected CustomBar(Context context, int orientation, String layoutPath,
+    protected CustomBar(BridgeContext context, int orientation, String layoutPath,
             String name, int simulatedPlatformVersion) throws XmlPullParserException {
         super(context);
         mSimulatedPlatformVersion = simulatedPlatformVersion;
@@ -197,7 +201,7 @@ abstract class CustomBar extends LinearLayout {
 
 
                 ResourceValue textColor = res.findItemInStyle(textStyle, "textColor",
-                        true /*isFrameworkAttr*/);
+                        true);
                 textColor = res.resolveResValue(textColor);
                 if (textColor != null) {
                     ColorStateList stateList = ResourceHelper.getColorStateList(
@@ -210,12 +214,39 @@ abstract class CustomBar extends LinearLayout {
         }
     }
 
+    /**
+     * Given a theme attribute name, get the color referenced by it. The theme attribute may be
+     * used in a layout like "?attr/foo".
+     * <p/>
+     * Returns 0 if not found.
+     *
+     * @throws NumberFormatException if color resolved to an invalid string.
+     */
+    protected int getThemeAttrColor(@NonNull String attrName, boolean isFramework) {
+        if (!Config.isGreaterOrEqual(mSimulatedPlatformVersion, LOLLIPOP)) {
+            return 0;
+        }
+        assert mContext instanceof BridgeContext;
+        BridgeContext context = ((BridgeContext) mContext);
+        RenderResources renderResources = context.getRenderResources();
+        // From ?attr/foo to @color/bar. This is most likely an ItemResourceValue.
+        ResourceValue resource = renderResources.findItemInTheme(attrName, isFramework);
+        if (resource != null) {
+            // Form @color/bar to the #AARRGGBB
+            resource = renderResources.resolveResValue(resource);
+        }
+        if (resource != null && ResourceType.COLOR.equals(resource.getResourceType())) {
+            return ResourceHelper.getColor(resource.getValue());
+        }
+        return 0;
+    }
+
     private ResourceValue getResourceValue(String reference) {
         BridgeContext bridgeContext = (BridgeContext) mContext;
         RenderResources res = bridgeContext.getRenderResources();
 
         // find the resource
-        ResourceValue value = res.findResValue(reference, false /*isFramework*/);
+        ResourceValue value = res.findResValue(reference, false);
 
         // resolve it if needed
         return res.resolveResValue(value);
index 283ff57..9450b6c 100644 (file)
 
 package com.android.layoutlib.bridge.bars;
 
+import com.android.layoutlib.bridge.android.BridgeContext;
 import com.android.resources.Density;
 
 import org.xmlpull.v1.XmlPullParserException;
 
-import android.content.Context;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 public class NavigationBar extends CustomBar {
 
-    public NavigationBar(Context context, Density density, int orientation, boolean isRtl,
+    /** Navigation bar background color attribute name. */
+    private static final String ATTR_COLOR = "navigationBarColor";
+
+    public NavigationBar(BridgeContext context, Density density, int orientation, boolean isRtl,
             boolean rtlEnabled, int simulatedPlatformVersion) throws XmlPullParserException {
         super(context, orientation, "/bars/navigation_bar.xml", "navigation_bar.xml",
                 simulatedPlatformVersion);
 
-        setBackgroundColor(0xFF000000);
+        int color = getThemeAttrColor(ATTR_COLOR, true);
+        setBackgroundColor(color == 0 ? 0xFF000000 : color);
 
         // Cannot access the inside items through id because no R.id values have been
         // created for them.
index c7c62d6..e5f1f68 100644 (file)
@@ -25,7 +25,6 @@ import com.android.resources.Density;
 
 import org.xmlpull.v1.XmlPullParserException;
 
-import android.content.Context;
 import android.graphics.drawable.Drawable;
 import android.view.Gravity;
 import android.view.View;
@@ -38,20 +37,21 @@ import java.io.InputStream;
 
 public class StatusBar extends CustomBar {
 
-    private final Context mContext;
     private final int mSimulatedPlatformVersion;
+    /** Status bar background color attribute name. */
+    private static final String ATTR_COLOR = "colorPrimaryDark";
 
-    public StatusBar(Context context, Density density, int direction, boolean RtlEnabled,
+    public StatusBar(BridgeContext context, Density density, int direction, boolean RtlEnabled,
             int simulatedPlatformVersion) throws XmlPullParserException {
         // FIXME: if direction is RTL but it's not enabled in application manifest, mirror this bar.
         super(context, LinearLayout.HORIZONTAL, "/bars/status_bar.xml", "status_bar.xml",
                 simulatedPlatformVersion);
-        mContext = context;
         mSimulatedPlatformVersion = simulatedPlatformVersion;
 
         // FIXME: use FILL_H?
         setGravity(Gravity.START | Gravity.TOP | Gravity.RIGHT);
-        setBackgroundColor(Config.getStatusBarColor(simulatedPlatformVersion));
+        int color = getThemeAttrColor(ATTR_COLOR, true);
+        setBackgroundColor(color == 0 ? Config.getStatusBarColor(simulatedPlatformVersion) : color);
 
         // Cannot access the inside items through id because no R.id values have been
         // created for them.
@@ -82,10 +82,8 @@ public class StatusBar extends CustomBar {
                 try {
                     BridgeXmlBlockParser parser = new BridgeXmlBlockParser(
                             ParserFactory.create(stream, null), (BridgeContext) mContext, true);
-                    Drawable drawable = Drawable.createFromXml(mContext.getResources(), parser);
-                    if (drawable != null) {
-                        imageView.setImageDrawable(drawable);
-                    }
+                    imageView.setImageDrawable(
+                            Drawable.createFromXml(mContext.getResources(), parser));
                 } catch (XmlPullParserException e) {
                     Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e,
                             null);
index 10f1383..c610601 100644 (file)
 
 package com.android.layoutlib.bridge.bars;
 
+import com.android.layoutlib.bridge.android.BridgeContext;
+
 import org.xmlpull.v1.XmlPullParserException;
 
-import android.content.Context;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
@@ -26,7 +27,7 @@ public class TitleBar extends CustomBar {
 
     private TextView mTextView;
 
-    public TitleBar(Context context, String label, int simulatedPlatformVersion)
+    public TitleBar(BridgeContext context, String label, int simulatedPlatformVersion)
             throws XmlPullParserException {
         super(context, LinearLayout.HORIZONTAL, "/bars/title_bar.xml", "title_bar.xml",
                 simulatedPlatformVersion);