From 1e29d674c913b2bff04a507d98356d3ddd182462 Mon Sep 17 00:00:00 2001 From: usman18 Date: Thu, 4 Jul 2019 18:50:28 +0530 Subject: [PATCH 1/5] Firebase, git ignore setup --- .gitignore | 74 ++++++++++++++-- .idea/assetWizardSettings.xml | 46 ---------- .idea/caches/build_file_checksums.ser | Bin 537 -> 534 bytes .idea/misc.xml | 13 +-- app/build.gradle | 3 + .../phonenumberverification/MainActivity.java | 82 +++++++++--------- build.gradle | 5 +- 7 files changed, 120 insertions(+), 103 deletions(-) delete mode 100644 .idea/assetWizardSettings.xml diff --git a/.gitignore b/.gitignore index 5edb4ee..a4a200a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,68 @@ +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ *.iml -.gradle -/local.properties -/.idea/libraries -/.idea/modules.xml -/.idea/workspace.xml -.DS_Store -/build -/captures +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +.idea/caches + +# Keystore files +# Uncomment the following line if you do not want to check your keystore files in. +#*.jks + +# External native build folder generated in Android Studio 2.2 and later .externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md + + +/src/main/java/com/uk/espresso/APIKeys.java diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml deleted file mode 100644 index 52311d0..0000000 --- a/.idea/assetWizardSettings.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index da09a94186c0ca92b55d992902b0e50cde7a25b5..a1b04c82fdfa8d93c3f62894bb5466b40d269336 100644 GIT binary patch delta 210 zcmbQqGL2=zbk^*>%EGY|=lJ{a7pIn#Wagz8>!lYZrsSlS7BMhIJFWRF{)3Gp>P72Ifg$#8JY#^5~T$vJN*QHvUAbU%n{l&uB6#x(A BPOAU_ delta 217 zcmbQnGLvP(bk_7|PeRvCoa3J^kXTRECsMEXk|0E|xIx7pIn#Wagz8!!#B$)G@Gu)Xuw^u+Qc` zd!BBBo>aKg;e{m(Jd@uuiV0*j^Pk$`Sd)J?EIaR*U;B>9y^OM2yLo`j`-n{3mJ KpPdy*ssI3J>Q!$5 diff --git a/.idea/misc.xml b/.idea/misc.xml index 99202cc..1dc7455 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,30 +5,31 @@ - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index ceb3222..b8bfda2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 28 @@ -28,4 +29,6 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'com.google.firebase:firebase-core:17.0.0' + implementation 'com.google.firebase:firebase-auth:18.0.0' } diff --git a/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java b/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java index 4d741f3..5d46240 100644 --- a/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java +++ b/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java @@ -1,8 +1,8 @@ package com.shashank.platform.phonenumberverification; import android.content.Intent; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.text.Html; import android.view.View; @@ -14,44 +14,44 @@ import android.widget.Toast; public class MainActivity extends AppCompatActivity { - - TextView otp; - Button generate_otp; - EditText mobile_number; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - requestWindowFeature(Window.FEATURE_NO_TITLE); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_main); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); - toolbar.setNavigationOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - finish(); - } - }); - otp = findViewById(R.id.otp); - generate_otp = findViewById(R.id.generate_otp); - mobile_number = findViewById(R.id.mobile_number); - otp.setText(Html.fromHtml(getResources().getString(R.string.otp))); - generate_otp.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if(mobile_number.getText().toString().equals("")) - Toast.makeText(getApplicationContext(),"Please enter the mobile no.",Toast.LENGTH_SHORT).show(); - else if(mobile_number.getText().length()<10) - Toast.makeText(getApplicationContext(),"Please enter correct mobile no.",Toast.LENGTH_SHORT).show(); - else{ - Intent intent = new Intent(getApplicationContext(),Main2Activity.class); - startActivity(intent); - } - - } - }); - - } + + TextView otp; + Button generate_otp; + EditText mobile_number; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_NO_TITLE); + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); + setContentView(R.layout.activity_main); + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); + otp = findViewById(R.id.otp); + generate_otp = findViewById(R.id.generate_otp); + mobile_number = findViewById(R.id.mobile_number); + otp.setText(Html.fromHtml(getResources().getString(R.string.otp))); + generate_otp.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (mobile_number.getText().toString().equals("")) + Toast.makeText(getApplicationContext(), "Please enter the mobile no.", Toast.LENGTH_SHORT).show(); + else if (mobile_number.getText().length() < 10) + Toast.makeText(getApplicationContext(), "Please enter correct mobile no.", Toast.LENGTH_SHORT).show(); + else { + Intent intent = new Intent(getApplicationContext(), Main2Activity.class); + startActivity(intent); + } + + } + }); + + } } diff --git a/build.gradle b/build.gradle index 43c0708..f749873 100644 --- a/build.gradle +++ b/build.gradle @@ -3,13 +3,13 @@ buildscript { repositories { + mavenCentral() google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' - - + classpath 'com.google.gms:google-services:4.2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } @@ -17,6 +17,7 @@ buildscript { allprojects { repositories { + mavenCentral() google() jcenter() } From e11c10a5a69bad5ed1f17f4e29c4f81c2c84f269 Mon Sep 17 00:00:00 2001 From: usman18 Date: Thu, 4 Jul 2019 20:42:31 +0530 Subject: [PATCH 2/5] Migrated to Android X --- .idea/caches/build_file_checksums.ser | Bin 534 -> 534 bytes app/build.gradle | 12 ++++++------ .../phonenumberverification/Main2Activity.java | 5 +++-- .../phonenumberverification/MainActivity.java | 5 +++-- app/src/main/res/layout/activity_main.xml | 10 +++++----- app/src/main/res/layout/activity_main2.xml | 10 +++++----- build.gradle | 6 +++--- gradle.properties | 4 ++++ gradle/wrapper/gradle-wrapper.properties | 4 ++-- 9 files changed, 31 insertions(+), 25 deletions(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index a1b04c82fdfa8d93c3f62894bb5466b40d269336..4b91b1d08869de9fa9235b40f67b0e3622c6fbbc 100644 GIT binary patch delta 73 zcmV-P0Ji^@1eOGlnFBh72$7uO5O$G)b8qZ62b?F fX>DV%7{Zh20UZ%}4_#e0bI{Q@PNRlhVP-CP%Wfd# delta 73 zcmV-P0Ji^@1eOGlnFAveU6GvO5Pb(_R;qT3$S;xUz3diOArvdc2LjlDfl?5 fK*4bGVlR{E0UZ$l)Ra;_iY0wu9ojDk=%Jf - - - - + - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main2.xml b/app/src/main/res/layout/activity_main2.xml index 028a344..7760481 100644 --- a/app/src/main/res/layout/activity_main2.xml +++ b/app/src/main/res/layout/activity_main2.xml @@ -1,5 +1,5 @@ - - - - + - \ No newline at end of file + \ No newline at end of file diff --git a/build.gradle b/build.gradle index f749873..819ee11 100644 --- a/build.gradle +++ b/build.gradle @@ -1,15 +1,15 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - + repositories { mavenCentral() google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.3' - classpath 'com.google.gms:google-services:4.2.0' + classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.google.gms:google-services:4.0.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } diff --git a/gradle.properties b/gradle.properties index 743d692..4bf3f42 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,10 @@ # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. org.gradle.jvmargs=-Xmx1536m + +android.useAndroidX=true +android.enableJetifier=true + # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a6d24d8..8d0b0a9 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Dec 23 16:54:58 IST 2018 +#Thu Jul 04 19:10:50 IST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip From c00d07c0fd637d8f79aac2c2d483a55a1ea1376c Mon Sep 17 00:00:00 2001 From: usman18 Date: Thu, 4 Jul 2019 21:16:32 +0530 Subject: [PATCH 3/5] Variables converted to private, camelcase notations used --- .../Main2Activity.java | 278 ++++++++++-------- .../phonenumberverification/MainActivity.java | 33 ++- app/src/main/res/layout/activity_main2.xml | 10 + app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 2 +- 5 files changed, 187 insertions(+), 137 deletions(-) diff --git a/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java b/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java index b8b986b..c513ac7 100644 --- a/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java +++ b/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java @@ -14,129 +14,157 @@ import androidx.appcompat.widget.Toolbar; public class Main2Activity extends AppCompatActivity { - - TextView otp; - EditText otp_box_1,otp_box_2,otp_box_3,otp_box_4,otp_box_5,otp_box_6; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - requestWindowFeature(Window.FEATURE_NO_TITLE); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.activity_main2); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); - toolbar.setNavigationOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - finish(); - } - }); - otp = findViewById(R.id.otp); - otp_box_1 = findViewById(R.id.otp_box_1); - otp_box_2 = findViewById(R.id.otp_box_2); - otp_box_3 = findViewById(R.id.otp_box_3); - otp_box_4 = findViewById(R.id.otp_box_4); - otp_box_5 = findViewById(R.id.otp_box_5); - otp_box_6 = findViewById(R.id.otp_box_6); - otp.setText(Html.fromHtml(getResources().getString(R.string.otp1))); - otp_box_1.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void afterTextChanged(Editable editable) { - - if(editable!=null){ - if(editable.length()==1) - otp_box_2.requestFocus(); - } - } - }); - otp_box_2.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void afterTextChanged(Editable editable) { - if(editable!=null){ - if(editable.length()==1) - otp_box_3.requestFocus(); - } - } - }); - otp_box_3.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void afterTextChanged(Editable editable) { - if(editable!=null){ - if(editable.length()==1) - otp_box_4.requestFocus(); - } - } - }); - otp_box_4.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void afterTextChanged(Editable editable) { - if(editable!=null){ - if(editable.length()==1) - otp_box_5.requestFocus(); - } - } - }); - otp_box_5.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void afterTextChanged(Editable editable) { - if(editable!=null){ - if(editable.length()==1) - otp_box_6.requestFocus(); - } - } - }); - - } + + //Instance variables prefixed which tells what the component actually is, which other would be confusing + + private TextView tvOtpInstruction; + private TextView tvMobileNumber; + + private EditText etOtpBox1; + private EditText etOtpBox2; + private EditText etOtpBox3; + private EditText etOtpBox4; + private EditText etOtpBox5; + private EditText etOtpBox6; + + private String mobileNumber = ""; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_NO_TITLE); + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); + setContentView(R.layout.activity_main2); + + Bundle bundle = getIntent().getExtras(); + if (bundle != null) { + mobileNumber = bundle.getString(MainActivity.MOBILE_NUMBER); + if (mobileNumber != null) { + //Error + } + } + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); + + tvOtpInstruction = findViewById(R.id.otp); + tvMobileNumber = findViewById(R.id.tvMobileNumber); + + etOtpBox1 = findViewById(R.id.otp_box_1); + etOtpBox2 = findViewById(R.id.otp_box_2); + etOtpBox3 = findViewById(R.id.otp_box_3); + etOtpBox4 = findViewById(R.id.otp_box_4); + etOtpBox5 = findViewById(R.id.otp_box_5); + etOtpBox6 = findViewById(R.id.otp_box_6); + + String promptMessage = getResources().getString(R.string.otp1); + tvOtpInstruction.setText(Html.fromHtml(promptMessage)); + + tvMobileNumber.setText(mobileNumber); + + etOtpBox1.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void afterTextChanged(Editable editable) { + + if (editable != null) { + if (editable.length() == 1) + etOtpBox2.requestFocus(); + } + } + }); + etOtpBox2.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void afterTextChanged(Editable editable) { + if (editable != null) { + if (editable.length() == 1) + etOtpBox3.requestFocus(); + } + } + }); + etOtpBox3.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void afterTextChanged(Editable editable) { + if (editable != null) { + if (editable.length() == 1) + etOtpBox4.requestFocus(); + } + } + }); + etOtpBox4.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void afterTextChanged(Editable editable) { + if (editable != null) { + if (editable.length() == 1) + etOtpBox5.requestFocus(); + } + } + }); + etOtpBox5.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void afterTextChanged(Editable editable) { + if (editable != null) { + if (editable.length() == 1) + etOtpBox6.requestFocus(); + } + } + }); + + } } diff --git a/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java b/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java index ddfa0ad..92cf3eb 100644 --- a/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java +++ b/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.os.Bundle; import android.text.Html; +import android.text.TextUtils; import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -16,9 +17,14 @@ public class MainActivity extends AppCompatActivity { - TextView otp; - Button generate_otp; - EditText mobile_number; + //Instance variables prefixed which tells what the component actually is, which other would be confusing + private TextView tvOtpInstruction; + private Button btnGenerateOtp; + private EditText etMobileNumber; + + private String indiaCode = "+91"; + + public static final String MOBILE_NUMBER = "mobile_number"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -26,6 +32,7 @@ protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); @@ -35,19 +42,23 @@ public void onClick(View v) { finish(); } }); - otp = findViewById(R.id.otp); - generate_otp = findViewById(R.id.generate_otp); - mobile_number = findViewById(R.id.mobile_number); - otp.setText(Html.fromHtml(getResources().getString(R.string.otp))); - generate_otp.setOnClickListener(new View.OnClickListener() { + + tvOtpInstruction = findViewById(R.id.otp); + btnGenerateOtp = findViewById(R.id.generate_otp); + etMobileNumber = findViewById(R.id.mobile_number); + + tvOtpInstruction.setText(Html.fromHtml(getResources().getString(R.string.otp))); + + btnGenerateOtp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - if (mobile_number.getText().toString().equals("")) + if (TextUtils.isEmpty(etMobileNumber.getText().toString())) //Using TextUtils will not only make sure for empty string but also for a string with just spaces Toast.makeText(getApplicationContext(), "Please enter the mobile no.", Toast.LENGTH_SHORT).show(); - else if (mobile_number.getText().length() < 10) + else if (etMobileNumber.getText().length() < 10) Toast.makeText(getApplicationContext(), "Please enter correct mobile no.", Toast.LENGTH_SHORT).show(); else { - Intent intent = new Intent(getApplicationContext(), Main2Activity.class); + Intent intent = new Intent(MainActivity.this, Main2Activity.class); //"this" should be used as long as possible, getApplicationContext() should be passed when instantiating a service or a background task which doesn't have a UI element associated with it + intent.putExtra(MOBILE_NUMBER, indiaCode + etMobileNumber.getText().toString()); startActivity(intent); } diff --git a/app/src/main/res/layout/activity_main2.xml b/app/src/main/res/layout/activity_main2.xml index 7760481..2ba9bff 100644 --- a/app/src/main/res/layout/activity_main2.xml +++ b/app/src/main/res/layout/activity_main2.xml @@ -70,6 +70,16 @@ android:textAlignment="center" android:textSize="16sp" /> + + #2c987d #2c987d #2c987d + #000000 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9815b8c..923a13e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,5 @@ Phone Number Verification We will send you an One Time Password on this mobile number]]> - Enter OTP send to +91-9876-564-656]]> + Enter OTP sent to ]]> From 911d3a65449e0748e7f243c027d49e048765bbb3 Mon Sep 17 00:00:00 2001 From: usman18 Date: Fri, 5 Jul 2019 00:34:45 +0530 Subject: [PATCH 4/5] Firebase authentication successful --- .../Main2Activity.java | 215 +++++++++++++++++- .../phonenumberverification/MainActivity.java | 5 +- app/src/main/res/layout/activity_main2.xml | 1 + 3 files changed, 216 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java b/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java index c513ac7..d3db45a 100644 --- a/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java +++ b/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java @@ -1,21 +1,39 @@ package com.shashank.platform.phonenumberverification; +import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.Html; +import android.text.TextUtils; import android.text.TextWatcher; +import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; +import android.widget.Button; import android.widget.EditText; import android.widget.TextView; +import android.widget.Toast; +import com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.Task; +import com.google.android.gms.tasks.TaskExecutors; +import com.google.firebase.FirebaseException; +import com.google.firebase.auth.AuthResult; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.PhoneAuthCredential; +import com.google.firebase.auth.PhoneAuthProvider; + +import java.util.concurrent.TimeUnit; + +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; public class Main2Activity extends AppCompatActivity { - //Instance variables prefixed which tells what the component actually is, which other would be confusing + + private String verificationId; private TextView tvOtpInstruction; private TextView tvMobileNumber; @@ -27,8 +45,13 @@ public class Main2Activity extends AppCompatActivity { private EditText etOtpBox5; private EditText etOtpBox6; + private Button btnVerify; + private String mobileNumber = ""; + private FirebaseAuth mAuth; + + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -36,6 +59,19 @@ protected void onCreate(Bundle savedInstanceState) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main2); + getExtrasFromBundle(); + initialize(); + sendVerificationCode(mobileNumber); + + } + + + /** + * Retrieves the mobile number to which the verification code is supposed to be received + * + */ + private void getExtrasFromBundle() { + Bundle bundle = getIntent().getExtras(); if (bundle != null) { mobileNumber = bundle.getString(MainActivity.MOBILE_NUMBER); @@ -44,7 +80,19 @@ protected void onCreate(Bundle savedInstanceState) { } } - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + } + + + /* + * Initializes all widgets and variables and sets up required listeners + * + * */ + + private void initialize() { + + mAuth = FirebaseAuth.getInstance(); + + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @@ -64,11 +112,81 @@ public void onClick(View v) { etOtpBox5 = findViewById(R.id.otp_box_5); etOtpBox6 = findViewById(R.id.otp_box_6); + btnVerify = findViewById(R.id.verify); + String promptMessage = getResources().getString(R.string.otp1); tvOtpInstruction.setText(Html.fromHtml(promptMessage)); tvMobileNumber.setText(mobileNumber); + + btnVerify.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + String code = ""; + + if (TextUtils.isEmpty(etOtpBox1.getText().toString())) { + Toast.makeText(Main2Activity.this, "Please enter full code ", Toast.LENGTH_LONG) + .show(); + return; + } else { + code += etOtpBox1.getText().toString(); + } + + if (TextUtils.isEmpty(etOtpBox2.getText().toString())) { + Toast.makeText(Main2Activity.this, "Please enter full code ", Toast.LENGTH_LONG) + .show(); + return; + } else { + code += etOtpBox2.getText().toString(); + } + + + if (TextUtils.isEmpty(etOtpBox3.getText().toString())) { + Toast.makeText(Main2Activity.this, "Please enter full code ", Toast.LENGTH_LONG) + .show(); + return; + } else { + code += etOtpBox3.getText().toString(); + } + + + if (TextUtils.isEmpty(etOtpBox4.getText().toString())) { + Toast.makeText(Main2Activity.this, "Please enter full code ", Toast.LENGTH_LONG) + .show(); + return; + } else { + code += etOtpBox4.getText().toString(); + } + + + if (TextUtils.isEmpty(etOtpBox5.getText().toString())) { + Toast.makeText(Main2Activity.this, "Please enter full code ", Toast.LENGTH_LONG) + .show(); + return; + } else { + code += etOtpBox5.getText().toString(); + } + + + + if (TextUtils.isEmpty(etOtpBox6.getText().toString())) { + Toast.makeText(Main2Activity.this, "Please enter full code ", Toast.LENGTH_LONG) + .show(); + return; + } else { + code += etOtpBox6.getText().toString(); + } + + + verifyCode(code); + + } + }); + + + etOtpBox1.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { @@ -89,6 +207,8 @@ public void afterTextChanged(Editable editable) { } } }); + + etOtpBox2.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { @@ -108,6 +228,8 @@ public void afterTextChanged(Editable editable) { } } }); + + etOtpBox3.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { @@ -127,6 +249,8 @@ public void afterTextChanged(Editable editable) { } } }); + + etOtpBox4.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { @@ -146,6 +270,8 @@ public void afterTextChanged(Editable editable) { } } }); + + etOtpBox5.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { @@ -165,6 +291,91 @@ public void afterTextChanged(Editable editable) { } } }); + + + } + + /** + * Initiates the process of sending the verification code on the mobile number entered by the user in previous activity + */ + + private void sendVerificationCode(String number) { + //Todo : show progress bar here + + PhoneAuthProvider.getInstance().verifyPhoneNumber( + number, + 60, + TimeUnit.SECONDS, + TaskExecutors.MAIN_THREAD, + mCallBack + ); + + } + + + private PhoneAuthProvider.OnVerificationStateChangedCallbacks + mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { + + @Override + public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) { + super.onCodeSent(s, forceResendingToken); + verificationId = s; + } + + @Override + public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { + String code = phoneAuthCredential.getSmsCode(); + if (code != null) { + setCodeToEditTexts(code); + verifyCode(code); + } + } + + @Override + public void onVerificationFailed(FirebaseException e) { + Toast.makeText(Main2Activity.this, e.getMessage(), Toast.LENGTH_LONG).show(); + } + }; + + + + private void verifyCode(String code) { + PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); + signInWithCredential(credential); + } + + private void signInWithCredential(PhoneAuthCredential credential) { + mAuth.signInWithCredential(credential) + .addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + Toast.makeText(Main2Activity.this, "Verification successful", Toast.LENGTH_LONG) + .show(); + } else { + Toast.makeText(Main2Activity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show(); + } + } + }); + } + + + + private void setCodeToEditTexts(String code) { + + //Assuming code length is 6 + if (code.length() == 6) { + + etOtpBox1.setText(code.substring(0 ,1)); + etOtpBox2.setText(code.substring(1 ,2)); + etOtpBox3.setText(code.substring(2 ,3)); + etOtpBox4.setText(code.substring(3 ,4)); + etOtpBox5.setText(code.substring(4 ,5)); + etOtpBox6.setText(code.substring(5 ,6)); + + } } + + } diff --git a/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java b/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java index 92cf3eb..8bcb0a0 100644 --- a/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java +++ b/app/src/main/java/com/shashank/platform/phonenumberverification/MainActivity.java @@ -17,7 +17,6 @@ public class MainActivity extends AppCompatActivity { - //Instance variables prefixed which tells what the component actually is, which other would be confusing private TextView tvOtpInstruction; private Button btnGenerateOtp; private EditText etMobileNumber; @@ -52,12 +51,12 @@ public void onClick(View v) { btnGenerateOtp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - if (TextUtils.isEmpty(etMobileNumber.getText().toString())) //Using TextUtils will not only make sure for empty string but also for a string with just spaces + if (TextUtils.isEmpty(etMobileNumber.getText().toString())) Toast.makeText(getApplicationContext(), "Please enter the mobile no.", Toast.LENGTH_SHORT).show(); else if (etMobileNumber.getText().length() < 10) Toast.makeText(getApplicationContext(), "Please enter correct mobile no.", Toast.LENGTH_SHORT).show(); else { - Intent intent = new Intent(MainActivity.this, Main2Activity.class); //"this" should be used as long as possible, getApplicationContext() should be passed when instantiating a service or a background task which doesn't have a UI element associated with it + Intent intent = new Intent(MainActivity.this, Main2Activity.class); intent.putExtra(MOBILE_NUMBER, indiaCode + etMobileNumber.getText().toString()); startActivity(intent); } diff --git a/app/src/main/res/layout/activity_main2.xml b/app/src/main/res/layout/activity_main2.xml index 2ba9bff..40f90b5 100644 --- a/app/src/main/res/layout/activity_main2.xml +++ b/app/src/main/res/layout/activity_main2.xml @@ -78,6 +78,7 @@ android:layout_gravity="center" android:textSize="16sp" android:fontFamily="@font/gotham" + android:focusableInTouchMode="true" /> Date: Fri, 5 Jul 2019 00:43:28 +0530 Subject: [PATCH 5/5] Progress bar added to show loading --- .../Main2Activity.java | 99 +++++++++---------- app/src/main/res/layout/activity_main2.xml | 11 +++ app/src/main/res/values/strings.xml | 2 +- 3 files changed, 61 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java b/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java index d3db45a..3122328 100644 --- a/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java +++ b/app/src/main/java/com/shashank/platform/phonenumberverification/Main2Activity.java @@ -1,17 +1,16 @@ package com.shashank.platform.phonenumberverification; -import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.Html; import android.text.TextUtils; import android.text.TextWatcher; -import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -45,12 +44,36 @@ public class Main2Activity extends AppCompatActivity { private EditText etOtpBox5; private EditText etOtpBox6; + private ProgressBar loadingProgressBar; + private Button btnVerify; private String mobileNumber = ""; private FirebaseAuth mAuth; - + private PhoneAuthProvider.OnVerificationStateChangedCallbacks + mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { + + @Override + public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) { + super.onCodeSent(s, forceResendingToken); + verificationId = s; + } + + @Override + public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { + String code = phoneAuthCredential.getSmsCode(); + if (code != null) { + setCodeToEditTexts(code); + verifyCode(code); + } + } + + @Override + public void onVerificationFailed(FirebaseException e) { + Toast.makeText(Main2Activity.this, e.getMessage(), Toast.LENGTH_LONG).show(); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { @@ -67,8 +90,7 @@ protected void onCreate(Bundle savedInstanceState) { /** - * Retrieves the mobile number to which the verification code is supposed to be received - * + * Retrieves the mobile number to which the verification code is supposed to be received */ private void getExtrasFromBundle() { @@ -83,11 +105,12 @@ private void getExtrasFromBundle() { } - /* - * Initializes all widgets and variables and sets up required listeners - * - * */ + + /* + * Initializes all widgets and variables and sets up required listeners + * + * */ private void initialize() { mAuth = FirebaseAuth.getInstance(); @@ -112,6 +135,8 @@ public void onClick(View v) { etOtpBox5 = findViewById(R.id.otp_box_5); etOtpBox6 = findViewById(R.id.otp_box_6); + loadingProgressBar = findViewById(R.id.pbLoading); + btnVerify = findViewById(R.id.verify); String promptMessage = getResources().getString(R.string.otp1); @@ -170,7 +195,6 @@ public void onClick(View v) { } - if (TextUtils.isEmpty(etOtpBox6.getText().toString())) { Toast.makeText(Main2Activity.this, "Please enter full code ", Toast.LENGTH_LONG) .show(); @@ -186,7 +210,6 @@ public void onClick(View v) { }); - etOtpBox1.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { @@ -291,17 +314,18 @@ public void afterTextChanged(Editable editable) { } } }); + + + } - } /** - * Initiates the process of sending the verification code on the mobile number entered by the user in previous activity + * Initiates the process of sending the verification code on the mobile number entered by the user in previous activity */ - private void sendVerificationCode(String number) { - //Todo : show progress bar here - + loadingProgressBar.setVisibility(View.VISIBLE); + PhoneAuthProvider.getInstance().verifyPhoneNumber( number, 60, @@ -312,38 +336,12 @@ private void sendVerificationCode(String number) { } - - private PhoneAuthProvider.OnVerificationStateChangedCallbacks - mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { - - @Override - public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) { - super.onCodeSent(s, forceResendingToken); - verificationId = s; - } - - @Override - public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { - String code = phoneAuthCredential.getSmsCode(); - if (code != null) { - setCodeToEditTexts(code); - verifyCode(code); - } - } - - @Override - public void onVerificationFailed(FirebaseException e) { - Toast.makeText(Main2Activity.this, e.getMessage(), Toast.LENGTH_LONG).show(); - } - }; - - - private void verifyCode(String code) { PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); signInWithCredential(credential); } + private void signInWithCredential(PhoneAuthCredential credential) { mAuth.signInWithCredential(credential) .addOnCompleteListener(new OnCompleteListener() { @@ -355,23 +353,24 @@ public void onComplete(@NonNull Task task) { } else { Toast.makeText(Main2Activity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show(); } + + loadingProgressBar.setVisibility(View.INVISIBLE); } }); } - private void setCodeToEditTexts(String code) { //Assuming code length is 6 if (code.length() == 6) { - etOtpBox1.setText(code.substring(0 ,1)); - etOtpBox2.setText(code.substring(1 ,2)); - etOtpBox3.setText(code.substring(2 ,3)); - etOtpBox4.setText(code.substring(3 ,4)); - etOtpBox5.setText(code.substring(4 ,5)); - etOtpBox6.setText(code.substring(5 ,6)); + etOtpBox1.setText(code.substring(0, 1)); + etOtpBox2.setText(code.substring(1, 2)); + etOtpBox3.setText(code.substring(2, 3)); + etOtpBox4.setText(code.substring(3, 4)); + etOtpBox5.setText(code.substring(4, 5)); + etOtpBox6.setText(code.substring(5, 6)); } diff --git a/app/src/main/res/layout/activity_main2.xml b/app/src/main/res/layout/activity_main2.xml index 40f90b5..d86ed03 100644 --- a/app/src/main/res/layout/activity_main2.xml +++ b/app/src/main/res/layout/activity_main2.xml @@ -69,6 +69,8 @@ android:lineSpacingExtra="6dp" android:textAlignment="center" android:textSize="16sp" /> + + + + Phone Number Verification We will send you an One Time Password on this mobile number]]> - Enter OTP sent to ]]> + OTP will be detected automatically or you can manually enter OTP sent to ]]>