From cd1a8cc8824c4c65a012a9ce740ab80bc2a76d9b Mon Sep 17 00:00:00 2001 From: Joaquim Ley Date: Fri, 13 Apr 2018 15:51:15 +0200 Subject: [PATCH] Some more abstraction into different product flavors Sms module now has a mock() variant for easier testing, this was cared to not be possible to ship with mockRelease (check variant filtering in sms module build.gradle) With this, I hope to get a better better abstraction hence, better enviroment for integration tests. Notice how the main folder of the sms module keeps the interface which represents the contract to outer layers (mobile-ui) in this case, slowly but surely this moves us closer to the onion/clean arch. For #29 See also: #25, #38 --- transport-eta-android/mobile-ui/build.gradle | 64 +++++++-------- .../src/androidTest/AndroidManifest.xml | 2 - .../ui/di/component/TestAppComponent.kt | 3 +- .../ui/di/module/TestAppModule.kt | 11 +-- .../ui/home/FavoritesFragmentTest.kt | 8 +- .../mobile-ui/src/dev/AndroidManifest.xml | 9 +++ .../ui/testing}/TestFragmentActivity.kt | 2 +- .../ui/testing}/factory/DataFactory.kt | 2 +- .../factory/FavoriteViewTestFactory.kt | 5 +- .../di/component/TestActivityBindingModule.kt | 15 ++++ .../transporteta/ui/SmsControllerMock.kt | 22 ------ .../transporteta/ui/SmsControllerMockImpl.kt | 25 ++++++ .../home/favorite/FavoritesViewModelTest.kt | 1 + transport-eta-android/sms/build.gradle | 35 +++++++++ .../sms/src/main/AndroidManifest.xml | 3 +- .../sms/SmsBroadcastReceiverImpl.kt | 0 .../transporteta/sms/SmsControllerImpl.kt | 2 +- .../sms/SmsBroadcastReceiverImpl.kt | 29 +++++++ .../transporteta/sms/SmsControllerImpl.kt | 78 +++++++++++++++++++ 19 files changed, 240 insertions(+), 76 deletions(-) delete mode 100644 transport-eta-android/mobile-ui/src/androidTest/AndroidManifest.xml create mode 100644 transport-eta-android/mobile-ui/src/dev/AndroidManifest.xml rename transport-eta-android/mobile-ui/src/{androidTest/java/com/joaquimley/transporteta/ui/test => dev/java/com/joaquimley/transporteta/ui/testing}/TestFragmentActivity.kt (97%) rename transport-eta-android/mobile-ui/src/{androidTest/java/com/joaquimley/transporteta/ui/test => dev/java/com/joaquimley/transporteta/ui/testing}/factory/DataFactory.kt (92%) rename transport-eta-android/mobile-ui/src/{androidTest/java/com/joaquimley/transporteta/ui/test => dev/java/com/joaquimley/transporteta/ui/testing}/factory/FavoriteViewTestFactory.kt (82%) create mode 100644 transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/ui/di/component/TestActivityBindingModule.kt delete mode 100644 transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMock.kt create mode 100644 transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMockImpl.kt rename transport-eta-android/sms/src/{main => mock}/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt (100%) rename transport-eta-android/sms/src/{main => mock}/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt (95%) create mode 100644 transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt create mode 100644 transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt diff --git a/transport-eta-android/mobile-ui/build.gradle b/transport-eta-android/mobile-ui/build.gradle index 5c1a8f9..98340e0 100644 --- a/transport-eta-android/mobile-ui/build.gradle +++ b/transport-eta-android/mobile-ui/build.gradle @@ -13,37 +13,8 @@ android { versionName app_version.versionName vectorDrawables.useSupportLibrary = true testInstrumentationRunner "com.joaquimley.transporteta.ui.test.TestRunner" - } - - buildTypes { - debug { - testCoverageEnabled true - } - - release { - minifyEnabled true - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } - - productFlavors { - flavorDimensions "city", "enviroment" - - lx { - dimension "city" - } - - dev { -// applicationIdSuffix ".dev" - dimension "enviroment" - } - - mock { -// applicationIdSuffix ".mock" - dimension "enviroment" - } - + missingDimensionStrategy 'environment', 'mock', 'prod' } packagingOptions { @@ -59,6 +30,38 @@ android { abortOnError false ignoreWarnings true } + + buildTypes { + debug { + versionNameSuffix "-dev" + applicationIdSuffix ".debug" + debuggable true + testCoverageEnabled true + } + + release { + minifyEnabled true + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + +// productFlavors { +// flavorDimensions "city", "environment" +// +// lx { +// dimension "city" +// } +// +// dev { +// applicationIdSuffix ".dev" +// dimension "environment" +// } +// +// mock { +// applicationIdSuffix ".mock" +// dimension "environment" +// } +// } } kapt { @@ -76,7 +79,6 @@ configurations.all { } dependencies { - // Modules implementation project(':sms') diff --git a/transport-eta-android/mobile-ui/src/androidTest/AndroidManifest.xml b/transport-eta-android/mobile-ui/src/androidTest/AndroidManifest.xml deleted file mode 100644 index 5c958d4..0000000 --- a/transport-eta-android/mobile-ui/src/androidTest/AndroidManifest.xml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/component/TestAppComponent.kt b/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/component/TestAppComponent.kt index 9bf5e89..49b2f03 100644 --- a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/component/TestAppComponent.kt +++ b/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/component/TestAppComponent.kt @@ -1,7 +1,6 @@ package com.joaquimley.transporteta.ui.di.component import android.app.Application -import com.joaquimley.transporteta.ui.di.module.ActivityBindingModule import com.joaquimley.transporteta.ui.di.module.TestAppModule import com.joaquimley.transporteta.ui.injection.scope.PerApplication import com.joaquimley.transporteta.sms.SmsBroadcastReceiver @@ -11,7 +10,7 @@ import dagger.BindsInstance import dagger.Component import dagger.android.support.AndroidSupportInjectionModule -@Component(modules = arrayOf(TestAppModule::class, ActivityBindingModule::class, +@Component(modules = arrayOf(TestAppModule::class, TestActivityBindingModule::class, AndroidSupportInjectionModule::class)) @PerApplication interface TestAppComponent : AppComponent { diff --git a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/module/TestAppModule.kt b/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/module/TestAppModule.kt index 816191c..62ba72a 100644 --- a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/module/TestAppModule.kt +++ b/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/di/module/TestAppModule.kt @@ -2,14 +2,12 @@ package com.joaquimley.transporteta.ui.di.module import android.app.Application import android.content.Context -import com.joaquimley.transporteta.ui.injection.scope.PerApplication import com.joaquimley.transporteta.sms.SmsBroadcastReceiver -import com.joaquimley.transporteta.sms.SmsBroadcastReceiverImpl import com.joaquimley.transporteta.sms.SmsController -import com.joaquimley.transporteta.sms.SmsControllerImpl +import com.joaquimley.transporteta.ui.injection.scope.PerApplication +import com.nhaarman.mockito_kotlin.mock import dagger.Module import dagger.Provides -import org.mockito.Mockito @Module class TestAppModule { @@ -23,14 +21,13 @@ class TestAppModule { @Provides @PerApplication fun provideSmsController(): SmsController { - return Mockito.mock(SmsControllerImpl::class.java) + return mock() } @Provides @PerApplication internal fun provideSmsBroadcastReceiver(): SmsBroadcastReceiver { - return Mockito.mock(SmsBroadcastReceiverImpl::class.java) + return mock() } - } \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/home/FavoritesFragmentTest.kt b/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/home/FavoritesFragmentTest.kt index 560c7c1..cc1d2a6 100644 --- a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/home/FavoritesFragmentTest.kt +++ b/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/home/FavoritesFragmentTest.kt @@ -10,8 +10,8 @@ import android.support.test.rule.ActivityTestRule import android.support.test.runner.AndroidJUnit4 import com.joaquimley.transporteta.R import com.joaquimley.transporteta.ui.home.favorite.FavoritesFragment -import com.joaquimley.transporteta.ui.test.TestFragmentActivity -import com.joaquimley.transporteta.ui.test.factory.FavoriteViewTestFactory +import com.joaquimley.transporteta.ui.testing.TestFragmentActivity +import com.joaquimley.transporteta.ui.testing.factory.FavoriteViewTestFactory import org.junit.Before import org.junit.Rule import org.junit.Test @@ -22,13 +22,13 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class FavoritesFragmentTest { - @get:Rule val activityRule = ActivityTestRule(HomeActivity::class.java) + @get:Rule val activityRule = ActivityTestRule(TestFragmentActivity::class.java, true, true) private lateinit var favoritesFragment: FavoritesFragment @Before fun setup() { favoritesFragment = FavoritesFragment.newInstance() -// activityRule.activity.addFragment(favoritesFragment) + activityRule.activity.addFragment(favoritesFragment) } @Test diff --git a/transport-eta-android/mobile-ui/src/dev/AndroidManifest.xml b/transport-eta-android/mobile-ui/src/dev/AndroidManifest.xml new file mode 100644 index 0000000..e48c733 --- /dev/null +++ b/transport-eta-android/mobile-ui/src/dev/AndroidManifest.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/TestFragmentActivity.kt b/transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/TestFragmentActivity.kt similarity index 97% rename from transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/TestFragmentActivity.kt rename to transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/TestFragmentActivity.kt index c73a9bf..dcd0dc2 100644 --- a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/TestFragmentActivity.kt +++ b/transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/TestFragmentActivity.kt @@ -1,4 +1,4 @@ -package com.joaquimley.transporteta.ui.test +package com.joaquimley.transporteta.ui.testing import android.os.Bundle import android.support.v4.app.Fragment diff --git a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/factory/DataFactory.kt b/transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/factory/DataFactory.kt similarity index 92% rename from transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/factory/DataFactory.kt rename to transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/factory/DataFactory.kt index 2b2384a..2afa2e2 100644 --- a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/factory/DataFactory.kt +++ b/transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/factory/DataFactory.kt @@ -1,4 +1,4 @@ -package com.joaquimley.transporteta.ui.test.factory.ui +package com.joaquimley.transporteta.ui.testing.factory.ui import java.util.concurrent.ThreadLocalRandom diff --git a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/factory/FavoriteViewTestFactory.kt b/transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/factory/FavoriteViewTestFactory.kt similarity index 82% rename from transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/factory/FavoriteViewTestFactory.kt rename to transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/factory/FavoriteViewTestFactory.kt index 2cbe161..f0633c5 100644 --- a/transport-eta-android/mobile-ui/src/androidTest/java/com/joaquimley/transporteta/ui/test/factory/FavoriteViewTestFactory.kt +++ b/transport-eta-android/mobile-ui/src/dev/java/com/joaquimley/transporteta/ui/testing/factory/FavoriteViewTestFactory.kt @@ -1,9 +1,8 @@ -package com.joaquimley.transporteta.ui.test.factory +package com.joaquimley.transporteta.ui.testing.factory import android.support.annotation.RestrictTo import com.joaquimley.transporteta.ui.model.FavoriteView -import com.joaquimley.transporteta.ui.test.factory.ui.DataFactory -import java.util.* +import com.joaquimley.transporteta.ui.testing.factory.ui.DataFactory import kotlin.collections.ArrayList @RestrictTo(RestrictTo.Scope.TESTS) diff --git a/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/ui/di/component/TestActivityBindingModule.kt b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/ui/di/component/TestActivityBindingModule.kt new file mode 100644 index 0000000..7796125 --- /dev/null +++ b/transport-eta-android/mobile-ui/src/main/java/com/joaquimley/transporteta/ui/di/component/TestActivityBindingModule.kt @@ -0,0 +1,15 @@ +package com.joaquimley.transporteta.ui.di.component + +import com.joaquimley.transporteta.ui.di.module.HomeFragmentsBuildersModule +import com.joaquimley.transporteta.ui.injection.scope.PerActivity +import com.joaquimley.transporteta.ui.testing.TestFragmentActivity +import dagger.Module +import dagger.android.ContributesAndroidInjector + +@Module +abstract class TestActivityBindingModule { + + @PerActivity + @ContributesAndroidInjector(modules = arrayOf(HomeFragmentsBuildersModule::class)) + abstract fun bindTestFragmentActivity(): TestFragmentActivity +} \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMock.kt b/transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMock.kt deleted file mode 100644 index d6c0a9e..0000000 --- a/transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMock.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.joaquimley.transporteta.ui.test - -import com.joaquimley.transporteta.ui.sms.SmsBroadcastReceiver -import com.joaquimley.transporteta.ui.sms.SmsController -import com.joaquimley.transporteta.ui.sms.model.SmsModel -import com.joaquimley.transporteta.ui.test.factory.ui.DataFactory -import io.reactivex.Observable -import javax.inject.Inject - -class SmsControllerMock @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver) : SmsController { - override fun dispose() { - } - - override fun observeIncomingSms(): Observable { - SmsModel() - } - - override fun requestEta(busStopCode: Int) { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. - } - -} \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMockImpl.kt b/transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMockImpl.kt new file mode 100644 index 0000000..f6a7b2b --- /dev/null +++ b/transport-eta-android/mobile-ui/src/mock/java/com/joaquimley/transporteta/ui/SmsControllerMockImpl.kt @@ -0,0 +1,25 @@ +package com.joaquimley.transporteta.ui + +import com.joaquimley.transporteta.sms.SmsBroadcastReceiver +import com.joaquimley.transporteta.sms.SmsControllerImpl +import com.joaquimley.transporteta.sms.model.SmsModel +import io.reactivex.Observable +import java.util.* +import javax.inject.Inject + +class SmsControllerMockImpl @Inject constructor(smsBroadcastReceiver: SmsBroadcastReceiver) : SmsControllerImpl(smsBroadcastReceiver) { + override fun dispose() { + } + + override fun observeIncomingSms(): Observable { + return Observable.just(SmsModel(Random().nextInt(), UUID.randomUUID().toString()), + SmsModel(Random().nextInt(), UUID.randomUUID().toString()), + SmsModel(Random().nextInt(), UUID.randomUUID().toString()), + SmsModel(Random().nextInt(), UUID.randomUUID().toString())) + } + + override fun requestEta(busStopCode: Int) { + + } + +} \ No newline at end of file diff --git a/transport-eta-android/mobile-ui/src/test/java/com/joaquimley/transporteta/ui/home/favorite/FavoritesViewModelTest.kt b/transport-eta-android/mobile-ui/src/test/java/com/joaquimley/transporteta/ui/home/favorite/FavoritesViewModelTest.kt index 8a5dc8e..4408356 100644 --- a/transport-eta-android/mobile-ui/src/test/java/com/joaquimley/transporteta/ui/home/favorite/FavoritesViewModelTest.kt +++ b/transport-eta-android/mobile-ui/src/test/java/com/joaquimley/transporteta/ui/home/favorite/FavoritesViewModelTest.kt @@ -7,6 +7,7 @@ import com.joaquimley.transporteta.ui.model.data.Resource import com.joaquimley.transporteta.ui.model.data.ResourceState import com.joaquimley.transporteta.sms.SmsController import com.joaquimley.transporteta.sms.model.SmsModel +import com.joaquimley.transporteta.ui.home.favorite.FavoritesViewModel import com.nhaarman.mockito_kotlin.KArgumentCaptor import com.nhaarman.mockito_kotlin.times import com.nhaarman.mockito_kotlin.verify diff --git a/transport-eta-android/sms/build.gradle b/transport-eta-android/sms/build.gradle index 929ee08..18ae1fe 100644 --- a/transport-eta-android/sms/build.gradle +++ b/transport-eta-android/sms/build.gradle @@ -29,6 +29,41 @@ android { disable 'IconMissingDensityFolder' //For testing purpose. This is safe to remove. } + buildTypes { + debug { + testCoverageEnabled true + } + + release { + minifyEnabled true + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + productFlavors { + flavorDimensions "environment" + + prod { + dimension "environment" + } + + mock { + dimension "environment" + } + } + + /** + * Prevent shipping the mock flavor with release variant + * read more at: https://developer.android.com/studio/build/build-variants.html + */ + variantFilter { variant -> + def names = variant.flavors*.name + // To check for a certain build type, use variant.buildType.name == "" + if (names.contains("mock") && variant.buildType.name == "release") { + // Gradle ignores any variants that satisfy the conditions above. + setIgnore(true) + } + } } androidExtensions { diff --git a/transport-eta-android/sms/src/main/AndroidManifest.xml b/transport-eta-android/sms/src/main/AndroidManifest.xml index c64afb6..db665af 100644 --- a/transport-eta-android/sms/src/main/AndroidManifest.xml +++ b/transport-eta-android/sms/src/main/AndroidManifest.xml @@ -1,2 +1 @@ - + diff --git a/transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt b/transport-eta-android/sms/src/mock/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt similarity index 100% rename from transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt rename to transport-eta-android/sms/src/mock/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt diff --git a/transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt b/transport-eta-android/sms/src/mock/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt similarity index 95% rename from transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt rename to transport-eta-android/sms/src/mock/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt index 2308901..f046d93 100644 --- a/transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt +++ b/transport-eta-android/sms/src/mock/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt @@ -12,7 +12,7 @@ import javax.inject.Inject import javax.inject.Singleton @Singleton -class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver) : SmsController { +open class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver) : SmsController { private val serviceSms: PublishSubject = PublishSubject.create() private var busStopCode: Int = 0 diff --git a/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt b/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt new file mode 100644 index 0000000..8a3099a --- /dev/null +++ b/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsBroadcastReceiverImpl.kt @@ -0,0 +1,29 @@ +package com.joaquimley.transporteta.sms + +import android.content.Context +import android.content.Intent +import android.provider.Telephony +import javax.inject.Inject + +/** + * A broadcast receiver who listens for incoming SMS + */ + +class SmsBroadcastReceiverImpl @Inject constructor(serviceNumber: String, serviceSmsCondition: String) + : SmsBroadcastReceiver(serviceNumber, serviceSmsCondition) { + + 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 == serviceNumber && smsBody.startsWith(serviceSmsCondition)) { + broadcastServiceSms.onNext(smsBody) + } + } + } +} \ No newline at end of file diff --git a/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt b/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt new file mode 100644 index 0000000..f046d93 --- /dev/null +++ b/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt @@ -0,0 +1,78 @@ +package com.joaquimley.transporteta.sms + +import android.telephony.SmsManager +import android.util.Log +import com.joaquimley.transporteta.sms.model.SmsModel +import io.reactivex.Observable +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers +import io.reactivex.subjects.PublishSubject +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +open class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver) : SmsController { + + private val serviceSms: PublishSubject = PublishSubject.create() + private var busStopCode: Int = 0 + + private val disposable: Disposable? + + init { + // TODO: There might be an issue with race condition (busStopCode) + // TODO create a queue of requests and handle each synchronously + disposable = smsBroadcastReceiver.observeServiceSms() + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ serviceSms.onNext(SmsModel(busStopCode, it)) }, + { Log.e("SmsController", "Failed smsBroadcastReceiver.observeServiceSms(): ${it.message}")}) + } + + override fun observeIncomingSms(): Observable { + return serviceSms + } + + override fun requestEta(busStopCode: Int) { + this.busStopCode = busStopCode + SmsManager.getDefault().sendTextMessage(smsBroadcastReceiver.serviceNumber, null, "C $busStopCode", null, null) + Log.e("SmsController", "requestEta $busStopCode") + } + + override fun dispose() { + disposable?.dispose() + } +} + + +/** + +--------------------------------- +Send SMS to 3599 with the format: +--------------------------------- + +C (SPACE) Bus_stop_code + +--------------------- +Sms response example: +--------------------- + +SMS@Carris +759 ORIENTE +::14:52 [02m]:: +793 ROMA-AREEIRO +::14:54 [04m]:: +31B V.FORM. +::15:03 [13m]:: +759 ORIENTE +::15:18 [28m]:: + +---------------- + +Para consultar tempos de espera (em minutos), relativamente a todos os veículos que passam numa determinada paragem: +Deverá digitar C (espaço) Código da Paragem e enviar para o 3599 (custo de mensagem escrita normal); + +Para consultar tempos de espera dos próximos três veículos de uma carreira, numa determinada paragem: +Deverá digitar C (espaço) Código da Paragem (espaço) Nº da Carreira e enviar para o 3599 (custo de mensagem escrita normal) + + */