The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.app; |
| 18 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 19 | import android.os.Trace; |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 20 | import android.util.ArrayMap; |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 21 | import com.android.internal.os.PathClassLoaderFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import dalvik.system.PathClassLoader; |
| 23 | |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 24 | class ApplicationLoaders { |
| 25 | public static ApplicationLoaders getDefault() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | return gApplicationLoaders; |
| 27 | } |
| 28 | |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 29 | public ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled, |
| 30 | String librarySearchPath, String libraryPermittedPath, |
| 31 | ClassLoader parent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | /* |
| 33 | * This is the parent we use if they pass "null" in. In theory |
| 34 | * this should be the "system" class loader; in practice we |
| 35 | * don't use that and can happily (and more efficiently) use the |
| 36 | * bootstrap class loader. |
| 37 | */ |
| 38 | ClassLoader baseParent = ClassLoader.getSystemClassLoader().getParent(); |
| 39 | |
| 40 | synchronized (mLoaders) { |
| 41 | if (parent == null) { |
| 42 | parent = baseParent; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * If we're one step up from the base class loader, find |
| 47 | * something in our cache. Otherwise, we create a whole |
| 48 | * new ClassLoader for the zip archive. |
| 49 | */ |
| 50 | if (parent == baseParent) { |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 51 | ClassLoader loader = mLoaders.get(zip); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | if (loader != null) { |
| 53 | return loader; |
| 54 | } |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 55 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 56 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 57 | |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 58 | PathClassLoader pathClassloader = PathClassLoaderFactory.createClassLoader( |
| 59 | zip, |
| 60 | librarySearchPath, |
| 61 | libraryPermittedPath, |
| 62 | parent, |
| 63 | targetSdkVersion, |
| 64 | isBundled); |
Dimitry Ivanov | a55c7f1 | 2016-02-23 14:25:50 -0800 | [diff] [blame] | 65 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 66 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 67 | |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 68 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setupVulkanLayerPath"); |
Dimitry Ivanov | 0997908 | 2016-04-19 11:11:01 -0700 | [diff] [blame] | 69 | setupVulkanLayerPath(pathClassloader, librarySearchPath); |
Andreas Gampe | 4c8e542 | 2016-05-18 11:58:47 -0700 | [diff] [blame] | 70 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Dimitry Ivanov | 0997908 | 2016-04-19 11:11:01 -0700 | [diff] [blame] | 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | mLoaders.put(zip, pathClassloader); |
| 73 | return pathClassloader; |
| 74 | } |
| 75 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 76 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); |
| 77 | PathClassLoader pathClassloader = new PathClassLoader(zip, parent); |
| 78 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 79 | return pathClassloader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Dimitry Ivanov | 0997908 | 2016-04-19 11:11:01 -0700 | [diff] [blame] | 83 | private static native void setupVulkanLayerPath(ClassLoader classLoader, String librarySearchPath); |
| 84 | |
Todd Kennedy | 39bfee5 | 2016-02-24 10:28:21 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Adds a new path the classpath of the given loader. |
| 87 | * @throws IllegalStateException if the provided class loader is not a {@link PathClassLoader}. |
| 88 | */ |
| 89 | void addPath(ClassLoader classLoader, String dexPath) { |
| 90 | if (!(classLoader instanceof PathClassLoader)) { |
| 91 | throw new IllegalStateException("class loader is not a PathClassLoader"); |
| 92 | } |
| 93 | final PathClassLoader baseDexClassLoader = (PathClassLoader) classLoader; |
| 94 | baseDexClassLoader.addDexPath(dexPath); |
| 95 | } |
| 96 | |
Dianne Hackborn | add005c | 2013-07-17 18:43:12 -0700 | [diff] [blame] | 97 | private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<String, ClassLoader>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
Dimitry Ivanov | b1ef62b | 2016-04-20 14:09:32 -0700 | [diff] [blame] | 99 | private static final ApplicationLoaders gApplicationLoaders = new ApplicationLoaders(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |