Skip to content

Commit 4c0554e

Browse files
Allow2 Devruvnet
andcommitted
Add model classes
CheckResult, Activity, DayType, OAuthTokens, RequestResult, VoiceCodePair, RequestType enum, FeedbackCategory enum. Matches PHP SDK model surface. Co-Authored-By: claude-flow <ruv@ruv.net>
1 parent 424480e commit 4c0554e

8 files changed

Lines changed: 558 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.allow2.service.models;
2+
3+
import org.json.JSONObject;
4+
5+
/**
6+
* Represents the permission state of a single activity from a check result.
7+
*/
8+
public final class Activity {
9+
10+
/** Screen Time activity ID -- master device-level switch. */
11+
public static final int SCREEN_TIME = 8;
12+
13+
/** Gaming activity ID. */
14+
public static final int GAMING = 3;
15+
16+
/** Internet activity ID. */
17+
public static final int INTERNET = 1;
18+
19+
/** Social Media activity ID. */
20+
public static final int SOCIAL = 6;
21+
22+
private final int id;
23+
private final String name;
24+
private final boolean allowed;
25+
private final int remaining;
26+
private final boolean banned;
27+
private final boolean timeBlockAllowed;
28+
29+
public Activity(int id, String name, boolean allowed, int remaining, boolean banned, boolean timeBlockAllowed) {
30+
this.id = id;
31+
this.name = name;
32+
this.allowed = allowed;
33+
this.remaining = remaining;
34+
this.banned = banned;
35+
this.timeBlockAllowed = timeBlockAllowed;
36+
}
37+
38+
public int getId() {
39+
return id;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public boolean isAllowed() {
47+
return allowed;
48+
}
49+
50+
public int getRemaining() {
51+
return remaining;
52+
}
53+
54+
public boolean isBanned() {
55+
return banned;
56+
}
57+
58+
public boolean isTimeBlockAllowed() {
59+
return timeBlockAllowed;
60+
}
61+
62+
/**
63+
* Create from a single activity entry in the API check response.
64+
*
65+
* @param data The JSON object for this activity.
66+
* @return A new Activity instance.
67+
*/
68+
public static Activity fromJsonObject(JSONObject data) {
69+
String name = "";
70+
if (data.has("activity")) {
71+
name = data.getString("activity");
72+
} else if (data.has("name")) {
73+
name = data.getString("name");
74+
}
75+
76+
boolean timeBlock = true;
77+
if (data.has("timeblock")) {
78+
timeBlock = toBoolean(data.get("timeblock"));
79+
} else if (data.has("timeBlockAllowed")) {
80+
timeBlock = toBoolean(data.get("timeBlockAllowed"));
81+
}
82+
83+
return new Activity(
84+
data.getInt("id"),
85+
name,
86+
data.has("allowed") && toBoolean(data.get("allowed")),
87+
data.optInt("remaining", 0),
88+
data.has("banned") && toBoolean(data.get("banned")),
89+
timeBlock
90+
);
91+
}
92+
93+
private static boolean toBoolean(Object value) {
94+
if (value instanceof Boolean) {
95+
return (Boolean) value;
96+
}
97+
if (value instanceof Number) {
98+
return ((Number) value).intValue() != 0;
99+
}
100+
return Boolean.parseBoolean(String.valueOf(value));
101+
}
102+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.allow2.service.models;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
/**
11+
* Result of a permission check from the Allow2 service API.
12+
*
13+
* <p>Contains the overall allowed status, per-activity breakdown,
14+
* and current/upcoming day type information.</p>
15+
*/
16+
public final class CheckResult {
17+
18+
private final boolean allowed;
19+
private final List<Activity> activities;
20+
private final DayType todayDayType;
21+
private final DayType tomorrowDayType;
22+
private final JSONObject raw;
23+
24+
/**
25+
* @param allowed Whether the child is globally allowed right now.
26+
* @param activities Per-activity permission breakdown.
27+
* @param todayDayType The current day type (may be null).
28+
* @param tomorrowDayType The upcoming day type (may be null).
29+
* @param raw The raw API response for advanced use.
30+
*/
31+
public CheckResult(boolean allowed, List<Activity> activities, DayType todayDayType,
32+
DayType tomorrowDayType, JSONObject raw) {
33+
this.allowed = allowed;
34+
this.activities = activities != null ? Collections.unmodifiableList(new ArrayList<>(activities)) : Collections.emptyList();
35+
this.todayDayType = todayDayType;
36+
this.tomorrowDayType = tomorrowDayType;
37+
this.raw = raw != null ? raw : new JSONObject();
38+
}
39+
40+
public boolean isAllowed() {
41+
return allowed;
42+
}
43+
44+
public List<Activity> getActivities() {
45+
return activities;
46+
}
47+
48+
public DayType getTodayDayType() {
49+
return todayDayType;
50+
}
51+
52+
public DayType getTomorrowDayType() {
53+
return tomorrowDayType;
54+
}
55+
56+
public JSONObject getRaw() {
57+
return raw;
58+
}
59+
60+
/**
61+
* Get a specific activity by ID, or null if not present.
62+
*
63+
* @param activityId The activity ID to look up.
64+
* @return The Activity, or null.
65+
*/
66+
public Activity getActivity(int activityId) {
67+
for (Activity activity : activities) {
68+
if (activity.getId() == activityId) {
69+
return activity;
70+
}
71+
}
72+
return null;
73+
}
74+
75+
/**
76+
* Check whether a specific activity is allowed.
77+
*
78+
* @param activityId The activity ID.
79+
* @return true if the activity exists and is allowed.
80+
*/
81+
public boolean isActivityAllowed(int activityId) {
82+
Activity activity = getActivity(activityId);
83+
return activity != null && activity.isAllowed();
84+
}
85+
86+
/**
87+
* Get remaining seconds for a specific activity.
88+
*
89+
* @param activityId The activity ID.
90+
* @return Remaining seconds, or 0 if not found.
91+
*/
92+
public int getRemainingSeconds(int activityId) {
93+
Activity activity = getActivity(activityId);
94+
return activity != null ? activity.getRemaining() : 0;
95+
}
96+
97+
/**
98+
* Build from the raw API response.
99+
*
100+
* @param response The decoded JSON response from /serviceapi/check.
101+
* @return A new CheckResult instance.
102+
*/
103+
public static CheckResult fromApiResponse(JSONObject response) {
104+
List<Activity> activities = new ArrayList<>();
105+
JSONArray rawActivities = response.optJSONArray("activities");
106+
if (rawActivities != null) {
107+
for (int i = 0; i < rawActivities.length(); i++) {
108+
activities.add(Activity.fromJsonObject(rawActivities.getJSONObject(i)));
109+
}
110+
}
111+
112+
DayType todayDayType = null;
113+
if (response.has("dayType")) {
114+
todayDayType = DayType.fromJsonObject(response.getJSONObject("dayType"));
115+
} else if (response.has("today")) {
116+
todayDayType = DayType.fromJsonObject(response.getJSONObject("today"));
117+
}
118+
119+
DayType tomorrowDayType = null;
120+
if (response.has("tomorrowDayType")) {
121+
tomorrowDayType = DayType.fromJsonObject(response.getJSONObject("tomorrowDayType"));
122+
} else if (response.has("tomorrow")) {
123+
tomorrowDayType = DayType.fromJsonObject(response.getJSONObject("tomorrow"));
124+
}
125+
126+
// Global "allowed" is true only if ALL activities are allowed
127+
boolean allowed = true;
128+
for (Activity act : activities) {
129+
if (!act.isAllowed()) {
130+
allowed = false;
131+
break;
132+
}
133+
}
134+
135+
// Override with explicit server value if present
136+
if (response.has("allowed")) {
137+
allowed = toBoolean(response.get("allowed"));
138+
}
139+
140+
return new CheckResult(allowed, activities, todayDayType, tomorrowDayType, response);
141+
}
142+
143+
private static boolean toBoolean(Object value) {
144+
if (value instanceof Boolean) {
145+
return (Boolean) value;
146+
}
147+
if (value instanceof Number) {
148+
return ((Number) value).intValue() != 0;
149+
}
150+
return Boolean.parseBoolean(String.valueOf(value));
151+
}
152+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.allow2.service.models;
2+
3+
import org.json.JSONObject;
4+
5+
/**
6+
* Represents a day type in the Allow2 system (e.g., School Day, Weekend, Holiday).
7+
*/
8+
public final class DayType {
9+
10+
private final int id;
11+
private final String name;
12+
13+
public DayType(int id, String name) {
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
public int getId() {
19+
return id;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
/**
27+
* Create from API response data.
28+
*
29+
* @param data The JSON object containing id and name.
30+
* @return A new DayType instance.
31+
*/
32+
public static DayType fromJsonObject(JSONObject data) {
33+
return new DayType(
34+
data.getInt("id"),
35+
data.getString("name")
36+
);
37+
}
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.allow2.service.models;
2+
3+
/**
4+
* Categories for feedback submissions.
5+
*/
6+
public enum FeedbackCategory {
7+
8+
BUG("bug"),
9+
FEATURE_REQUEST("feature_request"),
10+
NOT_WORKING("not_working"),
11+
OTHER("other");
12+
13+
private final String value;
14+
15+
FeedbackCategory(String value) {
16+
this.value = value;
17+
}
18+
19+
/**
20+
* Get the API value string for this category.
21+
*
22+
* @return The API value.
23+
*/
24+
public String getValue() {
25+
return value;
26+
}
27+
}

0 commit comments

Comments
 (0)