StatusBar.java revision 4eb298a941c3f465944b63f1a06518e911681c89
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    /** 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        Integer color = getBarColor(ATTR_COLOR, ATTR_TRANSLUCENT);
75        setBackgroundColor(
76                color == null ? Config.getStatusBarColor(simulatedPlatformVersion) : color);
77
78        // Cannot access the inside items through id because no R.id values have been
79        // created for them.
80        // We do know the order though.
81        // 0 is the spacer
82        loadIcon(1, "stat_sys_wifi_signal_4_fully."
83                        + Config.getWifiIconType(simulatedPlatformVersion), density);
84        loadIcon(2, "stat_sys_battery_100.png", density);
85        setText(3, Config.getTime(simulatedPlatformVersion), false)
86                .setTextColor(Config.getTimeColor(simulatedPlatformVersion));
87    }
88
89    @Override
90    protected void loadIcon(int index, String iconName, Density density) {
91        if (!iconName.endsWith(".xml")) {
92            super.loadIcon(index, iconName, density);
93            return;
94        }
95        View child = getChildAt(index);
96        if (child instanceof ImageView) {
97            ImageView imageView = (ImageView) child;
98            // The xml is stored only in xhdpi.
99            IconLoader iconLoader = new IconLoader(iconName, Density.XHIGH,
100                    mSimulatedPlatformVersion, null);
101            InputStream stream = iconLoader.getIcon();
102
103            if (stream != null) {
104                try {
105                    BridgeXmlBlockParser parser = new BridgeXmlBlockParser(
106                            ParserFactory.create(stream, null), (BridgeContext) mContext, true);
107                    imageView.setImageDrawable(
108                            Drawable.createFromXml(mContext.getResources(), parser));
109                } catch (XmlPullParserException e) {
110                    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e,
111                            null);
112                } catch (IOException e) {
113                    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e,
114                            null);
115                }
116            }
117        }
118    }
119
120    @Override
121    protected TextView getStyleableTextView() {
122        return null;
123    }
124}
125