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.graphics.drawable.Drawable;
30import android.view.Gravity;
31import android.view.View;
32import android.widget.ImageView;
33import android.widget.LinearLayout;
34import android.widget.TextView;
35
36import java.io.IOException;
37import java.io.InputStream;
38
39public class StatusBar extends CustomBar {
40
41    private final Context mContext;
42    private final int mSimulatedPlatformVersion;
43
44    public StatusBar(Context 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        mContext = context;
50        mSimulatedPlatformVersion = simulatedPlatformVersion;
51
52        // FIXME: use FILL_H?
53        setGravity(Gravity.START | Gravity.TOP | Gravity.RIGHT);
54        setBackgroundColor(Config.getStatusBarColor(simulatedPlatformVersion));
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                    Drawable drawable = Drawable.createFromXml(mContext.getResources(), parser);
86                    if (drawable != null) {
87                        imageView.setImageDrawable(drawable);
88                    }
89                } catch (XmlPullParserException e) {
90                    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e,
91                            null);
92                } catch (IOException e) {
93                    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e,
94                            null);
95                }
96            }
97        }
98    }
99
100    @Override
101    protected TextView getStyleableTextView() {
102        return null;
103    }
104}
105