Skip to content

Commit

Permalink
fixed receive mms
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiragKalra committed Sep 18, 2020
1 parent 593c17c commit c04db76
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 276 deletions.
100 changes: 0 additions & 100 deletions MMSHandler.java

This file was deleted.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dependencies {

implementation 'com.mikhaellopez:circularprogressbar:3.0.3'
implementation 'io.michaelrocks:libphonenumber-android:8.12.6'
implementation 'com.squareup.picasso:picasso:2.71828'

implementation 'com.klinkerapps:android-smsmms:5.2.5'
}
182 changes: 182 additions & 0 deletions app/src/main/java/com/bruhascended/sms/data/IncomingMMSManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.bruhascended.sms.data

import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import android.webkit.MimeTypeMap
import androidx.room.Room
import com.bruhascended.sms.db.Conversation
import com.bruhascended.sms.db.ConversationDatabase
import com.bruhascended.sms.db.Message
import com.bruhascended.sms.db.MessageDatabase
import com.bruhascended.sms.ui.conversationDao
import com.bruhascended.sms.ui.conversationSender
import com.bruhascended.sms.ui.isMainViewModelNull
import com.bruhascended.sms.ui.main.MainViewModel
import com.bruhascended.sms.ui.mainViewModel
import java.io.*


class IncomingMMSManager(private val context: Context) {
private var date: Long = 0

@SuppressLint("Recycle")
private fun getAddressNumber(id: String): String? {
val selection = "type=137 AND msg_id=$id"
val uriAddress = Uri.parse("content://mms/${id}/addr")
val cursor = context.contentResolver.query(
uriAddress, arrayOf("address"), selection, null, null
)!!
var address = ""
if (cursor.moveToFirst()) {
do {
address = cursor.getString(cursor.getColumnIndex("address"))
if (address != null) break
} while (cursor.moveToNext())
}
cursor.close()
return address
}

private fun getMmsText(id: String): String {
val partURI = Uri.parse("content://mms/part/$id")
val inp: InputStream? = context.contentResolver.openInputStream(partURI)
val sb = StringBuilder()
if (inp != null) {
val isr = InputStreamReader(inp, "UTF-8")
val reader = BufferedReader(isr)
var temp: String? = reader.readLine()
while (temp != null) {
sb.append(temp)
temp = reader.readLine()
}
}
return sb.toString()
}

private fun saveFile(_id: String, typeString: String): String {
val partURI = Uri.parse("content://mms/part/$_id")
val ext = MimeTypeMap.getSingleton().getExtensionFromMimeType(typeString)
val name = "$date.$ext"
val destination = File(context.filesDir, name)
val output = FileOutputStream(destination)
val input = context.contentResolver.openInputStream(partURI) ?: return ""
val buffer = ByteArray(4 * 1024)
var read: Int
while (input.read(buffer).also { read = it } != -1) {
output.write(buffer, 0, read)
}
output.flush()
return destination.absolutePath
}

@SuppressLint("Recycle")
fun putMMS(uri: Uri) : Pair<Message, Conversation> {
date = System.currentTimeMillis()
val mmsId = uri.toString().split("/").last()

val selectionPart = "mid=$mmsId"
val partUri = Uri.parse("content://mms/part")
val cursor = context.contentResolver.query(
partUri, null,
selectionPart, null, null
)!!
var body = ""
var file: String? = null
if (cursor.moveToFirst()) {
do {
val partId: String = cursor.getString(cursor.getColumnIndex("_id"))
val typeString = cursor.getString(cursor.getColumnIndex("ct"))
if ("text/plain" == typeString) {
body = getMmsText(partId)
} else if (
typeString.startsWith("video") ||
typeString.startsWith("image") ||
typeString.startsWith("audio")
) {
file = saveFile(partId, typeString)
}
} while (cursor.moveToNext())
}
cursor.close()

val sender = getAddressNumber(mmsId)!!

val rawNumber = ContactsManager(context).getRaw(sender)
val senderNameMap = ContactsManager(context).getContactsHashMap()

val message = Message(
null,
sender,
body,
1,
date,
0,
false,
file
)

if (isMainViewModelNull()) {
mainViewModel = MainViewModel()
mainViewModel.daos = Array(6){
Room.databaseBuilder(
context, ConversationDatabase::class.java,
context.resources.getString(labelText[it])
).allowMainThreadQueries().build().manager()
}
}

var conversation: Conversation? = null
for (i in 0..4) {
val got = mainViewModel.daos[i].findBySender(rawNumber)
if (got.isNotEmpty()) {
conversation = got.first()
for (item in got.slice(1 until got.size))
mainViewModel.daos[i].delete(item)
break
}
}

conversation = if (conversation != null) {
conversation.apply {
read = false
time = message.time
lastSMS = message.text
lastMMS = true
label = 0
forceLabel = 0
name = senderNameMap[rawNumber]
mainViewModel.daos[0].update(this)
}
conversation
} else {
val con = Conversation(
null,
rawNumber,
senderNameMap[rawNumber],
"",
false,
message.time,
message.text,
0,
0,
FloatArray(5) {
if (it == 0) 1f else 0f
},
lastMMS = true
)
mainViewModel.daos[0].insert(con)
con.id = mainViewModel.daos[0].findBySender(rawNumber).first().id
con
}

val mdb = if (conversationSender == rawNumber) conversationDao
else Room.databaseBuilder(
context, MessageDatabase::class.java, rawNumber
).build().manager()

mdb.insert(message)

return mdb.search(message.time).first() to conversation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class IncomingSMSManager(context: Context) {
prediction,
-1,
FloatArray(5) {
if (it == 0) 1f else 0f
if (it == prediction) 1f else 0f
}
)
mainViewModel.daos[prediction].insert(con)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package com.bruhascended.sms.services

import android.content.Context
import android.net.Uri
import com.bruhascended.sms.data.IncomingMMSManager
import com.bruhascended.sms.ui.MessageNotificationManager
import com.klinker.android.send_message.MmsReceivedReceiver


class MMSReceiver : MmsReceivedReceiver() {

override fun onMessageReceived(context: Context, uri: Uri) {
val k = 1
val p = 1+k
MessageNotificationManager(context).sendSmsNotification(IncomingMMSManager(context).putMMS(uri))
}

override fun onError(p0: Context?, p1: String?) {
TODO("Not yet implemented")
}
override fun onError(p0: Context?, p1: String?) {}
}
55 changes: 29 additions & 26 deletions app/src/main/java/com/bruhascended/sms/ui/MediaPreviewManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,38 @@ class MediaPreviewManager(
playPauseButton.visibility = View.VISIBLE
mp.apply {
setDataSource(mActivity, mmsURI)
prepare()
seekBar.max = mp.duration / 500
prepareAsync()
setOnPreparedListener {
seekBar.max = mp.duration / 500

val mHandler = Handler(mActivity.mainLooper)
mActivity.runOnUiThread(object : Runnable {
override fun run() {
seekBar.progress = currentPosition / 500
mHandler.postDelayed(this, 500)
}
})
val mHandler = Handler(mActivity.mainLooper)
mActivity.runOnUiThread(object : Runnable {
override fun run() {
seekBar.progress = currentPosition / 500
mHandler.postDelayed(this, 500)
}
})

seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(
seekBar: SeekBar, progress: Int, fromUser: Boolean
) {
if (fromUser) seekTo(progress * 500)
}
})
seekBar.setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(
seekBar: SeekBar, progress: Int, fromUser: Boolean
) {
if (fromUser) seekTo(progress * 500)
}
})

playPauseButton.apply {
setOnClickListener {
if (isPlaying) {
pause()
setImageResource(R.drawable.ic_play)
} else {
start()
setImageResource(R.drawable.ic_pause)
playPauseButton.apply {
setOnClickListener {
if (isPlaying) {
pause()
setImageResource(R.drawable.ic_play)
} else {
start()
setImageResource(R.drawable.ic_pause)
}
}
}
}
Expand Down
Loading

0 comments on commit c04db76

Please sign in to comment.