1282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/* 2282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Copyright (C) 2011 The Android Open Source Project 3282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 4282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 5282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * you may not use this file except in compliance with the License. 6282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * You may obtain a copy of the License at 7282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 8282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 9282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 10282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Unless required by applicable law or agreed to in writing, software 11282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 12282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * See the License for the specific language governing permissions and 14282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * limitations under the License. 15282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 16282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 17282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipackage com.android.layoutlib.bridge.bars; 18282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 19e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Guptaimport com.android.ide.common.rendering.api.LayoutLog; 20282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.RenderResources; 21282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.ResourceValue; 22282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.StyleResourceValue; 23282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.Bridge; 24282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.android.BridgeContext; 25282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.android.BridgeXmlBlockParser; 26282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.impl.ParserFactory; 27282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.impl.ResourceHelper; 28282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.resources.Density; 298930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Guptaimport com.android.resources.LayoutDirection; 3058ca246c834d7ab45939367367a5932d8e348f41Deepanshu Guptaimport com.android.resources.ResourceType; 31282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 32282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport org.xmlpull.v1.XmlPullParser; 33282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport org.xmlpull.v1.XmlPullParserException; 34282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 35476e582d2ffdf25102d4c55f8c242baa3d21d37fDeepanshu Guptaimport android.annotation.NonNull; 36282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.content.res.ColorStateList; 37282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Bitmap; 38282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Bitmap_Delegate; 39282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.BitmapDrawable; 40282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.Drawable; 41282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.util.TypedValue; 42282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.view.Gravity; 43282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.view.LayoutInflater; 44282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.view.View; 45282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.widget.ImageView; 46282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.widget.LinearLayout; 47282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.widget.TextView; 48282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 49282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.IOException; 50282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.InputStream; 51282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 5258ca246c834d7ab45939367367a5932d8e348f41Deepanshu Guptaimport static android.os.Build.VERSION_CODES.LOLLIPOP; 5358ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta 54282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/** 55282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Base "bar" class for the window decor around the the edited layout. 56282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * This is basically an horizontal layout that loads a given layout on creation (it is read 57282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * through {@link Class#getResourceAsStream(String)}). 58282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 59282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * The given layout should be a merge layout so that all the children belong to this class directly. 60282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 61282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * It also provides a few utility methods to configure the content of the layout. 62282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 63282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiabstract class CustomBar extends LinearLayout { 64282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 651076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta 6642c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta private final int mSimulatedPlatformVersion; 6742c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta 68282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski protected abstract TextView getStyleableTextView(); 69282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 7058ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta protected CustomBar(BridgeContext context, int orientation, String layoutPath, 71b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta String name, int simulatedPlatformVersion) { 72282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski super(context); 7342c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta mSimulatedPlatformVersion = simulatedPlatformVersion; 74282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski setOrientation(orientation); 75282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (orientation == LinearLayout.HORIZONTAL) { 76282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski setGravity(Gravity.CENTER_VERTICAL); 77282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else { 78282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski setGravity(Gravity.CENTER_HORIZONTAL); 79282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 80282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 81fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta LayoutInflater inflater = LayoutInflater.from(mContext); 82282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 83b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta XmlPullParser parser; 84b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta try { 85b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta parser = ParserFactory.create(getClass().getResourceAsStream(layoutPath), name); 86b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta } catch (XmlPullParserException e) { 87b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta // Should not happen as the resource is bundled with the jar, and ParserFactory should 88b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta // have been initialized. 89b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta throw new AssertionError(e); 90b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta } 91282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 92b1484862e2367d87d3ccbd0fd0a6d2598ed5918aDeepanshu Gupta BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(parser, context, false); 93282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 94282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 95282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski inflater.inflate(bridgeParser, this, true); 96282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } finally { 97282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski bridgeParser.ensurePopped(); 98282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 99282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 100282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 101282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski protected void loadIcon(int index, String iconName, Density density) { 1028930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta loadIcon(index, iconName, density, false); 1038930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta } 1048930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta 1058930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta protected void loadIcon(int index, String iconName, Density density, boolean isRtl) { 106282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski View child = getChildAt(index); 107282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (child instanceof ImageView) { 108282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ImageView imageView = (ImageView) child; 109282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 1101076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta LayoutDirection dir = isRtl ? LayoutDirection.RTL : null; 11184cc05cacd2786261b01d3be2364944c90765801Deepanshu Gupta IconLoader iconLoader = new IconLoader(iconName, density, mSimulatedPlatformVersion, 11284cc05cacd2786261b01d3be2364944c90765801Deepanshu Gupta dir); 11384cc05cacd2786261b01d3be2364944c90765801Deepanshu Gupta InputStream stream = iconLoader.getIcon(); 114282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 115282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (stream != null) { 11684cc05cacd2786261b01d3be2364944c90765801Deepanshu Gupta density = iconLoader.getDensity(); 11784cc05cacd2786261b01d3be2364944c90765801Deepanshu Gupta String path = iconLoader.getPath(); 118282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // look for a cached bitmap 1191076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta Bitmap bitmap = Bridge.getCachedBitmap(path, true /*isFramework*/); 120282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (bitmap == null) { 121282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 122282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski bitmap = Bitmap_Delegate.createBitmap(stream, false /*isMutable*/, density); 1231076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta Bridge.setCachedBitmap(path, bitmap, true /*isFramework*/); 124282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } catch (IOException e) { 125282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 126282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 127282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 128282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 129282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (bitmap != null) { 130282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(), 131282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski bitmap); 132282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski imageView.setImageDrawable(drawable); 133282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 134282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 135282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 136282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 137282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 138553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta protected TextView setText(int index, String string, boolean reference) { 139282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski View child = getChildAt(index); 140282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (child instanceof TextView) { 141282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski TextView textView = (TextView) child; 142553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta setText(textView, string, reference); 143282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return textView; 144282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 145282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 146282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 147282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 148282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 149553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta private void setText(TextView textView, String string, boolean reference) { 150553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta if (reference) { 151553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta ResourceValue value = getResourceValue(string); 152553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta if (value != null) { 153553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta string = value.getValue(); 154553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta } 155282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 156553a4e91385111f20ddf426f81b3193b9e951762Deepanshu Gupta textView.setText(string); 157282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 158282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 159282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski protected void setStyle(String themeEntryName) { 160282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 161fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta BridgeContext bridgeContext = getContext(); 162282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski RenderResources res = bridgeContext.getRenderResources(); 163282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 164282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue value = res.findItemInTheme(themeEntryName, true /*isFrameworkAttr*/); 165282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = res.resolveResValue(value); 166282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 16742c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta if (!(value instanceof StyleResourceValue)) { 168282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return; 169282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 170282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 171282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski StyleResourceValue style = (StyleResourceValue) value; 172282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 173282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the background 174282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue backgroundValue = res.findItemInStyle(style, "background", 175282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski true /*isFrameworkAttr*/); 176282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski backgroundValue = res.resolveResValue(backgroundValue); 177282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (backgroundValue != null) { 178282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Drawable d = ResourceHelper.getDrawable(backgroundValue, bridgeContext); 179282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (d != null) { 180282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski setBackground(d); 181282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 182282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 183282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 184282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski TextView textView = getStyleableTextView(); 185282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (textView != null) { 186282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the text style 187282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue textStyleValue = res.findItemInStyle(style, "titleTextStyle", 188282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski true /*isFrameworkAttr*/); 189282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski textStyleValue = res.resolveResValue(textStyleValue); 190282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (textStyleValue instanceof StyleResourceValue) { 191282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski StyleResourceValue textStyle = (StyleResourceValue) textStyleValue; 192282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 193282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue textSize = res.findItemInStyle(textStyle, "textSize", 194282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski true /*isFrameworkAttr*/); 195282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski textSize = res.resolveResValue(textSize); 196282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 197282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (textSize != null) { 198282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski TypedValue out = new TypedValue(); 199282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (ResourceHelper.parseFloatAttribute("textSize", textSize.getValue(), out, 200282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski true /*requireUnit*/)) { 201d8df6b570f10cec486b2af708e9a393f3012317dDeepanshu Gupta textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 202282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski out.getDimension(bridgeContext.getResources().getDisplayMetrics())); 203282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 204282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 205282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 206282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 207282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue textColor = res.findItemInStyle(textStyle, "textColor", 20858ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta true); 209282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski textColor = res.resolveResValue(textColor); 210282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (textColor != null) { 211282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ColorStateList stateList = ResourceHelper.getColorStateList( 212282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski textColor, bridgeContext); 213282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (stateList != null) { 214282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski textView.setTextColor(stateList); 215282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 216282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 217282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 218282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 219282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 220282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 221fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta @Override 222fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta public BridgeContext getContext() { 223fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta return (BridgeContext) mContext; 224fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta } 225fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta 22658ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta /** 227fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * Find the background color for this bar from the theme attributes. Only relevant to StatusBar 228fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * and NavigationBar. 22958ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta * <p/> 23058ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta * Returns 0 if not found. 23158ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta * 232fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * @param colorAttrName the attribute name for the background color 233fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * @param translucentAttrName the attribute name for the translucency property of the bar. 234fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * 23558ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta * @throws NumberFormatException if color resolved to an invalid string. 23658ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta */ 237fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta protected int getBarColor(@NonNull String colorAttrName, @NonNull String translucentAttrName) { 23858ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta if (!Config.isGreaterOrEqual(mSimulatedPlatformVersion, LOLLIPOP)) { 23958ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta return 0; 24058ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta } 241fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta RenderResources renderResources = getContext().getRenderResources(); 242fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta // First check if the bar is translucent. 243fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta boolean translucent = ResourceHelper.getBooleanThemeValue(renderResources, 244fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta translucentAttrName, true, false); 245fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta if (translucent) { 246fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta // Keep in sync with R.color.system_bar_background_semi_transparent from system ui. 247fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta return 0x66000000; // 40% black. 248fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta } 249fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta boolean transparent = ResourceHelper.getBooleanThemeValue(renderResources, 250fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta "windowDrawsSystemBarBackgrounds", true, false); 251fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta if (transparent) { 252fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta return getColor(renderResources, colorAttrName); 25358ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta } 254fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta return 0; 255fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta } 256fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta 257fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta private static int getColor(RenderResources renderResources, String attr) { 258fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta // From ?attr/foo to @color/bar. This is most likely an ItemResourceValue. 259fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta ResourceValue resource = renderResources.findItemInTheme(attr, true); 260fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta // Form @color/bar to the #AARRGGBB 261fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta resource = renderResources.resolveResValue(resource); 262e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta if (resource != null) { 263e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta ResourceType type = resource.getResourceType(); 264e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta if (type == null || type == ResourceType.COLOR) { 265e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta // if no type is specified, the value may have been specified directly in the style 266e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta // file, rather than referencing a color resource value. 267e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta try { 268e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta return ResourceHelper.getColor(resource.getValue()); 269e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta } catch (NumberFormatException e) { 270e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta // Conversion failed. 271e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, 272e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta "Theme attribute @android:" + attr + 273e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta " does not reference a color, instead is '" + 274e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta resource.getValue() + "'.", resource); 275e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta } 276e3f9834ebd8a6e4ee155c8e4c9456dfc98e8b832Deepanshu Gupta } 27758ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta } 27858ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta return 0; 27958ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta } 28058ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta 281282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private ResourceValue getResourceValue(String reference) { 282fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta RenderResources res = getContext().getRenderResources(); 283282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 284282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // find the resource 28558ca246c834d7ab45939367367a5932d8e348f41Deepanshu Gupta ResourceValue value = res.findResValue(reference, false); 286282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 287282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // resolve it if needed 288282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return res.resolveResValue(value); 289282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 290282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski} 291