Skip to content

Commit 8d98af2

Browse files
committed
fully functional
1 parent 1f44135 commit 8d98af2

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

app/src/main/java/com/atom/letmein/MainActivity.kt

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package com.atom.letmein
22

3+
import android.app.ActivityManager
34
import android.app.Notification
45
import android.app.NotificationChannel
56
import android.app.NotificationManager
7+
import android.app.PendingIntent
8+
import android.content.BroadcastReceiver
69
import android.content.Context
10+
import android.content.Intent
711
import android.graphics.BitmapFactory
812
import android.graphics.Color
913
import android.os.Build
1014
import androidx.appcompat.app.AppCompatActivity
1115
import android.os.Bundle
1216
import android.view.View
17+
import android.widget.Button
1318
import android.widget.EditText
1419
import androidx.appcompat.app.AlertDialog
20+
import androidx.core.app.NotificationCompat
1521
import com.beust.klaxon.Json
1622
import org.java_websocket.client.WebSocketClient
1723
import org.java_websocket.handshake.ServerHandshake
@@ -22,15 +28,18 @@ import com.beust.klaxon.*
2228
private val klaxon = Klaxon()
2329

2430
class 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-
200250
data class ServerResponse (
201251
@Json(name = "ID")
202252
val id: String,

app/src/main/res/layout/activity_main.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@
9696
android:layout_centerHorizontal="true"
9797
android:layout_below="@id/northButton"
9898
android:onClick="reqSouthStairwell"/>
99+
100+
<Button
101+
android:id="@+id/cancelButton"
102+
android:layout_width="match_parent"
103+
android:layout_height="wrap_content"
104+
android:layout_below="@id/southButton"
105+
android:layout_centerHorizontal="true"
106+
android:backgroundTint="@color/secondary"
107+
android:onClick="sendNVM"
108+
android:text="@string/cancel"
109+
android:visibility="invisible"/>
99110
</RelativeLayout>
100111

101112
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
<string name="req_ack_exp">Someone answered your request! They will be arriving shortly.</string>
2020
<string name="req_error">Connection failed! 🔴</string>
2121
<string name="req_error_exp">Unfortunately, there was an error in your request. Please try again later.</string>
22+
<string name="req_cancel">Request cancelled! 🟡</string>
23+
<string name="req_cancel_exp">Your request was cancelled! If you believe this was by mistake, try again.</string>
2224
</resources>

0 commit comments

Comments
 (0)