aboutsummaryrefslogtreecommitdiff
path: root/src/com/google/android/droiddriver/scroll/Direction.java
blob: cbbfd6b05fab28310c2be5c53890fc24ade95a76 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) 2013 DroidDriver committers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.droiddriver.scroll;

import static com.google.android.droiddriver.scroll.Direction.PhysicalDirection.DOWN;
import static com.google.android.droiddriver.scroll.Direction.PhysicalDirection.LEFT;
import static com.google.android.droiddriver.scroll.Direction.PhysicalDirection.RIGHT;
import static com.google.android.droiddriver.scroll.Direction.PhysicalDirection.UP;

/**
 * A namespace to hold interfaces and constants for scroll directions.
 */
public class Direction {
  /** Logical directions */
  public enum LogicalDirection {
    FORWARD {
      @Override
      public LogicalDirection reverse() {
        return BACKWARD;
      }
    },
    BACKWARD {
      @Override
      public LogicalDirection reverse() {
        return FORWARD;
      }
    };
    public abstract LogicalDirection reverse();
  }

  /** Physical directions */
  public enum PhysicalDirection {
    UP {
      @Override
      public PhysicalDirection reverse() {
        return DOWN;
      }

      @Override
      public Axis axis() {
        return Axis.VERTICAL;
      }
    },
    DOWN {
      @Override
      public PhysicalDirection reverse() {
        return UP;
      }

      @Override
      public Axis axis() {
        return Axis.VERTICAL;
      }
    },
    LEFT {
      @Override
      public PhysicalDirection reverse() {
        return RIGHT;
      }

      @Override
      public Axis axis() {
        return Axis.HORIZONTAL;
      }
    },
    RIGHT {
      @Override
      public PhysicalDirection reverse() {
        return LEFT;
      }

      @Override
      public Axis axis() {
        return Axis.HORIZONTAL;
      }
    };
    public abstract PhysicalDirection reverse();

    public abstract Axis axis();
  }

  public enum Axis {
    HORIZONTAL {
      private final PhysicalDirection[] directions = {LEFT, RIGHT};

      @Override
      public PhysicalDirection[] getPhysicalDirections() {
        return directions;
      }
    },
    VERTICAL {
      private final PhysicalDirection[] directions = {UP, DOWN};

      @Override
      public PhysicalDirection[] getPhysicalDirections() {
        return directions;
      }
    };

    public abstract PhysicalDirection[] getPhysicalDirections();
  }

  /**
   * Converts between PhysicalDirection and LogicalDirection. It's possible to
   * override this for RTL (right-to-left) views, for example.
   */
  public static abstract class DirectionConverter {

    /** Follows standard convention: up-to-down, left-to-right */
    public static final DirectionConverter STANDARD_CONVERTER = new DirectionConverter() {
      @Override
      public PhysicalDirection horizontalForwardDirection() {
        return RIGHT;
      }

      @Override
      public PhysicalDirection verticalForwardDirection() {
        return DOWN;
      }
    };

    /** Follows RTL convention: up-to-down, right-to-left */
    public static final DirectionConverter RTL_CONVERTER = new DirectionConverter() {
      @Override
      public PhysicalDirection horizontalForwardDirection() {
        return LEFT;
      }

      @Override
      public PhysicalDirection verticalForwardDirection() {
        return DOWN;
      }
    };

    public abstract PhysicalDirection horizontalForwardDirection();

    public abstract PhysicalDirection verticalForwardDirection();

    public final PhysicalDirection horizontalBackwardDirection() {
      return horizontalForwardDirection().reverse();
    }

    public final PhysicalDirection verticalBackwardDirection() {
      return verticalForwardDirection().reverse();
    }

    /** Converts PhysicalDirection to LogicalDirection */
    public final LogicalDirection toLogicalDirection(PhysicalDirection physicalDirection) {
      LogicalDirection forward = LogicalDirection.FORWARD;
      if (toPhysicalDirection(physicalDirection.axis(), forward) == physicalDirection) {
        return forward;
      }
      return forward.reverse();
    }

    /** Converts LogicalDirection to PhysicalDirection */
    public final PhysicalDirection toPhysicalDirection(Axis axis, LogicalDirection logicalDirection) {
      switch (axis) {
        case HORIZONTAL:
          switch (logicalDirection) {
            case BACKWARD:
              return horizontalBackwardDirection();
            case FORWARD:
              return horizontalForwardDirection();
          }
          break;
        case VERTICAL:
          switch (logicalDirection) {
            case BACKWARD:
              return verticalBackwardDirection();
            case FORWARD:
              return verticalForwardDirection();
          }
      }
      return null;
    }
  }

  private Direction() {}
}