Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmsController bootstrap #14

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions transport-eta-android/mobile-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions transport-eta-android/mobile-ui/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -24,13 +25,13 @@
</activity>


<receiver
android:name=".sms.SmsBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!--<receiver-->
<!--android:name=".sms.SmsBroadcastReceiver"-->
<!--android:enabled="true"-->
<!--android:exported="true">-->
<!--<intent-filter android:priority="1000">-->
<!--<action android:name="android.provider.Telephony.SMS_RECEIVED" />-->
<!--</intent-filter>-->
<!--</receiver>-->
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@ 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

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() {
Expand All @@ -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
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SmsModel> = PublishSubject.create()

Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,78 @@
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/home_container"
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/bottom_navigation_view_height"/>
android:layout_height="match_parent">

<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3">

<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:inputType="number"
android:layout_height="wrap_content"/>

</android.support.design.widget.TextInputLayout>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout"/>

<Button
android:id="@+id/button"
style="@style/ButtonRoundStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>


<!--<FrameLayout-->
<!--android:id="@+id/home_container"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_marginBottom="@dimen/bottom_navigation_view_height"/>-->

<!--<android.support.design.widget.BottomNavigationView-->
<!--android:id="@+id/navigation"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom"-->
<!--android:background="@color/colorPrimaryDark"-->
<!--android:elevation="@dimen/bottom_navigation_view_elevation"-->
<!--app:itemIconTint="@drawable/selector_navigation_view_buttons"-->
<!--app:itemTextColor="@drawable/selector_navigation_view_buttons"-->
<!--app:menu="@menu/navigation"-->
<!--tools:targetApi="lollipop"/>-->

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:layout_width="40dp"
android:layout_height="40dp"/>

</android.support.design.widget.CoordinatorLayout>
<!--android:id="@+id/navigation"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom"-->
<!--android:background="@color/colorPrimaryDark"-->
<!--android:elevation="@dimen/bottom_navigation_view_elevation"-->
<!--app:itemIconTint="@drawable/selector_navigation_view_buttons"-->
<!--app:itemTextColor="@drawable/selector_navigation_view_buttons"-->
<!--app:menu="@menu/navigation"-->
<!--tools:targetApi="lollipop"/>-->
2 changes: 2 additions & 0 deletions transport-eta-android/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ versions.atsl_runner = "1.0.1"
versions.atsl_rules = "1.0.1"
versions.hamcrest = "1.3"
versions.glassfish_annotation = "10.0-b28"
versions.kaction = "0.0.1"
def deps = [:]

def support = [:]
Expand Down Expand Up @@ -136,6 +137,7 @@ deps.rx_android = "io.reactivex.rxjava2:rxandroid:$versions.rx_android"
deps.hamcrest = "org.hamcrest:hamcrest-all:$versions.hamcrest"
deps.android_gradle_plugin = "com.android.tools.build:gradle:$versions.android_gradle_plugin"
deps.jacoco_android_plugin = "com.dicedmelon.gradle:jacoco-android:$versions.jacoco_android_version"
deps.kaction = "com.tonsser:kaction:$versions.kaction"
ext.deps = deps

def build_versions = [:]
Expand Down