Skip to content

Commit

Permalink
Merge pull request #2 from DareAngeL/test
Browse files Browse the repository at this point in the history
Bug Fixes
  • Loading branch information
DareAngeL committed Mar 5, 2023
2 parents 918e22a + a64de85 commit 7e5ef6c
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 59 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
compileSdk 33

defaultConfig {
applicationId "com.dareangel.tmessager"
applicationId 'com.dareangel.tmessager'
minSdk 21
targetSdk 33
versionCode 1
Expand Down
Binary file removed app/release/TMessager-Eziel.apk
Binary file not shown.
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<application
android:allowBackup="true"
android:name="TMessagerApp"
android:name="com.dareangel.tmessager.TMessagerApp"
android:hardwareAccelerated="true"
android:usesCleartextTraffic="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -21,7 +21,7 @@
android:theme="@style/Theme.TMessager"
tools:targetApi="31">
<activity
android:name=".ui.view.MainActivity"
android:name="com.dareangel.tmessager.ui.view.MainActivity"
android:windowSoftInputMode="adjustPan|stateHidden"
android:hardwareAccelerated="true"
android:exported="true">
Expand All @@ -33,7 +33,7 @@
</activity>

<service
android:name=".data.database.SocketService"
android:name="com.dareangel.tmessager.data.database.SocketService"
android:enabled="true"
android:exported="false" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ object DBConstants {
val TABLE_NAME = "RENE_TABLE"

// COLUMNS
val ID = "ID"
val POSITION = "POSITION" // message position in the database
val MESSAGE = "MESSAGE"
val SENDER = "SENDER" // user or chat mate
Expand All @@ -12,6 +13,7 @@ object DBConstants {
val CREATE_MSGS_TABLE = "create table $TABLE_NAME " +
"(" +
"$POSITION INTEGER PRIMARY KEY, " +
"$ID TEXT NOT NULL, " +
"$MESSAGE TEXT NOT NULL, " +
"$SENDER TEXT NOT NULL, " +
"$STATUS TEXT NOT NULL" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MessagesDB : IMessagesTable {
val cursor = mDatabase.query(
DBConstants.TABLE_NAME,
arrayOf(
DBConstants.ID,
DBConstants.POSITION, DBConstants.MESSAGE,
DBConstants.SENDER, DBConstants.STATUS
),
Expand All @@ -71,6 +72,7 @@ class MessagesDB : IMessagesTable {
cursor.moveToFirst()
for (i in 0 until cursor.count) {
this@apply.add(Message(
cursor.getString(cursor.getColumnIndexOrThrow(DBConstants.ID)),
cursor.getString(cursor.getColumnIndexOrThrow(DBConstants.MESSAGE)),
cursor.getString(cursor.getColumnIndexOrThrow(DBConstants.SENDER)),
cursor.getInt(cursor.getColumnIndexOrThrow(DBConstants.POSITION)),
Expand All @@ -94,6 +96,7 @@ class MessagesDB : IMessagesTable {
mCoroutineScope.launch(Dispatchers.IO) {
ContentValues().apply {
put(DBConstants.POSITION, msg.pos)
put(DBConstants.ID, msg.id)
put(DBConstants.MESSAGE, msg.msg)
put(DBConstants.SENDER, msg.sender)
put(DBConstants.STATUS, msg.status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import android.app.*
import android.content.Context
import android.content.Intent
import android.os.*
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.dareangel.tmessager.R
import com.dareangel.tmessager.ui.view.ChatHandler
import com.dareangel.tmessager.ui.view.bubblechat.BubbleChatWindow
import com.dareangel.tmessager.R
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.socket.client.IO
Expand Down Expand Up @@ -94,6 +95,7 @@ class SocketService: Service() {
)

val pos = (map["pos"] as Double).toInt()
val id = map["id"] as String
val _msg = map["msg"] as String
val status = map["status"] as String

Expand All @@ -102,7 +104,7 @@ class SocketService: Service() {

// sends the message to the other peer
if (status != com.dareangel.tmessager.data.model.Message.NOT_SENT) {
sendMessage(_msg, pos+1)
sendMessage(id, _msg, pos+1, com.dareangel.tmessager.data.model.Message.SENDING)
}
}
DELETE_MSGS_CODE -> {
Expand Down Expand Up @@ -160,7 +162,7 @@ class SocketService: Service() {

// init the socket io
try {
mSocket = IO.socket(getString(R.string.server)/*"http://192.168.254.105:2022"*/)
mSocket = IO.socket(getString(R.string.server)) /*"http://192.168.254.105:2022"*/
_initSocketEvents()
} catch (e: Exception) {
throw Exception(e.message)
Expand Down Expand Up @@ -213,13 +215,17 @@ class SocketService: Service() {
}, 1000)
}

mSocket.on(Socket.EVENT_DISCONNECT) {
mConnected = false
}

mSocket.on(Socket.EVENT_CONNECT_ERROR) {
val errorBuilder = StringBuilder()
// val errorBuilder = StringBuilder()
mConnected = false

for (i in it.indices) {
errorBuilder.append(it[i])
}
// for (i in it.indices) {
// errorBuilder.append(it[i])
// }

toJSONSend(CONN_ERROR_CODE)
}
Expand Down Expand Up @@ -297,8 +303,8 @@ class SocketService: Service() {
* @param msg The message to be sent
* @param position The position of the message on the recyclerview.
*/
fun sendMessage(msg : String, position: Int) {
mSocket.emit(SEND_MSG, ROOM_NAME, ChatHandler.USER, msg, position.toString())
fun sendMessage(id: String, msg : String, position: Int, status: String) {
mSocket.emit(SEND_MSG, ROOM_NAME, id, ChatHandler.USER, msg, position.toString(), status)
}

/**
Expand Down Expand Up @@ -366,7 +372,6 @@ class SocketService: Service() {
val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
return notificationBuilder.setOngoing(true)
.setContentTitle("TMessager is running")
.setContentText("You are online")
.setSmallIcon(R.drawable.appicon)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MessagesDBTableHelper(

companion object {
val DB_NAME = "MESSAGES.DB"
val DB_VERSION = 1
val DB_VERSION = 2
}

override fun onCreate(db: SQLiteDatabase?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dareangel.tmessager.data.model

import java.util.UUID

/**
* The model for the message
* @param sender who is the sender of the message
Expand All @@ -8,6 +10,7 @@ package com.dareangel.tmessager.data.model
* @param status the status of the message, either sending/sent or not sent
*/
data class Message(
val id: String? = null,
val msg: String? = null,
val sender: String? = null,
val pos: Int? = null,
Expand All @@ -32,5 +35,7 @@ data class Message(
enum class Type {
RESEND, SEND
}

fun getUniqueID() : String = UUID.randomUUID().toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.*
import android.util.Log
import com.dareangel.tmessager.data.database.MessagesDB
import com.dareangel.tmessager.data.database.SocketService
import com.dareangel.tmessager.data.model.interfaces.IServerListener
Expand Down Expand Up @@ -62,6 +63,7 @@ class DataManager(private val invoker: String) {
when (msg.what) {
// called when the client is connected to the server
SocketService.CONNECTED_CODE -> {
connected = true
mSocketListener.onConnect(true)
}
// called when the client connection is error
Expand Down Expand Up @@ -229,6 +231,7 @@ class DataManager(private val invoker: String) {
*/
fun sendMessage(
from: Int,
id: String,
_msg : String,
position: Int,
status: String,
Expand All @@ -237,6 +240,7 @@ class DataManager(private val invoker: String) {
) {
val map = HashMap<String, Any>().apply {
put("from", from)
put("id", id)
put("msg", _msg)
put("pos", position)
put("type", type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SoundPlayer(
private val mSoundPool = SoundPool.Builder()
.setMaxStreams(2)
.setAudioAttributes(AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
Expand Down
37 changes: 28 additions & 9 deletions app/src/main/java/com/dareangel/tmessager/ui/view/ChatHandler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dareangel.tmessager.ui.view

import android.content.Context
import android.util.Log
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.content.res.AppCompatResources
Expand All @@ -17,6 +18,7 @@ import com.google.gson.internal.LinkedTreeMap
import com.google.gson.reflect.TypeToken
import io.socket.emitter.Emitter
import kotlinx.coroutines.*
import okhttp3.internal.Util
import org.json.JSONArray
import org.json.JSONObject

Expand Down Expand Up @@ -93,12 +95,18 @@ class ChatHandler(
}

private fun _addMessageFromChatmate(msgJson: JSONObject, isAddToDB: Boolean = true) {
addMessage("", msgJson.getString("MSG"), "", isAddToDB)
addMessage(
msgJson.getString("ID"),
"",
msgJson.getString("MSG"),
"",
isAddToDB
)
}

fun addMessage(sender: String, msg: String, status: String?, isAddToDB: Boolean) {
fun addMessage(id: String, sender: String, msg: String, status: String?, isAddToDB: Boolean) {
val pos = mMessagesAdapter!!.rawDataSize
val _msg = Message(msg, sender, pos, status)
val _msg = Message(id, msg, sender, pos, status)

mMessagesAdapter!!.appendMessage(mDataManager!!, _msg, isAddToDB)
msgRecyclerview!!.smoothScrollToPosition(mMessagesAdapter!!.itemCount-1)
Expand Down Expand Up @@ -224,7 +232,7 @@ class ChatHandler(
}
}

fun updateAdapterOnReceived(msgJSON: JSONObject) {
private fun updateAdapterOnReceived(msgJSON: JSONObject) {
val pos = msgJSON.getString("POS").toInt()
// gets the position of the message at the data level
val posAtDataLevel = mMessagesAdapter?.getPosition(pos, MessagesAdapter.PositionType.DATA_POSITION)!!
Expand Down Expand Up @@ -257,18 +265,29 @@ class ChatHandler(
checkUnseenMessagesFromServer(it)
}

fun checkUnseenMessagesFromServer(it: Array<Any>) {
/**
* Invoked by onEnteredRoom function
*/
private fun checkUnseenMessagesFromServer(it: Array<Any>) {
mCoroutineScope.launch(Dispatchers.IO) {
val msgs = JSONObject(it[0] as String) // JSONArray(it[0] as String)
val reneUnseenMsgs = msgs.getJSONArray("Rene")
val ezielUnseenMsgs = msgs.getJSONArray("Eziel")

if (USER.contains("Rene")) {
if (reneUnseenMsgs.length() > 0)
onMessageReceived(arrayOf(reneUnseenMsgs.toString()))
if (reneUnseenMsgs.length() > 0) {
val msgObj = reneUnseenMsgs[0] as JSONObject
if (msgObj.getString("STAT").equals(Message.SEEN)) {
onMessageReceived(arrayOf(reneUnseenMsgs.toString()))
}
}
} else {
if (ezielUnseenMsgs.length() > 0)
onMessageReceived(arrayOf(ezielUnseenMsgs.toString()))
if (ezielUnseenMsgs.length() > 0) {
val msgObj = ezielUnseenMsgs[0] as JSONObject
if (msgObj.getString("STAT").equals(Message.SEEN)) {
onMessageReceived(arrayOf(ezielUnseenMsgs.toString()))
}
}
}

val chatmateUnseenMsgs: JSONArray = if (USER.contains("Rene")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.appcompat.app.AppCompatActivity
import com.dareangel.tmessager.R
import com.dareangel.tmessager.databinding.ActivityMainBinding
import com.dareangel.tmessager.manager.DataManager
import com.dareangel.tmessager.ui.sound.SoundPlayer
import com.dareangel.tmessager.ui.view.fragments.ChatRoomFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder

Expand Down Expand Up @@ -116,7 +115,6 @@ class MainActivity : AppCompatActivity() {
}

override fun onStop() {
mDataManager.socket?.scheduleServiceDestroy()
mDataManager.socket?.unBind()
mDataManager.msgsDBTable.close(true)
finishAffinity()
Expand Down
Loading

0 comments on commit 7e5ef6c

Please sign in to comment.