1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.M; 4 import static org.robolectric.RuntimeEnvironment.getApiLevel; 5 import static org.robolectric.shadow.api.Shadow.directlyOn; 6 7 import android.content.Context; 8 import android.graphics.drawable.Drawable; 9 import android.view.Window; 10 import android.widget.ProgressBar; 11 import org.robolectric.annotation.Implementation; 12 import org.robolectric.annotation.Implements; 13 import org.robolectric.annotation.RealObject; 14 import org.robolectric.util.ReflectionHelpers; 15 import org.robolectric.util.ReflectionHelpers.ClassParameter; 16 17 @SuppressWarnings({"UnusedDeclaration"}) 18 @Implements(Window.class) 19 public class ShadowWindow { 20 private @RealObject Window realWindow; 21 22 protected CharSequence title = ""; 23 protected Drawable backgroundDrawable; 24 private int flags; 25 private int softInputMode; 26 create(Context context)27 public static Window create(Context context) throws Exception { 28 String className = getApiLevel() >= M 29 ? "com.android.internal.policy.PhoneWindow" 30 : "com.android.internal.policy.impl.PhoneWindow"; 31 Class<? extends Window> phoneWindowClass = 32 (Class<? extends Window>) Window.class.getClassLoader().loadClass(className); 33 return ReflectionHelpers.callConstructor(phoneWindowClass, ClassParameter.from(Context.class, context)); 34 } 35 36 @Implementation setFlags(int flags, int mask)37 protected void setFlags(int flags, int mask) { 38 this.flags = (this.flags & ~mask) | (flags & mask); 39 directlyOn(realWindow, Window.class, "setFlags", ClassParameter.from(int.class, flags), ClassParameter.from(int.class, mask)); 40 } 41 42 @Implementation setSoftInputMode(int softInputMode)43 protected void setSoftInputMode(int softInputMode) { 44 this.softInputMode = softInputMode; 45 directlyOn(realWindow, Window.class, "setSoftInputMode", ClassParameter.from(int.class, softInputMode)); 46 } 47 getFlag(int flag)48 public boolean getFlag(int flag) { 49 return (flags & flag) == flag; 50 } 51 getTitle()52 public CharSequence getTitle() { 53 return title; 54 } 55 getSoftInputMode()56 public int getSoftInputMode() { 57 return softInputMode; 58 } 59 getBackgroundDrawable()60 public Drawable getBackgroundDrawable() { 61 return backgroundDrawable; 62 } 63 getProgressBar()64 public ProgressBar getProgressBar() { 65 return (ProgressBar) directlyOn(realWindow, realWindow.getClass().getName(), "getHorizontalProgressBar", ClassParameter.from(boolean.class, false)); 66 } 67 getIndeterminateProgressBar()68 public ProgressBar getIndeterminateProgressBar() { 69 return (ProgressBar) directlyOn(realWindow, realWindow.getClass().getName(), "getCircularProgressBar", ClassParameter.from(boolean.class, false)); 70 } 71 } 72