aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetInputStream.java
blob: 9b91a22c1adb9f5e7b7f9e659adeef23d1e69395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.content.res.AssetManager.AssetInputStream;
import java.io.InputStream;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.res.android.Asset;
import org.robolectric.res.android.Registries;
import org.robolectric.shadows.ShadowAssetInputStream.Picker;
import org.robolectric.util.reflector.ForType;

@SuppressWarnings("UnusedDeclaration")
@Implements(value = AssetInputStream.class, shadowPicker = Picker.class)
public class ShadowArscAssetInputStream extends ShadowAssetInputStream {

  @RealObject
  private AssetInputStream realObject;

  @Override
  InputStream getDelegate() {
    return realObject;
  }

  private Asset getAsset() {
    int apiLevel = RuntimeEnvironment.getApiLevel();
    long assetPtr;
    if (apiLevel >= LOLLIPOP) {
      assetPtr = reflector(_AssetInputStream_.class, realObject).getNativeAsset();
    } else {
      assetPtr = reflector(_AssetInputStream_.class, realObject).getAssetInt();
    }
    return Registries.NATIVE_ASSET_REGISTRY.getNativeObject(assetPtr);
  }

  @Override
  boolean isNinePatch() {
    Asset asset = getAsset();
    return asset != null && asset.isNinePatch();
  }

  /** Accessor interface for {@link AssetInputStream}'s internals. */
  @ForType(AssetInputStream.class)
  private interface _AssetInputStream_ {
    int getAssetInt();

    long getNativeAsset();
  }
}