StatusBar.java revision 58ca246c834d7ab45939367367a5932d8e348f41
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.layoutlib.bridge.bars; 18 19import com.android.ide.common.rendering.api.LayoutLog; 20import com.android.layoutlib.bridge.Bridge; 21import com.android.layoutlib.bridge.android.BridgeContext; 22import com.android.layoutlib.bridge.android.BridgeXmlBlockParser; 23import com.android.layoutlib.bridge.impl.ParserFactory; 24import com.android.resources.Density; 25 26import org.xmlpull.v1.XmlPullParserException; 27 28import android.graphics.drawable.Drawable; 29import android.view.Gravity; 30import android.view.View; 31import android.widget.ImageView; 32import android.widget.LinearLayout; 33import android.widget.TextView; 34 35import java.io.IOException; 36import java.io.InputStream; 37 38public class StatusBar extends CustomBar { 39 40 private final int mSimulatedPlatformVersion; 41 /** Status bar background color attribute name. */ 42 private static final String ATTR_COLOR = "colorPrimaryDark"; 43 44 public StatusBar(BridgeContext context, Density density, int direction, boolean RtlEnabled, 45 int simulatedPlatformVersion) throws XmlPullParserException { 46 // FIXME: if direction is RTL but it's not enabled in application manifest, mirror this bar. 47 super(context, LinearLayout.HORIZONTAL, "/bars/status_bar.xml", "status_bar.xml", 48 simulatedPlatformVersion); 49 mSimulatedPlatformVersion = simulatedPlatformVersion; 50 51 // FIXME: use FILL_H? 52 setGravity(Gravity.START | Gravity.TOP | Gravity.RIGHT); 53 int color = getThemeAttrColor(ATTR_COLOR, true); 54 setBackgroundColor(color == 0 ? Config.getStatusBarColor(simulatedPlatformVersion) : color); 55 56 // Cannot access the inside items through id because no R.id values have been 57 // created for them. 58 // We do know the order though. 59 // 0 is the spacer 60 loadIcon(1, "stat_sys_wifi_signal_4_fully." 61 + Config.getWifiIconType(simulatedPlatformVersion), density); 62 loadIcon(2, "stat_sys_battery_100.png", density); 63 setText(3, Config.getTime(simulatedPlatformVersion), false) 64 .setTextColor(Config.getTimeColor(simulatedPlatformVersion)); 65 } 66 67 @Override 68 protected void loadIcon(int index, String iconName, Density density) { 69 if (!iconName.endsWith(".xml")) { 70 super.loadIcon(index, iconName, density); 71 return; 72 } 73 View child = getChildAt(index); 74 if (child instanceof ImageView) { 75 ImageView imageView = (ImageView) child; 76 // The xml is stored only in xhdpi. 77 IconLoader iconLoader = new IconLoader(iconName, Density.XHIGH, 78 mSimulatedPlatformVersion, null); 79 InputStream stream = iconLoader.getIcon(); 80 81 if (stream != null) { 82 try { 83 BridgeXmlBlockParser parser = new BridgeXmlBlockParser( 84 ParserFactory.create(stream, null), (BridgeContext) mContext, true); 85 imageView.setImageDrawable( 86 Drawable.createFromXml(mContext.getResources(), parser)); 87 } catch (XmlPullParserException e) { 88 Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, 89 null); 90 } catch (IOException e) { 91 Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, 92 null); 93 } 94 } 95 } 96 } 97 98 @Override 99 protected TextView getStyleableTextView() { 100 return null; 101 } 102} 103