Home | History | Annotate | Download | only in app
      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 
     19 import android.os.Trace;
     20 import android.util.ArrayMap;
     21 import com.android.internal.os.PathClassLoaderFactory;
     22 import dalvik.system.PathClassLoader;
     23 
     24 class ApplicationLoaders {
     25     public static ApplicationLoaders getDefault() {
     26         return gApplicationLoaders;
     27     }
     28 
     29     public ClassLoader getClassLoader(String zip, int targetSdkVersion, boolean isBundled,
     30                                       String librarySearchPath, String libraryPermittedPath,
     31                                       ClassLoader parent) {
     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) {
     51                 ClassLoader loader = mLoaders.get(zip);
     52                 if (loader != null) {
     53                     return loader;
     54                 }
     55 
     56                 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);
     57 
     58                 PathClassLoader pathClassloader = PathClassLoaderFactory.createClassLoader(
     59                                                       zip,
     60                                                       librarySearchPath,
     61                                                       libraryPermittedPath,
     62                                                       parent,
     63                                                       targetSdkVersion,
     64                                                       isBundled);
     65 
     66                 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
     67 
     68                 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setupVulkanLayerPath");
     69                 setupVulkanLayerPath(pathClassloader, librarySearchPath);
     70                 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
     71 
     72                 mLoaders.put(zip, pathClassloader);
     73                 return pathClassloader;
     74             }
     75 
     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;
     80         }
     81     }
     82 
     83     private static native void setupVulkanLayerPath(ClassLoader classLoader, String librarySearchPath);
     84 
     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 
     97     private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<String, ClassLoader>();
     98 
     99     private static final ApplicationLoaders gApplicationLoaders = new ApplicationLoaders();
    100 }
    101