diff --git a/README.md b/README.md index 3ee78ccad..94bd49e56 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ We'll go into each of these steps below. ### Add SDK dependencies -Since FirebaseUI depends on the SDKs of various providers, we'll need to include those in our depedencies as well. +Since FirebaseUI depends on the SDKs of various providers, we'll need to include those in our dependencies as well. ``` dependencies { @@ -62,9 +62,9 @@ dependencies { } ``` -### Add Facebook/Twitter/Google keys to strings.xml +### Add Facebook/Twitter/Google keys -Open your `res/values/strings.xml` file and add the following lines, replacing `[VALUE]` with your key. +Open your application's `res/values/strings.xml` file and add the following lines, replacing `[VALUE]` with your key. Keep in mind, these are all optional. You only have to provide values for the providers you plan to use. @@ -74,9 +74,10 @@ Keep in mind, these are all optional. You only have to provide values for the pr [VALUE] [VALUE] [VALUE] -[VALUE] ``` +If you're using Google authentication, place your `google-services.json` in the app folder. + ### Change our AndroidManifest.xml Open your `manifests/AndroidManifest.xml` file. This will allow Android to recognize the various activities that FirebaseUI exposes. @@ -114,18 +115,6 @@ If you're using Facebook authentication, add the following to your ` ``` -If you're using Google authentication, add the following to your `` tag. - -```xml - - - ``` - -**Note:** If you're using Google Sign-in you'll also need to ensure that your `google-services.json` file is created -and placed in your app folder. - ### Inherit from FirebaseLoginBaseActivity Now we get to the juicy bits. Open your `MainActivity` and change your activity to extend `FirebaseLoginBaseActivity` @@ -147,27 +136,34 @@ public class MainActivity extends FirebaseLoginBaseActivity { } @Override - public void onFirebaseLoggedIn(AuthData authData) { - // TODO: Handle successful login + public void onFirebaseLoginProviderError(FirebaseLoginError firebaseError) { + // TODO: Handle an error from the authentication provider } @Override - public void onFirebaseLoggedOut() { - // TODO: Handle logout + public void onFirebaseLoginUserError(FirebaseLoginError firebaseError) { + // TODO: Handle an error from the user } +} +``` + +In addition you can override these methods to customize what happens when a user logs in or out: +``` @Override - public void onFirebaseLoginProviderError(FirebaseLoginError firebaseError) { - // TODO: Handle an error from the authentication provider + public void onFirebaseLoggedIn(AuthData authData) { + // TODO: Handle successful login } @Override - public void onFirebaseLoginUserError(FirebaseLoginError firebaseError) { - // TODO: Handle an error from the user + public void onFirebaseLoggedOut() { + // TODO: Handle logout } -} + ``` +If you want to know the current `AuthData` at any point, you can call `getAuth()`. This will return the `AuthData` for the currently authenticated user, or `null` if no user is authenticated. + ### Enable Authentication Providers Now that our activity is set up, we can enable authentication providers. The FirebaseUI login prompt will only display providers you enable here, so don't worry if you don't want to use them all! @@ -179,15 +175,15 @@ public class MainActivity extends FirebaseLoginBaseActivity { protected void onStart() { super.onStart(); // All providers are optional! Remove any you don't want. - setEnabledAuthProvider(SocialProvider.facebook); - setEnabledAuthProvider(SocialProvider.twitter); - setEnabledAuthProvider(SocialProvider.google); - setEnabledAuthProvider(SocialProvider.password); + setEnabledAuthProvider(AuthProviderType.FACEBOOK); + setEnabledAuthProvider(AuthProviderType.TWITTER); + setEnabledAuthProvider(AuthProviderType.GOOGLE); + setEnabledAuthProvider(AuthProviderType.PASSWORD); } ``` -### Call showFirebaseLoginDialog(); +### Call showFirebaseLoginPrompt(); You're now ready to display the login dialog! @@ -331,7 +327,7 @@ protected void onCreate(Bundle savedInstanceState) { mAdapter = new FirebaseListAdapter(this, ChatMessage.class, android.R.layout.two_line_list_item, ref) { @Override - protected void populateView(View view, ChatMessage chatMessage) { + protected void populateView(View view, ChatMessage chatMessage, int position) { ((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName()); ((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage()); @@ -385,7 +381,7 @@ protected void onCreate(Bundle savedInstanceState) { mAdapter = new FirebaseListAdapter(this, ChatMessage.class, android.R.layout.two_line_list_item, ref) { @Override - protected void populateView(View view, ChatMessage chatMessage) { + protected void populateView(View view, ChatMessage chatMessage, int position) { ((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName()); ((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage()); } @@ -428,7 +424,7 @@ If we use the same layout as before (`android.R.layout.two_line_list_item`), the We can wrap that in a ViewHolder with: ```java -private static class ChatMessageViewHolder extends RecyclerView.ViewHolder { +public static class ChatMessageViewHolder extends RecyclerView.ViewHolder { TextView messageText; TextView nameText; @@ -442,18 +438,18 @@ private static class ChatMessageViewHolder extends RecyclerView.ViewHolder { There's nothing magical going on here; we're just mapping numeric IDs and casts into a nice, type-safe contract. -### Create a custom FirebaseListAdapter +### Create a custom FirebaseRecyclerAdapter -Just like we did for FirebaseListAdapter, we'll create an anonymous subclass for our ChatMessages: +Just like we did for `FirebaseListAdapter`, we'll create an anonymous subclass for our ChatMessages, but this time we'll use `FirebaseRecyclerAdapter`: ```java RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler); recycler.setHasFixedSize(true); recycler.setLayoutManager(new LinearLayoutManager(this)); -mAdapter = new FirebaseRecyclerViewAdapter(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, mRef) { +mAdapter = new FirebaseRecyclerAdapter(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, mRef) { @Override - public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage) { + public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage, int position) { chatMessageViewHolder.nameText.setText(chatMessage.getName()); chatMessageViewHolder.messageText.setText(chatMessage.getMessage()); } diff --git a/app/build.gradle b/app/build.gradle index d1b57835f..c0be12624 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' android { compileSdkVersion 22 @@ -23,7 +22,7 @@ android { exclude 'META-INF/NOTICE' exclude 'META-INF/NOTICE.txt' } - + } dependencies { @@ -34,4 +33,4 @@ dependencies { compile 'com.android.support:recyclerview-v7:22.2.1' compile 'com.facebook.android:facebook-android-sdk:4.6.0' compile project(':library') -} \ No newline at end of file +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d4bf418a6..924e8dac3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,15 +1,15 @@ + - + android:label="@string/app_name" + android:theme="@style/Theme.AppCompat.Light" > @@ -22,12 +22,13 @@ + + android:value="@string/twitter_app_key" /> + android:value="@string/twitter_app_secret" /> + - diff --git a/app/src/main/java/com/firebase/uidemo/RecyclerViewDemoActivity.java b/app/src/main/java/com/firebase/uidemo/RecyclerViewDemoActivity.java index 46187825d..5a0a4b28e 100644 --- a/app/src/main/java/com/firebase/uidemo/RecyclerViewDemoActivity.java +++ b/app/src/main/java/com/firebase/uidemo/RecyclerViewDemoActivity.java @@ -23,7 +23,7 @@ import com.firebase.ui.auth.core.FirebaseLoginBaseActivity; import com.firebase.ui.FirebaseRecyclerAdapter; import com.firebase.ui.auth.core.FirebaseLoginError; -import com.firebase.ui.auth.core.SocialProvider; +import com.firebase.ui.auth.core.AuthProviderType; public class RecyclerViewDemoActivity extends FirebaseLoginBaseActivity { @@ -99,10 +99,10 @@ public void populateViewHolder(ChatHolder chatView, Chat chat, int position) { @Override protected void onStart() { super.onStart(); - setEnabledAuthProvider(SocialProvider.facebook); - setEnabledAuthProvider(SocialProvider.twitter); - setEnabledAuthProvider(SocialProvider.google); - setEnabledAuthProvider(SocialProvider.password); + setEnabledAuthProvider(AuthProviderType.FACEBOOK); + setEnabledAuthProvider(AuthProviderType.TWITTER); + setEnabledAuthProvider(AuthProviderType.GOOGLE); + setEnabledAuthProvider(AuthProviderType.PASSWORD); } @Override @@ -135,6 +135,7 @@ public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } + @Override public void onFirebaseLoggedIn(AuthData authData) { Log.i(TAG, "Logged in to " + authData.getProvider().toString()); @@ -162,13 +163,14 @@ public void onFirebaseLoggedOut() { @Override public void onFirebaseLoginProviderError(FirebaseLoginError firebaseError) { - Log.i(TAG, "Login provider error: " + firebaseError.toString()); + Log.e(TAG, "Login provider error: " + firebaseError.toString()); + resetFirebaseLoginPrompt(); } @Override public void onFirebaseLoginUserError(FirebaseLoginError firebaseError) { - resetFirebaseLoginDialog(); - Log.i(TAG, "Login user error: " + firebaseError.toString()); + Log.e(TAG, "Login user error: "+firebaseError.toString()); + resetFirebaseLoginPrompt(); } @Override diff --git a/codelabs/chat/README.md b/codelabs/chat/README.md index 6b87728f7..193564ceb 100644 --- a/codelabs/chat/README.md +++ b/codelabs/chat/README.md @@ -2,8 +2,8 @@ In this code lab you'll build a chat application for Android using Firebase and Android Studio. -![Chat login](images/0_0.png) -![Chat messages](images/0_1.png) +Chat login +Chat messages What you'll learn: @@ -61,7 +61,7 @@ In this step we'll create a project in Android Studio. ![Minimum Android API level](images/2_3.png) We've left it on 10 (Gingerbread) here, since that is the lowest API level Firebase supports. -4. Start with a Blank Activity +4. Start with a Empty Activity ![Add an activity](images/2_4.png) @@ -83,18 +83,18 @@ In this step we'll create a project in Android Studio. Before we can start writing code that interacts with our Firebase database, we'll need to make Android Studio aware that we'll be using Firebase. We need to do this in a few places: in the `gradle.build` script for our app and in its `AndroidManifest.xml`. -1. open Gradle Scripts > build.gradle (Module: app) +First, open Gradle Scripts > build.gradle (Module: app) This file contains the steps that Android Studio uses to build our app. We'll add a reference to Firebase to it, so we can start using it. -2. add the following lines to the dependencies object at the bottom: +Then add the following lines to the dependencies object at the bottom: - compile 'com.firebase:firebase-client-android:2.3.1' - compile 'com.firebaseui:firebase-ui:0.2.0' + compile 'com.firebase:firebase-client-android:2.5.0' + compile 'com.firebaseui:firebase-ui:0.3.0' This tells Gradle to include the Firebase SDK and the FirebaseUI library. -3. Add the following inside the `android` object: +Add the following inside the `android` object: packagingOptions { exclude 'META-INF/LICENSE' @@ -106,42 +106,52 @@ Before we can start writing code that interacts with our Firebase database, we'l ![gradle.build with Firebase additions](images/3_1.png) -4. At this stage you'll need to synchronize the project with the gradle files again. Either click the Sync Now link in the notification bar or the corresponding button in the toolbar: Sync Project with Gradle Files. +At this stage you'll need to synchronize the project with the gradle files again. Either click the Sync Now link in the notification bar or the corresponding button in the toolbar: Sync Project with Gradle Files. ![Sync Project with Gradle Files button in toolbar](images/3_2.png) Android Studio will parse the gradle files and pick up our changes. -5. Since Firebase is a hosted service, our app will need to be able to access the internet. -6. Open app > manifests > AndroidManifest.xml -7. Add this line inside the `manifest` element: +Since Firebase is a hosted service, our app will need to be able to access the internet. Open app > manifests > AndroidManifest.xml then add this line inside the `manifest` element: - +```html + +``` - ![INTERNET permission in AndroidManifest.xml](images/3_3.png) +![INTERNET permission in AndroidManifest.xml](images/3_3.png) -8. Import Firebase at the top of your MainActivity by adding the following line: +Import Firebase at the top of your MainActivity by adding the following line: - import com.firebase.client.Firebase; +```java +import com.firebase.client.Firebase; +``` -9. Now we can get to the Java code. The first step there is to set up initial connection between our code and its Firebase backend. +Now we can get to the Java code. The first step there is to set up initial connection between our code and its Firebase backend. open `MainActivity.java` and add this code to the end of the `onCreate` method: - Firebase.setAndroidContext(this); +```java +Firebase.setAndroidContext(this); +``` - This code allows the Firebase client to keep its context. -10. If Android Studio is having trouble finding the Firebase class, be sure that you've added dependencies and have synchronized the build file with the project. -11. We also want to create a connection to our database. We'll keep this connection in a member field: +This code allows the Firebase client to keep its context. - private Firebase mFirebaseRef; +**If Android Studio is having trouble finding the Firebase class, be sure that you've added dependencies and have synchronized the build file with the project.** + +We also want to create a connection to our database. We'll keep this connection in a member field: + +```java +private Firebase mFirebaseRef; +``` that we initialize in onCreate: - mFirebaseRef = new Firebase("https://.firebaseio.com"); +```java +mFirebaseRef = new Firebase("https://.firebaseio.com"); +``` - Be sure to replace `` with the name of the Firebase app you created in the first section. +**Be sure to replace `` with the name of the Firebase app you created in the first section.** - ![MainActivity with setAndroidContext and mFirebaseRef](images/3_4.png) +![MainActivity with setAndroidContext and mFirebaseRef](images/3_4.png) That's all the setup that is required. Next up we'll allow the user to enter a message in our app and send the message to Firebase. @@ -151,63 +161,70 @@ Next we'll send data to Firebase! In this step we'll allow the user to enter a m ![Data dashboard and app for sending a message](images/4_1.png) -1. We'll first add the necessary views to activity_main.xml: +We'll first add the necessary views to activity_main.xml: - - -