-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Replace deprecated jcenter by mavencentral * Add threading annotations and fix some lint warnings * Improve readability of Prefs writes * Wrap some exceptions for more helpful logging * Change visibility of most classes to internal and add logging control * Add Seconds to minimumSubmitInterval name for clarity * Use coroutines for threading * Don't throw non-fatal Exceptions, log them instead * Don't ignore submitinterval for debug builds * Close connection in httpPost, where it is created * Remove loglevel instruction from code snippet to other part of README.md * Clarify Q42StatsConfig property names
- Loading branch information
1 parent
bb675d9
commit 213a589
Showing
16 changed files
with
106 additions
and
84 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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
51 changes: 28 additions & 23 deletions
51
library/src/main/java/com/q42/q42stats/library/HttpService.kt
This file contains 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 |
---|---|---|
@@ -1,48 +1,53 @@ | ||
package com.q42.q42stats.library | ||
|
||
import org.json.JSONException | ||
import androidx.annotation.WorkerThread | ||
import org.json.JSONObject | ||
import java.io.BufferedWriter | ||
import java.io.IOException | ||
import java.io.OutputStreamWriter | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
import javax.net.ssl.HttpsURLConnection | ||
|
||
object HttpService { | ||
/** Synchronously send the stats. Make sure to run this on a worker thread */ | ||
@WorkerThread | ||
internal object HttpService { | ||
|
||
fun sendStatsSync(config: Q42StatsConfig, data: JSONObject) { | ||
sendStatsSync( | ||
"https://firestore.googleapis.com/v1/projects/${config.fireBaseProject}/" + | ||
"databases/(default)/documents/${config.firebaseCollection}?mask.fieldPaths=_", | ||
"https://firestore.googleapis.com/v1/projects/${config.firebaseProjectId}/" + | ||
"databases/(default)/documents/${config.firebaseCollectionId}?mask.fieldPaths=_", | ||
data | ||
) | ||
} | ||
|
||
/** Synchronously send the stats. Make sure to run this on a worker thread */ | ||
private fun sendStatsSync(url: String, data: JSONObject) { | ||
httpPost(url, data) | ||
} | ||
|
||
@Throws(IOException::class, JSONException::class) | ||
private fun httpPost(url: String, jsonObject: JSONObject) { | ||
val conn = URL(url).openConnection() as HttpsURLConnection | ||
conn.requestMethod = "POST" | ||
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8") | ||
setPostRequestContent(conn, jsonObject) | ||
conn.connect() | ||
Q42StatsLogger.d(TAG, "Response: ${conn.responseCode} ${conn.responseMessage}") | ||
try { | ||
conn.requestMethod = "POST" | ||
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8") | ||
sendPostRequestContent(conn, jsonObject) | ||
} catch (e: Throwable) { | ||
Q42StatsLogger.e(TAG, "Could not send stats to server", e) | ||
} finally { | ||
conn.disconnect() | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun setPostRequestContent(conn: HttpURLConnection, jsonObject: JSONObject) { | ||
|
||
val os = conn.outputStream | ||
val writer = BufferedWriter(OutputStreamWriter(os, "UTF-8")) | ||
writer.write(jsonObject.toString()) | ||
Q42StatsLogger.d(TAG, "Sending JSON: $jsonObject") | ||
writer.flush() | ||
writer.close() | ||
os.close() | ||
private fun sendPostRequestContent(conn: HttpURLConnection, jsonObject: JSONObject) { | ||
try { | ||
conn.outputStream.use { os -> | ||
BufferedWriter(OutputStreamWriter(os, "UTF-8")).use { writer -> | ||
writer.write(jsonObject.toString()) | ||
Q42StatsLogger.d(TAG, "Sending JSON: $jsonObject") | ||
writer.flush() | ||
} | ||
} | ||
Q42StatsLogger.d(TAG, "Response: ${conn.responseCode} ${conn.responseMessage}") | ||
} catch (e: Throwable) { | ||
Q42StatsLogger.e(TAG, "Could not add data to POST request", e) | ||
} | ||
} | ||
} |
This file contains 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
6 changes: 3 additions & 3 deletions
6
library/src/main/java/com/q42/q42stats/library/Q42StatsConfig.kt
This file contains 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package com.q42.q42stats.library | ||
|
||
data class Q42StatsConfig( | ||
val fireBaseProject: String, | ||
val firebaseCollection: String, | ||
val firebaseProjectId: String, | ||
val firebaseCollectionId: String, | ||
/** Data collection is skipped when less than this many seconds have passed | ||
* since the previous run */ | ||
val minimumSubmitInterval: Long | ||
val minimumSubmitIntervalSeconds: Long | ||
) |
This file contains 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 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 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.