-
Notifications
You must be signed in to change notification settings - Fork 103
feat: Add REST API and Web UI for remote time tracking #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
av1m
wants to merge
1
commit into
Razeeman:dev
Choose a base branch
from
av1m:web
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
app/src/play/java/com/example/util/simpletimetracker/api/WebApiAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package com.example.util.simpletimetracker.api | ||
|
|
||
| import com.example.util.simpletimetracker.wear_api.WearCommunicationAPI | ||
| import com.example.util.simpletimetracker.wear_api.WearStartActivityRequest | ||
| import com.example.util.simpletimetracker.wear_api.WearStopActivityRequest | ||
| import fi.iki.elonen.NanoHTTPD | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
| import javax.inject.Inject | ||
|
|
||
| class WebApiAdapter @Inject constructor( | ||
| // Reuse the SAME interface that Wear OS uses! | ||
| private val wearApi: WearCommunicationAPI, | ||
| ) : NanoHTTPD(8080) { | ||
|
|
||
| override fun serve(session: IHTTPSession): Response { | ||
| val headers = mutableMapOf( | ||
| "Access-Control-Allow-Origin" to "*", | ||
| "Access-Control-Allow-Methods" to "GET, POST, OPTIONS", | ||
| "Access-Control-Allow-Headers" to "Content-Type" | ||
| ) | ||
|
|
||
| if (session.method == Method.OPTIONS) { | ||
| return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "") | ||
| .apply { headers.forEach { (k, v) -> addHeader(k, v) } } | ||
| } | ||
|
|
||
| return try { | ||
| when { | ||
| // GET /api/activities | ||
| session.uri == "/api/activities" && session.method == Method.GET -> { | ||
| getAllActivities(headers) | ||
| } | ||
| // GET /api/running | ||
| session.uri == "/api/running" && session.method == Method.GET -> { | ||
| getRunningActivities(headers) | ||
| } | ||
| // POST /api/start/:id | ||
| session.uri.startsWith("/api/start/") && session.method == Method.POST -> { | ||
| val id = session.uri.substringAfterLast("/").toLongOrNull() | ||
| startActivity(id, headers) | ||
| } | ||
| // POST /api/stop/:id | ||
| session.uri.startsWith("/api/stop/") && session.method == Method.POST -> { | ||
| val id = session.uri.substringAfterLast("/").toLongOrNull() | ||
| stopActivity(id, headers) | ||
| } | ||
| else -> { | ||
| newFixedLengthResponse( | ||
| Response.Status.NOT_FOUND, | ||
| "application/json", | ||
| """{"error": "Not found"}""" | ||
| ).apply { headers.forEach { (k, v) -> addHeader(k, v) } } | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| newFixedLengthResponse( | ||
| Response.Status.INTERNAL_ERROR, | ||
| "application/json", | ||
| """{"error": "${e.message}"}""" | ||
| ).apply { headers.forEach { (k, v) -> addHeader(k, v) } } | ||
| } | ||
| } | ||
|
|
||
| private fun getAllActivities(headers: Map<String, String>): Response = runBlocking { | ||
| // Reuse the EXACT same method Wear OS uses! | ||
| val activities = wearApi.queryActivities() | ||
| val currentState = wearApi.queryCurrentActivities() | ||
| val runningIds = currentState.currentActivities.map { it.id }.toSet() | ||
|
|
||
| val json = JSONArray() | ||
| activities.forEach { activity -> | ||
| json.put(JSONObject().apply { | ||
| put("id", activity.id) | ||
| put("name", activity.name) | ||
| put("icon", activity.icon) | ||
| put("color", activity.color) | ||
| put("isRunning", runningIds.contains(activity.id)) | ||
| }) | ||
| } | ||
|
|
||
| newFixedLengthResponse( | ||
| Response.Status.OK, | ||
| "application/json", | ||
| json.toString() | ||
| ).apply { | ||
| headers.forEach { (k, v) -> addHeader(k, v) } | ||
| } | ||
| } | ||
|
|
||
| private fun getRunningActivities(headers: Map<String, String>): Response = runBlocking { | ||
| // Reuse the EXACT same method Wear OS uses! | ||
| val currentState = wearApi.queryCurrentActivities() | ||
| val activities = wearApi.queryActivities().associateBy { it.id } | ||
|
|
||
| val json = JSONArray() | ||
| currentState.currentActivities.forEach { current -> | ||
| val activity = activities[current.id] | ||
| json.put(JSONObject().apply { | ||
| put("id", current.id) | ||
| put("name", activity?.name ?: "Unknown") | ||
| put("timeStarted", current.startedAt) | ||
| put("duration", System.currentTimeMillis() - current.startedAt) | ||
| }) | ||
| } | ||
|
|
||
| newFixedLengthResponse( | ||
| Response.Status.OK, | ||
| "application/json", | ||
| json.toString() | ||
| ).apply { | ||
| headers.forEach { (k, v) -> addHeader(k, v) } | ||
| } | ||
| } | ||
|
|
||
| private fun startActivity(id: Long?, headers: Map<String, String>): Response = runBlocking { | ||
| if (id == null) { | ||
| return@runBlocking newFixedLengthResponse( | ||
| Response.Status.BAD_REQUEST, | ||
| "application/json", | ||
| """{"error": "Invalid ID"}""" | ||
| ) | ||
| } | ||
|
|
||
| // Reuse the EXACT same method Wear OS uses! | ||
| wearApi.startActivity(WearStartActivityRequest(id = id, tags = null)) | ||
|
|
||
| newFixedLengthResponse( | ||
| Response.Status.OK, | ||
| "application/json", | ||
| """{"success": true}""" | ||
| ).apply { | ||
| headers.forEach { (k, v) -> addHeader(k, v) } | ||
| } | ||
| } | ||
|
|
||
| private fun stopActivity(id: Long?, headers: Map<String, String>): Response = runBlocking { | ||
| if (id == null) { | ||
| return@runBlocking newFixedLengthResponse( | ||
| Response.Status.BAD_REQUEST, | ||
| "application/json", | ||
| """{"error": "Invalid ID"}""" | ||
| ) | ||
| } | ||
|
|
||
| // Reuse the EXACT same method Wear OS uses! | ||
| wearApi.stopActivity(WearStopActivityRequest(id = id)) | ||
|
|
||
| newFixedLengthResponse( | ||
| Response.Status.OK, | ||
| "application/json", | ||
| """{"success": true}""" | ||
| ).apply { | ||
| headers.forEach { (k, v) -> addHeader(k, v) } | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
app/src/play/java/com/example/util/simpletimetracker/api/WebApiModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.example.util.simpletimetracker.api | ||
|
|
||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object WebApiModule { | ||
| @Provides | ||
| @Singleton | ||
| fun provideWebApiAdapter( | ||
| wearApi: com.example.util.simpletimetracker.wear_api.WearCommunicationAPI | ||
| ): WebApiAdapter { | ||
| return WebApiAdapter(wearApi) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for this PR, looks very cool!
Unfortunately the only guarantee of data privacy is not having an internet permission. Is it possible to implement this feature without adding it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Razeeman,
I don't want to reply in place of @av1m, but I don't think this kind of feature is realistically doable without this permission. I'm no Android dev, but the documentation states that this permission is required to open a network socket, so any LAN communication would require this permission (unless using bluetooth or some other exotic communication channel I guess).
However, as this PR currently is, this feature would only be available in the play flavor of the app which nicely solves this "data privacy guarantee" conundrum. In my opinion, in the same way Wear OS support was handled & merged, this feature could be play flavor only. "Privacy wary" people could use or keep using the F-Droid version (which they probably already use), without this feature nor the internet permission in the manifest.
As far as I'm concerned, I understand how not asking for this permission is a quick and easy way to "guarantee" no data leakage over the internet. However, there are other ways to guarantee this (reviewing the source code, sandboxing the app, using something akin to https://reports.exodus-privacy.eu.org, etc.) and other way the data could be poorly handled / leaked. In my opinion, my data in this app is far from critical anyway, so I'm not asking for the highest level of security guarantees anyway. The usefulness of a very convenient feature like this one outweighs the potential privacy concerns. I also understand that as a maintainer and being the creator of the app, you may disagree :)
Cheers,
Florian