From 7823f835cd37c4765d67dbbcd7249816edd107ec Mon Sep 17 00:00:00 2001 From: Joaquim Ley Date: Sun, 25 Mar 2018 18:15:34 +0200 Subject: [PATCH 1/2] Sending and receiving sms and showing on UI This is still very rough, DI will defenitly help, not using the SmsBroadcastReceiver class but copy pasta the logic for now. For #12 --- transport-eta-android/mobile-ui/build.gradle | 1 + .../mobile-ui/src/main/AndroidManifest.xml | 17 ++-- .../java/com/joaquimley/transporteta/App.kt | 50 ++++++++++ .../transporteta/home/HomeActivity.kt | 15 ++- .../transporteta/sms/SmsBroadcastReceiver.kt | 22 +---- .../transporteta/sms/SmsController.kt | 6 +- .../src/main/res/layout/activity_home.xml | 93 ++++++++++++++----- transport-eta-android/versions.gradle | 2 + 8 files changed, 150 insertions(+), 56 deletions(-) create mode 100644 transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/App.kt diff --git a/transport-eta-android/mobile-ui/build.gradle b/transport-eta-android/mobile-ui/build.gradle index 0371231..857a557 100644 --- a/transport-eta-android/mobile-ui/build.gradle +++ b/transport-eta-android/mobile-ui/build.gradle @@ -76,6 +76,7 @@ dependencies { implementation deps.support.constraint implementation deps.support.design // Utils + implementation deps.kaction implementation deps.timber implementation deps.rx_android implementation deps.kotterknife diff --git a/transport-eta-android/mobile-ui/src/main/AndroidManifest.xml b/transport-eta-android/mobile-ui/src/main/AndroidManifest.xml index 16008a8..158aa97 100644 --- a/transport-eta-android/mobile-ui/src/main/AndroidManifest.xml +++ b/transport-eta-android/mobile-ui/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ - - - - - + + + + + + + + \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/App.kt b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/App.kt new file mode 100644 index 0000000..fa85f5d --- /dev/null +++ b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/App.kt @@ -0,0 +1,50 @@ +package com.joaquimley.transporteta + +import android.app.Application +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.provider.Telephony +import android.widget.Toast +import com.joaquimley.transporteta.sms.SMS_CONDITION +import com.joaquimley.transporteta.sms.SMS_SERVICE_NUMBER +import com.joaquimley.transporteta.sms.SmsController +import com.joaquimley.transporteta.sms.SmsModel + + +/** + * Created by joaquimley on 25/03/2018. + */ +class App : Application() { + + val smsController = SmsController() + + private val mMessageReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) { + var smsBody = "" + var smsSender = "" + for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) { + smsSender = smsMessage.displayOriginatingAddress + smsBody += smsMessage.messageBody + } + + if (smsSender == SMS_SERVICE_NUMBER && smsBody.startsWith(SMS_CONDITION)) { + smsController.serviceSms.onNext(SmsModel(smsBody)) +// Toast.makeText(context, "BroadcastReceiver caught conditional SMS: " + smsBody, Toast.LENGTH_LONG).show(); + } + } + } + } + + override fun onCreate() { + super.onCreate() + instance = this + registerReceiver(mMessageReceiver, IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) + } + + companion object { + lateinit var instance: App + } +} \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/home/HomeActivity.kt b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/home/HomeActivity.kt index 1d3886e..0e313f0 100644 --- a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/home/HomeActivity.kt +++ b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/home/HomeActivity.kt @@ -7,9 +7,10 @@ import android.os.Bundle import android.support.v4.app.ActivityCompat import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity +import com.joaquimley.transporteta.App import com.joaquimley.transporteta.R import com.joaquimley.transporteta.presentation.HomeViewModel -import com.joaquimley.transporteta.sms.SmsController +import io.reactivex.android.schedulers.AndroidSchedulers import kotlinx.android.synthetic.main.activity_home.* const val SMS_PERMISSION_CODE = 1337 @@ -21,13 +22,19 @@ class HomeActivity : AppCompatActivity() /*, BottomNavigationView.OnNavigationIt override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_home) + setupViewModel() if (hasReadSmsPermission().not()) { requestReadAndSendSmsPermission() } - setupViewModel() - val smsController = SmsController() - fab.setOnClickListener { smsController.requestEta(3113) } + button.setOnClickListener { + App.instance.smsController.requestEta(edit_text.text.toString().toInt()) + edit_text.text.clear() + } + + App.instance.smsController.observeIncomingSms().subscribeOn(AndroidSchedulers.mainThread()).subscribe { + text.text = it.message + } } private fun setupViewModel() { diff --git a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiver.kt b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiver.kt index b166015..1a0c325 100644 --- a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiver.kt +++ b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiver.kt @@ -4,8 +4,6 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.provider.Telephony -import android.telephony.SmsManager -import android.util.Log import android.widget.Toast /** @@ -15,27 +13,17 @@ import android.widget.Toast class SmsBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { - if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) { + if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) { var smsBody = "" + var smsSender = "" for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) { - smsBody += smsMessage.getMessageBody(); + smsSender = smsMessage.displayOriginatingAddress + smsBody += smsMessage.messageBody } - if (smsBody.startsWith(SMS_CONDITION)) { + if (smsSender == SMS_SERVICE_NUMBER && smsBody.startsWith(SMS_CONDITION)) { Toast.makeText(context, "BroadcastReceiver caught conditional SMS: " + smsBody, Toast.LENGTH_LONG).show(); } } } -} - - -object SmsHelper { - - - - - - fun sendDebugSms(number: String, smsBody: String) { - SmsManager.getDefault().sendTextMessage(number, null, smsBody, null, null) - } } \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsController.kt b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsController.kt index b149395..e8528b0 100644 --- a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsController.kt +++ b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/sms/SmsController.kt @@ -5,13 +5,13 @@ import io.reactivex.Observable import io.reactivex.subjects.PublishSubject const val SMS_CONDITION = "SMS@Carris" -const val SERVICE_NUMBER = "3599" +const val SMS_SERVICE_NUMBER = "3599" fun String.isValidPhoneNumber(phoneNumber: String): Boolean { return android.util.Patterns.PHONE.matcher(phoneNumber).matches() } -class SmsController() { +class SmsController { val serviceSms: PublishSubject = PublishSubject.create() @@ -20,7 +20,7 @@ class SmsController() { } fun requestEta(busStopCode: Int) { - SmsManager.getDefault().sendTextMessage(SERVICE_NUMBER, null, "C $busStopCode", null, null) + SmsManager.getDefault().sendTextMessage(SMS_SERVICE_NUMBER, null, "C $busStopCode", null, null) } } diff --git a/transport-eta-android/mobile-ui/src/main/res/layout/activity_home.xml b/transport-eta-android/mobile-ui/src/main/res/layout/activity_home.xml index 26e0b72..01c8036 100644 --- a/transport-eta-android/mobile-ui/src/main/res/layout/activity_home.xml +++ b/transport-eta-android/mobile-ui/src/main/res/layout/activity_home.xml @@ -2,33 +2,78 @@ - + android:layout_height="match_parent"> + + + + + + + + + +