Les MMS fonctionnent je crois
This commit is contained in:
parent
7678e79fee
commit
49830d3caf
|
@ -16,7 +16,7 @@ import java.lang.ref.WeakReference
|
|||
* Base de données de Norn Talk
|
||||
*/
|
||||
@Database(entities = arrayOf(Message::class, Member::class, Media::class,
|
||||
Conversation::class, Contact::class), version=5, exportSchema=false)
|
||||
Conversation::class, Contact::class), version=6, exportSchema=false)
|
||||
abstract class NornDatabase : RoomDatabase() {
|
||||
|
||||
companion object {
|
||||
|
|
|
@ -9,8 +9,9 @@ import android.arch.persistence.room.PrimaryKey
|
|||
* Représente un média
|
||||
*
|
||||
* @property type MIME type du média
|
||||
* @property data Contenu du média
|
||||
* @property uri Adresse du document
|
||||
* @property messageId Identifiant du message auquel le média est associé
|
||||
* @property name nom du document
|
||||
* @property position position du document dans le message
|
||||
*/
|
||||
@Entity(foreignKeys = arrayOf(ForeignKey(entity= Message::class,
|
||||
|
@ -18,8 +19,7 @@ import android.arch.persistence.room.PrimaryKey
|
|||
childColumns = arrayOf("messageId"),
|
||||
onDelete = ForeignKey.CASCADE)))
|
||||
data class Media(var type: String,
|
||||
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
|
||||
var data: ByteArray,
|
||||
var uri: String,
|
||||
@ColumnInfo(index=true)
|
||||
var messageId: Long,
|
||||
var name: String,
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package xyz.johnny.norntalk.messages
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
|
@ -59,8 +58,6 @@ class NornMMSReceiver : BroadcastReceiver() {
|
|||
val path = intent.getStringExtra(EXTRA_FILE_PATH)
|
||||
Log.d(TAG, path)
|
||||
|
||||
var errorMessage: String? = null
|
||||
|
||||
try {
|
||||
val mDownloadFile = File(path)
|
||||
val nBytes = mDownloadFile.length().toInt()
|
||||
|
@ -87,19 +84,15 @@ class NornMMSReceiver : BroadcastReceiver() {
|
|||
}
|
||||
|
||||
} catch (e: FileNotFoundException) {
|
||||
errorMessage = "MMS received, file not found exception"
|
||||
val errorMessage = "MMS received, file not found exception"
|
||||
Log.e(TAG, errorMessage, e)
|
||||
} catch (e: IOException) {
|
||||
errorMessage = "MMS received, io exception"
|
||||
val errorMessage = "MMS received, io exception"
|
||||
Log.e(TAG, errorMessage, e)
|
||||
}
|
||||
|
||||
handleHttpError(context, intent)
|
||||
DownloadManager.finishDownload(intent.getStringExtra(EXTRA_LOCATION_URL))
|
||||
|
||||
if (errorMessage != null) {
|
||||
onError(errorMessage)
|
||||
}
|
||||
}
|
||||
|
||||
fun createMessage(context: Context, pdu: RetrieveConf): NornMessage {
|
||||
|
@ -120,32 +113,22 @@ class NornMMSReceiver : BroadcastReceiver() {
|
|||
val addresses = setOf(pdu.from.string).plus(pdu.to.map { it.string }.toTypedArray())
|
||||
val members = addresses.map { NornContact.getContact(it, context) }.toTypedArray()
|
||||
val conversation = NornConversation.getConversation(members, context)
|
||||
val message = NornMessage(text, conversation, Date(), context)
|
||||
val message = NornMessage(text, pdu.from.string, conversation, Date(), context)
|
||||
for (media in medias)
|
||||
message.medias.add(media)
|
||||
|
||||
return message
|
||||
}
|
||||
|
||||
fun writeFile(context: Context, data: ByteArray): Uri {
|
||||
fun writeFile(context: Context, data: ByteArray): String {
|
||||
val fileName = "media." + System.currentTimeMillis() + "_" + Math.abs(Random().nextInt()).toString() + ".dat"
|
||||
val file = File(context.cacheDir, fileName)
|
||||
val writerUri = Uri.Builder()
|
||||
.authority("xyz.johnny.norntalk.messages.NornMMSFileProvider")
|
||||
.path(fileName)
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.build()
|
||||
val file = File(context.filesDir, fileName)
|
||||
|
||||
FileOutputStream(file).use { writer ->
|
||||
writer.write(data)
|
||||
}
|
||||
|
||||
val uri = writerUri
|
||||
return uri
|
||||
}
|
||||
|
||||
protected fun onError(error: String) {
|
||||
|
||||
return fileName
|
||||
}
|
||||
|
||||
private fun handleHttpError(context: Context, intent: Intent) {
|
||||
|
|
|
@ -9,34 +9,23 @@ import xyz.johnny.norntalk.database.NornDatabase
|
|||
import xyz.johnny.norntalk.database.entities.Media
|
||||
import xyz.johnny.norntalk.security.Curve
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
|
||||
|
||||
/**
|
||||
* Classe permettant de gérer un document contenu dans un MMS
|
||||
*/
|
||||
class NornMedia private constructor(context: Context, val mimeType: String) {
|
||||
class NornMedia(context: Context, val mimeType: String, val uri: String) {
|
||||
|
||||
private var _data: ByteArray? = null
|
||||
val bitmap by lazy {
|
||||
if (this.uri != null)
|
||||
this.getBitmap(context)
|
||||
else
|
||||
null
|
||||
|
||||
}
|
||||
private var uri: Uri? = null
|
||||
val bitmap by lazy { this.getBitmap(context) }
|
||||
private var _name: String = "media_" + System.currentTimeMillis()
|
||||
val name get() = _name
|
||||
|
||||
constructor(context: Context, mimeType: String, uri: Uri) : this(context, mimeType) {
|
||||
this.uri = uri
|
||||
}
|
||||
constructor(context: Context, mimeType: String, uri: Uri) : this(context, mimeType, uri.toString())
|
||||
|
||||
constructor(context: Context, data: ByteArray, mimeType: String, name: String): this(context, mimeType) {
|
||||
this._data = data
|
||||
this._name = name
|
||||
}
|
||||
|
||||
private val data: ByteArray by lazy {
|
||||
this._data ?: Message.bitmapToByteArray(this.bitmap)
|
||||
|
@ -55,16 +44,22 @@ class NornMedia private constructor(context: Context, val mimeType: String) {
|
|||
}
|
||||
|
||||
private fun getBitmap(context: Context): Bitmap {
|
||||
context.contentResolver.openFileDescriptor(uri, "r").use { parcelFileDescriptor ->
|
||||
val fileDescriptor = parcelFileDescriptor.getFileDescriptor()
|
||||
return BitmapFactory.decodeFileDescriptor(fileDescriptor)
|
||||
if (this.uri.startsWith("content:")) {
|
||||
context.contentResolver.openFileDescriptor(Uri.parse(this.uri), "r").use { parcelFileDescriptor ->
|
||||
val fileDescriptor = parcelFileDescriptor.fileDescriptor
|
||||
return BitmapFactory.decodeFileDescriptor(fileDescriptor)
|
||||
}
|
||||
} else {
|
||||
FileOutputStream(File(context.filesDir, this.uri)).use { fileOutputStream ->
|
||||
return BitmapFactory.decodeFileDescriptor(fileOutputStream.fd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun from(context: Context, media: Media): NornMedia {
|
||||
return NornMedia(context, media.data, media.type, media.name)
|
||||
return NornMedia(context, media.type, media.uri)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -78,7 +73,7 @@ class NornMedia private constructor(context: Context, val mimeType: String) {
|
|||
*/
|
||||
fun insert(context: Context, message: NornMessage, position: Int) {
|
||||
NornDatabase.Helper(context).run { db ->
|
||||
val media = Media(this.mimeType, this.data, message.id, this.name, position)
|
||||
val media = Media(this.mimeType, this.uri, message.id, this.name, position)
|
||||
db.mediaDao().insertMedia(media)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue