11package com.atom.letmein
22
3- import android.app.ActivityManager
43import android.app.Notification
54import android.app.NotificationChannel
65import android.app.NotificationManager
76import android.app.PendingIntent
8- import android.content.BroadcastReceiver
97import android.content.Context
108import android.content.Intent
119import android.graphics.BitmapFactory
@@ -17,7 +15,6 @@ import android.view.View
1715import android.widget.Button
1816import android.widget.EditText
1917import androidx.appcompat.app.AlertDialog
20- import androidx.core.app.NotificationCompat
2118import com.beust.klaxon.Json
2219import org.java_websocket.client.WebSocketClient
2320import org.java_websocket.handshake.ServerHandshake
@@ -29,9 +26,9 @@ private val klaxon = Klaxon()
2926
3027class MainActivity : AppCompatActivity () {
3128 // Declaring the notification items
32- lateinit var notificationManager: NotificationManager
33- lateinit var notificationChannel: NotificationChannel
34- lateinit var builder: Notification .Builder
29+ private lateinit var notificationManager: NotificationManager
30+ private lateinit var notificationChannel: NotificationChannel
31+ private lateinit var builder: Notification .Builder
3532 private val channelId = " i.apps.notifications"
3633
3734 // Declaring websocket items
@@ -50,7 +47,7 @@ class MainActivity : AppCompatActivity() {
5047 private fun initWebSocket (location : String ) {
5148 val hostString = getString(R .string.wsURI)
5249 val wsURL = " wss://$hostString /knock/socket/$location "
53- val wsURI: URI ? = URI (wsURL)
50+ val wsURI = URI (wsURL)
5451
5552 // Verify that name is filled in
5653 if (findViewById<EditText >(R .id.userName).text.isEmpty()) {
@@ -87,21 +84,21 @@ class MainActivity : AppCompatActivity() {
8784 println (response?.toJson())
8885 val id = response?.id?.split(" _" )?.last()?.toInt()
8986 id?.let {
90- when (response? .event) {
87+ when (response.event) {
9188 " LOCATION" -> {
9289 if (idWS == 0 ) {
9390 idWS = it
94- sendPermanentPush(getString(R .string.req_sent), " ${getString(R .string.req_sent_exp)} (${response? .currentTime} s)" , it)
91+ sendPermanentPush(getString(R .string.req_sent), " ${getString(R .string.req_sent_exp)} (${response.currentTime} s)" , it)
9592 }
9693 }
97- " COUNTDOWN" -> updatePush(" ${getString(R .string.req_sent_exp)} (${response? .currentTime} s)" , it)
94+ " COUNTDOWN" -> updatePush(" ${getString(R .string.req_sent_exp)} (${response.currentTime} s)" , it)
9895 " TIMEOUT" -> {
9996 sendTemporaryPush(getString(R .string.req_timeout), getString(R .string.req_timeout_exp))
100- this .close()
97+ this .close(0 , " Request timed out " )
10198 }
10299 " ACKNOWLEDGE" -> {
103100 sendTemporaryPush(getString(R .string.req_ack), getString(R .string.req_ack_exp))
104- this .close()
101+ this .close(0 , " Request was acknowledged " )
105102 }
106103 }
107104 }
@@ -122,7 +119,7 @@ class MainActivity : AppCompatActivity() {
122119 sendTemporaryPush(getString(R .string.req_error), getString(R .string.req_error_exp), 2024 )
123120 idWS = 0
124121 runOnUiThread { findViewById<Button >(R .id.cancelButton).visibility = View .INVISIBLE }
125- close()
122+ close(0 , " Ran into an error! " )
126123 }
127124 }
128125 }
@@ -142,7 +139,7 @@ class MainActivity : AppCompatActivity() {
142139 // Add intent to return to application
143140 val actIntent = Intent (this , MainActivity ::class .java).apply {
144141 flags = Intent .FLAG_ACTIVITY_CLEAR_TOP or Intent .FLAG_ACTIVITY_SINGLE_TOP
145- setAction( Intent .ACTION_MAIN )
142+ action = Intent .ACTION_MAIN
146143 addCategory(Intent .CATEGORY_LAUNCHER )
147144 }
148145 val pendingIntent: PendingIntent = PendingIntent .getActivity(this , 0 , actIntent,
@@ -177,7 +174,7 @@ class MainActivity : AppCompatActivity() {
177174 // Add intent to return to application
178175 val actIntent = Intent (this , MainActivity ::class .java).apply {
179176 flags = Intent .FLAG_ACTIVITY_CLEAR_TOP or Intent .FLAG_ACTIVITY_SINGLE_TOP
180- setAction( Intent .ACTION_MAIN )
177+ action = Intent .ACTION_MAIN
181178 addCategory(Intent .CATEGORY_LAUNCHER )
182179 }
183180 val pendingIntent: PendingIntent = PendingIntent .getActivity(this , 0 , actIntent,
@@ -206,15 +203,15 @@ class MainActivity : AppCompatActivity() {
206203 println (" running nvm" )
207204 if (this ::webSocketClient.isInitialized) {
208205 if (webSocketClient.isOpen) {
209- val payload = """ {"Event":"NEVERMIND", "LOCATION": "${ locationWS} "}"""
206+ val payload = """ {"Event":"NEVERMIND", "LOCATION": "$locationWS "}"""
210207 webSocketClient.send(payload)
211- webSocketClient.close()
208+ webSocketClient.close(0 , " Request cancelled " )
212209 sendTemporaryPush(getString(R .string.req_cancel), getString(R .string.req_cancel_exp), 2024 )
213210 }
214211 }
215212 }
216213
217- fun showAlert (title : String , description : String ) {
214+ private fun showAlert (title : String , description : String ) {
218215 val builder = AlertDialog .Builder (this )
219216 builder.setTitle(title)
220217 builder.setMessage(description)
@@ -276,9 +273,9 @@ data class ServerResponse (
276273 val slackMessageTS : String
277274)
278275{
279- public fun toJson () = klaxon.toJsonString(this )
276+ fun toJson () = klaxon.toJsonString(this )
280277
281278 companion object {
282- public fun fromJson (json : String ) = klaxon.parse<ServerResponse >(json)
279+ fun fromJson (json : String ) = klaxon.parse<ServerResponse >(json)
283280 }
284281}
0 commit comments