Fix: crash lorsque l'utilisateur rentre un numéro invalide

This commit is contained in:
odrling 2018-03-11 18:04:01 +01:00
parent cac383095c
commit ec8a3cf5b2
5 changed files with 36 additions and 17 deletions

View file

@ -16,6 +16,8 @@ import xyz.johnny.norntalk.messages.NornMessageDispatcher
import xyz.johnny.norntalk.security.QRCode
import xyz.johnny.norntalk.ui_components.ComposeView
import xyz.johnny.norntalk.ui_components.NornCaptureActivity
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.util.*
@ -33,14 +35,14 @@ class ComposeActivity : ComposeView.SendActivity() {
// vérifie que le numéro de téléphone est correct
if (!PhoneNumberUtils.isWellFormedSmsAddress(sender)) {
Toast.makeText(this, "Invalid sms number", Toast.LENGTH_SHORT).show()
Toast.makeText(this, getString(R.string.invalid_number), Toast.LENGTH_SHORT).show()
return
}
// envoyer le message en parallèle
object: AsyncTask<Void, Void, NornMessage>() {
object: AsyncTask<Void, Void, NornMessage?>() {
override fun doInBackground(vararg params: Void): NornMessage {
override fun doInBackground(vararg params: Void): NornMessage? {
try {
// liste des contacts
val contacts = arrayOf(NornContact.getContact(number.text.toString(), this@ComposeActivity))
@ -52,12 +54,20 @@ class ComposeActivity : ComposeView.SendActivity() {
msg.send(this@ComposeActivity)
return msg
} catch (e: Exception) {
e.printStackTrace()
throw e
val stream = ByteArrayOutputStream()
val out = PrintStream(stream)
e.printStackTrace(out)
Log.d(this::class.java.simpleName, stream.toString())
return null
}
}
override fun onPostExecute(message: NornMessage) {
override fun onPostExecute(message: NornMessage?) {
if (message == null) {
Toast.makeText(this@ComposeActivity, getString(R.string.invalid_number), Toast.LENGTH_SHORT).show()
return
}
// Transmettre le message aux autres activités
NornMessageDispatcher.send(message)

View file

@ -2,6 +2,7 @@ package xyz.johnny.norntalk.messages
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.graphics.drawable.Drawable
import android.net.Uri
import android.provider.ContactsContract
@ -187,19 +188,26 @@ class NornContact private constructor(val number: String, context: Context) {
val projection = arrayOf(ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID)
val cursor = context.contentResolver.query(this.getPhoneLookupUri(), projection, null, null, null)
// récupérer le nom et l'identifiant du contact
var cursor : Cursor? = null
var name: String? = null
var id: Long? = null
if (cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME))
id = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID))
try {
cursor = context.contentResolver.query(this.getPhoneLookupUri(), projection, null, null, null)
// récupérer le nom et l'identifiant du contact
if (cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME))
id = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID))
}
}
cursor.close()
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException(context.getString(R.string.invalid_number))
} finally {
if (cursor != null)
cursor.close()
}
// retourner le nom et l'identifiant dans un dictionnaire

View file

@ -281,16 +281,15 @@ class NornMessage constructor(text: String?, var ciphertext: String?, sender: St
}
if (parts.size == 1)
// envoyer le message en un seul SMS
// envoyer le message en un seul SMS
smsManager.sendTextMessage(this.contact.number, null, text, piSent?.get(0), piDelivered?.get(0))
else
// envoyer le message en plusieurs SMS
// envoyer le message en plusieurs SMS
smsManager.sendMultipartTextMessage(this.contact.number, null, parts, piSent, piDelivered)
// insérer le message dans la base de données
if (insert)
this.insertMessage(context)
}
/**

View file

@ -41,4 +41,5 @@
<string name="correct_number">Numéro de téléphone correct</string>
<string name="generating_qrcode">QR code en cours de génération</string>
<string name="qrcode_generated">QR code généré</string>
<string name="invalid_number">Numéro de téléphone invalide</string>
</resources>

View file

@ -42,4 +42,5 @@
<string name="correct_number">Correct phone number</string>
<string name="generating_qrcode">Generating QR code</string>
<string name="qrcode_generated">QR code generated</string>
<string name="invalid_number">Invalid sms number</string>
</resources>