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