diff --git a/README.md b/README.md index ed1fab1..0776c0e 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ SendOTP Android Sdk! The **SendOtp** Verification SDK makes verifying phone numbers easy. SDK supports the verification of phone numbers via **SMS & Calls**. + + + ---------- Getting started @@ -22,7 +25,7 @@ Just add below dependency in project's app level build.gradle file dependencies { ... - implementation 'com.msg91.sendotpandroid.library:library:1.3.8' + implementation ‘com.msg91.sendotpandroid.library:library:1.4’ ... } @@ -33,7 +36,7 @@ Also, add below url in project's project level build.gradle file url "https://dl.bintray.com/walkover/Android-Libs" } - +**New Update**: Auto verify if same mobile number sim is insrted in device. Maven ------ @@ -70,7 +73,7 @@ After login at [MSG91](https://control.msg91.com/) follow below steps to ge > initialize'**SendOTP**' in your Application class. - public class ApplicationClass extends Application { + public class AppController extends Application { @Override public void onCreate() { super.onCreate(); @@ -131,14 +134,6 @@ manually **verifying OTP** SendOTP.getInstance().getTrigger().resend(RetryType.TEXT); -**onDestroy** - - @Override - protected void onDestroy() { - super.onDestroy(); - SendOTP.getInstance().getTrigger().stop(); - } - **customize message text** : diff --git a/app/build.gradle b/app/build.gradle index 2010718..771be5c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,8 +25,10 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.1.0' + implementation "com.msg91.sendotpandroid.library:library:1.4" // added sednotp sdk dependecy + + testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - implementation 'com.msg91.sendotpandroid.library:library:1.3.8' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e8e9a2d..d08524f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,8 +3,15 @@ package="com.msg91.sendotp.sample"> + + + + + + + @@ -22,7 +29,7 @@ diff --git a/app/src/main/java/com/msg91/sendotp/sample/AppController.java b/app/src/main/java/com/msg91/sendotp/sample/AppController.java new file mode 100644 index 0000000..9c0458f --- /dev/null +++ b/app/src/main/java/com/msg91/sendotp/sample/AppController.java @@ -0,0 +1,26 @@ +package com.msg91.sendotp.sample; + +import android.app.Application; + +import com.msg91.sendotpandroid.library.internal.SendOTP; + + +/** + * Copyright (C) sendotp-android - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + *

+ * Created by Samset on 23,October,2020 at 6:34 PM for sendotp-android. + *

+ * New Delhi,India + */ + + +public class AppController extends Application { + @Override + public void onCreate() { + super.onCreate(); + SendOTP.initializeApp(this,"authKey"); + + } +} diff --git a/app/src/main/java/com/msg91/sendotp/sample/ApplicationClass.java b/app/src/main/java/com/msg91/sendotp/sample/ApplicationClass.java deleted file mode 100644 index 273526e..0000000 --- a/app/src/main/java/com/msg91/sendotp/sample/ApplicationClass.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.msg91.sendotp.sample; - -import android.app.Application; - -import com.msg91.sendotpandroid.library.internal.SendOTP; - -public class ApplicationClass extends Application { - @Override - public void onCreate() { - super.onCreate(); - SendOTP.initializeApp(this,"authKey"); - } -} diff --git a/app/src/main/java/com/msg91/sendotp/sample/DataManager.java b/app/src/main/java/com/msg91/sendotp/sample/core/DataManager.java similarity index 85% rename from app/src/main/java/com/msg91/sendotp/sample/DataManager.java rename to app/src/main/java/com/msg91/sendotp/sample/core/DataManager.java index 77d189a..832c2c6 100644 --- a/app/src/main/java/com/msg91/sendotp/sample/DataManager.java +++ b/app/src/main/java/com/msg91/sendotp/sample/core/DataManager.java @@ -1,4 +1,4 @@ -package com.msg91.sendotp.sample; +package com.msg91.sendotp.sample.core; import android.app.Dialog; import android.content.Context; @@ -8,6 +8,18 @@ import android.view.WindowManager; import android.widget.TextView; +import com.msg91.sendotp.sample.R; + +/** + * Copyright (C) sendotp-android - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + *

+ * Created by Samset on 23,October,2020 at 6:34 PM for sendotp-android. + *

+ * New Delhi,India + */ + public class DataManager { private static final DataManager ourInstance = new DataManager(); private boolean isProgressDialogRunning; diff --git a/app/src/main/java/com/msg91/sendotp/sample/MainActivity.java b/app/src/main/java/com/msg91/sendotp/sample/ui/MainActivity.java similarity index 87% rename from app/src/main/java/com/msg91/sendotp/sample/MainActivity.java rename to app/src/main/java/com/msg91/sendotp/sample/ui/MainActivity.java index f7d87d3..013a191 100644 --- a/app/src/main/java/com/msg91/sendotp/sample/MainActivity.java +++ b/app/src/main/java/com/msg91/sendotp/sample/ui/MainActivity.java @@ -1,28 +1,37 @@ -package com.msg91.sendotp.sample; +package com.msg91.sendotp.sample.ui; import android.Manifest; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Color; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.os.Bundle; import android.telephony.TelephonyManager; import android.text.Editable; import android.text.TextWatcher; +import android.util.Log; import android.view.View; import android.widget.EditText; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; +import com.msg91.sendotp.sample.R; +import com.msg91.sendotp.sample.utils.CountrySpinner; +import com.msg91.sendotp.sample.utils.NetworkUtils; import com.msg91.sendotpandroid.library.internal.Iso2Phone; import com.msg91.sendotpandroid.library.utils.PhoneNumberFormattingTextWatcher; import com.msg91.sendotpandroid.library.utils.PhoneNumberUtils; +import java.lang.reflect.Method; import java.util.Locale; + public class MainActivity extends AppCompatActivity { public static final String INTENT_PHONENUMBER = "phonenumber"; @@ -32,15 +41,15 @@ public class MainActivity extends AppCompatActivity { private TextView mSmsButton; private String mCountryIso; private TextWatcher mNumberTextWatcher; + private NetworkUtils networkConnectivity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main_new); mPhoneNumber = findViewById(R.id.phoneNumber); mSmsButton = findViewById(R.id.smsVerificationButton); - + networkConnectivity = NetworkUtils.getInstance(); mCountryIso = PhoneNumberUtils.getDefaultCountryIso(this); final String defaultCountryName = new Locale("", mCountryIso).getDisplayName(); final CountrySpinner spinner = (CountrySpinner) findViewById(R.id.spinner); @@ -59,6 +68,7 @@ public void onCountryIsoSelected(String selectedIso) { resetNumberTextWatcher(mCountryIso); tryAndPrefillPhoneNumber(); + } private void tryAndPrefillPhoneNumber() { @@ -75,8 +85,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in tryAndPrefillPhoneNumber(); } else { if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) { - Toast.makeText(this, "This application needs permission to read your phone number to automatically " - + "pre-fill it", Toast.LENGTH_LONG).show(); + Toast.makeText(this, "This application needs permission to read your phone number to automatically pre-fill it", Toast.LENGTH_LONG).show(); } } } @@ -93,7 +102,12 @@ private void setButtonsEnabled(boolean enabled) { } public void onButtonClicked(View view) { - openActivity(getE164Number()); + if (networkConnectivity.isNetworkAvailable(this)) { + openActivity(getE164Number()); + } else { + Toast.makeText(this, "Internet not availble ", Toast.LENGTH_SHORT).show(); + } + } private void resetNumberTextWatcher(String countryIso) { @@ -137,4 +151,6 @@ private String getE164Number() { return mPhoneNumber.getText().toString().replaceAll("\\D", "").trim(); // return PhoneNumberUtils.formatNumberToE164(mPhoneNumber.getText().toString(), mCountryIso); } + + } diff --git a/app/src/main/java/com/msg91/sendotp/sample/VerificationActivity.java b/app/src/main/java/com/msg91/sendotp/sample/ui/VerificationActivity.java similarity index 86% rename from app/src/main/java/com/msg91/sendotp/sample/VerificationActivity.java rename to app/src/main/java/com/msg91/sendotp/sample/ui/VerificationActivity.java index 79e111f..436744a 100644 --- a/app/src/main/java/com/msg91/sendotp/sample/VerificationActivity.java +++ b/app/src/main/java/com/msg91/sendotp/sample/ui/VerificationActivity.java @@ -1,39 +1,43 @@ -package com.msg91.sendotp.sample; +package com.msg91.sendotp.sample.ui; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; -import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; +import android.widget.Toast; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import com.msg91.sendotp.sample.R; +import com.msg91.sendotp.sample.core.DataManager; +import com.msg91.sendotp.sample.utils.NetworkUtils; +import com.msg91.sendotp.sample.utils.OtpEditText; import com.msg91.sendotpandroid.library.internal.SendOTP; import com.msg91.sendotpandroid.library.listners.VerificationListener; import com.msg91.sendotpandroid.library.roots.RetryType; import com.msg91.sendotpandroid.library.roots.SendOTPConfigBuilder; import com.msg91.sendotpandroid.library.roots.SendOTPResponseCode; - public class VerificationActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback, VerificationListener { private static final String TAG = "VerificationActivity"; private static final int OTP_LNGTH = 4; TextView resend_timer; private OtpEditText mOtpEditText; + private NetworkUtils networkConnectivity; + @Override protected void onCreate(Bundle savedInstanceState) { - - super.onCreate(savedInstanceState); setContentView(R.layout.activity_verification); resend_timer = (TextView) findViewById(R.id.resend_timer); @@ -46,8 +50,15 @@ public void onClick(View v) { startTimer(); mOtpEditText = findViewById(R.id.inputCode); mOtpEditText.setMaxLength(OTP_LNGTH); - enableInputField(true); - initiateVerification(); + networkConnectivity = NetworkUtils.getInstance(); + if (networkConnectivity.isNetworkAvailable(this)) { + enableInputField(true); + initiateVerification(); + } else { + Toast.makeText(this, "Internet not availble ", Toast.LENGTH_SHORT).show(); + } + + } void createVerification(String phoneNumber, int countryCode) { @@ -65,7 +76,6 @@ void createVerification(String phoneNumber, int countryCode) { .setMessage("##OTP## is Your verification digits.") .setOtpLength(OTP_LNGTH) .setVerificationCallBack(this).build(); - SendOTP.getInstance().getTrigger().initiate(); @@ -91,14 +101,18 @@ public void ResendCode() { public void onSubmitClicked(View view) { String code = mOtpEditText.getText().toString(); - if (!code.isEmpty()) { - hideKeypad(); - verifyOtp(code); - DataManager.getInstance().showProgressMessage(this, ""); - TextView messageText = (TextView) findViewById(R.id.textView); - messageText.setText("Verification in progress"); - enableInputField(false); + if (networkConnectivity.isNetworkAvailable(this)) { + if (!code.isEmpty()) { + hideKeypad(); + verifyOtp(code); + DataManager.getInstance().showProgressMessage(this, ""); + TextView messageText = (TextView) findViewById(R.id.textView); + messageText.setText("Verification in progress"); + enableInputField(false); + } + }else { + Toast.makeText(this, "Internet not availble ", Toast.LENGTH_SHORT).show(); } } @@ -160,7 +174,7 @@ public void onSendOtpResponse(final SendOTPResponseCode responseCode, final Stri runOnUiThread(new Runnable() { @Override public void run() { - Log.e(TAG, "onSendOtpResponse: " + responseCode.getCode() + "=======" + message); + //Log.e(TAG, "onSendOtpResponse: " + responseCode.getCode() + "=======" + message); if (responseCode == SendOTPResponseCode.DIRECT_VERIFICATION_SUCCESSFUL_FOR_NUMBER || responseCode == SendOTPResponseCode.OTP_VERIFIED) { DataManager.getInstance().hideProgressMessage(); enableInputField(false); @@ -194,12 +208,14 @@ public void run() { DataManager.getInstance().hideProgressMessage(); } else if (responseCode == SendOTPResponseCode.NO_INTERNET_CONNECTED) { DataManager.getInstance().hideProgressMessage(); + Toast.makeText(VerificationActivity.this, "Internet not availble ", Toast.LENGTH_SHORT).show(); } else { DataManager.getInstance().hideProgressMessage(); hideKeypad(); hideProgressBarAndShowMessage(R.string.failed); enableInputField(true); } + SendOTP.getInstance().getTrigger().stop(); } }); } @@ -233,9 +249,5 @@ private void hideKeypad() { } } - @Override - protected void onDestroy() { - super.onDestroy(); - SendOTP.getInstance().getTrigger().stop(); - } + } diff --git a/app/src/main/java/com/msg91/sendotp/sample/CountrySpinner.java b/app/src/main/java/com/msg91/sendotp/sample/utils/CountrySpinner.java similarity index 87% rename from app/src/main/java/com/msg91/sendotp/sample/CountrySpinner.java rename to app/src/main/java/com/msg91/sendotp/sample/utils/CountrySpinner.java index a5916f4..bc97a5f 100644 --- a/app/src/main/java/com/msg91/sendotp/sample/CountrySpinner.java +++ b/app/src/main/java/com/msg91/sendotp/sample/utils/CountrySpinner.java @@ -1,4 +1,4 @@ -package com.msg91.sendotp.sample; +package com.msg91.sendotp.sample.utils; import android.content.Context; import android.util.AttributeSet; @@ -10,12 +10,24 @@ import androidx.appcompat.widget.AppCompatSpinner; import androidx.core.content.ContextCompat; +import com.msg91.sendotp.sample.R; + import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.TreeMap; +/** + * Copyright (C) sendotp-android - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + *

+ * Created by Samset on 23,October,2020 at 6:34 PM for sendotp-android. + *

+ * New Delhi,India + */ + public class CountrySpinner extends AppCompatSpinner { private Map mCountries = new TreeMap<>(); private List mListeners = new ArrayList<>(); diff --git a/app/src/main/java/com/msg91/sendotp/sample/utils/NetworkUtils.java b/app/src/main/java/com/msg91/sendotp/sample/utils/NetworkUtils.java new file mode 100644 index 0000000..6e1b3de --- /dev/null +++ b/app/src/main/java/com/msg91/sendotp/sample/utils/NetworkUtils.java @@ -0,0 +1,52 @@ +package com.msg91.sendotp.sample.utils; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkInfo; +import android.os.Build; + +public class NetworkUtils { + + + private static NetworkUtils ourInstance; + + + private NetworkUtils() { + } + + public static NetworkUtils getInstance() { + return ourInstance == null ? new NetworkUtils() : ourInstance; + } + + + public boolean isNetworkAvailable(Context context) { + final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + + if (cm != null) { + if (Build.VERSION.SDK_INT < 23) { + final NetworkInfo ni = cm.getActiveNetworkInfo(); + + if (ni != null) { + return (ni.isConnected() && (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_MOBILE)); + } + } else { + final Network n = cm.getActiveNetwork(); + + if (n != null) { + final NetworkCapabilities nc = cm.getNetworkCapabilities(n); + + if (nc != null) { + return (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)); + } + } + } + } + + return false; + } + + +} + diff --git a/app/src/main/java/com/msg91/sendotp/sample/OtpEditText.java b/app/src/main/java/com/msg91/sendotp/sample/utils/OtpEditText.java similarity index 91% rename from app/src/main/java/com/msg91/sendotp/sample/OtpEditText.java rename to app/src/main/java/com/msg91/sendotp/sample/utils/OtpEditText.java index a3f1dd2..ea9783b 100644 --- a/app/src/main/java/com/msg91/sendotp/sample/OtpEditText.java +++ b/app/src/main/java/com/msg91/sendotp/sample/utils/OtpEditText.java @@ -1,4 +1,4 @@ -package com.msg91.sendotp.sample; +package com.msg91.sendotp.sample.utils; import android.content.Context; import android.graphics.Canvas; @@ -11,6 +11,18 @@ import androidx.appcompat.widget.AppCompatEditText; +import com.msg91.sendotp.sample.R; + +/** + * Copyright (C) sendotp-android - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited + * Proprietary and confidential + *

+ * Created by Samset on 23,October,2020 at 6:34 PM for sendotp-android. + *

+ * New Delhi,India + */ + public class OtpEditText extends AppCompatEditText { private float mSpace = 10; //24 dp by default, space between the lines private float mNumChars = 4; diff --git a/app/src/main/res/layout/activity_main_new.xml b/app/src/main/res/layout/activity_main_new.xml index f06e4e3..f7e88e8 100644 --- a/app/src/main/res/layout/activity_main_new.xml +++ b/app/src/main/res/layout/activity_main_new.xml @@ -4,7 +4,7 @@ android:layout_height="match_parent" android:background="@drawable/gradient_bg" android:padding="0dip" - tools:context=".MainActivity"> + tools:context=".ui.MainActivity"> - + tools:context=".ui.VerificationActivity"> - -