• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 
4 import android.content.res.ApkAssets;
5 import android.content.res.AssetManager;
6 import android.util.ArraySet;
7 import com.google.common.annotations.VisibleForTesting;
8 import com.google.common.collect.Ordering;
9 import java.nio.file.Path;
10 import java.util.Collection;
11 import java.util.List;
12 import org.robolectric.RuntimeEnvironment;
13 import org.robolectric.res.android.AssetPath;
14 import org.robolectric.res.android.CppAssetManager;
15 import org.robolectric.res.android.ResTable;
16 import org.robolectric.res.android.String8;
17 import org.robolectric.util.reflector.Accessor;
18 import org.robolectric.util.reflector.Direct;
19 import org.robolectric.util.reflector.ForType;
20 import org.robolectric.util.reflector.Static;
21 
22 abstract public class ShadowAssetManager {
23 
24   public static final Ordering<String> ATTRIBUTE_TYPE_PRECIDENCE =
25       Ordering.explicit(
26           "reference",
27           "color",
28           "boolean",
29           "integer",
30           "fraction",
31           "dimension",
32           "float",
33           "enum",
34           "flag",
35           "flags",
36           "string");
37 
38   public static class Picker extends ResourceModeShadowPicker<ShadowAssetManager> {
39 
Picker()40     public Picker() {
41       super(
42           ShadowArscAssetManager.class,
43           ShadowArscAssetManager9.class,
44           ShadowArscAssetManager10.class,
45           ShadowArscAssetManager14.class);
46     }
47   }
48 
getAllAssetDirs()49   abstract Collection<Path> getAllAssetDirs();
50 
51   @VisibleForTesting
getNativePtr()52   abstract long getNativePtr();
53 
54   public abstract static class ArscBase extends ShadowAssetManager {
55     private ResTable compileTimeResTable;
56 
57     /**
58      * @deprecated Avoid use.
59      */
60     @Deprecated
getCompileTimeResTable()61     synchronized public ResTable getCompileTimeResTable() {
62       if (compileTimeResTable == null) {
63         CppAssetManager compileTimeCppAssetManager = new CppAssetManager();
64         for (AssetPath assetPath : getAssetPaths()) {
65           if (assetPath.isSystem) {
66             compileTimeCppAssetManager.addDefaultAssets(
67                 RuntimeEnvironment.compileTimeSystemResourcesFile);
68           } else {
69             compileTimeCppAssetManager.addAssetPath(new String8(assetPath.file), null, false);
70           }
71         }
72         compileTimeResTable = compileTimeCppAssetManager.getResources();
73       }
74 
75       return compileTimeResTable;
76     }
77 
getAssetPaths()78     abstract List<AssetPath> getAssetPaths();
79   }
80 
81   /** Accessor interface for {@link AssetManager}'s internals. */
82   @ForType(AssetManager.class)
83   interface _AssetManager_ {
84     @Direct
85     @Static
getSystem()86     AssetManager getSystem();
87 
88     @Static
89     @Accessor("sSystem")
setSystem(AssetManager o)90     void setSystem(AssetManager o);
91 
92     @Accessor("mObject")
getNativePtr()93     long getNativePtr();
94   }
95 
96   /** Accessor interface for {@link AssetManager}'s internals added in API level 28. */
97   @ForType(AssetManager.class)
98   interface _AssetManager28_ extends _AssetManager_ {
99 
100     @Static @Accessor("sSystemApkAssets")
getSystemApkAssets()101     ApkAssets[] getSystemApkAssets();
102 
103     @Static @Accessor("sSystemApkAssets")
setSystemApkAssets(ApkAssets[] apkAssets)104     void setSystemApkAssets(ApkAssets[] apkAssets);
105 
106     @Static @Accessor("sSystemApkAssetsSet")
getSystemApkAssetsSet()107     ArraySet<ApkAssets> getSystemApkAssetsSet();
108 
109     @Static @Accessor("sSystemApkAssetsSet")
setSystemApkAssetsSet(ArraySet<ApkAssets> assetsSet)110     void setSystemApkAssetsSet(ArraySet<ApkAssets> assetsSet);
111 
getApkAssets()112     ApkAssets[] getApkAssets();
113   }
114 }
115