1 /* 2 * Copyright (C) 2014 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 android.content.res; 18 19 import com.android.layoutlib.bridge.impl.DelegateManager; 20 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 21 22 import android.util.SparseArray; 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 27 /** 28 * Delegate used to provide implementation of a select few native methods of {@link AssetManager} 29 * <p/> 30 * Through the layoutlib_create tool, the original native methods of AssetManager have been replaced 31 * by calls to methods of the same name in this delegate class. 32 * 33 */ 34 public class AssetManager_Delegate { 35 36 // ---- delegate manager ---- 37 38 private static final DelegateManager<AssetManager_Delegate> sManager = 39 new DelegateManager<>(AssetManager_Delegate.class); 40 getDelegateManager()41 public static DelegateManager<AssetManager_Delegate> getDelegateManager() { 42 return sManager; 43 } 44 45 // ---- delegate methods. ---- 46 47 @LayoutlibDelegate nativeCreate()48 /*package*/ static long nativeCreate() { 49 AssetManager_Delegate delegate = new AssetManager_Delegate(); 50 return sManager.addNewDelegate(delegate); 51 } 52 53 @LayoutlibDelegate nativeDestroy(long ptr)54 /*package*/ static void nativeDestroy(long ptr) { 55 sManager.removeJavaReferenceFor(ptr); 56 } 57 58 @LayoutlibDelegate open(AssetManager mgr, String fileName)59 public static InputStream open(AssetManager mgr, String fileName) throws IOException { 60 return mgr.open_Original(fileName); 61 } 62 63 @LayoutlibDelegate open(AssetManager mgr, String fileName, int accessMode)64 public static InputStream open(AssetManager mgr, String fileName, int accessMode) 65 throws IOException { 66 if (!(mgr instanceof BridgeAssetManager)) { 67 return mgr.open_Original(fileName, accessMode); 68 } 69 return ((BridgeAssetManager) mgr).getAssetRepository().openAsset(fileName, accessMode); 70 } 71 72 @LayoutlibDelegate nativeThemeCreate(long ptr)73 /*package*/ static long nativeThemeCreate(long ptr) { 74 return Resources_Theme_Delegate.getDelegateManager() 75 .addNewDelegate(new Resources_Theme_Delegate()); 76 } 77 78 @LayoutlibDelegate nativeThemeDestroy(long theme)79 /*package*/ static void nativeThemeDestroy(long theme) { 80 Resources_Theme_Delegate.getDelegateManager().removeJavaReferenceFor(theme); 81 } 82 83 @LayoutlibDelegate getAssignedPackageIdentifiers(AssetManager manager)84 /*package*/ static SparseArray<String> getAssignedPackageIdentifiers(AssetManager manager) { 85 return new SparseArray<>(); 86 } 87 88 @LayoutlibDelegate nativeCreateIdmapsForStaticOverlaysTargetingAndroid()89 /*package*/ static String[] nativeCreateIdmapsForStaticOverlaysTargetingAndroid() { 90 // AssetManager requires this not to be null 91 return new String[0]; 92 } 93 } 94