CustomBar.java revision 1076be31f1c14f39295cc2ce7a747ee9ad96ee73
1282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/*
2282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Copyright (C) 2011 The Android Open Source Project
3282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
4282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
5282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * you may not use this file except in compliance with the License.
6282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * You may obtain a copy of the License at
7282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
8282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
9282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
10282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Unless required by applicable law or agreed to in writing, software
11282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
12282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * See the License for the specific language governing permissions and
14282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * limitations under the License.
15282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */
16282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
17282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipackage com.android.layoutlib.bridge.bars;
18282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
19282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.RenderResources;
20282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.ResourceValue;
21282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.StyleResourceValue;
22282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.Bridge;
23282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.android.BridgeContext;
24282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
25282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.impl.ParserFactory;
26282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.impl.ResourceHelper;
27282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.resources.Density;
288930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Guptaimport com.android.resources.LayoutDirection;
29282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
30282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport org.xmlpull.v1.XmlPullParser;
31282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport org.xmlpull.v1.XmlPullParserException;
32282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
33282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.content.Context;
34282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.content.res.ColorStateList;
35282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Bitmap;
36282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Bitmap_Delegate;
37282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.BitmapDrawable;
38282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.Drawable;
39282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.util.TypedValue;
40282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.view.Gravity;
41282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.view.LayoutInflater;
42282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.view.View;
43282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.widget.ImageView;
44282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.widget.LinearLayout;
45282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.widget.TextView;
46282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
47282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.IOException;
48282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.InputStream;
49282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
501076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Guptaimport static com.android.layoutlib.bridge.bars.Config.DEFAULT_RESOURCE_DIR;
511076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta
52282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/**
53282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Base "bar" class for the window decor around the the edited layout.
54282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * This is basically an horizontal layout that loads a given layout on creation (it is read
55282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * through {@link Class#getResourceAsStream(String)}).
56282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
57282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * The given layout should be a merge layout so that all the children belong to this class directly.
58282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski *
59282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * It also provides a few utility methods to configure the content of the layout.
60282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */
61282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiabstract class CustomBar extends LinearLayout {
62282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
631076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta    // An upper-bound on the length of the path to the directory to find the icon in.
641076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta    // This assumes that resource directory name for different api levels have same length.
651076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta    private static final int ICON_PATH_LENGTH = DEFAULT_RESOURCE_DIR.length()
661076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            + LayoutDirection.RTL.getResourceValue().length() + 9;  // 9 = "-xxxhdpi/".length
671076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta
681076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta
6942c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta    private final int mSimulatedPlatformVersion;
7042c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta
71282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    protected abstract TextView getStyleableTextView();
72282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
7342c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta    protected CustomBar(Context context, int orientation, String layoutPath,
7442c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta            String name, int simulatedPlatformVersion) throws XmlPullParserException {
75282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        super(context);
7642c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta        mSimulatedPlatformVersion = simulatedPlatformVersion;
77282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        setOrientation(orientation);
78282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        if (orientation == LinearLayout.HORIZONTAL) {
79282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            setGravity(Gravity.CENTER_VERTICAL);
80282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        } else {
81282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            setGravity(Gravity.CENTER_HORIZONTAL);
82282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
83282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
84282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
85282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                Context.LAYOUT_INFLATER_SERVICE);
86282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
87282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        XmlPullParser parser = ParserFactory.create(getClass().getResourceAsStream(layoutPath),
88282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                name);
89282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
90282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(
91282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                parser, (BridgeContext) context, false /*platformFile*/);
92282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
93282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        try {
94282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            inflater.inflate(bridgeParser, this, true);
95282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        } finally {
96282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            bridgeParser.ensurePopped();
97282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
98282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
99282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
1008930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta    private InputStream getIcon(String iconName, Density[] densityInOut, LayoutDirection direction,
1011076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            StringBuilder[] pathOut, boolean tryOtherDensities) {
1021076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta        pathOut[0] = new StringBuilder(ICON_PATH_LENGTH + iconName.length());
1031076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta
1041076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta        if (Config.usesCustomResourceDir(mSimulatedPlatformVersion)) {
1051076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            // current density.
1061076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            Density density = densityInOut[0];
1071076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            InputStream stream = getIcon(iconName, Config.getResourceDir(mSimulatedPlatformVersion),
1081076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    densityInOut, direction, pathOut, tryOtherDensities);
1091076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            if (stream != null) {
1101076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                return stream;
1111076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            }
1121076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            // reset the density.
1131076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            densityInOut[0] = density;
1141076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta        }
1151076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta        return getIcon(iconName, DEFAULT_RESOURCE_DIR, densityInOut, direction, pathOut,
1161076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                tryOtherDensities);
1171076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta
1181076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta    }
1191076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta
1201076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta    private InputStream getIcon(String iconName, String dir, Density[] densityInOut,
1211076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            LayoutDirection direction, StringBuilder[] pathOut, boolean tryOtherDensities) {
122282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        // current density
123282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        Density density = densityInOut[0];
124282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
1251076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta        pathOut[0].setLength(0);
1261076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta
127282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        // bitmap url relative to this class
1281076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta        if (direction == LayoutDirection.RTL) {
1291076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            pathOut[0].append(dir)
1301076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append(direction.getResourceValue())
1311076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append('-')
1321076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append(density.getResourceValue())
1331076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append('/')
1341076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append(iconName);
1358930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta        } else {
1361076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            // Since we do not have any ldltr resource, skip the check.
1371076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            pathOut[0].append(dir)
1381076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append(density.getResourceValue())
1391076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append('/')
1401076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    .append(iconName);
1418930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta        }
142282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
1431076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta        InputStream stream = getClass().getResourceAsStream(pathOut[0].toString());
144282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        if (stream == null && tryOtherDensities) {
145282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            for (Density d : Density.values()) {
146282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                if (d != density) {
147282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    densityInOut[0] = d;
1481076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                    stream = getIcon(iconName, dir, densityInOut, direction, pathOut,
1498930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta                            false /*tryOtherDensities*/);
150282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    if (stream != null) {
151282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                        return stream;
152282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    }
153282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                }
1541076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            }
1551076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            // couldn't find resource with direction qualifier, try without.
1561076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            if (direction == LayoutDirection.RTL) {
1571076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                densityInOut[0] = density;
1581076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                stream = getIcon(iconName, dir, densityInOut, null, pathOut,
1591076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                        true /*tryOtherDensities*/);
160282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            }
161282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
162282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
163282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        return stream;
164282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
165282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
166282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    protected void loadIcon(int index, String iconName, Density density) {
1678930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta        loadIcon(index, iconName, density, false);
1688930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta    }
1698930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta
1708930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta    protected void loadIcon(int index, String iconName, Density density, boolean isRtl) {
171282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        View child = getChildAt(index);
172282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        if (child instanceof ImageView) {
173282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            ImageView imageView = (ImageView) child;
174282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
1751076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            StringBuilder[] pathOut = new StringBuilder[1];
1761076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            Density[] densityInOut = new Density[]{density};
1771076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            LayoutDirection dir = isRtl ? LayoutDirection.RTL : null;
1788930cb4f37ff0fbde65f7afd4bbe06ab77677f78Deepanshu Gupta            InputStream stream = getIcon(iconName, densityInOut, dir, pathOut,
179282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    true /*tryOtherDensities*/);
180282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            density = densityInOut[0];
1811076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta            String path = pathOut[0].toString();
182282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
183282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            if (stream != null) {
184282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                // look for a cached bitmap
1851076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                Bitmap bitmap = Bridge.getCachedBitmap(path, true /*isFramework*/);
186282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                if (bitmap == null) {
187282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    try {
188282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                        bitmap = Bitmap_Delegate.createBitmap(stream, false /*isMutable*/, density);
1891076be31f1c14f39295cc2ce7a747ee9ad96ee73Deepanshu Gupta                        Bridge.setCachedBitmap(path, bitmap, true /*isFramework*/);
190282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    } catch (IOException e) {
191282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                        return;
192282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    }
193282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                }
194282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
195282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                if (bitmap != null) {
196282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(),
197282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                            bitmap);
198282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    imageView.setImageDrawable(drawable);
199282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                }
200282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            }
201282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
202282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
203282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
204282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    protected TextView setText(int index, String stringReference) {
205282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        View child = getChildAt(index);
206282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        if (child instanceof TextView) {
207282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            TextView textView = (TextView) child;
208282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            setText(textView, stringReference);
209282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            return textView;
210282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
211282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
212282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        return null;
213282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
214282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
215282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    private void setText(TextView textView, String stringReference) {
216282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        ResourceValue value = getResourceValue(stringReference);
217282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        if (value != null) {
218282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            textView.setText(value.getValue());
219282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        } else {
220282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            textView.setText(stringReference);
221282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
222282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
223282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
224282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    protected void setStyle(String themeEntryName) {
225282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
226282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        BridgeContext bridgeContext = (BridgeContext) mContext;
227282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        RenderResources res = bridgeContext.getRenderResources();
228282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
229282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        ResourceValue value = res.findItemInTheme(themeEntryName, true /*isFrameworkAttr*/);
230282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        value = res.resolveResValue(value);
231282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
23242c7ce02f4023c94b315253754a20c20800be731Deepanshu Gupta        if (!(value instanceof StyleResourceValue)) {
233282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            return;
234282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
235282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
236282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        StyleResourceValue style = (StyleResourceValue) value;
237282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
238282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        // get the background
239282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        ResourceValue backgroundValue = res.findItemInStyle(style, "background",
240282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                true /*isFrameworkAttr*/);
241282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        backgroundValue = res.resolveResValue(backgroundValue);
242282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        if (backgroundValue != null) {
243282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            Drawable d = ResourceHelper.getDrawable(backgroundValue, bridgeContext);
244282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            if (d != null) {
245282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                setBackground(d);
246282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            }
247282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
248282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
249282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        TextView textView = getStyleableTextView();
250282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        if (textView != null) {
251282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            // get the text style
252282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            ResourceValue textStyleValue = res.findItemInStyle(style, "titleTextStyle",
253282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    true /*isFrameworkAttr*/);
254282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            textStyleValue = res.resolveResValue(textStyleValue);
255282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            if (textStyleValue instanceof StyleResourceValue) {
256282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                StyleResourceValue textStyle = (StyleResourceValue) textStyleValue;
257282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
258282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                ResourceValue textSize = res.findItemInStyle(textStyle, "textSize",
259282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                        true /*isFrameworkAttr*/);
260282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                textSize = res.resolveResValue(textSize);
261282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
262282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                if (textSize != null) {
263282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    TypedValue out = new TypedValue();
264282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    if (ResourceHelper.parseFloatAttribute("textSize", textSize.getValue(), out,
265282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                            true /*requireUnit*/)) {
266d8df6b570f10cec486b2af708e9a393f3012317dDeepanshu Gupta                        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
267282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                                out.getDimension(bridgeContext.getResources().getDisplayMetrics()));
268282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    }
269282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                }
270282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
271282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
272282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                ResourceValue textColor = res.findItemInStyle(textStyle, "textColor",
273282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                        true /*isFrameworkAttr*/);
274282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                textColor = res.resolveResValue(textColor);
275282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                if (textColor != null) {
276282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    ColorStateList stateList = ResourceHelper.getColorStateList(
277282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                            textColor, bridgeContext);
278282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    if (stateList != null) {
279282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                        textView.setTextColor(stateList);
280282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                    }
281282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski                }
282282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski            }
283282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        }
284282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
285282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
286282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    private ResourceValue getResourceValue(String reference) {
287282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        BridgeContext bridgeContext = (BridgeContext) mContext;
288282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        RenderResources res = bridgeContext.getRenderResources();
289282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
290282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        // find the resource
291282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        ResourceValue value = res.findResValue(reference, false /*isFramework*/);
292282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski
293282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        // resolve it if needed
294282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski        return res.resolveResValue(value);
295282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski    }
296282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski}
297