1 /* 2 * Copyright (C) 2018 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 17 package com.android.layoutlib.bridge.resources; 18 19 import com.android.ide.common.rendering.api.ResourceNamespace; 20 import com.android.layoutlib.bridge.Bridge; 21 import com.android.layoutlib.bridge.android.BridgeContext; 22 import com.android.layoutlib.bridge.android.BridgeXmlBlockParser; 23 import com.android.layoutlib.bridge.bars.Config; 24 import com.android.layoutlib.bridge.impl.ParserFactory; 25 import com.android.resources.Density; 26 import com.android.resources.LayoutDirection; 27 import com.android.tools.layoutlib.annotations.NotNull; 28 29 import org.xmlpull.v1.XmlPullParser; 30 import org.xmlpull.v1.XmlPullParserException; 31 32 import android.content.Context; 33 import android.graphics.Bitmap; 34 import android.graphics.Bitmap_Delegate; 35 import android.graphics.drawable.BitmapDrawable; 36 import android.widget.ImageView; 37 38 import java.io.IOException; 39 import java.io.InputStream; 40 41 public class SysUiResources { 42 private static final ResourceNamespace PRIVATE_LAYOUTLIB_NAMESPACE = 43 ResourceNamespace.fromPackageName("com.android.layoutlib"); 44 45 @NotNull loadXml(BridgeContext context, int apiLevel, String layoutName)46 public static BridgeXmlBlockParser loadXml(BridgeContext context, int apiLevel, String 47 layoutName) { 48 for (String resourceRepository : Config.getResourceDirs(apiLevel)) { 49 String path = resourceRepository + layoutName; 50 InputStream stream = SysUiResources.class.getResourceAsStream(path); 51 if (stream != null) { 52 try { 53 XmlPullParser parser = ParserFactory.create(stream, layoutName); 54 55 // TODO(namespaces): does the namespace matter here? 56 return new BridgeXmlBlockParser(parser, context, PRIVATE_LAYOUTLIB_NAMESPACE); 57 } catch (XmlPullParserException e) { 58 // Should not happen as the resource is bundled with the jar, and ParserFactory should 59 // have been initialized. 60 assert false; 61 } 62 } 63 } 64 65 assert false; 66 return null; 67 } 68 loadIcon(Context context, int api, ImageView imageView, String iconName, Density density, boolean isRtl)69 public static ImageView loadIcon(Context context, int api, ImageView imageView, String 70 iconName, 71 Density density, boolean 72 isRtl) { 73 LayoutDirection dir = isRtl ? LayoutDirection.RTL : null; 74 IconLoader iconLoader = new IconLoader(iconName, density, api, 75 dir); 76 InputStream stream = iconLoader.getIcon(); 77 78 if (stream != null) { 79 density = iconLoader.getDensity(); 80 String path = iconLoader.getPath(); 81 // look for a cached bitmap 82 Bitmap bitmap = Bridge.getCachedBitmap(path, Boolean.TRUE /*isFramework*/); 83 if (bitmap == null) { 84 try { 85 bitmap = Bitmap_Delegate.createBitmap(stream, false /*isMutable*/, density); 86 Bridge.setCachedBitmap(path, bitmap, Boolean.TRUE /*isFramework*/); 87 } catch (IOException e) { 88 return imageView; 89 } 90 } 91 92 if (bitmap != null) { 93 BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); 94 imageView.setImageDrawable(drawable); 95 } 96 } 97 98 return imageView; 99 } 100 } 101