1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.Q; 4 import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE; 5 6 import android.graphics.Rect; 7 import android.view.InsetsSource; 8 import com.google.errorprone.annotations.CanIgnoreReturnValue; 9 import org.robolectric.RuntimeEnvironment; 10 import org.robolectric.annotation.Implements; 11 import org.robolectric.annotation.RealObject; 12 import org.robolectric.annotation.ReflectorObject; 13 import org.robolectric.util.reflector.ForType; 14 15 /** Shadow of {@link InsetsSource}. */ 16 @Implements(value = InsetsSource.class, minSdk = Q, isInAndroidSdk = false) 17 public class ShadowInsetsSource { 18 @RealObject private InsetsSource realInsetsSource; 19 @ReflectorObject private InsetsSourceReflector insetsSourceReflector; 20 21 /** 22 * Backwards compatible version of {@link InsetsSource#setVisible(boolean)} which changed in U 23 * from returning {@code void} to {@link InsetsSource}. 24 */ 25 @CanIgnoreReturnValue setVisible(boolean isVisible)26 ShadowInsetsSource setVisible(boolean isVisible) { 27 if (RuntimeEnvironment.getApiLevel() >= UPSIDE_DOWN_CAKE) { 28 realInsetsSource.setVisible(isVisible); 29 } else { 30 insetsSourceReflector.setVisible(isVisible); 31 } 32 return this; 33 } 34 35 /** 36 * Backwards compatible version of {@link InsetsSource#setFrame(Rect)} which changed in U from 37 * returning {@code void} to {@link InsetsSource}. 38 */ 39 @CanIgnoreReturnValue setFrame(Rect frame)40 ShadowInsetsSource setFrame(Rect frame) { 41 if (RuntimeEnvironment.getApiLevel() >= UPSIDE_DOWN_CAKE) { 42 realInsetsSource.setFrame(frame); 43 } else { 44 insetsSourceReflector.setFrame(frame); 45 } 46 return this; 47 } 48 49 @ForType(InsetsSource.class) 50 interface InsetsSourceReflector { 51 // Prior to U this method returned void setFrame(Rect frame)52 void setFrame(Rect frame); 53 54 // Prior to U this method returned void setVisible(boolean isVisible)55 void setVisible(boolean isVisible); 56 } 57 } 58