aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/DragEventBuilder.java
blob: 6b10fac1526b025c46399cd1063eacef78da53e4 (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
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.R;

import android.content.ClipData;
import android.content.ClipDescription;
import android.view.DragEvent;
import android.view.SurfaceControl;
import androidx.annotation.Nullable;
import com.android.internal.view.IDragAndDropPermissions;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;

/** Builder for {@link DragEvent}. */
public class DragEventBuilder {
  private int action;
  private float x;
  private float y;
  @Nullable private Object localState;
  @Nullable private ClipDescription clipDescription;
  @Nullable private ClipData clipData;
  private boolean result;

  private DragEventBuilder() {}

  public static DragEventBuilder newBuilder() {
    return new DragEventBuilder();
  }

  public DragEventBuilder setAction(int action) {
    this.action = action;
    return this;
  }

  public DragEventBuilder setX(float x) {
    this.x = x;
    return this;
  }

  public DragEventBuilder setY(float y) {
    this.y = y;
    return this;
  }

  public DragEventBuilder setLocalState(@Nullable Object localState) {
    this.localState = localState;
    return this;
  }

  public DragEventBuilder setClipDescription(@Nullable ClipDescription clipDescription) {
    this.clipDescription = clipDescription;
    return this;
  }

  public DragEventBuilder setClipData(@Nullable ClipData clipData) {
    this.clipData = clipData;
    return this;
  }

  public DragEventBuilder setResult(boolean result) {
    this.result = result;
    return this;
  }

  public DragEvent build() {
    int api = RuntimeEnvironment.getApiLevel();
    if (api <= M) {
      return ReflectionHelpers.callStaticMethod(
          DragEvent.class,
          "obtain",
          ClassParameter.from(int.class, action),
          ClassParameter.from(float.class, x),
          ClassParameter.from(float.class, y),
          ClassParameter.from(Object.class, localState),
          ClassParameter.from(ClipDescription.class, clipDescription),
          ClassParameter.from(ClipData.class, clipData),
          ClassParameter.from(boolean.class, result));
    } else if (api <= R) {
      return ReflectionHelpers.callStaticMethod(
          DragEvent.class,
          "obtain",
          ClassParameter.from(int.class, action),
          ClassParameter.from(float.class, x),
          ClassParameter.from(float.class, y),
          ClassParameter.from(Object.class, localState),
          ClassParameter.from(ClipDescription.class, clipDescription),
          ClassParameter.from(ClipData.class, clipData),
          ClassParameter.from(IDragAndDropPermissions.class, null),
          ClassParameter.from(boolean.class, result));
    } else {
      return ReflectionHelpers.callStaticMethod(
          DragEvent.class,
          "obtain",
          ClassParameter.from(int.class, action),
          ClassParameter.from(float.class, x),
          ClassParameter.from(float.class, y),
          ClassParameter.from(float.class, 0),
          ClassParameter.from(float.class, 0),
          ClassParameter.from(Object.class, localState),
          ClassParameter.from(ClipDescription.class, clipDescription),
          ClassParameter.from(ClipData.class, clipData),
          ClassParameter.from(SurfaceControl.class, null),
          ClassParameter.from(IDragAndDropPermissions.class, null),
          ClassParameter.from(boolean.class, result));
    }
  }
}