From 3fd66d6f0bf9c08609e82a8247f5a01ff6c41c30 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Wed, 16 Oct 2019 13:02:54 +0600 Subject: [PATCH 01/20] I am using 3.5 android studio. and Now after adding deependancy on build.gradle its work and running. --- build.gradle | 12 +++++++++++- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 67fa30cbc..b2ee6be7a 100644 --- a/build.gradle +++ b/build.gradle @@ -4,9 +4,11 @@ buildscript { repositories { jcenter() mavenLocal() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'com.android.tools.build:gradle:3.5.0' + classpath "io.realm:realm-gradle-plugin:6.0.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -23,3 +25,11 @@ allprojects { task clean(type: Delete) { delete rootProject.buildDir } + + +allprojects { + repositories { + jcenter() + maven { url 'https://maven.google.com' } + } +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ce88022b7..f0be5ac14 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Aug 16 17:43:49 PDT 2016 +#Wed Oct 16 11:08:18 BDT 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip From 319de1b92591b8707f4b537479a48aacc584f2c9 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sat, 19 Oct 2019 10:13:12 +0600 Subject: [PATCH 02/20] Gradle File Update --- app/build.gradle | 16 ++++++++-------- build.gradle | 2 ++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 113f182b5..b693cd439 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,13 +8,13 @@ repositories { } android { - compileSdkVersion 24 - buildToolsVersion "24.0.1" + compileSdkVersion 28 + buildToolsVersion "29.0.0" defaultConfig { applicationId "com.google.firebase.udacity.friendlychat" minSdkVersion 16 - targetSdkVersion 24 + targetSdkVersion 28 versionCode 1 versionName "1.0" } @@ -32,12 +32,12 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' - compile 'com.android.support:design:24.2.0' - compile 'com.android.support:appcompat-v7:24.2.0' + implementation 'com.android.support:design:28.0.0' + implementation 'com.android.support:appcompat-v7:28.0.0' // Displaying images - compile 'com.github.bumptech.glide:glide:3.6.1' + implementation 'com.github.bumptech.glide:glide:3.6.1' } \ No newline at end of file diff --git a/build.gradle b/build.gradle index b2ee6be7a..a4061caf0 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,8 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath "io.realm:realm-gradle-plugin:6.0.0" + classpath 'com.google.firebase:firebase-auth:16.0.5' + classpath 'com.google.gms:google-services:4.3.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files From be3baf9940a245b68d4c479f744143d903b37a8f Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Mon, 21 Oct 2019 07:58:39 +0600 Subject: [PATCH 03/20] Add Firebase Database Dependencies --- app/build.gradle | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index b693cd439..dfedf7bd9 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' repositories { mavenLocal() @@ -33,6 +34,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.google.firebase:firebase-database:16.0.4' testImplementation 'junit:junit:4.12' implementation 'com.android.support:design:28.0.0' @@ -40,4 +42,4 @@ dependencies { // Displaying images implementation 'com.github.bumptech.glide:glide:3.6.1' -} \ No newline at end of file +} From a8581e1afa4e013e411d9ee67e2f6abe622f8523 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Mon, 21 Oct 2019 10:28:12 +0600 Subject: [PATCH 04/20] Susscssfully Push Massage To The Database. --- .../udacity/friendlychat/MainActivity.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index b9b486cd1..ba358ce7b 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -30,6 +30,9 @@ import android.widget.ListView; import android.widget.ProgressBar; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; + import java.util.ArrayList; import java.util.List; @@ -49,6 +52,9 @@ public class MainActivity extends AppCompatActivity { private String mUsername; + private FirebaseDatabase mFirebaseDatabase; + private DatabaseReference mMessagesDatabaseReference; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -56,6 +62,10 @@ protected void onCreate(Bundle savedInstanceState) { mUsername = ANONYMOUS; + mFirebaseDatabase = FirebaseDatabase.getInstance(); + mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); + + // Initialize references to views mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mMessageListView = (ListView) findViewById(R.id.messageListView); @@ -105,7 +115,9 @@ public void afterTextChanged(Editable editable) { @Override public void onClick(View view) { // TODO: Send messages on click - + // Send messages on click + FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null); + mMessagesDatabaseReference.push().setValue(friendlyMessage); // Clear input box mMessageEditText.setText(""); } From 22ab610739017a0aeaf4ec8bb5246581d7f230cd Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Mon, 21 Oct 2019 10:49:06 +0600 Subject: [PATCH 05/20] Show Massage Back to the apps. --- .../udacity/friendlychat/MainActivity.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index ba358ce7b..aeefe3132 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -16,6 +16,8 @@ package com.google.firebase.udacity.friendlychat; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.InputFilter; @@ -30,6 +32,9 @@ import android.widget.ListView; import android.widget.ProgressBar; +import com.google.firebase.database.ChildEventListener; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; @@ -54,6 +59,7 @@ public class MainActivity extends AppCompatActivity { private FirebaseDatabase mFirebaseDatabase; private DatabaseReference mMessagesDatabaseReference; + private ChildEventListener mChildEventListener; @Override protected void onCreate(Bundle savedInstanceState) { @@ -122,6 +128,34 @@ public void onClick(View view) { mMessageEditText.setText(""); } }); + mChildEventListener = new ChildEventListener() { + @Override + public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class); + mMessageAdapter.add(friendlyMessage); + } + + @Override + public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + + } + + @Override + public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { + + } + + @Override + public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + + } + + @Override + public void onCancelled(@NonNull DatabaseError databaseError) { + + } + }; + mMessagesDatabaseReference.addChildEventListener(mChildEventListener); } @Override From 6784fa471b448c036f8145d69e4c07da77582932 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Tue, 22 Oct 2019 09:46:58 +0600 Subject: [PATCH 06/20] Add Auth dependencies --- app/build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index dfedf7bd9..24499e78f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,8 +35,11 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.google.firebase:firebase-database:16.0.4' + implementation 'com.google.firebase:firebase-auth:16.0.5' + implementation 'com.firebaseui:firebase-ui-auth:6.0.2' testImplementation 'junit:junit:4.12' + //noinspection GradleCompatible implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:appcompat-v7:28.0.0' From 905e35b84767345f439e949ba7075141efc1f048 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Tue, 22 Oct 2019 11:25:51 +0600 Subject: [PATCH 07/20] Project Migrate To Android X --- app/build.gradle | 4 ++-- .../google/firebase/udacity/friendlychat/MainActivity.java | 6 +++--- build.gradle | 1 + gradle.properties | 4 +++- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 24499e78f..d1bc7d898 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,8 +40,8 @@ dependencies { testImplementation 'junit:junit:4.12' //noinspection GradleCompatible - implementation 'com.android.support:design:28.0.0' - implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'com.google.android.material:material:1.0.0' + implementation 'androidx.appcompat:appcompat:1.0.0' // Displaying images implementation 'com.github.bumptech.glide:glide:3.6.1' diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index aeefe3132..3e94cf35e 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -16,9 +16,9 @@ package com.google.firebase.udacity.friendlychat; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.app.AppCompatActivity; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; diff --git a/build.gradle b/build.gradle index a4061caf0..6ed6fa35e 100644 --- a/build.gradle +++ b/build.gradle @@ -33,5 +33,6 @@ allprojects { repositories { jcenter() maven { url 'https://maven.google.com' } + google() } } diff --git a/gradle.properties b/gradle.properties index 1d3591c8a..915f0e66f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,6 @@ # 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 -# org.gradle.parallel=true \ No newline at end of file +# org.gradle.parallel=true +android.enableJetifier=true +android.useAndroidX=true \ No newline at end of file From 896e993f0c12dcb750e2b14ff36a9a2728d41418 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Tue, 22 Oct 2019 15:02:47 +0600 Subject: [PATCH 08/20] Back To the past. Apps is working. After commenting Troubling coge --- app/build.gradle | 10 ++++++---- .../firebase/udacity/friendlychat/MainActivity.java | 10 +++++++--- build.gradle | 2 +- gradle.properties | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d1bc7d898..3deeff67f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,13 +35,15 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.google.firebase:firebase-database:16.0.4' - implementation 'com.google.firebase:firebase-auth:16.0.5' - implementation 'com.firebaseui:firebase-ui-auth:6.0.2' +// implementation 'com.google.firebase:firebase-auth:16.0.5' +// implementation 'com.firebaseui:firebase-ui-auth:6.0.2' testImplementation 'junit:junit:4.12' //noinspection GradleCompatible - implementation 'com.google.android.material:material:1.0.0' - implementation 'androidx.appcompat:appcompat:1.0.0' + implementation 'com.android.support:design:28.0.0' + //implementation 'com.google.android.material:material:1.0.0' + implementation 'com.android.support:appcompat-v7:28.0.0' + //implementation 'androidx.appcompat:appcompat:1.0.0' // Displaying images implementation 'com.github.bumptech.glide:glide:3.6.1' diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 3e94cf35e..4332df1e0 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -16,9 +16,13 @@ package com.google.firebase.udacity.friendlychat; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +//import androidx.annotation.NonNull; +//import androidx.annotation.Nullable; +//import androidx.appcompat.app.AppCompatActivity; import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; diff --git a/build.gradle b/build.gradle index 6ed6fa35e..4121496e7 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,6 @@ allprojects { repositories { jcenter() maven { url 'https://maven.google.com' } - google() + //google() } } diff --git a/gradle.properties b/gradle.properties index 915f0e66f..d5129dccc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,5 +16,5 @@ # 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 # org.gradle.parallel=true -android.enableJetifier=true -android.useAndroidX=true \ No newline at end of file +#android.enableJetifier=true +#android.useAndroidX=true \ No newline at end of file From c163062346ee5962bd2cea2921f31830ee6a16cf Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Thu, 24 Oct 2019 11:32:49 +0600 Subject: [PATCH 09/20] Old Auth that tutorials working. --- app/build.gradle | 6 ++++-- app/src/main/AndroidManifest.xml | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3deeff67f..ab00adcbc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,8 +35,10 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.google.firebase:firebase-database:16.0.4' -// implementation 'com.google.firebase:firebase-auth:16.0.5' -// implementation 'com.firebaseui:firebase-ui-auth:6.0.2' + implementation 'com.google.firebase:firebase-auth:16.0.5' + + implementation 'com.firebaseui:firebase-ui-auth:0.6.0' + testImplementation 'junit:junit:4.12' //noinspection GradleCompatible diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bcd0e213c..b4bf18094 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ + + + + + + From 5c9bcc11e5944a6b7cd3456fc9e562bae570a558 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Fri, 25 Oct 2019 09:27:40 +0600 Subject: [PATCH 10/20] Add Sign mathot but apps is crassing --- app/build.gradle | 1 + .../udacity/friendlychat/MainActivity.java | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index ab00adcbc..c629e3ea9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,7 @@ dependencies { implementation 'com.google.firebase:firebase-database:16.0.4' implementation 'com.google.firebase:firebase-auth:16.0.5' + //implementation 'com.firebaseui:firebase-ui-auth:5.0.0' implementation 'com.firebaseui:firebase-ui-auth:0.6.0' testImplementation 'junit:junit:4.12' diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 4332df1e0..a0961a1e9 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -35,7 +35,11 @@ import android.widget.ImageButton; import android.widget.ListView; import android.widget.ProgressBar; +import android.widget.Toast; +import com.firebase.ui.auth.AuthUI; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; @@ -43,6 +47,7 @@ import com.google.firebase.database.FirebaseDatabase; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { @@ -51,6 +56,7 @@ public class MainActivity extends AppCompatActivity { public static final String ANONYMOUS = "anonymous"; public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000; + public static final int RC_SIGN_IN = 1; private ListView mMessageListView; private MessageAdapter mMessageAdapter; @@ -64,6 +70,8 @@ public class MainActivity extends AppCompatActivity { private FirebaseDatabase mFirebaseDatabase; private DatabaseReference mMessagesDatabaseReference; private ChildEventListener mChildEventListener; + private FirebaseAuth mFirebaseAuth; + private FirebaseAuth.AuthStateListener mAuthStateListener; @Override protected void onCreate(Bundle savedInstanceState) { @@ -73,6 +81,8 @@ protected void onCreate(Bundle savedInstanceState) { mUsername = ANONYMOUS; mFirebaseDatabase = FirebaseDatabase.getInstance(); + mFirebaseAuth = FirebaseAuth.getInstance(); + mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); @@ -160,6 +170,33 @@ public void onCancelled(@NonNull DatabaseError databaseError) { } }; mMessagesDatabaseReference.addChildEventListener(mChildEventListener); + mAuthStateListener = new FirebaseAuth.AuthStateListener() { + @Override + public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { + FirebaseUser user = firebaseAuth.getCurrentUser(); + if(user != null){ + Toast.makeText(MainActivity.this,"Your are now Sign in",Toast.LENGTH_SHORT).show(); + + }else { +// List providers = Arrays.asList( +// new AuthUI.IdpConfig.EmailBuilder().build(), +// new AuthUI.IdpConfig.GoogleBuilder().build()); + + startActivityForResult( + AuthUI.getInstance() + .createSignInIntentBuilder() + .setIsSmartLockEnabled(false) +// .setAvailableProviders(Arrays.asList( +// new AuthUI.IdpConfig.GoogleBuilder().build(), +// new AuthUI.IdpConfig.EmailBuilder().build())) + .setProviders( + AuthUI.EMAIL_PROVIDER, + AuthUI.GOOGLE_PROVIDER) + .build(), + RC_SIGN_IN ); + } + } + }; } @Override @@ -173,4 +210,17 @@ public boolean onCreateOptionsMenu(Menu menu) { public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } + + @Override + protected void onPause() { + super.onPause(); + mFirebaseAuth.removeAuthStateListener(mAuthStateListener); + } + + @Override + protected void onResume() { + super.onResume(); + mFirebaseAuth.addAuthStateListener(mAuthStateListener); + } + } From 7a4c330d9b2a7b317c1aeecdbbad8658da19bf6e Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sun, 27 Oct 2019 11:26:17 +0600 Subject: [PATCH 11/20] Migrate To Android X --- app/build.gradle | 22 ++-- .../udacity/friendlychat/MainActivity.java | 106 +----------------- build.gradle | 1 + gradle.properties | 4 +- 4 files changed, 14 insertions(+), 119 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c629e3ea9..478738bcf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'com.android.application' -apply plugin: 'com.google.gms.google-services' repositories { mavenLocal() @@ -34,20 +33,13 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.google.firebase:firebase-database:16.0.4' - implementation 'com.google.firebase:firebase-auth:16.0.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' - //implementation 'com.firebaseui:firebase-ui-auth:5.0.0' - implementation 'com.firebaseui:firebase-ui-auth:0.6.0' - - testImplementation 'junit:junit:4.12' - - //noinspection GradleCompatible - implementation 'com.android.support:design:28.0.0' - //implementation 'com.google.android.material:material:1.0.0' - implementation 'com.android.support:appcompat-v7:28.0.0' - //implementation 'androidx.appcompat:appcompat:1.0.0' +// implementation 'com.android.support:design:24.2.0' +// implementation 'com.android.support:support-compat:28.0.0' + implementation 'androidx.appcompat:appcompat:1.1.0' // Displaying images - implementation 'com.github.bumptech.glide:glide:3.6.1' -} + implementation 'com.github.bumptech.glide:glide:4.10.0' + annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0' +} \ No newline at end of file diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index a0961a1e9..784056c34 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -14,15 +14,11 @@ * limitations under the License. */ package com.google.firebase.udacity.friendlychat; +import androidx.appcompat.app.AppCompatActivity; + import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.app.AppCompatActivity; -//import androidx.annotation.NonNull; -//import androidx.annotation.Nullable; -//import androidx.appcompat.app.AppCompatActivity; import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; @@ -35,19 +31,8 @@ import android.widget.ImageButton; import android.widget.ListView; import android.widget.ProgressBar; -import android.widget.Toast; - -import com.firebase.ui.auth.AuthUI; -import com.google.firebase.auth.FirebaseAuth; -import com.google.firebase.auth.FirebaseUser; -import com.google.firebase.database.ChildEventListener; -import com.google.firebase.database.DataSnapshot; -import com.google.firebase.database.DatabaseError; -import com.google.firebase.database.DatabaseReference; -import com.google.firebase.database.FirebaseDatabase; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { @@ -56,7 +41,6 @@ public class MainActivity extends AppCompatActivity { public static final String ANONYMOUS = "anonymous"; public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000; - public static final int RC_SIGN_IN = 1; private ListView mMessageListView; private MessageAdapter mMessageAdapter; @@ -67,12 +51,6 @@ public class MainActivity extends AppCompatActivity { private String mUsername; - private FirebaseDatabase mFirebaseDatabase; - private DatabaseReference mMessagesDatabaseReference; - private ChildEventListener mChildEventListener; - private FirebaseAuth mFirebaseAuth; - private FirebaseAuth.AuthStateListener mAuthStateListener; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -80,12 +58,6 @@ protected void onCreate(Bundle savedInstanceState) { mUsername = ANONYMOUS; - mFirebaseDatabase = FirebaseDatabase.getInstance(); - mFirebaseAuth = FirebaseAuth.getInstance(); - - mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); - - // Initialize references to views mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mMessageListView = (ListView) findViewById(R.id.messageListView); @@ -135,68 +107,11 @@ public void afterTextChanged(Editable editable) { @Override public void onClick(View view) { // TODO: Send messages on click - // Send messages on click - FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null); - mMessagesDatabaseReference.push().setValue(friendlyMessage); + // Clear input box mMessageEditText.setText(""); } }); - mChildEventListener = new ChildEventListener() { - @Override - public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { - FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class); - mMessageAdapter.add(friendlyMessage); - } - - @Override - public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { - - } - - @Override - public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { - - } - - @Override - public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { - - } - - @Override - public void onCancelled(@NonNull DatabaseError databaseError) { - - } - }; - mMessagesDatabaseReference.addChildEventListener(mChildEventListener); - mAuthStateListener = new FirebaseAuth.AuthStateListener() { - @Override - public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { - FirebaseUser user = firebaseAuth.getCurrentUser(); - if(user != null){ - Toast.makeText(MainActivity.this,"Your are now Sign in",Toast.LENGTH_SHORT).show(); - - }else { -// List providers = Arrays.asList( -// new AuthUI.IdpConfig.EmailBuilder().build(), -// new AuthUI.IdpConfig.GoogleBuilder().build()); - - startActivityForResult( - AuthUI.getInstance() - .createSignInIntentBuilder() - .setIsSmartLockEnabled(false) -// .setAvailableProviders(Arrays.asList( -// new AuthUI.IdpConfig.GoogleBuilder().build(), -// new AuthUI.IdpConfig.EmailBuilder().build())) - .setProviders( - AuthUI.EMAIL_PROVIDER, - AuthUI.GOOGLE_PROVIDER) - .build(), - RC_SIGN_IN ); - } - } - }; } @Override @@ -210,17 +125,4 @@ public boolean onCreateOptionsMenu(Menu menu) { public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } - - @Override - protected void onPause() { - super.onPause(); - mFirebaseAuth.removeAuthStateListener(mAuthStateListener); - } - - @Override - protected void onResume() { - super.onResume(); - mFirebaseAuth.addAuthStateListener(mAuthStateListener); - } - -} +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4121496e7..351ff0549 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,7 @@ buildscript { // 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 d5129dccc..915f0e66f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,5 +16,5 @@ # 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 # org.gradle.parallel=true -#android.enableJetifier=true -#android.useAndroidX=true \ No newline at end of file +android.enableJetifier=true +android.useAndroidX=true \ No newline at end of file From f25200a854815f0da3d5b57a81878e2482c8c6ee Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sun, 27 Oct 2019 12:16:29 +0600 Subject: [PATCH 12/20] Data is showing again after firebase implitation. --- app/build.gradle | 4 +- .../udacity/friendlychat/MainActivity.java | 53 +++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 478738bcf..8f693ba0a 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' repositories { mavenLocal() @@ -33,6 +34,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.google.firebase:firebase-database:16.0.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' // implementation 'com.android.support:design:24.2.0' @@ -42,4 +44,4 @@ dependencies { // Displaying images implementation 'com.github.bumptech.glide:glide:4.10.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0' -} \ No newline at end of file +} diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 784056c34..348b1d41d 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -14,11 +14,8 @@ * limitations under the License. */ package com.google.firebase.udacity.friendlychat; -import androidx.appcompat.app.AppCompatActivity; - import android.os.Bundle; - import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; @@ -32,6 +29,17 @@ import android.widget.ListView; import android.widget.ProgressBar; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; + + +import com.google.firebase.database.ChildEventListener; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.database.annotations.Nullable; + import java.util.ArrayList; import java.util.List; @@ -51,6 +59,10 @@ public class MainActivity extends AppCompatActivity { private String mUsername; + private FirebaseDatabase mFirebaseDatabase; + private DatabaseReference mMessagesDatabaseReference; + private ChildEventListener mChildEventListener; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -58,6 +70,10 @@ protected void onCreate(Bundle savedInstanceState) { mUsername = ANONYMOUS; + + mFirebaseDatabase = FirebaseDatabase.getInstance(); + mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); + // Initialize references to views mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mMessageListView = (ListView) findViewById(R.id.messageListView); @@ -107,11 +123,42 @@ public void afterTextChanged(Editable editable) { @Override public void onClick(View view) { // TODO: Send messages on click + // Send messages on click + FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null); + mMessagesDatabaseReference.push().setValue(friendlyMessage); // Clear input box mMessageEditText.setText(""); } }); + mChildEventListener = new ChildEventListener() { + @Override + public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class); + mMessageAdapter.add(friendlyMessage); + } + + @Override + public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + + } + + @Override + public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { + + } + + @Override + public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + + } + + @Override + public void onCancelled(@NonNull DatabaseError databaseError) { + + } + }; + mMessagesDatabaseReference.addChildEventListener(mChildEventListener); } @Override From b04a43a187f12e3e57dd64530e59d659b6eb18d4 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sun, 27 Oct 2019 13:02:02 +0600 Subject: [PATCH 13/20] Firebase UI Working and Its Just Too Nice too see. --- app/build.gradle | 6 +++ .../udacity/friendlychat/MainActivity.java | 53 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + build.gradle | 2 +- 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 8f693ba0a..97db316e5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,4 +44,10 @@ dependencies { // Displaying images implementation 'com.github.bumptech.glide:glide:4.10.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0' + + // Firebase UI Implimentation + + implementation 'com.firebaseui:firebase-ui-auth:4.3.1' + + } diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 348b1d41d..c8fa12433 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -28,11 +28,16 @@ import android.widget.ImageButton; import android.widget.ListView; import android.widget.ProgressBar; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import com.firebase.ui.auth.AuthUI; +import com.firebase.ui.auth.IdpResponse; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; @@ -41,6 +46,7 @@ import com.google.firebase.database.annotations.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { @@ -62,6 +68,10 @@ public class MainActivity extends AppCompatActivity { private FirebaseDatabase mFirebaseDatabase; private DatabaseReference mMessagesDatabaseReference; private ChildEventListener mChildEventListener; + private FirebaseAuth mFirebaseAuth; + private FirebaseAuth.AuthStateListener mAuthStateListener; + + private static final int RC_SIGN_IN = 1; @Override protected void onCreate(Bundle savedInstanceState) { @@ -74,6 +84,13 @@ protected void onCreate(Bundle savedInstanceState) { mFirebaseDatabase = FirebaseDatabase.getInstance(); mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); + + // Initialize Firebase components + mFirebaseDatabase = FirebaseDatabase.getInstance(); + mFirebaseAuth = FirebaseAuth.getInstance(); + + + // Initialize references to views mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mMessageListView = (ListView) findViewById(R.id.messageListView); @@ -159,6 +176,42 @@ public void onCancelled(@NonNull DatabaseError databaseError) { } }; mMessagesDatabaseReference.addChildEventListener(mChildEventListener); + + mAuthStateListener = new FirebaseAuth.AuthStateListener() { + @Override + public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { + FirebaseUser user = firebaseAuth.getCurrentUser(); + if (user != null) { + // User is signed in + Toast.makeText(MainActivity.this, "You're now signed in. Welcome to FriendlyChat.", Toast.LENGTH_SHORT).show(); + } else { + // User is signed out + startActivityForResult( + AuthUI.getInstance() + .createSignInIntentBuilder() + .setAvailableProviders(Arrays.asList( + new AuthUI.IdpConfig.GoogleBuilder().build(), + new AuthUI.IdpConfig.EmailBuilder().build(), + new AuthUI.IdpConfig.PhoneBuilder().build())) + .build(), + RC_SIGN_IN); + } + } + }; + } + + @Override + protected void onResume() { + super.onResume(); + mFirebaseAuth.addAuthStateListener(mAuthStateListener); + } + + @Override + protected void onPause() { + super.onPause(); + if (mAuthStateListener != null) { + mFirebaseAuth.removeAuthStateListener(mAuthStateListener); + } } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8bf7fbac7..a4dac53e2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,4 +2,6 @@ Friendly Chat Sign Out Send + + diff --git a/build.gradle b/build.gradle index 351ff0549..cded88118 100644 --- a/build.gradle +++ b/build.gradle @@ -34,6 +34,6 @@ allprojects { repositories { jcenter() maven { url 'https://maven.google.com' } - //google() + google() } } From e99cfc1d96bcabcbe296c4e29791687dabb76aea Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sun, 27 Oct 2019 13:38:17 +0600 Subject: [PATCH 14/20] Finish Sign In Flow --- .../udacity/friendlychat/MainActivity.java | 99 ++++++++++++------- 1 file changed, 61 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index c8fa12433..5f2d92831 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -51,28 +51,23 @@ public class MainActivity extends AppCompatActivity { - private static final String TAG = "MainActivity"; - public static final String ANONYMOUS = "anonymous"; public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000; - + private static final String TAG = "MainActivity"; + private static final int RC_SIGN_IN = 1; private ListView mMessageListView; private MessageAdapter mMessageAdapter; private ProgressBar mProgressBar; private ImageButton mPhotoPickerButton; private EditText mMessageEditText; private Button mSendButton; - private String mUsername; - private FirebaseDatabase mFirebaseDatabase; private DatabaseReference mMessagesDatabaseReference; private ChildEventListener mChildEventListener; private FirebaseAuth mFirebaseAuth; private FirebaseAuth.AuthStateListener mAuthStateListener; - private static final int RC_SIGN_IN = 1; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -90,7 +85,6 @@ protected void onCreate(Bundle savedInstanceState) { mFirebaseAuth = FirebaseAuth.getInstance(); - // Initialize references to views mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mMessageListView = (ListView) findViewById(R.id.messageListView); @@ -148,47 +142,20 @@ public void onClick(View view) { mMessageEditText.setText(""); } }); - mChildEventListener = new ChildEventListener() { - @Override - public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { - FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class); - mMessageAdapter.add(friendlyMessage); - } - - @Override - public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { - } - - @Override - public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { - - } - - @Override - public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { - - } - - @Override - public void onCancelled(@NonNull DatabaseError databaseError) { - - } - }; - mMessagesDatabaseReference.addChildEventListener(mChildEventListener); mAuthStateListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { - // User is signed in - Toast.makeText(MainActivity.this, "You're now signed in. Welcome to FriendlyChat.", Toast.LENGTH_SHORT).show(); + onSignedInInitialize(user.getDisplayName()); } else { - // User is signed out + onSignOutCleanup(); startActivityForResult( AuthUI.getInstance() .createSignInIntentBuilder() + .setIsSmartLockEnabled(false) .setAvailableProviders(Arrays.asList( new AuthUI.IdpConfig.GoogleBuilder().build(), new AuthUI.IdpConfig.EmailBuilder().build(), @@ -212,6 +179,8 @@ protected void onPause() { if (mAuthStateListener != null) { mFirebaseAuth.removeAuthStateListener(mAuthStateListener); } + detachDatabaseReadListener(); + mMessageAdapter.clear(); } @Override @@ -225,4 +194,58 @@ public boolean onCreateOptionsMenu(Menu menu) { public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } + + private void onSignedInInitialize(String username) { + mUsername = username; + attachDatabaseReadListener(); + + } + + private void onSignOutCleanup() { + mUsername = ANONYMOUS; + mMessageAdapter.clear(); + detachDatabaseReadListener(); + + } + + + private void attachDatabaseReadListener() { + if (mChildEventListener == null) { + mChildEventListener = new ChildEventListener() { + @Override + public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class); + mMessageAdapter.add(friendlyMessage); + } + + @Override + public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + + } + + @Override + public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { + + } + + @Override + public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { + + } + + @Override + public void onCancelled(@NonNull DatabaseError databaseError) { + + } + }; + mMessagesDatabaseReference.addChildEventListener(mChildEventListener); + } + } + + private void detachDatabaseReadListener() { + if (mChildEventListener != null) { + mMessagesDatabaseReference.removeEventListener(mChildEventListener); + mChildEventListener = null; + } + } } \ No newline at end of file From 7422b2ae4316149a3738895e1b106fb999bbb88a Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sun, 27 Oct 2019 13:58:20 +0600 Subject: [PATCH 15/20] Back Button Working --- .../udacity/friendlychat/MainActivity.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 5f2d92831..fd8691428 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -15,6 +15,7 @@ */ package com.google.firebase.udacity.friendlychat; +import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.InputFilter; @@ -167,6 +168,22 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { }; } + @Override + protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if(requestCode == RC_SIGN_IN){ + if(resultCode == RESULT_OK){ + Toast.makeText(this,"Sign In",Toast.LENGTH_SHORT).show(); + }else if(resultCode == RESULT_CANCELED){ + Toast.makeText(this,"Sign In Canseled",Toast.LENGTH_SHORT).show(); + finish(); + } + } + } + + + + @Override protected void onResume() { super.onResume(); From 32bb05a4bdaa1b8132a70a1bd0675a8e8738a9c4 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sun, 27 Oct 2019 16:07:58 +0600 Subject: [PATCH 16/20] Sign Out Work Fine --- .../firebase/udacity/friendlychat/MainActivity.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index fd8691428..f74b06181 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -209,7 +209,14 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(MenuItem item) { - return super.onOptionsItemSelected(item); + switch (item.getItemId()){ + case R.id.sign_out_menu: + AuthUI.getInstance().signOut(this); + return true; + + default: + return super.onOptionsItemSelected(item); + } } private void onSignedInInitialize(String username) { From 71dc9f7cf42807924b49aaab3f7b7e654a440e02 Mon Sep 17 00:00:00 2001 From: Sushen Biswas Date: Sun, 27 Oct 2019 17:21:51 +0600 Subject: [PATCH 17/20] Start Working With Firebase Storage. --- app/build.gradle | 6 +-- .../udacity/friendlychat/MainActivity.java | 45 +++++++++++-------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 97db316e5..faac83c8b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,10 +35,11 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.google.firebase:firebase-database:16.0.4' + implementation 'com.google.firebase:firebase-storage:16.0.4' + implementation 'com.google.firebase:firebase-auth:16.0.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' -// implementation 'com.android.support:design:24.2.0' -// implementation 'com.android.support:support-compat:28.0.0' + implementation 'androidx.appcompat:appcompat:1.1.0' // Displaying images @@ -46,7 +47,6 @@ dependencies { annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0' // Firebase UI Implimentation - implementation 'com.firebaseui:firebase-ui-auth:4.3.1' diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index f74b06181..a7d2b8a3d 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -45,6 +45,8 @@ import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.annotations.Nullable; +import com.google.firebase.storage.FirebaseStorage; +import com.google.firebase.storage.StorageReference; import java.util.ArrayList; import java.util.Arrays; @@ -55,7 +57,11 @@ public class MainActivity extends AppCompatActivity { public static final String ANONYMOUS = "anonymous"; public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000; private static final String TAG = "MainActivity"; + + private static final int RC_SIGN_IN = 1; + private static final int RC_PHOTO_PICKER = 2; + private ListView mMessageListView; private MessageAdapter mMessageAdapter; private ProgressBar mProgressBar; @@ -63,11 +69,14 @@ public class MainActivity extends AppCompatActivity { private EditText mMessageEditText; private Button mSendButton; private String mUsername; + private FirebaseDatabase mFirebaseDatabase; private DatabaseReference mMessagesDatabaseReference; private ChildEventListener mChildEventListener; private FirebaseAuth mFirebaseAuth; private FirebaseAuth.AuthStateListener mAuthStateListener; + private FirebaseStorage mFirebaseStorage; + private StorageReference mStorageReference; @Override protected void onCreate(Bundle savedInstanceState) { @@ -77,13 +86,12 @@ protected void onCreate(Bundle savedInstanceState) { mUsername = ANONYMOUS; - mFirebaseDatabase = FirebaseDatabase.getInstance(); - mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); - - // Initialize Firebase components mFirebaseDatabase = FirebaseDatabase.getInstance(); mFirebaseAuth = FirebaseAuth.getInstance(); + mFirebaseStorage = FirebaseStorage.getInstance(); + + mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); // Initialize references to views @@ -102,12 +110,15 @@ protected void onCreate(Bundle savedInstanceState) { mProgressBar.setVisibility(ProgressBar.INVISIBLE); // ImagePickerButton shows an image picker to upload a image for a message - mPhotoPickerButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - // TODO: Fire an intent to show an image picker - } - }); + mPhotoPickerButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("image/jpeg"); + intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); + startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER); + } + }); // Enable Send button when there's text to send mMessageEditText.addTextChangedListener(new TextWatcher() { @@ -171,19 +182,17 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { @Override protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); - if(requestCode == RC_SIGN_IN){ - if(resultCode == RESULT_OK){ - Toast.makeText(this,"Sign In",Toast.LENGTH_SHORT).show(); - }else if(resultCode == RESULT_CANCELED){ - Toast.makeText(this,"Sign In Canseled",Toast.LENGTH_SHORT).show(); + if (requestCode == RC_SIGN_IN) { + if (resultCode == RESULT_OK) { + Toast.makeText(this, "Sign In", Toast.LENGTH_SHORT).show(); + } else if (resultCode == RESULT_CANCELED) { + Toast.makeText(this, "Sign In Canseled", Toast.LENGTH_SHORT).show(); finish(); } } } - - @Override protected void onResume() { super.onResume(); @@ -209,7 +218,7 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()){ + switch (item.getItemId()) { case R.id.sign_out_menu: AuthUI.getInstance().signOut(this); return true; From 2b00153fb4ef37c16ab64d48b5da22077daedb24 Mon Sep 17 00:00:00 2001 From: sushen Date: Sun, 27 Oct 2019 19:31:06 +0600 Subject: [PATCH 18/20] Code for photo upload. --- app/src/main/AndroidManifest.xml | 7 +------ .../udacity/friendlychat/MainActivity.java | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b4bf18094..0ac0fbcb7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,12 +9,6 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> - - - - - - @@ -25,4 +19,5 @@ + diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index a7d2b8a3d..66ceae123 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -16,6 +16,7 @@ package com.google.firebase.udacity.friendlychat; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.text.Editable; import android.text.InputFilter; @@ -37,6 +38,8 @@ import com.firebase.ui.auth.AuthUI; import com.firebase.ui.auth.IdpResponse; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.android.gms.tasks.Task; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; @@ -47,6 +50,7 @@ import com.google.firebase.database.annotations.Nullable; import com.google.firebase.storage.FirebaseStorage; import com.google.firebase.storage.StorageReference; +import com.google.firebase.storage.UploadTask; import java.util.ArrayList; import java.util.Arrays; @@ -76,7 +80,7 @@ public class MainActivity extends AppCompatActivity { private FirebaseAuth mFirebaseAuth; private FirebaseAuth.AuthStateListener mAuthStateListener; private FirebaseStorage mFirebaseStorage; - private StorageReference mStorageReference; + private StorageReference mChatPhotosStorageReference; @Override protected void onCreate(Bundle savedInstanceState) { @@ -92,6 +96,7 @@ protected void onCreate(Bundle savedInstanceState) { mFirebaseStorage = FirebaseStorage.getInstance(); mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); + mChatPhotosStorageReference = mFirebaseStorage.getReference().child("chat_photos"); // Initialize references to views @@ -188,6 +193,18 @@ protected void onActivityResult(int requestCode, int resultCode, @androidx.annot } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "Sign In Canseled", Toast.LENGTH_SHORT).show(); finish(); + }else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){ + Uri selectedImageUri = data.getData(); + StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); + photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener() { + @Override + public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { + Task downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl(); + FriendlyMessage friendlyMessage = + new FriendlyMessage(null,mUsername,downloadUrl.toString()); + mMessagesDatabaseReference.push().setValue(friendlyMessage); + } + }); } } } From 5f57dff6be7479fde812cb0ba4245ea253feb830 Mon Sep 17 00:00:00 2001 From: sushen Date: Tue, 29 Oct 2019 19:11:46 +0600 Subject: [PATCH 19/20] Photo Upload Option Working --- .../udacity/friendlychat/MainActivity.java | 125 +++++++++++++++--- 1 file changed, 106 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 66ceae123..2dc985c4b 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -38,6 +38,8 @@ import com.firebase.ui.auth.AuthUI; import com.firebase.ui.auth.IdpResponse; +import com.google.android.gms.tasks.Continuation; +import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.Task; import com.google.firebase.auth.FirebaseAuth; @@ -187,26 +189,111 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { @Override protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); - if (requestCode == RC_SIGN_IN) { - if (resultCode == RESULT_OK) { - Toast.makeText(this, "Sign In", Toast.LENGTH_SHORT).show(); - } else if (resultCode == RESULT_CANCELED) { - Toast.makeText(this, "Sign In Canseled", Toast.LENGTH_SHORT).show(); - finish(); - }else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){ - Uri selectedImageUri = data.getData(); - StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); - photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener() { - @Override - public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { - Task downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl(); - FriendlyMessage friendlyMessage = - new FriendlyMessage(null,mUsername,downloadUrl.toString()); - mMessagesDatabaseReference.push().setValue(friendlyMessage); - } - }); - } + + + + switch (requestCode){ + case RC_SIGN_IN: + if (resultCode == RESULT_OK){ + Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show(); + }else if (resultCode == RESULT_CANCELED){ + Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show(); + finish(); + } + break; + case RC_PHOTO_PICKER: + if (resultCode == RESULT_OK){ + Toast.makeText(this, "picker in!", Toast.LENGTH_SHORT).show(); + Uri selectedImageUri = data.getData(); + + // Get a reference to store file at chat_photos/ + final StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); + + // Upload file to Firebase Storage + photoRef.putFile(selectedImageUri) + .continueWithTask(new Continuation>() { + @Override + public Task then(@NonNull Task task) throws Exception { + if (!task.isSuccessful()) { + throw task.getException(); + } + + // Continue with the task to get the download URL + return photoRef.getDownloadUrl(); + } + }).addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + Uri downloadUri = task.getResult(); + FriendlyMessage friendlyMessage= new FriendlyMessage(null,mUsername,downloadUri.toString()); + mMessagesDatabaseReference.push().setValue(friendlyMessage); + } else { + // Handle failures + // ... + } + } + }); + } + break; + + default: + Toast.makeText(this, "Something Wrong", Toast.LENGTH_SHORT).show(); + break; } + + +// +// +// +// if (requestCode == RC_SIGN_IN) { +// if (resultCode == RESULT_OK) { +// Toast.makeText(this, "Sign In", Toast.LENGTH_SHORT).show(); +// } else if (resultCode == RESULT_CANCELED) { +// Toast.makeText(this, "Sign In Canseled", Toast.LENGTH_SHORT).show(); +// finish(); +// }else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){ +// Uri selectedImageUri = data.getData(); +// StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); +// photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener() { +//// @Override +//// public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { +//// mChatPhotosStorageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { +//// @Override +//// public void onSuccess(Uri uri) { +//// String url = uri.toString(); +//// +//// //Do what you need to do with url +//// } +//// }); +//// } +// +//// +// @Override +// public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { +// +// Task urlTask = taskSnapshot.getStorage().getDownloadUrl(); +// while (!urlTask.isSuccessful()); +// Uri downloadUrl = urlTask.getResult(); +// final String sdownload_url = String.valueOf(downloadUrl); +// +// FriendlyMessage friendlyMessage = +// new FriendlyMessage(null,mUsername,sdownload_url); +// mMessagesDatabaseReference.push().setValue(friendlyMessage); +// } +// +//// @Override +//// public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { +//// Uri downloadUrl = taskSnapshot.getDownloadUrl(); +//// Uri downloadUrl= taskSnapshot.getMetadata().getReference().getDownloadUrl().toString(); +//// +//// FriendlyMessage friendlyMessage = +//// new FriendlyMessage(null,mUsername,downloadUrl.toString()); +//// mMessagesDatabaseReference.push().setValue(friendlyMessage); +//// } +// }); +// } +// } } From 6008edaf1fa27768169ba4a91c8c4944078c3ab6 Mon Sep 17 00:00:00 2001 From: sushen Date: Tue, 29 Oct 2019 19:57:46 +0600 Subject: [PATCH 20/20] Fire-base Cloud Messaging Work Fine. --- app/build.gradle | 1 + .../udacity/friendlychat/MainActivity.java | 86 ++++--------------- 2 files changed, 17 insertions(+), 70 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index faac83c8b..0eb2276ef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,7 @@ dependencies { implementation 'com.google.firebase:firebase-database:16.0.4' implementation 'com.google.firebase:firebase-storage:16.0.4' implementation 'com.google.firebase:firebase-auth:16.0.5' + implementation 'com.google.firebase:firebase-messaging:17.3.4' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 2dc985c4b..69458cfbf 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -66,7 +66,7 @@ public class MainActivity extends AppCompatActivity { private static final int RC_SIGN_IN = 1; - private static final int RC_PHOTO_PICKER = 2; + private static final int RC_PHOTO_PICKER = 2; private ListView mMessageListView; private MessageAdapter mMessageAdapter; @@ -117,15 +117,15 @@ protected void onCreate(Bundle savedInstanceState) { mProgressBar.setVisibility(ProgressBar.INVISIBLE); // ImagePickerButton shows an image picker to upload a image for a message - mPhotoPickerButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Intent intent = new Intent(Intent.ACTION_GET_CONTENT); - intent.setType("image/jpeg"); - intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); - startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER); - } - }); + mPhotoPickerButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("image/jpeg"); + intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); + startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER); + } + }); // Enable Send button when there's text to send mMessageEditText.addTextChangedListener(new TextWatcher() { @@ -191,18 +191,17 @@ protected void onActivityResult(int requestCode, int resultCode, @androidx.annot super.onActivityResult(requestCode, resultCode, data); - - switch (requestCode){ + switch (requestCode) { case RC_SIGN_IN: - if (resultCode == RESULT_OK){ + if (resultCode == RESULT_OK) { Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show(); - }else if (resultCode == RESULT_CANCELED){ + } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show(); finish(); } break; case RC_PHOTO_PICKER: - if (resultCode == RESULT_OK){ + if (resultCode == RESULT_OK) { Toast.makeText(this, "picker in!", Toast.LENGTH_SHORT).show(); Uri selectedImageUri = data.getData(); @@ -226,7 +225,7 @@ public Task then(@NonNull Task task) throws Except public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { Uri downloadUri = task.getResult(); - FriendlyMessage friendlyMessage= new FriendlyMessage(null,mUsername,downloadUri.toString()); + FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUri.toString()); mMessagesDatabaseReference.push().setValue(friendlyMessage); } else { // Handle failures @@ -241,60 +240,7 @@ public void onComplete(@NonNull Task task) { Toast.makeText(this, "Something Wrong", Toast.LENGTH_SHORT).show(); break; } - - -// -// -// -// if (requestCode == RC_SIGN_IN) { -// if (resultCode == RESULT_OK) { -// Toast.makeText(this, "Sign In", Toast.LENGTH_SHORT).show(); -// } else if (resultCode == RESULT_CANCELED) { -// Toast.makeText(this, "Sign In Canseled", Toast.LENGTH_SHORT).show(); -// finish(); -// }else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){ -// Uri selectedImageUri = data.getData(); -// StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); -// photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener() { -//// @Override -//// public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { -//// mChatPhotosStorageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { -//// @Override -//// public void onSuccess(Uri uri) { -//// String url = uri.toString(); -//// -//// //Do what you need to do with url -//// } -//// }); -//// } -// -//// -// @Override -// public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { -// -// Task urlTask = taskSnapshot.getStorage().getDownloadUrl(); -// while (!urlTask.isSuccessful()); -// Uri downloadUrl = urlTask.getResult(); -// final String sdownload_url = String.valueOf(downloadUrl); -// -// FriendlyMessage friendlyMessage = -// new FriendlyMessage(null,mUsername,sdownload_url); -// mMessagesDatabaseReference.push().setValue(friendlyMessage); -// } -// -//// @Override -//// public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { -//// Uri downloadUrl = taskSnapshot.getDownloadUrl(); -//// Uri downloadUrl= taskSnapshot.getMetadata().getReference().getDownloadUrl().toString(); -//// -//// FriendlyMessage friendlyMessage = -//// new FriendlyMessage(null,mUsername,downloadUrl.toString()); -//// mMessagesDatabaseReference.push().setValue(friendlyMessage); -//// } -// }); -// } -// } - } + } @Override