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..5b4797d --- /dev/null +++ b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/App.kt @@ -0,0 +1,48 @@ +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 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)) + } + } + } + } + + 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..3b2bf43 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,13 +7,12 @@ 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 - class HomeActivity : AppCompatActivity() /*, BottomNavigationView.OnNavigationItemSelectedListener*/ { lateinit var viewModel: HomeViewModel @@ -21,13 +20,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() { @@ -49,6 +54,10 @@ class HomeActivity : AppCompatActivity() /*, BottomNavigationView.OnNavigationIt ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_PHONE_STATE, Manifest.permission.SEND_SMS, Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS), SMS_PERMISSION_CODE) } + + companion object { + const val SMS_PERMISSION_CODE = 1337 + } } 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..75ee1ee 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,38 +4,26 @@ 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 /** * A broadcast receiver who listens for incoming SMS */ -class SmsBroadcastReceiver : BroadcastReceiver() { +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"> + + + + + + + + + +