Skip to content

Commit c5cb686

Browse files
committed
Smol bugfix
1 parent 57d2682 commit c5cb686

23 files changed

+618
-83
lines changed

api/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

api/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'java-library'
3+
}
4+
5+
dependencies {
6+
implementation libs.json
7+
implementation libs.okhttp
8+
}
9+
10+
java {
11+
sourceCompatibility JavaVersion.VERSION_21
12+
targetCompatibility JavaVersion.VERSION_21
13+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package space.itoncek.trailcompass.client.api;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
6+
import java.io.IOException;
7+
8+
import okhttp3.MediaType;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.RequestBody;
12+
import okhttp3.Response;
13+
14+
public abstract class HideAndSeekAPI {
15+
public static final MediaType JSON = MediaType.get("application/json");
16+
17+
OkHttpClient client = new OkHttpClient();
18+
19+
public abstract HideAndSeekConfig getConfig();
20+
public abstract void saveConfig(HideAndSeekConfig cfg);
21+
22+
public ServerValidity checkValidity() throws IOException {
23+
Response response = get(getConfig().base_url + "/");
24+
boolean successful = response.isSuccessful();
25+
if(!successful) return ServerValidity.NOT_FOUND;
26+
assert response.body() != null;
27+
String version = response.body().string();
28+
response.close();
29+
if(version.equals("vDEVELOPMENT")) return ServerValidity.DEVELOPMENT_VERSION;
30+
else return SERVER_VERSIONS.SERVER_VERSION.equals(version)?ServerValidity.OK:ServerValidity.INCOMPATIBLE_VERSION;
31+
}
32+
33+
public long getPing() throws IOException {
34+
Request request = new Request.Builder()
35+
.url(getConfig().base_url+"/time")
36+
.build();
37+
38+
long start = System.currentTimeMillis();
39+
Response execute = client.newCall(request).execute();
40+
long end = System.currentTimeMillis();
41+
42+
assert execute.body() != null;
43+
long mid = Long.parseLong(execute.body().string());
44+
execute.close();
45+
return ((mid-start) + (end-mid))/2L;
46+
}
47+
48+
public LoginResponse login() throws IOException {
49+
HideAndSeekConfig cfg = getConfig();
50+
51+
ServerValidity serverValidity = checkValidity();
52+
if(serverValidity == ServerValidity.NOT_FOUND) return LoginResponse.UNABLE_TO_CONNECT;
53+
if(serverValidity.equals(ServerValidity.INCOMPATIBLE_VERSION)) return LoginResponse.INCOMPATIBLE_BACKEND;
54+
55+
String req = new JSONObject()
56+
.put("username", cfg.username)
57+
.put("passwordhash", cfg.passwordHash)
58+
.toString(4);
59+
60+
Response res = post(cfg.base_url + "/uac/login", req);
61+
62+
if(!res.isSuccessful()) return LoginResponse.UNABLE_TO_AUTH;
63+
64+
try {
65+
assert res.body() != null;
66+
cfg.jwt_token = new JSONObject(res.body().string()).getString("token");
67+
saveConfig(cfg);
68+
} catch (IOException | JSONException e) {
69+
return LoginResponse.UNABLE_TO_AUTH;
70+
}
71+
res.close();
72+
return LoginResponse.OK;
73+
}
74+
75+
public Response get(String url) throws IOException {
76+
Request request = new Request.Builder()
77+
.url(url)
78+
.build();
79+
80+
return client.newCall(request).execute();
81+
}
82+
83+
public Response post(String url, String json) throws IOException {
84+
RequestBody body = RequestBody.create(json, JSON);
85+
Request request = new Request.Builder()
86+
.url(url)
87+
.post(body)
88+
.build();
89+
90+
return client.newCall(request).execute();
91+
}
92+
93+
public Response postAuthd(String url, String json) throws IOException {
94+
RequestBody body = RequestBody.create(json, JSON);
95+
Request request = new Request.Builder()
96+
.url(url)
97+
.addHeader("Authorization","Bearer " + getConfig().jwt_token)
98+
.post(body)
99+
.build();
100+
101+
return client.newCall(request).execute();
102+
}
103+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package space.itoncek.trailcompass.client.api;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import java.io.Serial;
6+
import java.io.Serializable;
7+
import java.util.Objects;
8+
9+
public final class HideAndSeekConfig implements Serializable {
10+
public String jwt_token;
11+
public String base_url;
12+
public String username;
13+
public String passwordHash;
14+
15+
public HideAndSeekConfig(String jwt_token,
16+
String base_url,
17+
String username,
18+
String passwordHash) {
19+
20+
this.jwt_token = jwt_token;
21+
this.base_url = base_url;
22+
this.username = username;
23+
this.passwordHash = passwordHash;
24+
}
25+
26+
public static HideAndSeekConfig generateEmptyConfig() {
27+
return new HideAndSeekConfig("","","","");
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package space.itoncek.trailcompass.client.api;
2+
3+
public enum LoginResponse {
4+
UNABLE_TO_CONNECT,
5+
INCOMPATIBLE_BACKEND,
6+
UNABLE_TO_AUTH,
7+
OK
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package space.itoncek.trailcompass.client.api;
2+
3+
public class SERVER_VERSIONS {
4+
public static final String SERVER_VERSION = "v0.0.0.4";
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package space.itoncek.trailcompass.client.api;
2+
3+
public enum ServerValidity {
4+
NOT_FOUND,
5+
INCOMPATIBLE_VERSION,
6+
DEVELOPMENT_VERSION,
7+
OK
8+
}

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
implementation libs.firebase.analytics
5454
implementation libs.firebase.crashlytics
5555
implementation libs.firebase.perf
56+
implementation project(':api')
5657
//tests
5758
testImplementation libs.junit
5859
androidTestImplementation libs.ext.junit

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@
2727
android:label="@string/app_name"
2828
android:roundIcon="@mipmap/icon_round"
2929
android:supportsRtl="true"
30+
android:usesCleartextTraffic="true"
3031
android:theme="@style/Theme.TrailCompass">
32+
<activity
33+
android:name=".hideandseek.ui.LoginActivity"
34+
android:exported="false" />
35+
<activity
36+
android:name=".hideandseek.ui.MapView"
37+
android:exported="false" />
3138
<activity
3239
android:name=".mainmenu.MainMenuActivity"
3340
android:exported="true">
3441
<intent-filter>
3542
<action android:name="android.intent.action.MAIN" />
43+
3644
<category android:name="android.intent.category.LAUNCHER" />
3745
</intent-filter>
3846
</activity>
@@ -47,8 +55,7 @@
4755
android:exported="false" />
4856
<activity
4957
android:name=".debug.TestActivity"
50-
android:exported="true">
51-
</activity>
58+
android:exported="true" />
5259
</application>
5360

5461
</manifest>

app/src/main/java/space/itoncek/trailcompass/app/hideandseek/MainmenuHideandseekLogin.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)