Skip to content

Commit

Permalink
added callback when you input invalid or empty number
Browse files Browse the repository at this point in the history
  • Loading branch information
raquezha committed Sep 8, 2018
1 parent 616af08 commit 459a915
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
ext.navigation_ui_ktx = '1.0.0-alpha05'
ext.constraintlayout = '1.1.3'
ext.playground_version = '0.0.5.0'

ext.libphonenumber_version = '7.2.2'
repositories {
google()
jcenter()
Expand Down
1 change: 1 addition & 0 deletions hooker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//implementation "com.googlecode.libphonenumber:libphonenumber:$libphonenumber_version"
}
repositories {
mavenCentral()
Expand Down
97 changes: 66 additions & 31 deletions hooker/src/main/kotlin/net/raquezha/hookr/Hook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,124 @@ import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import androidx.annotation.NonNull


object Hook {
@JvmStatic
fun up(context: Context): Hookers {
return Hookers(context)
}
}

class Hookers(var ctx: Context) {
class Hookers(@NonNull var ctx: Context) {
var invalidMobileNumber = "Invalid mobile number"
var requiredMobileNumber = "Mobile Number is required"
var requiredfacebookId = "Facebook ID is required"

fun withMessenger(@NonNull facebookUserId: String) {
startHooking(Hookrs.MESSENGER, facebookUserId, null)
fun withMessenger(@NonNull facebookUserId: String, errorMsg: ((String) -> Unit)? = null) {
startHooking(Hookrs.MESSENGER, facebookUserId, null, errorMsg)
}

fun withViber(@NonNull mobileNumber: String) {
startHooking(Hookrs.VIBER, mobileNumber)
fun withViber(@NonNull mobileNumber: String, errorMsg: ((String) -> Unit)? = null) {
startHooking(Hookrs.VIBER, mobileNumber, errorMsg)
}

fun withSMS(@NonNull mobileNumber: String) {
startHooking(Hookrs.MESSAGES, null, mobileNumber)
fun withSMS(@NonNull mobileNumber: String, errorMsg: ((String) -> Unit)? = null) {
startHooking(Hookrs.MESSAGES, null, mobileNumber, errorMsg)
}

fun withPhoneCall(@NonNull mobileNumber: String) {
startHooking(Hookrs.PHONE_CALL, null, mobileNumber)
fun withPhoneCall(@NonNull mobileNumber: String, errorMsg: ((String) -> Unit)? = null) {
startHooking(Hookrs.PHONE_CALL, null, mobileNumber, errorMsg)
}

fun withWhatsApp(@NonNull mobileNumber: String) {
startHooking(Hookrs.WHATSAPP, null, mobileNumber)
fun withWhatsApp(@NonNull mobileNumber: String, errorMsg: ((String) -> Unit)? = null) {
startHooking(Hookrs.WHATSAPP, null, mobileNumber, errorMsg)
}

private fun startHooking(app: Hookrs, mobileNumber: String) {
startHooking(app, null, mobileNumber)
private fun startHooking(app: Hookrs, mobileNumber: String, errorMsg: ((String) -> Unit)? = null) {
startHooking(app, null, mobileNumber, errorMsg)
}

private fun startHooking(app: Hookrs, facebookUserId: String?, mobileNumber: String?) {
private fun startHooking(app: Hookrs, facebookUserId: String?, mobileNumber: String?, errorMsg: ((String) -> Unit)? = null) {
try {
ctx.startActivity(getHookingIntent(app, facebookUserId, mobileNumber))

val intent = getHookingIntent(app, facebookUserId, mobileNumber, errorMsg)
if (intent != null)
ctx.startActivity(intent)
} catch (ex: ActivityNotFoundException) {
Toast.makeText(ctx, "Please Install ${app.appName}", Toast.LENGTH_LONG).show()
errorMsg?.invoke("Please Install ${app.appName}")

}
}

private fun getHookingIntent(app: Hookrs, facebookUserId: String?, mobileNumber: String?): Intent {
private fun getHookingIntent(app: Hookrs, facebookUserId: String?, mobileNumber: String?, listener: ((String) -> Unit)? = null): Intent? {
val intent = Intent(app.intent)
when (app) {
Hookrs.VIBER -> {
checkNotNull(mobileNumber) {
throw NullPointerException("Mobile Number is required.")
throw NullPointerException(requiredMobileNumber)
}
if (isValidMobileNumber(mobileNumber)) {
intent.data = Uri.parse("tel:" + Uri.encode(mobileNumber))
intent.setClassName(app.appId, "com.viber.voip.WelcomeActivity")
} else {
listener?.invoke(invalidMobileNumber)
return null
}
intent.data = Uri.parse("tel:" + Uri.encode(mobileNumber))
intent.setClassName(app.appId, "com.viber.voip.WelcomeActivity")
}
Hookrs.MESSAGES -> {
checkNotNull(mobileNumber) {
throw NullPointerException("Mobile Number is required.")
throw NullPointerException(requiredMobileNumber)
}
if (isValidMobileNumber(mobileNumber)) {
intent.data = Uri.parse("smsto:$mobileNumber")
} else {
listener?.invoke(requiredMobileNumber)
return null
}
intent.data = Uri.parse("smsto:$mobileNumber")
}
Hookrs.WHATSAPP -> {
checkNotNull(mobileNumber) {
throw NullPointerException("Mobile Number is required.")
throw NullPointerException(requiredMobileNumber)
}
if (isValidMobileNumber(mobileNumber)) {
intent.data = Uri.parse("https://wa.up/$mobileNumber")
} else {
listener?.invoke(requiredMobileNumber)
return null
}
intent.data = Uri.parse("https://wa.up/$mobileNumber")

}
Hookrs.MESSENGER -> {
checkNotNull(facebookUserId) {
throw NullPointerException("Facebook User ID is required.")
throw NullPointerException(requiredfacebookId)
}
intent.data = Uri.parse("fb-messenger://user/$facebookUserId")
if (facebookUserId!!.isEmpty()) {
listener?.invoke(requiredfacebookId)
return null
} else
intent.data = Uri.parse("fb-messenger://user/$facebookUserId")

}
Hookrs.PHONE_CALL -> {
checkNotNull(mobileNumber) {
throw NullPointerException("Mobile Number is required.")
throw NullPointerException(requiredMobileNumber)
}
if (isValidMobileNumber(mobileNumber)) {
intent.data = Uri.parse("tel:$mobileNumber")
} else {
return null
listener?.invoke(requiredMobileNumber)
}
intent.data = Uri.parse("tel:$mobileNumber")
}
}
return intent
}

private fun isValidMobileNumber(mobileNumber: String?): Boolean {
var isValid = true
if (mobileNumber!! == "") isValid = false
return isValid
}
}


Expand Down
20 changes: 13 additions & 7 deletions sample/src/main/kotlin/com/raquezha/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_help.*
import net.raquezha.hookr.Hook

import net.raquezha.playground.extensions.toast

import java.text.SimpleDateFormat
import java.util.*

Expand All @@ -25,25 +25,31 @@ class MainActivity : AppCompatActivity() {
tvAppVersion.text = "App Version: ${BuildConfig.VERSION_NAME}"
tvBuildDate.text = "Build Date: $buildDate"

val sample_number = "+639123456780"
val sampleNumber = "9154543084"
btnSMS.setOnClickListener {
Hook.up(this@MainActivity).withSMS(sample_number)
Hook.up(this@MainActivity).withSMS(sampleNumber) { message ->
toast(message)
}
}

btnCall.setOnClickListener {
Hook.up(this@MainActivity).withPhoneCall(sample_number)
Hook.up(this@MainActivity).withPhoneCall(sampleNumber)
}

btnMessenger.setOnClickListener {
Hook.up(this@MainActivity).withMessenger("1232131232")
Hook.up(this@MainActivity).withMessenger("1232131232") {
toast(it)
}
}

btnViber.setOnClickListener {
Hook.up(this@MainActivity).withViber(sample_number)
Hook.up(this@MainActivity).withViber(sampleNumber) {
toast(it)
}
}

btnWhatsapp.setOnClickListener {
Hook.up(this@MainActivity).withWhatsApp(sample_number)
Hook.up(this@MainActivity).withWhatsApp(sampleNumber)
}
}
}

0 comments on commit 459a915

Please sign in to comment.