11package com.atom.letmein
22
3+ import android.app.ActivityManager
34import android.app.Notification
45import android.app.NotificationChannel
56import android.app.NotificationManager
7+ import android.app.PendingIntent
8+ import android.content.BroadcastReceiver
69import android.content.Context
10+ import android.content.Intent
711import android.graphics.BitmapFactory
812import android.graphics.Color
913import android.os.Build
1014import androidx.appcompat.app.AppCompatActivity
1115import android.os.Bundle
1216import android.view.View
17+ import android.widget.Button
1318import android.widget.EditText
1419import androidx.appcompat.app.AlertDialog
20+ import androidx.core.app.NotificationCompat
1521import com.beust.klaxon.Json
1622import org.java_websocket.client.WebSocketClient
1723import org.java_websocket.handshake.ServerHandshake
@@ -22,15 +28,18 @@ import com.beust.klaxon.*
2228private val klaxon = Klaxon ()
2329
2430class MainActivity : AppCompatActivity () {
31+ // Declaring the notification items
2532 lateinit var notificationManager: NotificationManager
2633 lateinit var notificationChannel: NotificationChannel
2734 lateinit var builder: Notification .Builder
2835 private val channelId = " i.apps.notifications"
2936
37+ // Declaring websocket items
3038 private lateinit var webSocketClient: WebSocketClient
31- companion object {
32- const val TAG = " LETMEIN"
33- }
39+ private var locationWS: String = " "
40+ private var idWS: Int = 0
41+
42+ // Function called on creation of activity
3443 override fun onCreate (savedInstanceState : Bundle ? ) {
3544 super .onCreate(savedInstanceState)
3645 setContentView(R .layout.activity_main)
@@ -43,10 +52,13 @@ class MainActivity : AppCompatActivity() {
4352 val wsURL = " wss://$hostString /knock/socket/$location "
4453 val wsURI: URI ? = URI (wsURL)
4554
55+ // Verify that name is filled in
4656 if (findViewById<EditText >(R .id.userName).text.isEmpty()) {
4757 showAlert(" Error" , " Missing name!" )
4858 return
4959 }
60+
61+ // Verify that web socket isn't in use
5062 if (this ::webSocketClient.isInitialized) {
5163 if (webSocketClient.isOpen) {
5264 showAlert(" Error" , " There is already a request running!" )
@@ -60,13 +72,13 @@ class MainActivity : AppCompatActivity() {
6072
6173 private fun createWebSocketClient (wsURI : URI ? , location : String ) {
6274 webSocketClient = object : WebSocketClient (wsURI) {
63- var idWS : Int = 0
75+
6476 override fun onOpen (handshakedata : ServerHandshake ? ) {
6577 println (" Connection opened!" )
6678 val name = findViewById<EditText >(R .id.userName).text
6779 val payload = """ {"Event": "NAME", "Name": "$name ", "Location": "$location "}"""
6880 send(payload)
69- // TODO("link nvm button")
81+ runOnUiThread { findViewById< Button >( R .id.cancelButton).visibility = View . VISIBLE }
7082 }
7183
7284 override fun onMessage (message : String? ) {
@@ -99,13 +111,18 @@ class MainActivity : AppCompatActivity() {
99111 override fun onClose (code : Int , reason : String? , remote : Boolean ) {
100112 println (" Connection was closed! Reason: $reason " )
101113 notificationManager.cancel(idWS)
114+ idWS = 0
115+ runOnUiThread { findViewById<Button >(R .id.cancelButton).visibility = View .INVISIBLE }
102116 }
103117
104118 override fun onError (ex : Exception ? ) {
105119 println (" Connection ran into an error: ${ex.toString()} " )
106120 ex?.printStackTrace()
107121 notificationManager.cancel(idWS)
108122 sendTemporaryPush(getString(R .string.req_error), getString(R .string.req_error_exp), 2024 )
123+ idWS = 0
124+ runOnUiThread { findViewById<Button >(R .id.cancelButton).visibility = View .INVISIBLE }
125+ close()
109126 }
110127 }
111128 }
@@ -121,13 +138,24 @@ class MainActivity : AppCompatActivity() {
121138 } else {
122139 builder = Notification .Builder (this )
123140 }
141+
142+ // Add intent to return to application
143+ val actIntent = Intent (this , MainActivity ::class .java).apply {
144+ flags = Intent .FLAG_ACTIVITY_CLEAR_TOP or Intent .FLAG_ACTIVITY_SINGLE_TOP
145+ setAction(Intent .ACTION_MAIN )
146+ addCategory(Intent .CATEGORY_LAUNCHER )
147+ }
148+ val pendingIntent: PendingIntent = PendingIntent .getActivity(this , 0 , actIntent,
149+ PendingIntent .FLAG_UPDATE_CURRENT )
150+
124151 builder.setSmallIcon(R .drawable.csh_logo)
125152 .setLargeIcon(BitmapFactory .decodeResource(this .resources, R .drawable.csh_logo))
126153 .setContentTitle(subject)
127154 .setContentText(description)
128155 .setAutoCancel(false )
129156 .setOngoing(true )
130157 .setOnlyAlertOnce(true )
158+ .setContentIntent(pendingIntent)
131159 .setColor(Color .rgb(55 , 0 , 179 ))
132160 .style = Notification .BigTextStyle ()
133161 .bigText(description)
@@ -145,10 +173,22 @@ class MainActivity : AppCompatActivity() {
145173 } else {
146174 builder = Notification .Builder (this )
147175 }
176+
177+ // Add intent to return to application
178+ val actIntent = Intent (this , MainActivity ::class .java).apply {
179+ flags = Intent .FLAG_ACTIVITY_CLEAR_TOP or Intent .FLAG_ACTIVITY_SINGLE_TOP
180+ setAction(Intent .ACTION_MAIN )
181+ addCategory(Intent .CATEGORY_LAUNCHER )
182+ }
183+ val pendingIntent: PendingIntent = PendingIntent .getActivity(this , 0 , actIntent,
184+ PendingIntent .FLAG_UPDATE_CURRENT )
185+
148186 builder.setSmallIcon(R .drawable.csh_logo)
149187 .setLargeIcon(BitmapFactory .decodeResource(this .resources, R .drawable.csh_logo))
150188 .setContentTitle(subject)
151189 .setContentText(description)
190+ .setContentIntent(pendingIntent)
191+ .setAutoCancel(true )
152192 .setColor(Color .rgb(55 , 0 , 179 ))
153193 .style = Notification .BigTextStyle ()
154194 .bigText(description)
@@ -162,6 +202,17 @@ class MainActivity : AppCompatActivity() {
162202 notificationManager.notify(code, builder.build())
163203 }
164204
205+ fun sendNVM (view : View ) {
206+ println (" running nvm" )
207+ if (this ::webSocketClient.isInitialized) {
208+ if (webSocketClient.isOpen) {
209+ val payload = """ {"Event":"NEVERMIND", "LOCATION": "${locationWS} "}"""
210+ webSocketClient.send(payload)
211+ webSocketClient.close()
212+ sendTemporaryPush(getString(R .string.req_cancel), getString(R .string.req_cancel_exp), 2024 )
213+ }
214+ }
215+ }
165216
166217 fun showAlert (title : String , description : String ) {
167218 val builder = AlertDialog .Builder (this )
@@ -196,7 +247,6 @@ class MainActivity : AppCompatActivity() {
196247 }
197248}
198249
199-
200250data class ServerResponse (
201251 @Json(name = " ID" )
202252 val id : String ,
0 commit comments