aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowWindowManagerImpl.java
blob: 0f6972b26a4cf977f827eb59ee987f3c15e9eb79 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.JELLY_BEAN;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S_V2;
import static android.view.View.SYSTEM_UI_FLAG_VISIBLE;
import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
import static org.robolectric.RuntimeEnvironment.getApiLevel;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Insets;
import android.graphics.Rect;
import android.os.Build.VERSION_CODES;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.InsetsState;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.WindowManagerImpl;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import java.util.HashMap;
import java.util.List;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowViewRootImpl.ViewRootImplReflector;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;

@Implements(value = WindowManagerImpl.class, isInAndroidSdk = false)
public class ShadowWindowManagerImpl extends ShadowWindowManager {

  private static Display defaultDisplayJB;

  @RealObject WindowManagerImpl realObject;
  private static final Multimap<Integer, View> views = ArrayListMultimap.create();

  // removed from WindowManagerImpl in S
  public static final int NEW_INSETS_MODE_FULL = 2;

  /** internal only */
  public static void configureDefaultDisplayForJBOnly(
      Configuration configuration, DisplayMetrics displayMetrics) {
    Class<?> arg2Type =
        ReflectionHelpers.loadClass(
            ShadowWindowManagerImpl.class.getClassLoader(), "android.view.CompatibilityInfoHolder");

    defaultDisplayJB =
        ReflectionHelpers.callConstructor(
            Display.class, ClassParameter.from(int.class, 0), ClassParameter.from(arg2Type, null));
    ShadowDisplay shadowDisplay = Shadow.extract(defaultDisplayJB);
    shadowDisplay.configureForJBOnly(configuration, displayMetrics);
  }

  @Implementation
  public void addView(View view, android.view.ViewGroup.LayoutParams layoutParams) {
    views.put(realObject.getDefaultDisplay().getDisplayId(), view);
    // views.add(view);
    reflector(ReflectorWindowManagerImpl.class, realObject).addView(view, layoutParams);
  }

  @Implementation
  public void removeView(View view) {
    views.remove(realObject.getDefaultDisplay().getDisplayId(), view);
    reflector(ReflectorWindowManagerImpl.class, realObject).removeView(view);
  }

  @Implementation
  protected void removeViewImmediate(View view) {
    views.remove(realObject.getDefaultDisplay().getDisplayId(), view);
    reflector(ReflectorWindowManagerImpl.class, realObject).removeViewImmediate(view);
  }

  public List<View> getViews() {
    return ImmutableList.copyOf(views.get(realObject.getDefaultDisplay().getDisplayId()));
  }

  @Implementation(maxSdk = JELLY_BEAN)
  public Display getDefaultDisplay() {
    if (getApiLevel() > JELLY_BEAN) {
      return reflector(ReflectorWindowManagerImpl.class, realObject).getDefaultDisplay();
    } else {
      return defaultDisplayJB;
    }
  }

  @Implements(className = "android.view.WindowManagerImpl$CompatModeWrapper", maxSdk = JELLY_BEAN)
  public static class ShadowCompatModeWrapper {
    @Implementation(maxSdk = JELLY_BEAN)
    protected Display getDefaultDisplay() {
      return defaultDisplayJB;
    }
  }

  /** Re implement to avoid server call */
  @Implementation(minSdk = R, maxSdk = S_V2)
  protected WindowInsets getWindowInsetsFromServer(WindowManager.LayoutParams attrs, Rect bounds) {
    Context context = reflector(ReflectorWindowManagerImpl.class, realObject).getContext();
    final Rect systemWindowInsets = new Rect();
    final Rect stableInsets = new Rect();
    final DisplayCutout.ParcelableWrapper displayCutout = new DisplayCutout.ParcelableWrapper();
    final InsetsState insetsState = new InsetsState();
    final boolean alwaysConsumeSystemBars = true;

    final boolean isScreenRound = context.getResources().getConfiguration().isScreenRound();
    if (getApiLevel() <= R
        && reflector(ViewRootImplReflector.class).getNewInsetsMode() == NEW_INSETS_MODE_FULL) {
      return ReflectionHelpers.callInstanceMethod(
          insetsState,
          "calculateInsets",
          ClassParameter.from(Rect.class, bounds),
          null,
          ClassParameter.from(Boolean.TYPE, isScreenRound),
          ClassParameter.from(Boolean.TYPE, alwaysConsumeSystemBars),
          ClassParameter.from(DisplayCutout.ParcelableWrapper.class, displayCutout.get()),
          ClassParameter.from(int.class, SOFT_INPUT_ADJUST_NOTHING),
          ClassParameter.from(int.class, SYSTEM_UI_FLAG_VISIBLE),
          null);
    } else {
      return new WindowInsets.Builder()
          .setAlwaysConsumeSystemBars(alwaysConsumeSystemBars)
          .setRound(isScreenRound)
          .setSystemWindowInsets(Insets.of(systemWindowInsets))
          .setStableInsets(Insets.of(stableInsets))
          .setDisplayCutout(displayCutout.get())
          .build();
    }
  }

  @ForType(WindowManagerImpl.class)
  interface ReflectorWindowManagerImpl {

    @Direct
    void addView(View view, ViewGroup.LayoutParams layoutParams);

    @Direct
    void removeView(View view);

    @Direct
    void removeViewImmediate(View view);

    @Direct
    Display getDefaultDisplay();

    @Accessor("mContext")
    Context getContext();
  }

  @Resetter
  public static void reset() {
    defaultDisplayJB = null;
    views.clear();
    if (getApiLevel() <= VERSION_CODES.JELLY_BEAN) {
      ReflectionHelpers.setStaticField(
          WindowManagerImpl.class,
          "sWindowManager",
          ReflectionHelpers.newInstance(WindowManagerImpl.class));
      HashMap windowManagers =
          ReflectionHelpers.getStaticField(WindowManagerImpl.class, "sCompatWindowManagers");
      windowManagers.clear();
    }
  }
}