Home | History | Annotate | Download | only in bars
      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 
     17 package com.android.layoutlib.bridge.bars;
     18 
     19 import com.android.ide.common.rendering.api.LayoutLog;
     20 import com.android.layoutlib.bridge.Bridge;
     21 import com.android.layoutlib.bridge.android.BridgeContext;
     22 import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
     23 import com.android.layoutlib.bridge.impl.ParserFactory;
     24 import com.android.resources.Density;
     25 
     26 import org.xmlpull.v1.XmlPullParserException;
     27 
     28 import android.content.Context;
     29 import android.content.pm.ApplicationInfo;
     30 import android.graphics.drawable.Drawable;
     31 import android.util.AttributeSet;
     32 import android.view.Gravity;
     33 import android.view.View;
     34 import android.widget.ImageView;
     35 import android.widget.LinearLayout;
     36 import android.widget.TextView;
     37 
     38 import java.io.IOException;
     39 import java.io.InputStream;
     40 
     41 public class StatusBar extends CustomBar {
     42 
     43     private final int mSimulatedPlatformVersion;
     44     /** Status bar background color attribute name. */
     45     private static final String ATTR_COLOR = "statusBarColor";
     46     /** Attribute for translucency property. */
     47     public static final String ATTR_TRANSLUCENT = "windowTranslucentStatus";
     48 
     49     /**
     50      * Constructor to be used when creating the {@link StatusBar} as a regular control. This
     51      * is currently used by the theme editor.
     52      */
     53     @SuppressWarnings("UnusedParameters")
     54     public StatusBar(Context context, AttributeSet attrs) {
     55         this((BridgeContext) context,
     56                 Density.getEnum(((BridgeContext) context).getMetrics().densityDpi),
     57                 ((BridgeContext) context).getConfiguration().getLayoutDirection() ==
     58                         View.LAYOUT_DIRECTION_RTL,
     59                 (context.getApplicationInfo().flags & ApplicationInfo.FLAG_SUPPORTS_RTL) != 0,
     60                 context.getApplicationInfo().targetSdkVersion);
     61     }
     62 
     63     @SuppressWarnings("UnusedParameters")
     64     public StatusBar(BridgeContext context, Density density, boolean isRtl, boolean rtlEnabled,
     65             int simulatedPlatformVersion) {
     66         // FIXME: if direction is RTL but it's not enabled in application manifest, mirror this bar.
     67         super(context, LinearLayout.HORIZONTAL, "/bars/status_bar.xml", "status_bar.xml",
     68                 simulatedPlatformVersion);
     69         mSimulatedPlatformVersion = simulatedPlatformVersion;
     70 
     71         // FIXME: use FILL_H?
     72         setGravity(Gravity.START | Gravity.TOP | Gravity.RIGHT);
     73 
     74         int color = getBarColor(ATTR_COLOR, ATTR_TRANSLUCENT);
     75         setBackgroundColor(color == 0 ? Config.getStatusBarColor(simulatedPlatformVersion) : color);
     76 
     77         // Cannot access the inside items through id because no R.id values have been
     78         // created for them.
     79         // We do know the order though.
     80         // 0 is the spacer
     81         loadIcon(1, "stat_sys_wifi_signal_4_fully."
     82                         + Config.getWifiIconType(simulatedPlatformVersion), density);
     83         loadIcon(2, "stat_sys_battery_100.png", density);
     84         setText(3, Config.getTime(simulatedPlatformVersion), false)
     85                 .setTextColor(Config.getTimeColor(simulatedPlatformVersion));
     86     }
     87 
     88     @Override
     89     protected void loadIcon(int index, String iconName, Density density) {
     90         if (!iconName.endsWith(".xml")) {
     91             super.loadIcon(index, iconName, density);
     92             return;
     93         }
     94         View child = getChildAt(index);
     95         if (child instanceof ImageView) {
     96             ImageView imageView = (ImageView) child;
     97             // The xml is stored only in xhdpi.
     98             IconLoader iconLoader = new IconLoader(iconName, Density.XHIGH,
     99                     mSimulatedPlatformVersion, null);
    100             InputStream stream = iconLoader.getIcon();
    101 
    102             if (stream != null) {
    103                 try {
    104                     BridgeXmlBlockParser parser = new BridgeXmlBlockParser(
    105                             ParserFactory.create(stream, null), (BridgeContext) mContext, true);
    106                     imageView.setImageDrawable(
    107                             Drawable.createFromXml(mContext.getResources(), parser));
    108                 } catch (XmlPullParserException e) {
    109                     Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e,
    110                             null);
    111                 } catch (IOException e) {
    112                     Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e,
    113                             null);
    114                 }
    115             }
    116         }
    117     }
    118 
    119     @Override
    120     protected TextView getStyleableTextView() {
    121         return null;
    122     }
    123 }
    124