/** * Get specific color of related theme from theme apk asset resource path. * * @param colorName the key string of the color. * @return the color value for the current theme, if the current theme is the default theme, or * the colorName is not present in the color.xml, return 0. */ public int getThemeColor(String colorName) { if (EncapsulationConstant.USE_MTK_PLATFORM) { return mResources.getThemeColor(colorName); } else { InputStream raw = null; AssetManager am = mResources.getAssets(); String themepath = SystemProperties.get("persist.sys.skin", DEFAULT_THEME_PATH); // If the current theme is the default theme, return 0 directly. if (DEFAULT_THEME_PATH.equals(themepath)) { return 0; } // get themeColor from cache Integer themeColor = mMtkColorCache.get(colorName); if (themeColor != null) { return themeColor; } // Add theme path to asset path to access it, if add asset path failed, // return 0 directly. int cookie = am.addAssetPath(themepath); if (cookie == 0) { return 0; } // Get color value from xml file. try { // Open color.xml as assets. raw = am.openNonAsset(cookie, THEME_COLOR_PATH, AssetManager.ACCESS_STREAMING); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setValidating(false); XmlPullParser myxml = factory.newPullParser(); myxml.setInput(raw, null); int eventType = myxml.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if (STR_COLOR.equals(myxml.getName())) { if (colorName.equals(myxml.getAttributeValue(0))) { String colorStr = myxml.nextText(); themeColor = Color.parseColor(colorStr); mMtkColorCache.put(colorName, themeColor); return themeColor; } } break; case XmlPullParser.END_TAG: break; default: break; } eventType = myxml.next(); } } catch (IOException e) { Log.e(TAG, "IOException happened when getThemeColor, msg = " + e.getMessage()); } catch (XmlPullParserException e) { Log.e(TAG, "XmlPullParserException happened when getThemeColor, msg = " + e.getMessage()); } return 0; } }