diff --git a/build.gradle b/build.gradle index bb90068..747c9a4 100644 --- a/build.gradle +++ b/build.gradle @@ -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() diff --git a/hooker/build.gradle b/hooker/build.gradle index a2e3744..978504b 100644 --- a/hooker/build.gradle +++ b/hooker/build.gradle @@ -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() diff --git a/hooker/src/main/kotlin/net/raquezha/hookr/Hook.kt b/hooker/src/main/kotlin/net/raquezha/hookr/Hook.kt index 508c885..fb27ebc 100644 --- a/hooker/src/main/kotlin/net/raquezha/hookr/Hook.kt +++ b/hooker/src/main/kotlin/net/raquezha/hookr/Hook.kt @@ -4,9 +4,9 @@ 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 { @@ -14,79 +14,114 @@ object Hook { } } -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 + } } diff --git a/sample/src/main/kotlin/com/raquezha/sample/MainActivity.kt b/sample/src/main/kotlin/com/raquezha/sample/MainActivity.kt index a4cd5ad..c3bf37a 100644 --- a/sample/src/main/kotlin/com/raquezha/sample/MainActivity.kt +++ b/sample/src/main/kotlin/com/raquezha/sample/MainActivity.kt @@ -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.* @@ -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) } } }