1 package org.robolectric;
2 
3 import java.net.URL;
4 import java.util.HashMap;
5 import java.util.Map;
6 import javax.annotation.Nonnull;
7 import org.robolectric.internal.SdkEnvironment;
8 import org.robolectric.internal.dependency.DependencyJar;
9 import org.robolectric.internal.dependency.DependencyResolver;
10 import org.robolectric.manifest.AndroidManifest;
11 import org.robolectric.res.FsFile;
12 import org.robolectric.res.PackageResourceTable;
13 import org.robolectric.res.ResourceMerger;
14 import org.robolectric.res.ResourcePath;
15 import org.robolectric.res.ResourceTableFactory;
16 
17 /**
18  * Mediates loading of "APKs" in legacy mode.
19  */
20 public class ApkLoader {
21 
22   private final Map<AndroidManifest, PackageResourceTable> appResourceTableCache = new HashMap<>();
23   private PackageResourceTable compiletimeSdkResourceTable;
24 
25   private final DependencyResolver dependencyResolver;
26 
ApkLoader(DependencyResolver dependencyResolver)27   protected ApkLoader(DependencyResolver dependencyResolver) {
28     this.dependencyResolver = dependencyResolver;
29   }
30 
getSystemResourceTable(SdkEnvironment sdkEnvironment)31   public PackageResourceTable getSystemResourceTable(SdkEnvironment sdkEnvironment) {
32     return sdkEnvironment.getSystemResourceTable(dependencyResolver);
33   }
34 
getAppResourceTable(final AndroidManifest appManifest)35   synchronized public PackageResourceTable getAppResourceTable(final AndroidManifest appManifest) {
36     PackageResourceTable resourceTable = appResourceTableCache.get(appManifest);
37     if (resourceTable == null) {
38       resourceTable = new ResourceMerger().buildResourceTable(appManifest);
39 
40       appResourceTableCache.put(appManifest, resourceTable);
41     }
42     return resourceTable;
43   }
44 
45   /**
46    * Returns the ResourceTable for the compile time SDK.
47    */
48   @Nonnull
getCompileTimeSdkResourceTable()49   synchronized public PackageResourceTable getCompileTimeSdkResourceTable() {
50     if (compiletimeSdkResourceTable == null) {
51       ResourceTableFactory resourceTableFactory = new ResourceTableFactory();
52       compiletimeSdkResourceTable = resourceTableFactory
53           .newFrameworkResourceTable(new ResourcePath(android.R.class, null, null));
54     }
55     return compiletimeSdkResourceTable;
56   }
57 
getArtifactUrl(DependencyJar dependency)58   public URL getArtifactUrl(DependencyJar dependency) {
59     return dependencyResolver.getLocalArtifactUrl(dependency);
60   }
61 
getCompileTimeSystemResourcesFile(SdkEnvironment sdkEnvironment)62   public FsFile getCompileTimeSystemResourcesFile(SdkEnvironment sdkEnvironment) {
63     return sdkEnvironment.getCompileTimeSystemResourcesFile(dependencyResolver);
64   }
65 }
66