├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── app ├── .gitignore ├── build.gradle ├── google-services.json ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── delaroystudios │ │ └── firebasechat │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── delaroystudios │ │ │ └── firebasechat │ │ │ ├── FirebaseChatMainApp.java │ │ │ ├── core │ │ │ ├── chat │ │ │ │ ├── ChatContract.java │ │ │ │ ├── ChatInteractor.java │ │ │ │ └── ChatPresenter.java │ │ │ ├── login │ │ │ │ ├── LoginContract.java │ │ │ │ ├── LoginInteractor.java │ │ │ │ └── LoginPresenter.java │ │ │ ├── logout │ │ │ │ ├── LogoutContract.java │ │ │ │ ├── LogoutInteractor.java │ │ │ │ └── LogoutPresenter.java │ │ │ ├── registration │ │ │ │ ├── RegisterContract.java │ │ │ │ ├── RegisterInteractor.java │ │ │ │ └── RegisterPresenter.java │ │ │ └── users │ │ │ │ ├── add │ │ │ │ ├── AddUserContract.java │ │ │ │ ├── AddUserInteractor.java │ │ │ │ └── AddUserPresenter.java │ │ │ │ └── getall │ │ │ │ ├── GetUsersContract.java │ │ │ │ ├── GetUsersInteractor.java │ │ │ │ └── GetUsersPresenter.java │ │ │ ├── events │ │ │ └── PushNotificationEvent.java │ │ │ ├── fcm │ │ │ ├── FcmNotificationBuilder.java │ │ │ ├── MyFirebaseInstanceIDService.java │ │ │ └── MyFirebaseMessagingService.java │ │ │ ├── models │ │ │ ├── Chat.java │ │ │ ├── User.java │ │ │ └── Users.java │ │ │ ├── ui │ │ │ ├── activities │ │ │ │ ├── ChatActivity.java │ │ │ │ ├── LoginActivity.java │ │ │ │ ├── RegisterActivity.java │ │ │ │ ├── SplashActivity.java │ │ │ │ └── UserListingActivity.java │ │ │ ├── adapters │ │ │ │ ├── ChatRecyclerAdapter.java │ │ │ │ ├── UserListingPagerAdapter.java │ │ │ │ └── UserListingRecyclerAdapter.java │ │ │ └── fragments │ │ │ │ ├── ChatFragment.java │ │ │ │ ├── LoginFragment.java │ │ │ │ ├── RegisterFragment.java │ │ │ │ └── UsersFragment.java │ │ │ └── utils │ │ │ ├── Constants.java │ │ │ ├── ItemClickSupport.java │ │ │ ├── NetworkConnectionUtil.java │ │ │ └── SharedPrefUtil.java │ └── res │ │ ├── drawable │ │ ├── bg.jpg │ │ ├── chat_rounded_rect_bg.xml │ │ ├── circle_accent.xml │ │ ├── ic_error_24dp.xml │ │ ├── ic_messaging.png │ │ └── rounded_rect_bg.xml │ │ ├── layout │ │ ├── activity_chat.xml │ │ ├── activity_login.xml │ │ ├── activity_register.xml │ │ ├── activity_splash.xml │ │ ├── activity_user_listing.xml │ │ ├── fragment_chat.xml │ │ ├── fragment_login.xml │ │ ├── fragment_register.xml │ │ ├── fragment_users.xml │ │ ├── item_all_user_listing.xml │ │ ├── item_chat_mine.xml │ │ ├── item_chat_other.xml │ │ └── item_user_listing.xml │ │ ├── menu │ │ └── menu_user_listing.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── ids.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── delaroystudios │ └── firebasechat │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | FirebaseChat -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.delaroystudios.firebasechat" 9 | minSdkVersion 15 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:25.3.0' 26 | compile 'com.android.support:design:25.1.1' 27 | 28 | //firebase related dependencies 29 | compile 'com.google.firebase:firebase-database:9.6.1' 30 | compile 'com.google.firebase:firebase-messaging:9.6.1' 31 | compile 'com.google.firebase:firebase-auth:9.6.1' 32 | 33 | //okhttp 34 | compile 'com.squareup.okhttp3:okhttp:3.4.1' 35 | 36 | //event bus 37 | compile 'org.greenrobot:eventbus:3.0.0' 38 | } 39 | apply plugin: 'com.google.gms.google-services' -------------------------------------------------------------------------------- /app/google-services.json: -------------------------------------------------------------------------------- 1 | { 2 | "project_info": { 3 | "project_number": "633504866703", 4 | "firebase_url": "https://fir-database-8e745.firebaseio.com", 5 | "project_id": "fir-database-8e745", 6 | "storage_bucket": "fir-database-8e745.appspot.com" 7 | }, 8 | "client": [ 9 | { 10 | "client_info": { 11 | "mobilesdk_app_id": "1:633504866703:android:7b6360d7d7a73be8", 12 | "android_client_info": { 13 | "package_name": "com.delaroystudios.firebasedatabase" 14 | } 15 | }, 16 | "oauth_client": [ 17 | { 18 | "client_id": "633504866703-qs005i4319ioqrled8p7olopiohq4ks2.apps.googleusercontent.com", 19 | "client_type": 1, 20 | "android_info": { 21 | "package_name": "com.delaroystudios.firebasedatabase", 22 | "certificate_hash": "08fa7d5a643a79d3f9ec866c63deb2208a0b6640" 23 | } 24 | }, 25 | { 26 | "client_id": "633504866703-vvjru6l9cr2f9n29ejh2jpat45aransm.apps.googleusercontent.com", 27 | "client_type": 3 28 | } 29 | ], 30 | "api_key": [ 31 | { 32 | "current_key": "AIzaSyCMxSWR0eFN9D-At53uIdMFy3lN52vhTOQ" 33 | } 34 | ], 35 | "services": { 36 | "analytics_service": { 37 | "status": 1 38 | }, 39 | "appinvite_service": { 40 | "status": 1, 41 | "other_platform_oauth_client": [] 42 | }, 43 | "ads_service": { 44 | "status": 2 45 | } 46 | } 47 | }, 48 | { 49 | "client_info": { 50 | "mobilesdk_app_id": "1:633504866703:android:6035b4f5c6e27738", 51 | "android_client_info": { 52 | "package_name": "com.delaroystudios.firebaseemailauth" 53 | } 54 | }, 55 | "oauth_client": [ 56 | { 57 | "client_id": "633504866703-eop6d8fj23t1qicb4e8oi3au9udcojd4.apps.googleusercontent.com", 58 | "client_type": 1, 59 | "android_info": { 60 | "package_name": "com.delaroystudios.firebaseemailauth", 61 | "certificate_hash": "02400e13a85e876088e2abfd02909d56ee364c05" 62 | } 63 | }, 64 | { 65 | "client_id": "633504866703-vvjru6l9cr2f9n29ejh2jpat45aransm.apps.googleusercontent.com", 66 | "client_type": 3 67 | } 68 | ], 69 | "api_key": [ 70 | { 71 | "current_key": "AIzaSyCMxSWR0eFN9D-At53uIdMFy3lN52vhTOQ" 72 | } 73 | ], 74 | "services": { 75 | "analytics_service": { 76 | "status": 1 77 | }, 78 | "appinvite_service": { 79 | "status": 2, 80 | "other_platform_oauth_client": [ 81 | { 82 | "client_id": "633504866703-vvjru6l9cr2f9n29ejh2jpat45aransm.apps.googleusercontent.com", 83 | "client_type": 3 84 | } 85 | ] 86 | }, 87 | "ads_service": { 88 | "status": 2 89 | } 90 | } 91 | }, 92 | { 93 | "client_info": { 94 | "mobilesdk_app_id": "1:633504866703:android:11abcef06dd1434a", 95 | "android_client_info": { 96 | "package_name": "com.delaroystudios.firebasefacebookauth" 97 | } 98 | }, 99 | "oauth_client": [ 100 | { 101 | "client_id": "633504866703-c8qg0v1u26nb25e93q89mi6n5ik0ks7j.apps.googleusercontent.com", 102 | "client_type": 1, 103 | "android_info": { 104 | "package_name": "com.delaroystudios.firebasefacebookauth", 105 | "certificate_hash": "02400e13a85e876088e2abfd02909d56ee364c05" 106 | } 107 | }, 108 | { 109 | "client_id": "633504866703-vvjru6l9cr2f9n29ejh2jpat45aransm.apps.googleusercontent.com", 110 | "client_type": 3 111 | } 112 | ], 113 | "api_key": [ 114 | { 115 | "current_key": "AIzaSyCMxSWR0eFN9D-At53uIdMFy3lN52vhTOQ" 116 | } 117 | ], 118 | "services": { 119 | "analytics_service": { 120 | "status": 1 121 | }, 122 | "appinvite_service": { 123 | "status": 1, 124 | "other_platform_oauth_client": [] 125 | }, 126 | "ads_service": { 127 | "status": 2 128 | } 129 | } 130 | }, 131 | { 132 | "client_info": { 133 | "mobilesdk_app_id": "1:633504866703:android:665d4725c896ad75", 134 | "android_client_info": { 135 | "package_name": "com.delaroystudios.firebaseemailpasswordauth" 136 | } 137 | }, 138 | "oauth_client": [ 139 | { 140 | "client_id": "633504866703-vvjru6l9cr2f9n29ejh2jpat45aransm.apps.googleusercontent.com", 141 | "client_type": 3 142 | } 143 | ], 144 | "api_key": [ 145 | { 146 | "current_key": "AIzaSyCMxSWR0eFN9D-At53uIdMFy3lN52vhTOQ" 147 | } 148 | ], 149 | "services": { 150 | "analytics_service": { 151 | "status": 1 152 | }, 153 | "appinvite_service": { 154 | "status": 1, 155 | "other_platform_oauth_client": [] 156 | }, 157 | "ads_service": { 158 | "status": 2 159 | } 160 | } 161 | }, 162 | { 163 | "client_info": { 164 | "mobilesdk_app_id": "1:633504866703:android:2a993e92037f8aa7", 165 | "android_client_info": { 166 | "package_name": "com.delaroystudios.firebaselogin" 167 | } 168 | }, 169 | "oauth_client": [ 170 | { 171 | "client_id": "633504866703-vvjru6l9cr2f9n29ejh2jpat45aransm.apps.googleusercontent.com", 172 | "client_type": 3 173 | } 174 | ], 175 | "api_key": [ 176 | { 177 | "current_key": "AIzaSyCMxSWR0eFN9D-At53uIdMFy3lN52vhTOQ" 178 | } 179 | ], 180 | "services": { 181 | "analytics_service": { 182 | "status": 1 183 | }, 184 | "appinvite_service": { 185 | "status": 1, 186 | "other_platform_oauth_client": [] 187 | }, 188 | "ads_service": { 189 | "status": 2 190 | } 191 | } 192 | }, 193 | { 194 | "client_info": { 195 | "mobilesdk_app_id": "1:633504866703:android:1b17736f3a42b197", 196 | "android_client_info": { 197 | "package_name": "com.delaroystudios.firebasechat" 198 | } 199 | }, 200 | "oauth_client": [ 201 | { 202 | "client_id": "633504866703-vvjru6l9cr2f9n29ejh2jpat45aransm.apps.googleusercontent.com", 203 | "client_type": 3 204 | } 205 | ], 206 | "api_key": [ 207 | { 208 | "current_key": "AIzaSyCMxSWR0eFN9D-At53uIdMFy3lN52vhTOQ" 209 | } 210 | ], 211 | "services": { 212 | "analytics_service": { 213 | "status": 1 214 | }, 215 | "appinvite_service": { 216 | "status": 1, 217 | "other_platform_oauth_client": [] 218 | }, 219 | "ads_service": { 220 | "status": 2 221 | } 222 | } 223 | } 224 | ], 225 | "configuration_version": "1" 226 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/delaroy/opt/android-sdk-linux/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/delaroystudios/firebasechat/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 26 | 30 | 34 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/FirebaseChatMainApp.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat; 2 | 3 | import android.app.Application; 4 | 5 | 6 | public class FirebaseChatMainApp extends Application { 7 | private static boolean sIsChatActivityOpen = false; 8 | 9 | public static boolean isChatActivityOpen() { 10 | return sIsChatActivityOpen; 11 | } 12 | 13 | public static void setChatActivityOpen(boolean isChatActivityOpen) { 14 | FirebaseChatMainApp.sIsChatActivityOpen = isChatActivityOpen; 15 | } 16 | 17 | @Override 18 | public void onCreate() { 19 | super.onCreate(); 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/chat/ChatContract.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.chat; 2 | 3 | import android.content.Context; 4 | 5 | import com.delaroystudios.firebasechat.models.Chat; 6 | 7 | 8 | 9 | public interface ChatContract { 10 | interface View { 11 | void onSendMessageSuccess(); 12 | 13 | void onSendMessageFailure(String message); 14 | 15 | void onGetMessagesSuccess(Chat chat); 16 | 17 | void onGetMessagesFailure(String message); 18 | } 19 | 20 | interface Presenter { 21 | void sendMessage(Context context, Chat chat, String receiverFirebaseToken); 22 | 23 | void getMessage(String senderUid, String receiverUid); 24 | } 25 | 26 | interface Interactor { 27 | void sendMessageToFirebaseUser(Context context, Chat chat, String receiverFirebaseToken); 28 | 29 | void getMessageFromFirebaseUser(String senderUid, String receiverUid); 30 | } 31 | 32 | interface OnSendMessageListener { 33 | void onSendMessageSuccess(); 34 | 35 | void onSendMessageFailure(String message); 36 | } 37 | 38 | interface OnGetMessagesListener { 39 | void onGetMessagesSuccess(Chat chat); 40 | 41 | void onGetMessagesFailure(String message); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/chat/ChatInteractor.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.chat; 2 | 3 | import android.content.Context; 4 | import android.util.Log; 5 | 6 | import com.delaroystudios.firebasechat.fcm.FcmNotificationBuilder; 7 | import com.delaroystudios.firebasechat.models.Chat; 8 | import com.delaroystudios.firebasechat.utils.Constants; 9 | import com.delaroystudios.firebasechat.utils.SharedPrefUtil; 10 | import com.google.firebase.database.ChildEventListener; 11 | import com.google.firebase.database.DataSnapshot; 12 | import com.google.firebase.database.DatabaseError; 13 | import com.google.firebase.database.DatabaseReference; 14 | import com.google.firebase.database.FirebaseDatabase; 15 | import com.google.firebase.database.ValueEventListener; 16 | 17 | /** 18 | * Author: Kartik Sharma 19 | * Created on: 9/2/2016 , 10:08 PM 20 | * Project: FirebaseChat 21 | */ 22 | 23 | public class ChatInteractor implements ChatContract.Interactor { 24 | private static final String TAG = "ChatInteractor"; 25 | 26 | private ChatContract.OnSendMessageListener mOnSendMessageListener; 27 | private ChatContract.OnGetMessagesListener mOnGetMessagesListener; 28 | 29 | public ChatInteractor(ChatContract.OnSendMessageListener onSendMessageListener) { 30 | this.mOnSendMessageListener = onSendMessageListener; 31 | } 32 | 33 | public ChatInteractor(ChatContract.OnGetMessagesListener onGetMessagesListener) { 34 | this.mOnGetMessagesListener = onGetMessagesListener; 35 | } 36 | 37 | public ChatInteractor(ChatContract.OnSendMessageListener onSendMessageListener, 38 | ChatContract.OnGetMessagesListener onGetMessagesListener) { 39 | this.mOnSendMessageListener = onSendMessageListener; 40 | this.mOnGetMessagesListener = onGetMessagesListener; 41 | } 42 | 43 | @Override 44 | public void sendMessageToFirebaseUser(final Context context, final Chat chat, final String receiverFirebaseToken) { 45 | final String room_type_1 = chat.senderUid + "_" + chat.receiverUid; 46 | final String room_type_2 = chat.receiverUid + "_" + chat.senderUid; 47 | 48 | final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(); 49 | 50 | databaseReference.child(Constants.ARG_CHAT_ROOMS).getRef().addListenerForSingleValueEvent(new ValueEventListener() { 51 | @Override 52 | public void onDataChange(DataSnapshot dataSnapshot) { 53 | if (dataSnapshot.hasChild(room_type_1)) { 54 | Log.e(TAG, "sendMessageToFirebaseUser: " + room_type_1 + " exists"); 55 | databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_1).child(String.valueOf(chat.timestamp)).setValue(chat); 56 | } else if (dataSnapshot.hasChild(room_type_2)) { 57 | Log.e(TAG, "sendMessageToFirebaseUser: " + room_type_2 + " exists"); 58 | databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_2).child(String.valueOf(chat.timestamp)).setValue(chat); 59 | } else { 60 | Log.e(TAG, "sendMessageToFirebaseUser: success"); 61 | databaseReference.child(Constants.ARG_CHAT_ROOMS).child(room_type_1).child(String.valueOf(chat.timestamp)).setValue(chat); 62 | getMessageFromFirebaseUser(chat.senderUid, chat.receiverUid); 63 | } 64 | // send push notification to the receiver 65 | sendPushNotificationToReceiver(chat.sender, 66 | chat.message, 67 | chat.senderUid, 68 | new SharedPrefUtil(context).getString(Constants.ARG_FIREBASE_TOKEN), 69 | receiverFirebaseToken); 70 | mOnSendMessageListener.onSendMessageSuccess(); 71 | } 72 | 73 | @Override 74 | public void onCancelled(DatabaseError databaseError) { 75 | mOnSendMessageListener.onSendMessageFailure("Unable to send message: " + databaseError.getMessage()); 76 | } 77 | }); 78 | } 79 | 80 | private void sendPushNotificationToReceiver(String username, 81 | String message, 82 | String uid, 83 | String firebaseToken, 84 | String receiverFirebaseToken) { 85 | FcmNotificationBuilder.initialize() 86 | .title(username) 87 | .message(message) 88 | .username(username) 89 | .uid(uid) 90 | .firebaseToken(firebaseToken) 91 | .receiverFirebaseToken(receiverFirebaseToken) 92 | .send(); 93 | } 94 | 95 | @Override 96 | public void getMessageFromFirebaseUser(String senderUid, String receiverUid) { 97 | final String room_type_1 = senderUid + "_" + receiverUid; 98 | final String room_type_2 = receiverUid + "_" + senderUid; 99 | 100 | final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(); 101 | 102 | databaseReference.child(Constants.ARG_CHAT_ROOMS).getRef().addListenerForSingleValueEvent(new ValueEventListener() { 103 | @Override 104 | public void onDataChange(DataSnapshot dataSnapshot) { 105 | if (dataSnapshot.hasChild(room_type_1)) { 106 | Log.e(TAG, "getMessageFromFirebaseUser: " + room_type_1 + " exists"); 107 | FirebaseDatabase.getInstance() 108 | .getReference() 109 | .child(Constants.ARG_CHAT_ROOMS) 110 | .child(room_type_1).addChildEventListener(new ChildEventListener() { 111 | @Override 112 | public void onChildAdded(DataSnapshot dataSnapshot, String s) { 113 | Chat chat = dataSnapshot.getValue(Chat.class); 114 | mOnGetMessagesListener.onGetMessagesSuccess(chat); 115 | } 116 | 117 | @Override 118 | public void onChildChanged(DataSnapshot dataSnapshot, String s) { 119 | 120 | } 121 | 122 | @Override 123 | public void onChildRemoved(DataSnapshot dataSnapshot) { 124 | 125 | } 126 | 127 | @Override 128 | public void onChildMoved(DataSnapshot dataSnapshot, String s) { 129 | 130 | } 131 | 132 | @Override 133 | public void onCancelled(DatabaseError databaseError) { 134 | mOnGetMessagesListener.onGetMessagesFailure("Unable to get message: " + databaseError.getMessage()); 135 | } 136 | }); 137 | } else if (dataSnapshot.hasChild(room_type_2)) { 138 | Log.e(TAG, "getMessageFromFirebaseUser: " + room_type_2 + " exists"); 139 | FirebaseDatabase.getInstance() 140 | .getReference() 141 | .child(Constants.ARG_CHAT_ROOMS) 142 | .child(room_type_2).addChildEventListener(new ChildEventListener() { 143 | @Override 144 | public void onChildAdded(DataSnapshot dataSnapshot, String s) { 145 | Chat chat = dataSnapshot.getValue(Chat.class); 146 | mOnGetMessagesListener.onGetMessagesSuccess(chat); 147 | } 148 | 149 | @Override 150 | public void onChildChanged(DataSnapshot dataSnapshot, String s) { 151 | 152 | } 153 | 154 | @Override 155 | public void onChildRemoved(DataSnapshot dataSnapshot) { 156 | 157 | } 158 | 159 | @Override 160 | public void onChildMoved(DataSnapshot dataSnapshot, String s) { 161 | 162 | } 163 | 164 | @Override 165 | public void onCancelled(DatabaseError databaseError) { 166 | mOnGetMessagesListener.onGetMessagesFailure("Unable to get message: " + databaseError.getMessage()); 167 | } 168 | }); 169 | } else { 170 | Log.e(TAG, "getMessageFromFirebaseUser: no such room available"); 171 | } 172 | } 173 | 174 | @Override 175 | public void onCancelled(DatabaseError databaseError) { 176 | mOnGetMessagesListener.onGetMessagesFailure("Unable to get message: " + databaseError.getMessage()); 177 | } 178 | }); 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/chat/ChatPresenter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.chat; 2 | 3 | import android.content.Context; 4 | 5 | import com.delaroystudios.firebasechat.models.Chat; 6 | 7 | /** 8 | * Author: Kartik Sharma 9 | * Created on: 9/2/2016 , 10:05 PM 10 | * Project: FirebaseChat 11 | */ 12 | 13 | public class ChatPresenter implements ChatContract.Presenter, ChatContract.OnSendMessageListener, 14 | ChatContract.OnGetMessagesListener { 15 | private ChatContract.View mView; 16 | private ChatInteractor mChatInteractor; 17 | 18 | public ChatPresenter(ChatContract.View view) { 19 | this.mView = view; 20 | mChatInteractor = new ChatInteractor(this, this); 21 | } 22 | 23 | @Override 24 | public void sendMessage(Context context, Chat chat, String receiverFirebaseToken) { 25 | mChatInteractor.sendMessageToFirebaseUser(context, chat, receiverFirebaseToken); 26 | } 27 | 28 | @Override 29 | public void getMessage(String senderUid, String receiverUid) { 30 | mChatInteractor.getMessageFromFirebaseUser(senderUid, receiverUid); 31 | } 32 | 33 | @Override 34 | public void onSendMessageSuccess() { 35 | mView.onSendMessageSuccess(); 36 | } 37 | 38 | @Override 39 | public void onSendMessageFailure(String message) { 40 | mView.onSendMessageFailure(message); 41 | } 42 | 43 | @Override 44 | public void onGetMessagesSuccess(Chat chat) { 45 | mView.onGetMessagesSuccess(chat); 46 | } 47 | 48 | @Override 49 | public void onGetMessagesFailure(String message) { 50 | mView.onGetMessagesFailure(message); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/login/LoginContract.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.login; 2 | 3 | import android.app.Activity; 4 | 5 | /** 6 | * Author: Kartik Sharma 7 | * Created on: 8/28/2016 , 11:06 AM 8 | * Project: FirebaseChat 9 | */ 10 | 11 | public interface LoginContract { 12 | interface View { 13 | void onLoginSuccess(String message); 14 | 15 | void onLoginFailure(String message); 16 | } 17 | 18 | interface Presenter { 19 | void login(Activity activity, String email, String password); 20 | } 21 | 22 | interface Interactor { 23 | void performFirebaseLogin(Activity activity, String email, String password); 24 | } 25 | 26 | interface OnLoginListener { 27 | void onSuccess(String message); 28 | 29 | void onFailure(String message); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/login/LoginInteractor.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.login; 2 | 3 | import android.app.Activity; 4 | import android.support.annotation.NonNull; 5 | import android.util.Log; 6 | 7 | import com.delaroystudios.firebasechat.utils.Constants; 8 | import com.delaroystudios.firebasechat.utils.SharedPrefUtil; 9 | import com.google.android.gms.tasks.OnCompleteListener; 10 | import com.google.android.gms.tasks.Task; 11 | import com.google.firebase.auth.AuthResult; 12 | import com.google.firebase.auth.FirebaseAuth; 13 | import com.google.firebase.database.FirebaseDatabase; 14 | 15 | import static android.content.ContentValues.TAG; 16 | 17 | /** 18 | * Author: Kartik Sharma 19 | * Created on: 8/28/2016 , 11:10 AM 20 | * Project: FirebaseChat 21 | */ 22 | 23 | public class LoginInteractor implements LoginContract.Interactor { 24 | private LoginContract.OnLoginListener mOnLoginListener; 25 | 26 | public LoginInteractor(LoginContract.OnLoginListener onLoginListener) { 27 | this.mOnLoginListener = onLoginListener; 28 | } 29 | 30 | @Override 31 | public void performFirebaseLogin(final Activity activity, final String email, String password) { 32 | FirebaseAuth.getInstance() 33 | .signInWithEmailAndPassword(email, password) 34 | .addOnCompleteListener(activity, new OnCompleteListener() { 35 | @Override 36 | public void onComplete(@NonNull Task task) { 37 | Log.d(TAG, "performFirebaseLogin:onComplete:" + task.isSuccessful()); 38 | 39 | // If sign in fails, display a message to the user. If sign in succeeds 40 | // the auth state listener will be notified and logic to handle the 41 | // signed in user can be handled in the listener. 42 | if (task.isSuccessful()) { 43 | mOnLoginListener.onSuccess(task.getResult().toString()); 44 | updateFirebaseToken(task.getResult().getUser().getUid(), 45 | new SharedPrefUtil(activity.getApplicationContext()).getString(Constants.ARG_FIREBASE_TOKEN, null)); 46 | } else { 47 | mOnLoginListener.onFailure(task.getException().getMessage()); 48 | } 49 | } 50 | }); 51 | } 52 | 53 | private void updateFirebaseToken(String uid, String token) { 54 | FirebaseDatabase.getInstance() 55 | .getReference() 56 | .child(Constants.ARG_USERS) 57 | .child(uid) 58 | .child(Constants.ARG_FIREBASE_TOKEN) 59 | .setValue(token); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/login/LoginPresenter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.login; 2 | 3 | import android.app.Activity; 4 | 5 | /** 6 | * Author: Kartik Sharma 7 | * Created on: 8/28/2016 , 11:10 AM 8 | * Project: FirebaseChat 9 | */ 10 | 11 | public class LoginPresenter implements LoginContract.Presenter, LoginContract.OnLoginListener { 12 | private LoginContract.View mLoginView; 13 | private LoginInteractor mLoginInteractor; 14 | 15 | public LoginPresenter(LoginContract.View loginView) { 16 | this.mLoginView = loginView; 17 | mLoginInteractor = new LoginInteractor(this); 18 | } 19 | 20 | @Override 21 | public void login(Activity activity, String email, String password) { 22 | mLoginInteractor.performFirebaseLogin(activity, email, password); 23 | } 24 | 25 | @Override 26 | public void onSuccess(String message) { 27 | mLoginView.onLoginSuccess(message); 28 | } 29 | 30 | @Override 31 | public void onFailure(String message) { 32 | mLoginView.onLoginFailure(message); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/logout/LogoutContract.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.logout; 2 | 3 | 4 | 5 | public interface LogoutContract { 6 | interface View { 7 | void onLogoutSuccess(String message); 8 | 9 | void onLogoutFailure(String message); 10 | } 11 | 12 | interface Presenter { 13 | void logout(); 14 | } 15 | 16 | interface Interactor { 17 | void performFirebaseLogout(); 18 | } 19 | 20 | interface OnLogoutListener { 21 | void onSuccess(String message); 22 | 23 | void onFailure(String message); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/logout/LogoutInteractor.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.logout; 2 | 3 | import com.google.firebase.auth.FirebaseAuth; 4 | 5 | 6 | 7 | public class LogoutInteractor implements LogoutContract.Interactor { 8 | private LogoutContract.OnLogoutListener mOnLogoutListener; 9 | 10 | public LogoutInteractor(LogoutContract.OnLogoutListener onLogoutListener) { 11 | mOnLogoutListener = onLogoutListener; 12 | } 13 | 14 | @Override 15 | public void performFirebaseLogout() { 16 | if (FirebaseAuth.getInstance().getCurrentUser() != null) { 17 | FirebaseAuth.getInstance().signOut(); 18 | mOnLogoutListener.onSuccess("Successfully logged out!"); 19 | } else { 20 | mOnLogoutListener.onFailure("No user logged in yet!"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/logout/LogoutPresenter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.logout; 2 | 3 | 4 | 5 | public class LogoutPresenter implements LogoutContract.Presenter, LogoutContract.OnLogoutListener { 6 | private LogoutContract.View mLogoutView; 7 | private LogoutInteractor mLogoutInteractor; 8 | 9 | public LogoutPresenter(LogoutContract.View logoutView) { 10 | mLogoutView = logoutView; 11 | mLogoutInteractor = new LogoutInteractor(this); 12 | } 13 | 14 | @Override 15 | public void logout() { 16 | mLogoutInteractor.performFirebaseLogout(); 17 | } 18 | 19 | @Override 20 | public void onSuccess(String message) { 21 | mLogoutView.onLogoutSuccess(message); 22 | } 23 | 24 | @Override 25 | public void onFailure(String message) { 26 | mLogoutView.onLogoutFailure(message); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/registration/RegisterContract.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.registration; 2 | 3 | import android.app.Activity; 4 | 5 | import com.google.firebase.auth.FirebaseUser; 6 | 7 | 8 | public interface RegisterContract { 9 | interface View { 10 | void onRegistrationSuccess(FirebaseUser firebaseUser); 11 | 12 | void onRegistrationFailure(String message); 13 | } 14 | 15 | interface Presenter { 16 | void register(Activity activity, String email, String password); 17 | } 18 | 19 | interface Interactor { 20 | void performFirebaseRegistration(Activity activity, String email, String password); 21 | } 22 | 23 | interface OnRegistrationListener { 24 | void onSuccess(FirebaseUser firebaseUser); 25 | 26 | void onFailure(String message); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/registration/RegisterInteractor.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.registration; 2 | 3 | import android.app.Activity; 4 | import android.support.annotation.NonNull; 5 | import android.util.Log; 6 | 7 | import com.google.android.gms.tasks.OnCompleteListener; 8 | import com.google.android.gms.tasks.Task; 9 | import com.google.firebase.auth.AuthResult; 10 | import com.google.firebase.auth.FirebaseAuth; 11 | 12 | 13 | public class RegisterInteractor implements RegisterContract.Interactor { 14 | private static final String TAG = RegisterInteractor.class.getSimpleName(); 15 | 16 | private RegisterContract.OnRegistrationListener mOnRegistrationListener; 17 | 18 | public RegisterInteractor(RegisterContract.OnRegistrationListener onRegistrationListener) { 19 | this.mOnRegistrationListener = onRegistrationListener; 20 | } 21 | 22 | @Override 23 | public void performFirebaseRegistration(Activity activity, final String email, String password) { 24 | FirebaseAuth.getInstance() 25 | .createUserWithEmailAndPassword(email, password) 26 | .addOnCompleteListener(activity, new OnCompleteListener() { 27 | @Override 28 | public void onComplete(@NonNull Task task) { 29 | Log.e(TAG, "performFirebaseRegistration:onComplete:" + task.isSuccessful()); 30 | 31 | // If sign in fails, display a message to the user. If sign in succeeds 32 | // the auth state listener will be notified and logic to handle the 33 | // signed in user can be handled in the listener. 34 | if (!task.isSuccessful()) { 35 | mOnRegistrationListener.onFailure(task.getException().getMessage()); 36 | } else { 37 | // Add the user to users table. 38 | /*DatabaseReference database= FirebaseDatabase.getInstance().getReference(); 39 | User user = new User(task.getResult().getUser().getUid(), email); 40 | database.child("users").push().setValue(user);*/ 41 | 42 | mOnRegistrationListener.onSuccess(task.getResult().getUser()); 43 | } 44 | } 45 | }); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/registration/RegisterPresenter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.registration; 2 | 3 | import android.app.Activity; 4 | 5 | import com.google.firebase.auth.FirebaseUser; 6 | 7 | 8 | 9 | public class RegisterPresenter implements RegisterContract.Presenter, RegisterContract.OnRegistrationListener { 10 | private RegisterContract.View mRegisterView; 11 | private RegisterInteractor mRegisterInteractor; 12 | 13 | public RegisterPresenter(RegisterContract.View registerView) { 14 | this.mRegisterView = registerView; 15 | mRegisterInteractor = new RegisterInteractor(this); 16 | } 17 | 18 | @Override 19 | public void register(Activity activity, String email, String password) { 20 | mRegisterInteractor.performFirebaseRegistration(activity, email, password); 21 | } 22 | 23 | @Override 24 | public void onSuccess(FirebaseUser firebaseUser) { 25 | mRegisterView.onRegistrationSuccess(firebaseUser); 26 | } 27 | 28 | @Override 29 | public void onFailure(String message) { 30 | mRegisterView.onRegistrationFailure(message); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/users/add/AddUserContract.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.users.add; 2 | 3 | import android.content.Context; 4 | 5 | import com.google.firebase.auth.FirebaseUser; 6 | 7 | 8 | 9 | public interface AddUserContract { 10 | interface View { 11 | void onAddUserSuccess(String message); 12 | 13 | void onAddUserFailure(String message); 14 | } 15 | 16 | interface Presenter { 17 | void addUser(Context context, FirebaseUser firebaseUser); 18 | } 19 | 20 | interface Interactor { 21 | void addUserToDatabase(Context context, FirebaseUser firebaseUser); 22 | } 23 | 24 | interface OnUserDatabaseListener { 25 | void onSuccess(String message); 26 | 27 | void onFailure(String message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/users/add/AddUserInteractor.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.users.add; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | 6 | import com.delaroystudios.firebasechat.R; 7 | import com.delaroystudios.firebasechat.models.User; 8 | import com.delaroystudios.firebasechat.utils.Constants; 9 | import com.delaroystudios.firebasechat.utils.SharedPrefUtil; 10 | import com.google.android.gms.tasks.OnCompleteListener; 11 | import com.google.android.gms.tasks.Task; 12 | import com.google.firebase.auth.FirebaseUser; 13 | import com.google.firebase.database.DatabaseReference; 14 | import com.google.firebase.database.FirebaseDatabase; 15 | 16 | 17 | 18 | public class AddUserInteractor implements AddUserContract.Interactor { 19 | private AddUserContract.OnUserDatabaseListener mOnUserDatabaseListener; 20 | 21 | public AddUserInteractor(AddUserContract.OnUserDatabaseListener onUserDatabaseListener) { 22 | this.mOnUserDatabaseListener = onUserDatabaseListener; 23 | } 24 | 25 | @Override 26 | public void addUserToDatabase(final Context context, FirebaseUser firebaseUser) { 27 | DatabaseReference database = FirebaseDatabase.getInstance().getReference(); 28 | User user = new User(firebaseUser.getUid(), 29 | firebaseUser.getEmail(), 30 | new SharedPrefUtil(context).getString(Constants.ARG_FIREBASE_TOKEN)); 31 | database.child(Constants.ARG_USERS) 32 | .child(firebaseUser.getUid()) 33 | .setValue(user) 34 | .addOnCompleteListener(new OnCompleteListener() { 35 | @Override 36 | public void onComplete(@NonNull Task task) { 37 | if (task.isSuccessful()) { 38 | mOnUserDatabaseListener.onSuccess(context.getString(R.string.user_successfully_added)); 39 | } else { 40 | mOnUserDatabaseListener.onFailure(context.getString(R.string.user_unable_to_add)); 41 | } 42 | } 43 | }); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/users/add/AddUserPresenter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.users.add; 2 | 3 | import android.content.Context; 4 | 5 | import com.google.firebase.auth.FirebaseUser; 6 | 7 | 8 | 9 | public class AddUserPresenter implements AddUserContract.Presenter, AddUserContract.OnUserDatabaseListener { 10 | private AddUserContract.View mView; 11 | private AddUserInteractor mAddUserInteractor; 12 | 13 | public AddUserPresenter(AddUserContract.View view) { 14 | this.mView = view; 15 | mAddUserInteractor = new AddUserInteractor(this); 16 | } 17 | 18 | @Override 19 | public void addUser(Context context, FirebaseUser firebaseUser) { 20 | mAddUserInteractor.addUserToDatabase(context, firebaseUser); 21 | } 22 | 23 | @Override 24 | public void onSuccess(String message) { 25 | mView.onAddUserSuccess(message); 26 | } 27 | 28 | @Override 29 | public void onFailure(String message) { 30 | mView.onAddUserFailure(message); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/users/getall/GetUsersContract.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.users.getall; 2 | 3 | import com.delaroystudios.firebasechat.models.User; 4 | 5 | import java.util.List; 6 | 7 | 8 | 9 | public interface GetUsersContract { 10 | interface View { 11 | void onGetAllUsersSuccess(List users); 12 | 13 | void onGetAllUsersFailure(String message); 14 | 15 | void onGetChatUsersSuccess(List users); 16 | 17 | void onGetChatUsersFailure(String message); 18 | } 19 | 20 | interface Presenter { 21 | void getAllUsers(); 22 | 23 | void getChatUsers(); 24 | } 25 | 26 | interface Interactor { 27 | void getAllUsersFromFirebase(); 28 | 29 | void getChatUsersFromFirebase(); 30 | } 31 | 32 | interface OnGetAllUsersListener { 33 | void onGetAllUsersSuccess(List users); 34 | 35 | void onGetAllUsersFailure(String message); 36 | } 37 | 38 | interface OnGetChatUsersListener { 39 | void onGetChatUsersSuccess(List users); 40 | 41 | void onGetChatUsersFailure(String message); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/users/getall/GetUsersInteractor.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.users.getall; 2 | 3 | import android.text.TextUtils; 4 | 5 | import com.delaroystudios.firebasechat.models.User; 6 | import com.delaroystudios.firebasechat.utils.Constants; 7 | import com.google.firebase.auth.FirebaseAuth; 8 | import com.google.firebase.database.DataSnapshot; 9 | import com.google.firebase.database.DatabaseError; 10 | import com.google.firebase.database.FirebaseDatabase; 11 | import com.google.firebase.database.ValueEventListener; 12 | 13 | import java.util.ArrayList; 14 | import java.util.Iterator; 15 | import java.util.List; 16 | 17 | 18 | 19 | public class GetUsersInteractor implements GetUsersContract.Interactor { 20 | private static final String TAG = "GetUsersInteractor"; 21 | 22 | private GetUsersContract.OnGetAllUsersListener mOnGetAllUsersListener; 23 | 24 | public GetUsersInteractor(GetUsersContract.OnGetAllUsersListener onGetAllUsersListener) { 25 | this.mOnGetAllUsersListener = onGetAllUsersListener; 26 | } 27 | 28 | 29 | @Override 30 | public void getAllUsersFromFirebase() { 31 | FirebaseDatabase.getInstance().getReference().child(Constants.ARG_USERS).addListenerForSingleValueEvent(new ValueEventListener() { 32 | @Override 33 | public void onDataChange(DataSnapshot dataSnapshot) { 34 | Iterator dataSnapshots = dataSnapshot.getChildren().iterator(); 35 | List users = new ArrayList<>(); 36 | while (dataSnapshots.hasNext()) { 37 | DataSnapshot dataSnapshotChild = dataSnapshots.next(); 38 | User user = dataSnapshotChild.getValue(User.class); 39 | if (!TextUtils.equals(user.uid, FirebaseAuth.getInstance().getCurrentUser().getUid())) { 40 | users.add(user); 41 | } 42 | } 43 | mOnGetAllUsersListener.onGetAllUsersSuccess(users); 44 | } 45 | 46 | @Override 47 | public void onCancelled(DatabaseError databaseError) { 48 | mOnGetAllUsersListener.onGetAllUsersFailure(databaseError.getMessage()); 49 | } 50 | }); 51 | } 52 | 53 | @Override 54 | public void getChatUsersFromFirebase() { 55 | /*FirebaseDatabase.getInstance().getReference().child(Constants.ARG_CHAT_ROOMS).addListenerForSingleValueEvent(new ValueEventListener() { 56 | @Override 57 | public void onDataChange(DataSnapshot dataSnapshot) { 58 | Iterator dataSnapshots=dataSnapshot.getChildren().iterator(); 59 | List users=new ArrayList<>(); 60 | while (dataSnapshots.hasNext()){ 61 | DataSnapshot dataSnapshotChild=dataSnapshots.next(); 62 | dataSnapshotChild.getRef(). 63 | Chat chat=dataSnapshotChild.getValue(Chat.class); 64 | if(chat.)4 65 | if(!TextUtils.equals(user.uid,FirebaseAuth.getInstance().getCurrentUser().getUid())) { 66 | users.add(user); 67 | } 68 | } 69 | } 70 | 71 | @Override 72 | public void onCancelled(DatabaseError databaseError) { 73 | 74 | } 75 | });*/ 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/core/users/getall/GetUsersPresenter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.core.users.getall; 2 | 3 | import com.delaroystudios.firebasechat.models.User; 4 | 5 | import java.util.List; 6 | 7 | 8 | 9 | public class GetUsersPresenter implements GetUsersContract.Presenter, GetUsersContract.OnGetAllUsersListener { 10 | private GetUsersContract.View mView; 11 | private GetUsersInteractor mGetUsersInteractor; 12 | 13 | public GetUsersPresenter(GetUsersContract.View view) { 14 | this.mView = view; 15 | mGetUsersInteractor = new GetUsersInteractor(this); 16 | } 17 | 18 | @Override 19 | public void getAllUsers() { 20 | mGetUsersInteractor.getAllUsersFromFirebase(); 21 | } 22 | 23 | @Override 24 | public void getChatUsers() { 25 | mGetUsersInteractor.getChatUsersFromFirebase(); 26 | } 27 | 28 | @Override 29 | public void onGetAllUsersSuccess(List users) { 30 | mView.onGetAllUsersSuccess(users); 31 | } 32 | 33 | @Override 34 | public void onGetAllUsersFailure(String message) { 35 | mView.onGetAllUsersFailure(message); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/events/PushNotificationEvent.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.events; 2 | 3 | /** 4 | * Created by delaroy on 4/13/17. 5 | */ 6 | public class PushNotificationEvent { 7 | private String title; 8 | private String message; 9 | private String username; 10 | private String uid; 11 | private String fcmToken; 12 | public PushNotificationEvent() { 13 | } 14 | 15 | public PushNotificationEvent(String title, String message, String username, String uid, String fcmToken) { 16 | this.title = title; 17 | this.message = message; 18 | this.username = username; 19 | this.uid = uid; 20 | this.fcmToken = fcmToken; 21 | } 22 | 23 | public String getTitle() { 24 | return title; 25 | } 26 | 27 | public void setTitle(String title) { 28 | this.title = title; 29 | } 30 | 31 | public String getMessage() { 32 | return message; 33 | } 34 | 35 | public void setMessage(String message) { 36 | this.message = message; 37 | } 38 | 39 | public String getUsername() { 40 | return username; 41 | } 42 | 43 | public void setUsername(String username) { 44 | this.username = username; 45 | } 46 | 47 | public String getUid() { 48 | return uid; 49 | } 50 | 51 | public void setUid(String uid) { 52 | this.uid = uid; 53 | } 54 | 55 | public String getFcmToken() { 56 | return fcmToken; 57 | } 58 | 59 | public void setFcmToken(String fcmToken) { 60 | this.fcmToken = fcmToken; 61 | } 62 | } -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/fcm/FcmNotificationBuilder.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.fcm; 2 | 3 | import android.util.Log; 4 | 5 | import org.json.JSONException; 6 | import org.json.JSONObject; 7 | 8 | import java.io.IOException; 9 | 10 | import okhttp3.Call; 11 | import okhttp3.Callback; 12 | import okhttp3.MediaType; 13 | import okhttp3.OkHttpClient; 14 | import okhttp3.Request; 15 | import okhttp3.RequestBody; 16 | import okhttp3.Response; 17 | 18 | 19 | 20 | public class FcmNotificationBuilder { 21 | public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); 22 | private static final String TAG = "FcmNotificationBuilder"; 23 | private static final String SERVER_API_KEY = "YOUR_SERVER_API_KEY"; 24 | private static final String CONTENT_TYPE = "Content-Type"; 25 | private static final String APPLICATION_JSON = "application/json"; 26 | private static final String AUTHORIZATION = "Authorization"; 27 | private static final String AUTH_KEY = "key=" + SERVER_API_KEY; 28 | private static final String FCM_URL = "https://fcm.googleapis.com/fcm/send"; 29 | // json related keys 30 | private static final String KEY_TO = "to"; 31 | private static final String KEY_NOTIFICATION = "notification"; 32 | private static final String KEY_TITLE = "title"; 33 | private static final String KEY_TEXT = "text"; 34 | private static final String KEY_DATA = "data"; 35 | private static final String KEY_USERNAME = "username"; 36 | private static final String KEY_UID = "uid"; 37 | private static final String KEY_FCM_TOKEN = "fcm_token"; 38 | 39 | private String mTitle; 40 | private String mMessage; 41 | private String mUsername; 42 | private String mUid; 43 | private String mFirebaseToken; 44 | private String mReceiverFirebaseToken; 45 | 46 | private FcmNotificationBuilder() { 47 | 48 | } 49 | 50 | public static FcmNotificationBuilder initialize() { 51 | return new FcmNotificationBuilder(); 52 | } 53 | 54 | public FcmNotificationBuilder title(String title) { 55 | mTitle = title; 56 | return this; 57 | } 58 | 59 | public FcmNotificationBuilder message(String message) { 60 | mMessage = message; 61 | return this; 62 | } 63 | 64 | public FcmNotificationBuilder username(String username) { 65 | mUsername = username; 66 | return this; 67 | } 68 | 69 | public FcmNotificationBuilder uid(String uid) { 70 | mUid = uid; 71 | return this; 72 | } 73 | 74 | public FcmNotificationBuilder firebaseToken(String firebaseToken) { 75 | mFirebaseToken = firebaseToken; 76 | return this; 77 | } 78 | 79 | public FcmNotificationBuilder receiverFirebaseToken(String receiverFirebaseToken) { 80 | mReceiverFirebaseToken = receiverFirebaseToken; 81 | return this; 82 | } 83 | 84 | public void send() { 85 | RequestBody requestBody = null; 86 | try { 87 | requestBody = RequestBody.create(MEDIA_TYPE_JSON, getValidJsonBody().toString()); 88 | } catch (JSONException e) { 89 | e.printStackTrace(); 90 | } 91 | 92 | Request request = new Request.Builder() 93 | .addHeader(CONTENT_TYPE, APPLICATION_JSON) 94 | .addHeader(AUTHORIZATION, AUTH_KEY) 95 | .url(FCM_URL) 96 | .post(requestBody) 97 | .build(); 98 | 99 | Call call = new OkHttpClient().newCall(request); 100 | call.enqueue(new Callback() { 101 | @Override 102 | public void onFailure(Call call, IOException e) { 103 | Log.e(TAG, "onGetAllUsersFailure: " + e.getMessage()); 104 | } 105 | 106 | @Override 107 | public void onResponse(Call call, Response response) throws IOException { 108 | Log.e(TAG, "onResponse: " + response.body().string()); 109 | } 110 | }); 111 | } 112 | 113 | private JSONObject getValidJsonBody() throws JSONException { 114 | JSONObject jsonObjectBody = new JSONObject(); 115 | jsonObjectBody.put(KEY_TO, mReceiverFirebaseToken); 116 | 117 | JSONObject jsonObjectData = new JSONObject(); 118 | jsonObjectData.put(KEY_TITLE, mTitle); 119 | jsonObjectData.put(KEY_TEXT, mMessage); 120 | jsonObjectData.put(KEY_USERNAME, mUsername); 121 | jsonObjectData.put(KEY_UID, mUid); 122 | jsonObjectData.put(KEY_FCM_TOKEN, mFirebaseToken); 123 | jsonObjectBody.put(KEY_DATA, jsonObjectData); 124 | 125 | return jsonObjectBody; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/fcm/MyFirebaseInstanceIDService.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.fcm; 2 | 3 | import android.util.Log; 4 | 5 | import com.delaroystudios.firebasechat.utils.Constants; 6 | import com.delaroystudios.firebasechat.utils.SharedPrefUtil; 7 | import com.google.firebase.auth.FirebaseAuth; 8 | import com.google.firebase.database.FirebaseDatabase; 9 | import com.google.firebase.iid.FirebaseInstanceId; 10 | import com.google.firebase.iid.FirebaseInstanceIdService; 11 | 12 | public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 13 | private static final String TAG = "MyFirebaseIIDService"; 14 | 15 | /** 16 | * Called if InstanceID token is updated. This may occur if the security of 17 | * the previous token had been compromised. Note that this is called when the InstanceID token 18 | * is initially generated so this is where you would retrieve the token. 19 | */ 20 | // [START refresh_token] 21 | @Override 22 | public void onTokenRefresh() { 23 | // Get updated InstanceID token. 24 | String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 25 | Log.d(TAG, "Refreshed token: " + refreshedToken); 26 | 27 | // If you want to send messages to this application instance or 28 | // manage this apps subscriptions on the server side, send the 29 | // Instance ID token to your app server. 30 | sendRegistrationToServer(refreshedToken); 31 | } 32 | // [END refresh_token] 33 | 34 | /** 35 | * Persist token to third-party servers. 36 | *

