-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathOrientationOptions.java
More file actions
76 lines (63 loc) · 2.67 KB
/
OrientationOptions.java
File metadata and controls
76 lines (63 loc) · 2.67 KB
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
package com.reactnativenavigation.options;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.annotation.CheckResult;
public class OrientationOptions {
List<Orientation> orientations = new ArrayList<>();
public static OrientationOptions parse(JSONObject json) {
OrientationOptions options = new OrientationOptions();
if (json == null) return options;
JSONArray orientations = json.optJSONArray("orientation");
if (orientations == null) {
String orientation = json.optString("orientation", Orientation.Default.name);
options.orientations.add(Orientation.fromString(orientation));
} else {
List<Orientation> parsed = new ArrayList<>();
for (int i = 0; i < orientations.length(); i++) {
Orientation o = Orientation.fromString(orientations.optString(i, "default"));
if (o != null) {
parsed.add(o);
}
}
options.orientations = parsed;
}
return options;
}
public int getValue() {
if (!hasValue()) return Orientation.Default.orientationCode;
switch (orientations.get(0)) {
case Landscape:
return orientations.contains(Orientation.Portrait) ? Orientation.PortraitLandscape.orientationCode : Orientation.Landscape.orientationCode;
case Portrait:
return orientations.contains(Orientation.Landscape) ? Orientation.PortraitLandscape.orientationCode : Orientation.Portrait.orientationCode;
case SensorLandscape:
return Orientation.SensorLandscape.orientationCode;
case SensorPortrait:
return Orientation.SensorPortrait.orientationCode;
default:
case Default:
return Orientation.Default.orientationCode;
}
}
public boolean hasValue() {
if (orientations.isEmpty()) return false;
return orientations.size() != 1 || orientations.get(0) != Orientation.Default;
}
@CheckResult
public OrientationOptions copy() {
OrientationOptions result = new OrientationOptions();
result.orientations = new ArrayList<>(orientations);
return result;
}
public OrientationOptions mergeWithDefault(OrientationOptions defaultOptions) {
if (!hasValue()) orientations = defaultOptions.orientations;
return this;
}
@Override
public String toString() {
return hasValue() ? Arrays.toString(orientations.toArray(new Orientation[0])) : Orientation.Default.toString();
}
}