37 | * Modify this method to associate the user's FCM InstanceID token with any server-side account 38 | * maintained by your application. 39 | * 40 | * @param token The new token. 41 | */ 42 | private void sendRegistrationToServer(final String token) { 43 | new SharedPrefUtil(getApplicationContext()).saveString(Constants.ARG_FIREBASE_TOKEN, token); 44 | 45 | if (FirebaseAuth.getInstance().getCurrentUser() != null) { 46 | FirebaseDatabase.getInstance() 47 | .getReference() 48 | .child(Constants.ARG_USERS) 49 | .child(FirebaseAuth.getInstance().getCurrentUser().getUid()) 50 | .child(Constants.ARG_FIREBASE_TOKEN) 51 | .setValue(token); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/fcm/MyFirebaseMessagingService.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.fcm; 2 | 3 | import android.app.NotificationManager; 4 | import android.app.PendingIntent; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.media.RingtoneManager; 8 | import android.net.Uri; 9 | import android.support.v4.app.NotificationCompat; 10 | import android.util.Log; 11 | 12 | import com.delaroystudios.firebasechat.FirebaseChatMainApp; 13 | import com.delaroystudios.firebasechat.R; 14 | import com.delaroystudios.firebasechat.events.PushNotificationEvent; 15 | import com.delaroystudios.firebasechat.ui.activities.ChatActivity; 16 | import com.delaroystudios.firebasechat.utils.Constants; 17 | import com.google.firebase.messaging.FirebaseMessagingService; 18 | import com.google.firebase.messaging.RemoteMessage; 19 | 20 | import org.greenrobot.eventbus.EventBus; 21 | 22 | public class MyFirebaseMessagingService extends FirebaseMessagingService { 23 | 24 | private static final String TAG = "MyFirebaseMsgService"; 25 | 26 | /** 27 | * Called when message is received. 28 | * 29 | * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 30 | */ 31 | @Override 32 | public void onMessageReceived(RemoteMessage remoteMessage) { 33 | 34 | // TODO(developer): Handle FCM messages here. 35 | // Not getting messages here? See why this may be: https://goo.gl/39bRNJ 36 | Log.d(TAG, "From: " + remoteMessage.getFrom()); 37 | 38 | // Check if message contains a data payload. 39 | if (remoteMessage.getData().size() > 0) { 40 | Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 41 | 42 | String title = remoteMessage.getData().get("title"); 43 | String message = remoteMessage.getData().get("text"); 44 | String username = remoteMessage.getData().get("username"); 45 | String uid = remoteMessage.getData().get("uid"); 46 | String fcmToken = remoteMessage.getData().get("fcm_token"); 47 | 48 | // Don't show notification if chat activity is open. 49 | if (!FirebaseChatMainApp.isChatActivityOpen()) { 50 | sendNotification(title, 51 | message, 52 | username, 53 | uid, 54 | fcmToken); 55 | } else { 56 | EventBus.getDefault().post(new PushNotificationEvent(title, 57 | message, 58 | username, 59 | uid, 60 | fcmToken)); 61 | } 62 | } 63 | } 64 | 65 | /** 66 | * Create and show a simple notification containing the received FCM message. 67 | */ 68 | private void sendNotification(String title, 69 | String message, 70 | String receiver, 71 | String receiverUid, 72 | String firebaseToken) { 73 | Intent intent = new Intent(this, ChatActivity.class); 74 | intent.putExtra(Constants.ARG_RECEIVER, receiver); 75 | intent.putExtra(Constants.ARG_RECEIVER_UID, receiverUid); 76 | intent.putExtra(Constants.ARG_FIREBASE_TOKEN, firebaseToken); 77 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 78 | PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 79 | PendingIntent.FLAG_ONE_SHOT); 80 | 81 | Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 82 | NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 83 | .setSmallIcon(R.drawable.ic_messaging) 84 | .setContentTitle(title) 85 | .setContentText(message) 86 | .setAutoCancel(true) 87 | .setSound(defaultSoundUri) 88 | .setContentIntent(pendingIntent); 89 | 90 | NotificationManager notificationManager = 91 | (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 92 | 93 | notificationManager.notify(0, notificationBuilder.build()); 94 | } 95 | } -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/models/Chat.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.models; 2 | 3 | /** 4 | * Created by delaroy on 4/13/17. 5 | */ 6 | public class Chat { 7 | public String sender; 8 | public String receiver; 9 | public String senderUid; 10 | public String receiverUid; 11 | public String message; 12 | public long timestamp; 13 | 14 | public Chat(){ 15 | 16 | } 17 | 18 | public Chat(String sender, String receiver, String senderUid, String receiverUid, String message, long timestamp){ 19 | this.sender = sender; 20 | this.receiver = receiver; 21 | this.senderUid = senderUid; 22 | this.receiverUid = receiverUid; 23 | this.message = message; 24 | this.timestamp = timestamp; 25 | 26 | } 27 | 28 | 29 | 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/models/User.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.models; 2 | 3 | /** 4 | * Created by delaroy on 4/13/17. 5 | */ 6 | public class User { 7 | public String uid; 8 | public String email; 9 | public String firebaseToken; 10 | 11 | public User(){ 12 | 13 | } 14 | 15 | public User(String uid, String email, String firebaseToken){ 16 | this.uid = uid; 17 | this.email = email; 18 | this.firebaseToken = firebaseToken; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/models/Users.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.models; 2 | 3 | /** 4 | * Created by delaroy on 4/13/17. 5 | */ 6 | public class Users { 7 | 8 | private String emailId; 9 | private String lastMessage; 10 | private int notifCount; 11 | 12 | public String getEmailId(){ return emailId; } 13 | 14 | public void setEmailId(){ this.emailId = emailId; } 15 | 16 | public String getLastMessage(){ return lastMessage; } 17 | 18 | public void setLastMessage(){ this.lastMessage = lastMessage; } 19 | 20 | public int getNotifCount(){ return notifCount; } 21 | 22 | public void setNotifCount(int notifCount){ this.notifCount = notifCount; } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/activities/ChatActivity.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.activities; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v4.app.FragmentTransaction; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.Toolbar; 9 | 10 | import com.delaroystudios.firebasechat.FirebaseChatMainApp; 11 | import com.delaroystudios.firebasechat.R; 12 | import com.delaroystudios.firebasechat.ui.fragments.ChatFragment; 13 | import com.delaroystudios.firebasechat.utils.Constants; 14 | 15 | public class ChatActivity extends AppCompatActivity { 16 | private Toolbar mToolbar; 17 | 18 | public static void startActivity(Context context, 19 | String receiver, 20 | String receiverUid, 21 | String firebaseToken) { 22 | Intent intent = new Intent(context, ChatActivity.class); 23 | intent.putExtra(Constants.ARG_RECEIVER, receiver); 24 | intent.putExtra(Constants.ARG_RECEIVER_UID, receiverUid); 25 | intent.putExtra(Constants.ARG_FIREBASE_TOKEN, firebaseToken); 26 | context.startActivity(intent); 27 | } 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_chat); 33 | bindViews(); 34 | init(); 35 | } 36 | 37 | private void bindViews() { 38 | mToolbar = (Toolbar) findViewById(R.id.toolbar); 39 | } 40 | 41 | private void init() { 42 | // set the toolbar 43 | setSupportActionBar(mToolbar); 44 | 45 | // set toolbar title 46 | mToolbar.setTitle(getIntent().getExtras().getString(Constants.ARG_RECEIVER)); 47 | 48 | // set the register screen fragment 49 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 50 | fragmentTransaction.replace(R.id.frame_layout_content_chat, 51 | ChatFragment.newInstance(getIntent().getExtras().getString(Constants.ARG_RECEIVER), 52 | getIntent().getExtras().getString(Constants.ARG_RECEIVER_UID), 53 | getIntent().getExtras().getString(Constants.ARG_FIREBASE_TOKEN)), 54 | ChatFragment.class.getSimpleName()); 55 | fragmentTransaction.commit(); 56 | } 57 | 58 | @Override 59 | protected void onResume() { 60 | super.onResume(); 61 | FirebaseChatMainApp.setChatActivityOpen(true); 62 | } 63 | 64 | @Override 65 | protected void onPause() { 66 | super.onPause(); 67 | FirebaseChatMainApp.setChatActivityOpen(false); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/activities/LoginActivity.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.activities; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v4.app.FragmentTransaction; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.Toolbar; 9 | 10 | import com.delaroystudios.firebasechat.R; 11 | import com.delaroystudios.firebasechat.ui.fragments.LoginFragment; 12 | 13 | public class LoginActivity extends AppCompatActivity { 14 | private Toolbar mToolbar; 15 | 16 | public static void startIntent(Context context) { 17 | Intent intent = new Intent(context, LoginActivity.class); 18 | context.startActivity(intent); 19 | } 20 | 21 | public static void startIntent(Context context, int flags) { 22 | Intent intent = new Intent(context, LoginActivity.class); 23 | intent.setFlags(flags); 24 | context.startActivity(intent); 25 | } 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_login); 31 | bindViews(); 32 | init(); 33 | } 34 | 35 | private void bindViews() { 36 | mToolbar = (Toolbar) findViewById(R.id.toolbar); 37 | } 38 | 39 | private void init() { 40 | // set the toolbar 41 | setSupportActionBar(mToolbar); 42 | 43 | // set the login screen fragment 44 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 45 | fragmentTransaction.replace(R.id.frame_layout_content_login, 46 | LoginFragment.newInstance(), 47 | LoginFragment.class.getSimpleName()); 48 | fragmentTransaction.commit(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/activities/RegisterActivity.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.activities; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v4.app.FragmentTransaction; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.Toolbar; 9 | 10 | import com.delaroystudios.firebasechat.R; 11 | import com.delaroystudios.firebasechat.ui.fragments.RegisterFragment; 12 | 13 | public class RegisterActivity extends AppCompatActivity { 14 | private Toolbar mToolbar; 15 | 16 | public static void startActivity(Context context) { 17 | Intent intent = new Intent(context, RegisterActivity.class); 18 | context.startActivity(intent); 19 | } 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_register); 25 | bindViews(); 26 | init(); 27 | } 28 | 29 | private void bindViews() { 30 | mToolbar = (Toolbar) findViewById(R.id.toolbar); 31 | } 32 | 33 | private void init() { 34 | // set the toolbar 35 | setSupportActionBar(mToolbar); 36 | 37 | // set the register screen fragment 38 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 39 | fragmentTransaction.replace(R.id.frame_layout_content_register, 40 | RegisterFragment.newInstance(), 41 | RegisterFragment.class.getSimpleName()); 42 | fragmentTransaction.commit(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/activities/SplashActivity.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.activities; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.support.v7.app.AppCompatActivity; 6 | 7 | import com.delaroystudios.firebasechat.R; 8 | import com.google.firebase.auth.FirebaseAuth; 9 | 10 | public class SplashActivity extends AppCompatActivity { 11 | private static final int SPLASH_TIME_MS = 2000; 12 | private Handler mHandler; 13 | private Runnable mRunnable; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_splash); 19 | 20 | mHandler = new Handler(); 21 | 22 | mRunnable = new Runnable() { 23 | @Override 24 | public void run() { 25 | // check if user is already logged in or not 26 | if (FirebaseAuth.getInstance().getCurrentUser() != null) { 27 | // if logged in redirect the user to user listing activity 28 | UserListingActivity.startActivity(SplashActivity.this); 29 | } else { 30 | // otherwise redirect the user to login activity 31 | LoginActivity.startIntent(SplashActivity.this); 32 | } 33 | finish(); 34 | } 35 | }; 36 | 37 | mHandler.postDelayed(mRunnable, SPLASH_TIME_MS); 38 | } 39 | 40 | /*@Override 41 | protected void onPause() { 42 | super.onPause(); 43 | mHandler.removeCallbacks(mRunnable); 44 | } 45 | 46 | @Override 47 | protected void onResume() { 48 | super.onResume(); 49 | mHandler.postDelayed(mRunnable, SPLASH_TIME_MS); 50 | }*/ 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/activities/UserListingActivity.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.activities; 2 | 3 | import android.content.Context; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.support.design.widget.TabLayout; 8 | import android.support.v4.view.ViewPager; 9 | import android.support.v7.app.AlertDialog; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.support.v7.widget.Toolbar; 12 | import android.view.Menu; 13 | import android.view.MenuItem; 14 | import android.widget.Toast; 15 | 16 | import com.delaroystudios.firebasechat.R; 17 | import com.delaroystudios.firebasechat.core.logout.LogoutContract; 18 | import com.delaroystudios.firebasechat.core.logout.LogoutPresenter; 19 | import com.delaroystudios.firebasechat.ui.adapters.UserListingPagerAdapter; 20 | 21 | public class UserListingActivity extends AppCompatActivity implements LogoutContract.View { 22 | private Toolbar mToolbar; 23 | private TabLayout mTabLayoutUserListing; 24 | private ViewPager mViewPagerUserListing; 25 | 26 | private LogoutPresenter mLogoutPresenter; 27 | 28 | public static void startActivity(Context context) { 29 | Intent intent = new Intent(context, UserListingActivity.class); 30 | context.startActivity(intent); 31 | } 32 | 33 | public static void startActivity(Context context, int flags) { 34 | Intent intent = new Intent(context, UserListingActivity.class); 35 | intent.setFlags(flags); 36 | context.startActivity(intent); 37 | } 38 | 39 | @Override 40 | protected void onCreate(Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | setContentView(R.layout.activity_user_listing); 43 | bindViews(); 44 | init(); 45 | } 46 | 47 | private void bindViews() { 48 | mToolbar = (Toolbar) findViewById(R.id.toolbar); 49 | mTabLayoutUserListing = (TabLayout) findViewById(R.id.tab_layout_user_listing); 50 | mViewPagerUserListing = (ViewPager) findViewById(R.id.view_pager_user_listing); 51 | } 52 | 53 | private void init() { 54 | // set the toolbar 55 | setSupportActionBar(mToolbar); 56 | 57 | // set the view pager adapter 58 | UserListingPagerAdapter userListingPagerAdapter = new UserListingPagerAdapter(getSupportFragmentManager()); 59 | mViewPagerUserListing.setAdapter(userListingPagerAdapter); 60 | 61 | // attach tab layout with view pager 62 | mTabLayoutUserListing.setupWithViewPager(mViewPagerUserListing); 63 | 64 | mLogoutPresenter = new LogoutPresenter(this); 65 | } 66 | 67 | @Override 68 | public boolean onCreateOptionsMenu(Menu menu) { 69 | getMenuInflater().inflate(R.menu.menu_user_listing, menu); 70 | return super.onCreateOptionsMenu(menu); 71 | } 72 | 73 | @Override 74 | public boolean onOptionsItemSelected(MenuItem item) { 75 | switch (item.getItemId()) { 76 | case R.id.action_logout: 77 | logout(); 78 | break; 79 | } 80 | return super.onOptionsItemSelected(item); 81 | } 82 | 83 | private void logout() { 84 | new AlertDialog.Builder(this) 85 | .setTitle(R.string.logout) 86 | .setMessage(R.string.are_you_sure) 87 | .setPositiveButton(R.string.logout, new DialogInterface.OnClickListener() { 88 | @Override 89 | public void onClick(DialogInterface dialog, int which) { 90 | dialog.dismiss(); 91 | mLogoutPresenter.logout(); 92 | } 93 | }) 94 | .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 95 | @Override 96 | public void onClick(DialogInterface dialog, int which) { 97 | dialog.dismiss(); 98 | } 99 | }) 100 | .show(); 101 | } 102 | 103 | @Override 104 | public void onLogoutSuccess(String message) { 105 | Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 106 | LoginActivity.startIntent(this, 107 | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 108 | } 109 | 110 | @Override 111 | public void onLogoutFailure(String message) { 112 | Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/adapters/ChatRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.adapters; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.text.TextUtils; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import com.delaroystudios.firebasechat.R; 11 | import com.delaroystudios.firebasechat.models.Chat; 12 | import com.google.firebase.auth.FirebaseAuth; 13 | 14 | import java.util.List; 15 | 16 | 17 | 18 | public class ChatRecyclerAdapter extends RecyclerView.Adapter { 19 | private static final int VIEW_TYPE_ME = 1; 20 | private static final int VIEW_TYPE_OTHER = 2; 21 | 22 | private List mChats; 23 | 24 | public ChatRecyclerAdapter(List chats) { 25 | mChats = chats; 26 | } 27 | 28 | public void add(Chat chat) { 29 | mChats.add(chat); 30 | notifyItemInserted(mChats.size() - 1); 31 | } 32 | 33 | @Override 34 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 35 | LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); 36 | RecyclerView.ViewHolder viewHolder = null; 37 | switch (viewType) { 38 | case VIEW_TYPE_ME: 39 | View viewChatMine = layoutInflater.inflate(R.layout.item_chat_mine, parent, false); 40 | viewHolder = new MyChatViewHolder(viewChatMine); 41 | break; 42 | case VIEW_TYPE_OTHER: 43 | View viewChatOther = layoutInflater.inflate(R.layout.item_chat_other, parent, false); 44 | viewHolder = new OtherChatViewHolder(viewChatOther); 45 | break; 46 | } 47 | return viewHolder; 48 | } 49 | 50 | @Override 51 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 52 | if (TextUtils.equals(mChats.get(position).senderUid, 53 | FirebaseAuth.getInstance().getCurrentUser().getUid())) { 54 | configureMyChatViewHolder((MyChatViewHolder) holder, position); 55 | } else { 56 | configureOtherChatViewHolder((OtherChatViewHolder) holder, position); 57 | } 58 | } 59 | 60 | private void configureMyChatViewHolder(MyChatViewHolder myChatViewHolder, int position) { 61 | Chat chat = mChats.get(position); 62 | 63 | String alphabet = chat.sender.substring(0, 1); 64 | 65 | myChatViewHolder.txtChatMessage.setText(chat.message); 66 | myChatViewHolder.txtUserAlphabet.setText(alphabet); 67 | } 68 | 69 | private void configureOtherChatViewHolder(OtherChatViewHolder otherChatViewHolder, int position) { 70 | Chat chat = mChats.get(position); 71 | 72 | String alphabet = chat.sender.substring(0, 1); 73 | 74 | otherChatViewHolder.txtChatMessage.setText(chat.message); 75 | otherChatViewHolder.txtUserAlphabet.setText(alphabet); 76 | } 77 | 78 | @Override 79 | public int getItemCount() { 80 | if (mChats != null) { 81 | return mChats.size(); 82 | } 83 | return 0; 84 | } 85 | 86 | @Override 87 | public int getItemViewType(int position) { 88 | if (TextUtils.equals(mChats.get(position).senderUid, 89 | FirebaseAuth.getInstance().getCurrentUser().getUid())) { 90 | return VIEW_TYPE_ME; 91 | } else { 92 | return VIEW_TYPE_OTHER; 93 | } 94 | } 95 | 96 | private static class MyChatViewHolder extends RecyclerView.ViewHolder { 97 | private TextView txtChatMessage, txtUserAlphabet; 98 | 99 | public MyChatViewHolder(View itemView) { 100 | super(itemView); 101 | txtChatMessage = (TextView) itemView.findViewById(R.id.text_view_chat_message); 102 | txtUserAlphabet = (TextView) itemView.findViewById(R.id.text_view_user_alphabet); 103 | } 104 | } 105 | 106 | private static class OtherChatViewHolder extends RecyclerView.ViewHolder { 107 | private TextView txtChatMessage, txtUserAlphabet; 108 | 109 | public OtherChatViewHolder(View itemView) { 110 | super(itemView); 111 | txtChatMessage = (TextView) itemView.findViewById(R.id.text_view_chat_message); 112 | txtUserAlphabet = (TextView) itemView.findViewById(R.id.text_view_user_alphabet); 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/adapters/UserListingPagerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.adapters; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.support.v4.app.FragmentManager; 5 | import android.support.v4.app.FragmentPagerAdapter; 6 | 7 | import com.delaroystudios.firebasechat.ui.fragments.UsersFragment; 8 | 9 | 10 | public class UserListingPagerAdapter extends FragmentPagerAdapter { 11 | private static final Fragment[] sFragments = new Fragment[]{/*UsersFragment.newInstance(UsersFragment.TYPE_CHATS),*/ 12 | UsersFragment.newInstance(UsersFragment.TYPE_ALL)}; 13 | private static final String[] sTitles = new String[]{/*"Chats",*/ 14 | "All Users"}; 15 | 16 | public UserListingPagerAdapter(FragmentManager fm) { 17 | super(fm); 18 | } 19 | 20 | @Override 21 | public Fragment getItem(int position) { 22 | return sFragments[position]; 23 | } 24 | 25 | @Override 26 | public int getCount() { 27 | return sFragments.length; 28 | } 29 | 30 | @Override 31 | public CharSequence getPageTitle(int position) { 32 | return sTitles[position]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/adapters/UserListingRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.adapters; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.TextView; 8 | 9 | import com.delaroystudios.firebasechat.R; 10 | import com.delaroystudios.firebasechat.models.User; 11 | 12 | import java.util.List; 13 | 14 | 15 | 16 | public class UserListingRecyclerAdapter extends RecyclerView.Adapter { 17 | private List mUsers; 18 | 19 | public UserListingRecyclerAdapter(List users) { 20 | this.mUsers = users; 21 | } 22 | 23 | public void add(User user) { 24 | mUsers.add(user); 25 | notifyItemInserted(mUsers.size() - 1); 26 | } 27 | 28 | @Override 29 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 30 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_all_user_listing, parent, false); 31 | return new ViewHolder(view); 32 | } 33 | 34 | @Override 35 | public void onBindViewHolder(ViewHolder holder, int position) { 36 | User user = mUsers.get(position); 37 | 38 | if (user.email != null){ 39 | String alphabet = user.email.substring(0, 1); 40 | 41 | holder.txtUsername.setText(user.email); 42 | holder.txtUserAlphabet.setText(alphabet); 43 | } 44 | } 45 | 46 | @Override 47 | public int getItemCount() { 48 | if (mUsers != null) { 49 | return mUsers.size(); 50 | } 51 | return 0; 52 | } 53 | 54 | public User getUser(int position) { 55 | return mUsers.get(position); 56 | } 57 | 58 | static class ViewHolder extends RecyclerView.ViewHolder { 59 | private TextView txtUserAlphabet, txtUsername; 60 | 61 | ViewHolder(View itemView) { 62 | super(itemView); 63 | txtUserAlphabet = (TextView) itemView.findViewById(R.id.text_view_user_alphabet); 64 | txtUsername = (TextView) itemView.findViewById(R.id.text_view_username); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/fragments/ChatFragment.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.fragments; 2 | 3 | import android.app.ProgressDialog; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.KeyEvent; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.view.inputmethod.EditorInfo; 13 | import android.widget.EditText; 14 | import android.widget.TextView; 15 | import android.widget.Toast; 16 | 17 | import com.delaroystudios.firebasechat.R; 18 | import com.delaroystudios.firebasechat.core.chat.ChatContract; 19 | import com.delaroystudios.firebasechat.core.chat.ChatPresenter; 20 | import com.delaroystudios.firebasechat.events.PushNotificationEvent; 21 | import com.delaroystudios.firebasechat.models.Chat; 22 | import com.delaroystudios.firebasechat.ui.adapters.ChatRecyclerAdapter; 23 | import com.delaroystudios.firebasechat.utils.Constants; 24 | import com.google.firebase.auth.FirebaseAuth; 25 | 26 | import org.greenrobot.eventbus.EventBus; 27 | import org.greenrobot.eventbus.Subscribe; 28 | 29 | import java.util.ArrayList; 30 | 31 | 32 | 33 | public class ChatFragment extends Fragment implements ChatContract.View, TextView.OnEditorActionListener { 34 | private RecyclerView mRecyclerViewChat; 35 | private EditText mETxtMessage; 36 | 37 | private ProgressDialog mProgressDialog; 38 | 39 | private ChatRecyclerAdapter mChatRecyclerAdapter; 40 | 41 | private ChatPresenter mChatPresenter; 42 | 43 | public static ChatFragment newInstance(String receiver, 44 | String receiverUid, 45 | String firebaseToken) { 46 | Bundle args = new Bundle(); 47 | args.putString(Constants.ARG_RECEIVER, receiver); 48 | args.putString(Constants.ARG_RECEIVER_UID, receiverUid); 49 | args.putString(Constants.ARG_FIREBASE_TOKEN, firebaseToken); 50 | ChatFragment fragment = new ChatFragment(); 51 | fragment.setArguments(args); 52 | return fragment; 53 | } 54 | 55 | @Override 56 | public void onStart() { 57 | super.onStart(); 58 | EventBus.getDefault().register(this); 59 | } 60 | 61 | @Override 62 | public void onStop() { 63 | super.onStop(); 64 | EventBus.getDefault().unregister(this); 65 | } 66 | 67 | @Nullable 68 | @Override 69 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 70 | View fragmentView = inflater.inflate(R.layout.fragment_chat, container, false); 71 | bindViews(fragmentView); 72 | return fragmentView; 73 | } 74 | 75 | private void bindViews(View view) { 76 | mRecyclerViewChat = (RecyclerView) view.findViewById(R.id.recycler_view_chat); 77 | mETxtMessage = (EditText) view.findViewById(R.id.edit_text_message); 78 | } 79 | 80 | @Override 81 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 82 | super.onActivityCreated(savedInstanceState); 83 | init(); 84 | } 85 | 86 | private void init() { 87 | mProgressDialog = new ProgressDialog(getActivity()); 88 | mProgressDialog.setTitle(getString(R.string.loading)); 89 | mProgressDialog.setMessage(getString(R.string.please_wait)); 90 | mProgressDialog.setIndeterminate(true); 91 | 92 | mETxtMessage.setOnEditorActionListener(this); 93 | 94 | mChatPresenter = new ChatPresenter(this); 95 | mChatPresenter.getMessage(FirebaseAuth.getInstance().getCurrentUser().getUid(), 96 | getArguments().getString(Constants.ARG_RECEIVER_UID)); 97 | } 98 | 99 | @Override 100 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 101 | if (actionId == EditorInfo.IME_ACTION_SEND) { 102 | sendMessage(); 103 | return true; 104 | } 105 | return false; 106 | } 107 | 108 | private void sendMessage() { 109 | String message = mETxtMessage.getText().toString(); 110 | String receiver = getArguments().getString(Constants.ARG_RECEIVER); 111 | String receiverUid = getArguments().getString(Constants.ARG_RECEIVER_UID); 112 | String sender = FirebaseAuth.getInstance().getCurrentUser().getEmail(); 113 | String senderUid = FirebaseAuth.getInstance().getCurrentUser().getUid(); 114 | String receiverFirebaseToken = getArguments().getString(Constants.ARG_FIREBASE_TOKEN); 115 | Chat chat = new Chat(sender, 116 | receiver, 117 | senderUid, 118 | receiverUid, 119 | message, 120 | System.currentTimeMillis()); 121 | mChatPresenter.sendMessage(getActivity().getApplicationContext(), 122 | chat, 123 | receiverFirebaseToken); 124 | } 125 | 126 | @Override 127 | public void onSendMessageSuccess() { 128 | mETxtMessage.setText(""); 129 | Toast.makeText(getActivity(), "Message sent", Toast.LENGTH_SHORT).show(); 130 | } 131 | 132 | @Override 133 | public void onSendMessageFailure(String message) { 134 | Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show(); 135 | } 136 | 137 | @Override 138 | public void onGetMessagesSuccess(Chat chat) { 139 | if (mChatRecyclerAdapter == null) { 140 | mChatRecyclerAdapter = new ChatRecyclerAdapter(new ArrayList()); 141 | mRecyclerViewChat.setAdapter(mChatRecyclerAdapter); 142 | } 143 | mChatRecyclerAdapter.add(chat); 144 | mRecyclerViewChat.smoothScrollToPosition(mChatRecyclerAdapter.getItemCount() - 1); 145 | } 146 | 147 | @Override 148 | public void onGetMessagesFailure(String message) { 149 | Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show(); 150 | } 151 | 152 | @Subscribe 153 | public void onPushNotificationEvent(PushNotificationEvent pushNotificationEvent) { 154 | if (mChatRecyclerAdapter == null || mChatRecyclerAdapter.getItemCount() == 0) { 155 | mChatPresenter.getMessage(FirebaseAuth.getInstance().getCurrentUser().getUid(), 156 | pushNotificationEvent.getUid()); 157 | } 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/fragments/LoginFragment.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.fragments; 2 | 3 | import android.app.ProgressDialog; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.app.Fragment; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.Button; 12 | import android.widget.EditText; 13 | import android.widget.Toast; 14 | 15 | import com.delaroystudios.firebasechat.R; 16 | import com.delaroystudios.firebasechat.core.login.LoginContract; 17 | import com.delaroystudios.firebasechat.core.login.LoginPresenter; 18 | import com.delaroystudios.firebasechat.ui.activities.RegisterActivity; 19 | import com.delaroystudios.firebasechat.ui.activities.UserListingActivity; 20 | 21 | 22 | 23 | public class LoginFragment extends Fragment implements View.OnClickListener, LoginContract.View { 24 | private LoginPresenter mLoginPresenter; 25 | 26 | private EditText mETxtEmail, mETxtPassword; 27 | private Button mBtnLogin, mBtnRegister; 28 | 29 | private ProgressDialog mProgressDialog; 30 | 31 | public static LoginFragment newInstance() { 32 | Bundle args = new Bundle(); 33 | LoginFragment fragment = new LoginFragment(); 34 | fragment.setArguments(args); 35 | return fragment; 36 | } 37 | 38 | @Nullable 39 | @Override 40 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 41 | View fragmentView = inflater.inflate(R.layout.fragment_login, container, false); 42 | bindViews(fragmentView); 43 | return fragmentView; 44 | } 45 | 46 | private void bindViews(View view) { 47 | mETxtEmail = (EditText) view.findViewById(R.id.edit_text_email_id); 48 | mETxtPassword = (EditText) view.findViewById(R.id.edit_text_password); 49 | mBtnLogin = (Button) view.findViewById(R.id.button_login); 50 | mBtnRegister = (Button) view.findViewById(R.id.button_register); 51 | } 52 | 53 | @Override 54 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 55 | super.onActivityCreated(savedInstanceState); 56 | init(); 57 | } 58 | 59 | private void init() { 60 | mLoginPresenter = new LoginPresenter(this); 61 | 62 | mProgressDialog = new ProgressDialog(getActivity()); 63 | mProgressDialog.setTitle(getString(R.string.loading)); 64 | mProgressDialog.setMessage(getString(R.string.please_wait)); 65 | mProgressDialog.setIndeterminate(true); 66 | 67 | mBtnLogin.setOnClickListener(this); 68 | mBtnRegister.setOnClickListener(this); 69 | 70 | setDummyCredentials(); 71 | } 72 | 73 | private void setDummyCredentials() { 74 | mETxtEmail.setText("test@test.com"); 75 | mETxtPassword.setText("123456"); 76 | } 77 | 78 | @Override 79 | public void onClick(View view) { 80 | int viewId = view.getId(); 81 | 82 | switch (viewId) { 83 | case R.id.button_login: 84 | onLogin(view); 85 | break; 86 | case R.id.button_register: 87 | onRegister(view); 88 | break; 89 | } 90 | } 91 | 92 | private void onLogin(View view) { 93 | String emailId = mETxtEmail.getText().toString(); 94 | String password = mETxtPassword.getText().toString(); 95 | 96 | mLoginPresenter.login(getActivity(), emailId, password); 97 | mProgressDialog.show(); 98 | } 99 | 100 | private void onRegister(View view) { 101 | RegisterActivity.startActivity(getActivity()); 102 | } 103 | 104 | @Override 105 | public void onLoginSuccess(String message) { 106 | mProgressDialog.dismiss(); 107 | Toast.makeText(getActivity(), "Logged in successfully", Toast.LENGTH_SHORT).show(); 108 | UserListingActivity.startActivity(getActivity(), 109 | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 110 | } 111 | 112 | @Override 113 | public void onLoginFailure(String message) { 114 | mProgressDialog.dismiss(); 115 | Toast.makeText(getActivity(), "Error: " + message, Toast.LENGTH_SHORT).show(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/fragments/RegisterFragment.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.fragments; 2 | 3 | import android.app.ProgressDialog; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.app.Fragment; 8 | import android.util.Log; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.Button; 13 | import android.widget.EditText; 14 | import android.widget.Toast; 15 | 16 | import com.delaroystudios.firebasechat.R; 17 | import com.delaroystudios.firebasechat.core.registration.RegisterContract; 18 | import com.delaroystudios.firebasechat.core.registration.RegisterPresenter; 19 | import com.delaroystudios.firebasechat.core.users.add.AddUserContract; 20 | import com.delaroystudios.firebasechat.core.users.add.AddUserPresenter; 21 | import com.delaroystudios.firebasechat.ui.activities.UserListingActivity; 22 | import com.google.firebase.auth.FirebaseUser; 23 | 24 | 25 | 26 | public class RegisterFragment extends Fragment implements View.OnClickListener, RegisterContract.View, AddUserContract.View { 27 | private static final String TAG = RegisterFragment.class.getSimpleName(); 28 | 29 | private RegisterPresenter mRegisterPresenter; 30 | private AddUserPresenter mAddUserPresenter; 31 | 32 | private EditText mETxtEmail, mETxtPassword; 33 | private Button mBtnRegister; 34 | 35 | private ProgressDialog mProgressDialog; 36 | 37 | public static RegisterFragment newInstance() { 38 | Bundle args = new Bundle(); 39 | RegisterFragment fragment = new RegisterFragment(); 40 | fragment.setArguments(args); 41 | return fragment; 42 | } 43 | 44 | @Nullable 45 | @Override 46 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 47 | View fragmentView = inflater.inflate(R.layout.fragment_register, container, false); 48 | bindViews(fragmentView); 49 | return fragmentView; 50 | } 51 | 52 | private void bindViews(View view) { 53 | mETxtEmail = (EditText) view.findViewById(R.id.edit_text_email_id); 54 | mETxtPassword = (EditText) view.findViewById(R.id.edit_text_password); 55 | mBtnRegister = (Button) view.findViewById(R.id.button_register); 56 | } 57 | 58 | @Override 59 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 60 | super.onActivityCreated(savedInstanceState); 61 | init(); 62 | } 63 | 64 | private void init() { 65 | mRegisterPresenter = new RegisterPresenter(this); 66 | mAddUserPresenter = new AddUserPresenter(this); 67 | 68 | mProgressDialog = new ProgressDialog(getActivity()); 69 | mProgressDialog.setTitle(getString(R.string.loading)); 70 | mProgressDialog.setMessage(getString(R.string.please_wait)); 71 | mProgressDialog.setIndeterminate(true); 72 | 73 | mBtnRegister.setOnClickListener(this); 74 | } 75 | 76 | @Override 77 | public void onClick(View view) { 78 | int viewId = view.getId(); 79 | 80 | switch (viewId) { 81 | case R.id.button_register: 82 | onRegister(view); 83 | break; 84 | } 85 | } 86 | 87 | private void onRegister(View view) { 88 | String emailId = mETxtEmail.getText().toString(); 89 | String password = mETxtPassword.getText().toString(); 90 | 91 | mRegisterPresenter.register(getActivity(), emailId, password); 92 | mProgressDialog.show(); 93 | } 94 | 95 | @Override 96 | public void onRegistrationSuccess(FirebaseUser firebaseUser) { 97 | mProgressDialog.setMessage(getString(R.string.adding_user_to_db)); 98 | Toast.makeText(getActivity(), "Registration Successful!", Toast.LENGTH_SHORT).show(); 99 | mAddUserPresenter.addUser(getActivity().getApplicationContext(), firebaseUser); 100 | } 101 | 102 | @Override 103 | public void onRegistrationFailure(String message) { 104 | mProgressDialog.dismiss(); 105 | mProgressDialog.setMessage(getString(R.string.please_wait)); 106 | Log.e(TAG, "onRegistrationFailure: " + message); 107 | Toast.makeText(getActivity(), "Registration failed!+\n" + message, Toast.LENGTH_LONG).show(); 108 | } 109 | 110 | @Override 111 | public void onAddUserSuccess(String message) { 112 | mProgressDialog.dismiss(); 113 | Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show(); 114 | UserListingActivity.startActivity(getActivity(), 115 | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 116 | } 117 | 118 | @Override 119 | public void onAddUserFailure(String message) { 120 | mProgressDialog.dismiss(); 121 | Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show(); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/ui/fragments/UsersFragment.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.ui.fragments; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.widget.SwipeRefreshLayout; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.text.TextUtils; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.Toast; 13 | 14 | import com.delaroystudios.firebasechat.R; 15 | import com.delaroystudios.firebasechat.core.users.getall.GetUsersContract; 16 | import com.delaroystudios.firebasechat.core.users.getall.GetUsersPresenter; 17 | import com.delaroystudios.firebasechat.models.User; 18 | import com.delaroystudios.firebasechat.ui.activities.ChatActivity; 19 | import com.delaroystudios.firebasechat.ui.adapters.UserListingRecyclerAdapter; 20 | import com.delaroystudios.firebasechat.utils.ItemClickSupport; 21 | 22 | import java.util.List; 23 | 24 | 25 | 26 | public class UsersFragment extends Fragment implements GetUsersContract.View, ItemClickSupport.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { 27 | public static final String ARG_TYPE = "type"; 28 | public static final String TYPE_CHATS = "type_chats"; 29 | public static final String TYPE_ALL = "type_all"; 30 | 31 | private SwipeRefreshLayout mSwipeRefreshLayout; 32 | private RecyclerView mRecyclerViewAllUserListing; 33 | 34 | private UserListingRecyclerAdapter mUserListingRecyclerAdapter; 35 | 36 | private GetUsersPresenter mGetUsersPresenter; 37 | 38 | public static UsersFragment newInstance(String type) { 39 | Bundle args = new Bundle(); 40 | args.putString(ARG_TYPE, type); 41 | UsersFragment fragment = new UsersFragment(); 42 | fragment.setArguments(args); 43 | return fragment; 44 | } 45 | 46 | @Nullable 47 | @Override 48 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 49 | View fragmentView = inflater.inflate(R.layout.fragment_users, container, false); 50 | bindViews(fragmentView); 51 | return fragmentView; 52 | } 53 | 54 | private void bindViews(View view) { 55 | mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout); 56 | mRecyclerViewAllUserListing = (RecyclerView) view.findViewById(R.id.recycler_view_all_user_listing); 57 | } 58 | 59 | @Override 60 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 61 | super.onActivityCreated(savedInstanceState); 62 | init(); 63 | } 64 | 65 | private void init() { 66 | mGetUsersPresenter = new GetUsersPresenter(this); 67 | getUsers(); 68 | mSwipeRefreshLayout.post(new Runnable() { 69 | @Override 70 | public void run() { 71 | mSwipeRefreshLayout.setRefreshing(true); 72 | } 73 | }); 74 | 75 | ItemClickSupport.addTo(mRecyclerViewAllUserListing) 76 | .setOnItemClickListener(this); 77 | 78 | mSwipeRefreshLayout.setOnRefreshListener(this); 79 | } 80 | 81 | @Override 82 | public void onRefresh() { 83 | getUsers(); 84 | } 85 | 86 | private void getUsers() { 87 | if (TextUtils.equals(getArguments().getString(ARG_TYPE), TYPE_CHATS)) { 88 | 89 | } else if (TextUtils.equals(getArguments().getString(ARG_TYPE), TYPE_ALL)) { 90 | mGetUsersPresenter.getAllUsers(); 91 | } 92 | } 93 | 94 | @Override 95 | public void onItemClicked(RecyclerView recyclerView, int position, View v) { 96 | ChatActivity.startActivity(getActivity(), 97 | mUserListingRecyclerAdapter.getUser(position).email, 98 | mUserListingRecyclerAdapter.getUser(position).uid, 99 | mUserListingRecyclerAdapter.getUser(position).firebaseToken); 100 | } 101 | 102 | @Override 103 | public void onGetAllUsersSuccess(List users) { 104 | mSwipeRefreshLayout.post(new Runnable() { 105 | @Override 106 | public void run() { 107 | mSwipeRefreshLayout.setRefreshing(false); 108 | } 109 | }); 110 | mUserListingRecyclerAdapter = new UserListingRecyclerAdapter(users); 111 | mRecyclerViewAllUserListing.setAdapter(mUserListingRecyclerAdapter); 112 | mUserListingRecyclerAdapter.notifyDataSetChanged(); 113 | } 114 | 115 | @Override 116 | public void onGetAllUsersFailure(String message) { 117 | mSwipeRefreshLayout.post(new Runnable() { 118 | @Override 119 | public void run() { 120 | mSwipeRefreshLayout.setRefreshing(false); 121 | } 122 | }); 123 | Toast.makeText(getActivity(), "Error: " + message, Toast.LENGTH_SHORT).show(); 124 | } 125 | 126 | @Override 127 | public void onGetChatUsersSuccess(List users) { 128 | 129 | } 130 | 131 | @Override 132 | public void onGetChatUsersFailure(String message) { 133 | 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/utils/Constants.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.utils; 2 | 3 | 4 | 5 | public class Constants { 6 | public static final String ARG_USERS = "users"; 7 | public static final String ARG_RECEIVER = "receiver"; 8 | public static final String ARG_RECEIVER_UID = "receiver_uid"; 9 | public static final String ARG_CHAT_ROOMS = "chat_rooms"; 10 | public static final String ARG_FIREBASE_TOKEN = "firebaseToken"; 11 | public static final String ARG_FRIENDS = "friends"; 12 | public static final String ARG_UID = "uid"; 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/utils/ItemClickSupport.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.utils; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.View; 5 | 6 | import com.delaroystudios.firebasechat.R; 7 | 8 | public class ItemClickSupport { 9 | private final RecyclerView mRecyclerView; 10 | private OnItemClickListener mOnItemClickListener; 11 | private OnItemLongClickListener mOnItemLongClickListener; 12 | private View.OnClickListener mOnClickListener = new View.OnClickListener() { 13 | @Override 14 | public void onClick(View v) { 15 | if (mOnItemClickListener != null) { 16 | RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); 17 | mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v); 18 | } 19 | } 20 | }; 21 | private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() { 22 | @Override 23 | public boolean onLongClick(View v) { 24 | if (mOnItemLongClickListener != null) { 25 | RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); 26 | return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v); 27 | } 28 | return false; 29 | } 30 | }; 31 | private RecyclerView.OnChildAttachStateChangeListener mAttachListener 32 | = new RecyclerView.OnChildAttachStateChangeListener() { 33 | @Override 34 | public void onChildViewAttachedToWindow(View view) { 35 | if (mOnItemClickListener != null) { 36 | view.setOnClickListener(mOnClickListener); 37 | } 38 | if (mOnItemLongClickListener != null) { 39 | view.setOnLongClickListener(mOnLongClickListener); 40 | } 41 | } 42 | 43 | @Override 44 | public void onChildViewDetachedFromWindow(View view) { 45 | 46 | } 47 | }; 48 | 49 | private ItemClickSupport(RecyclerView recyclerView) { 50 | mRecyclerView = recyclerView; 51 | mRecyclerView.setTag(R.id.item_click_support, this); 52 | mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener); 53 | } 54 | 55 | public static ItemClickSupport addTo(RecyclerView view) { 56 | ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support); 57 | if (support == null) { 58 | support = new ItemClickSupport(view); 59 | } 60 | return support; 61 | } 62 | 63 | public static ItemClickSupport removeFrom(RecyclerView view) { 64 | ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support); 65 | if (support != null) { 66 | support.detach(view); 67 | } 68 | return support; 69 | } 70 | 71 | public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) { 72 | mOnItemClickListener = listener; 73 | return this; 74 | } 75 | 76 | public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) { 77 | mOnItemLongClickListener = listener; 78 | return this; 79 | } 80 | 81 | private void detach(RecyclerView view) { 82 | view.removeOnChildAttachStateChangeListener(mAttachListener); 83 | view.setTag(R.id.item_click_support, null); 84 | } 85 | 86 | public interface OnItemClickListener { 87 | 88 | void onItemClicked(RecyclerView recyclerView, int position, View v); 89 | } 90 | 91 | public interface OnItemLongClickListener { 92 | 93 | boolean onItemLongClicked(RecyclerView recyclerView, int position, View v); 94 | } 95 | } -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/utils/NetworkConnectionUtil.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.utils; 2 | 3 | import android.content.Context; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.net.ConnectivityManager; 7 | import android.net.NetworkInfo; 8 | import android.provider.Settings; 9 | import android.support.v7.app.AlertDialog; 10 | 11 | import com.delaroystudios.firebasechat.R; 12 | 13 | 14 | public class NetworkConnectionUtil { 15 | public static final String ERR_DIALOG_TITLE = "No internet connection detected !"; 16 | private static final String ERR_DIALOG_MSG = "Looks like our application is not able to detect an active internet connection, " + 17 | "please check your device's network settings."; 18 | private static final String ERR_DIALOG_POSITIVE_BTN = "Settings"; 19 | private static final String ERR_DIALOG_NEGATIVE_BTN = "Dismiss"; 20 | 21 | /** 22 | * Check if the device is connected to internet or not. 23 | * 24 | * @param context Current context of the application 25 | * @return true if device is connected to internet, otherwise false 26 | */ 27 | public static boolean isConnectedToInternet(Context context) { 28 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 29 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 30 | return networkInfo != null && networkInfo.isConnectedOrConnecting(); 31 | } 32 | 33 | /** 34 | * Check if the device is connected to internet via wifi or not. 35 | * 36 | * @param context Current context of the application 37 | * @return true if device is connected to internet via wifi, otherwise false 38 | */ 39 | public static boolean isConnectedToWifi(Context context) { 40 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 41 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 42 | return networkInfo != null && 43 | networkInfo.isConnectedOrConnecting() && 44 | networkInfo.getType() == ConnectivityManager.TYPE_WIFI; 45 | } 46 | 47 | /** 48 | * Check if the device is connected to internet via mobile network or not. 49 | * 50 | * @param context Current context of the application 51 | * @return true if device is connected to internet via mobile network, otherwise false 52 | */ 53 | public static boolean isConnectedToMobileNetwork(Context context) { 54 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 55 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 56 | return networkInfo != null && 57 | networkInfo.isConnectedOrConnecting() && 58 | networkInfo.getType() == ConnectivityManager.TYPE_MOBILE; 59 | } 60 | 61 | /** 62 | * Show a error dialog representing that no internet connection is available currently. 63 | * 64 | * @param context Current context of the application 65 | */ 66 | public static void showNoInternetAvailableErrorDialog(final Context context) { 67 | new AlertDialog.Builder(context) 68 | .setTitle(ERR_DIALOG_TITLE) 69 | .setMessage(ERR_DIALOG_MSG) 70 | .setIcon(R.drawable.ic_error_24dp) 71 | .setPositiveButton(ERR_DIALOG_POSITIVE_BTN, new DialogInterface.OnClickListener() { 72 | @Override 73 | public void onClick(DialogInterface dialogInterface, int i) { 74 | dialogInterface.dismiss(); 75 | Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); 76 | context.startActivity(intent); 77 | } 78 | }) 79 | .setNegativeButton(ERR_DIALOG_NEGATIVE_BTN, new DialogInterface.OnClickListener() { 80 | @Override 81 | public void onClick(DialogInterface dialogInterface, int i) { 82 | dialogInterface.dismiss(); 83 | } 84 | }) 85 | .show(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /app/src/main/java/com/delaroystudios/firebasechat/utils/SharedPrefUtil.java: -------------------------------------------------------------------------------- 1 | package com.delaroystudios.firebasechat.utils; 2 | 3 | import android.content.Context; 4 | import android.content.SharedPreferences; 5 | 6 | 7 | public class SharedPrefUtil { 8 | /** 9 | * Name of the preference file 10 | */ 11 | private static final String APP_PREFS = "application_preferences"; 12 | 13 | private Context mContext; 14 | private SharedPreferences mSharedPreferences; 15 | private SharedPreferences.Editor mEditor; 16 | 17 | public SharedPrefUtil(Context mContext) { 18 | this.mContext = mContext; 19 | } 20 | 21 | /** 22 | * Save a string into shared preference 23 | * 24 | * @param key The name of the preference to modify 25 | * @param value The new value for the preference 26 | */ 27 | public void saveString(String key, String value) { 28 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 29 | mEditor = mSharedPreferences.edit(); 30 | mEditor.putString(key, value); 31 | mEditor.commit(); 32 | } 33 | 34 | /** 35 | * Save a int into shared preference 36 | * 37 | * @param key The name of the preference to modify 38 | * @param value The new value for the preference 39 | */ 40 | public void saveInt(String key, int value) { 41 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 42 | mEditor = mSharedPreferences.edit(); 43 | mEditor.putInt(key, value); 44 | mEditor.commit(); 45 | } 46 | 47 | /** 48 | * Save a boolean into shared preference 49 | * 50 | * @param key The name of the preference to modify 51 | * @param value The new value for the preference 52 | */ 53 | public void saveBoolean(String key, boolean value) { 54 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 55 | mEditor = mSharedPreferences.edit(); 56 | mEditor.putBoolean(key, value); 57 | mEditor.commit(); 58 | } 59 | 60 | /** 61 | * Retrieve a String value from the preferences. 62 | * 63 | * @param key The name of the preference to retrieve. 64 | * @return Returns the preference value if it exists, or null. 65 | * Throws ClassCastException if there is a preference with this name that is not a String. 66 | */ 67 | public String getString(String key) { 68 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 69 | return mSharedPreferences.getString(key, null); 70 | } 71 | 72 | /** 73 | * Retrieve a String value from the preferences. 74 | * 75 | * @param key The name of the preference to retrieve. 76 | * @param defaultValue Value to return if this preference does not exist. 77 | * @return Returns the preference value if it exists, or defaultValue. 78 | * Throws ClassCastException if there is a preference with this name that is not a String. 79 | */ 80 | public String getString(String key, String defaultValue) { 81 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 82 | return mSharedPreferences.getString(key, defaultValue); 83 | } 84 | 85 | /** 86 | * Retrieve a int value from the preferences. 87 | * 88 | * @param key The name of the preference to retrieve. 89 | * @return Returns the preference value if it exists, or 0. 90 | * Throws ClassCastException if there is a preference with this name that is not a int. 91 | */ 92 | public int getInt(String key) { 93 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 94 | return mSharedPreferences.getInt(key, 0); 95 | } 96 | 97 | /** 98 | * Retrieve a int value from the preferences. 99 | * 100 | * @param key The name of the preference to retrieve. 101 | * @param defaultValue Value to return if this preference does not exist. 102 | * @return Returns the preference value if it exists, or defaultValue. 103 | * Throws ClassCastException if there is a preference with this name that is not a int. 104 | */ 105 | public int getInt(String key, int defaultValue) { 106 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 107 | return mSharedPreferences.getInt(key, defaultValue); 108 | } 109 | 110 | /** 111 | * Retrieve a boolean value from the preferences. 112 | * 113 | * @param key The name of the preference to retrieve. 114 | * @return Returns the preference value if it exists, or false. 115 | * Throws ClassCastException if there is a preference with this name that is not a boolean. 116 | */ 117 | public boolean getBoolean(String key) { 118 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 119 | return mSharedPreferences.getBoolean(key, false); 120 | } 121 | 122 | /** 123 | * Retrieve a boolean value from the preferences. 124 | * 125 | * @param key The name of the preference to retrieve. 126 | * @param defaultValue Value to return if this preference does not exist. 127 | * @return Returns the preference value if it exists, or defaultValue. 128 | * Throws ClassCastException if there is a preference with this name that is not a boolean. 129 | */ 130 | public boolean getBoolean(String key, boolean defaultValue) { 131 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 132 | return mSharedPreferences.getBoolean(key, defaultValue); 133 | } 134 | 135 | /** 136 | * Clears the shared preference file 137 | */ 138 | public void clear() { 139 | mSharedPreferences = mContext.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE); 140 | mSharedPreferences.edit().clear().apply(); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/delaroy/AndroidFirbaseChat/f6d09457b536c40d7e26c032191f3084f54c32fb/app/src/main/res/drawable/bg.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/chat_rounded_rect_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/circle_accent.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_error_24dp.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_messaging.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/delaroy/AndroidFirbaseChat/f6d09457b536c40d7e26c032191f3084f54c32fb/app/src/main/res/drawable/ic_messaging.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/rounded_rect_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_chat.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_register.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_splash.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_user_listing.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 24 | 25 | 26 | 27 | 32 | 33 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_chat.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 13 | 14 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 21 | 22 | 25 | 26 | 29 | 30 |