Browse Source

DEVX INITIAL COMMIT

master
LimeCake 2 months ago
commit
96b5e691ae
63 changed files with 4261 additions and 0 deletions
  1. 15
    0
      Android/.gitignore
  2. 8
    0
      Android/.idea/dictionaries/LimeCake.xml
  3. 4
    0
      Android/.idea/encodings.xml
  4. 18
    0
      Android/.idea/gradle.xml
  5. 12
    0
      Android/.idea/runConfigurations.xml
  6. 1
    0
      Android/app/.gitignore
  7. 48
    0
      Android/app/build.gradle
  8. BIN
      Android/app/debug/app-debug.apk
  9. 1
    0
      Android/app/debug/output.json
  10. 40
    0
      Android/app/google-services.json
  11. 21
    0
      Android/app/proguard-rules.pro
  12. BIN
      Android/app/release/app-release.apk
  13. 1
    0
      Android/app/release/output.json
  14. 26
    0
      Android/app/src/androidTest/java/kr/devx/whitechat/ExampleInstrumentedTest.java
  15. 44
    0
      Android/app/src/main/AndroidManifest.xml
  16. 15
    0
      Android/app/src/main/java/kr/devx/whitechat/Data/Message.java
  17. 21
    0
      Android/app/src/main/java/kr/devx/whitechat/Data/Room.java
  18. 30
    0
      Android/app/src/main/java/kr/devx/whitechat/Data/User.java
  19. 908
    0
      Android/app/src/main/java/kr/devx/whitechat/MainActivity.java
  20. 304
    0
      Android/app/src/main/java/kr/devx/whitechat/RoomFragment.java
  21. 198
    0
      Android/app/src/main/java/kr/devx/whitechat/SettingFragment.java
  22. 165
    0
      Android/app/src/main/java/kr/devx/whitechat/SocialFragment.java
  23. 36
    0
      Android/app/src/main/java/kr/devx/whitechat/Util/SmartFragmentStatePagerAdapter.java
  24. 61
    0
      Android/app/src/main/java/kr/devx/whitechat/Util/WhiteFirebaseMessagingService.java
  25. 125
    0
      Android/app/src/main/java/kr/devx/whitechat/Util/chatAdapter.java
  26. 90
    0
      Android/app/src/main/java/kr/devx/whitechat/Util/roomAdapter.java
  27. 92
    0
      Android/app/src/main/java/kr/devx/whitechat/Util/socialAdapter.java
  28. 23
    0
      Android/app/src/main/java/kr/devx/whitechat/WhiteApplication.java
  29. 8
    0
      Android/app/src/main/res/drawable/dotted_border.xml
  30. 6
    0
      Android/app/src/main/res/drawable/flat_dot.xml
  31. BIN
      Android/app/src/main/res/drawable/icon_room.png
  32. BIN
      Android/app/src/main/res/drawable/icon_setting.png
  33. BIN
      Android/app/src/main/res/drawable/icon_social.png
  34. 6
    0
      Android/app/src/main/res/drawable/lined_border.xml
  35. 52
    0
      Android/app/src/main/res/layout/fragment_room.xml
  36. 97
    0
      Android/app/src/main/res/layout/fragment_setting.xml
  37. 29
    0
      Android/app/src/main/res/layout/fragment_social.xml
  38. 120
    0
      Android/app/src/main/res/layout/item_chat.xml
  39. 54
    0
      Android/app/src/main/res/layout/item_room.xml
  40. 46
    0
      Android/app/src/main/res/layout/item_social.xml
  41. 318
    0
      Android/app/src/main/res/layout/main.xml
  42. BIN
      Android/app/src/main/res/mipmap-hdpi/ic_launcher.png
  43. BIN
      Android/app/src/main/res/mipmap-mdpi/ic_launcher.png
  44. BIN
      Android/app/src/main/res/mipmap-xhdpi/ic_launcher.png
  45. BIN
      Android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
  46. BIN
      Android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
  47. 7
    0
      Android/app/src/main/res/values/colors.xml
  48. 82
    0
      Android/app/src/main/res/values/strings.xml
  49. 11
    0
      Android/app/src/main/res/values/styles.xml
  50. 17
    0
      Android/app/src/test/java/kr/devx/whitechat/ExampleUnitTest.java
  51. 24
    0
      Android/build.gradle
  52. 15
    0
      Android/gradle.properties
  53. BIN
      Android/gradle/wrapper/gradle-wrapper.jar
  54. 6
    0
      Android/gradle/wrapper/gradle-wrapper.properties
  55. 172
    0
      Android/gradlew
  56. 84
    0
      Android/gradlew.bat
  57. 1
    0
      Android/settings.gradle
  58. 11
    0
      LICENSE
  59. 34
    0
      README.MD
  60. 134
    0
      Server/Modules/ListFactory.js
  61. 4
    0
      Server/Modules/_notes/dwsync.xml
  62. 6
    0
      Server/WhiteChat.env
  63. 610
    0
      Server/WhiteChat.js

+ 15
- 0
Android/.gitignore View File

@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea/misc.xml
/.idea/vcs.xml
.DS_Store
/build
/captures
.externalNativeBuild

+ 8
- 0
Android/.idea/dictionaries/LimeCake.xml View File

@@ -0,0 +1,8 @@
<component name="ProjectDictionaryState">
<dictionary name="LimeCake">
<words>
<w>devx</w>
<w>whitechat</w>
</words>
</dictionary>
</component>

+ 4
- 0
Android/.idea/encodings.xml View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>

+ 18
- 0
Android/.idea/gradle.xml View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings>
</option>
</component>
</project>

+ 12
- 0
Android/.idea/runConfigurations.xml View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
</set>
</option>
</component>
</project>

+ 1
- 0
Android/app/.gitignore View File

@@ -0,0 +1 @@
/build

+ 48
- 0
Android/app/build.gradle View File

@@ -0,0 +1,48 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "kr.devx.whitechat"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

apply plugin: 'com.google.gms.google-services'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.thomper:sweet-alert-dialog:1.4.0'
implementation 'devlight.io:navigationtabbar:1.2.5'
implementation ('io.socket:socket.io-client:1.0.0') {
exclude group: 'org.json', module: 'json'
}
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
implementation 'de.psdev.licensesdialog:licensesdialog:1.9.0'
}

BIN
Android/app/debug/app-debug.apk View File


+ 1
- 0
Android/app/debug/output.json View File

@@ -0,0 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-debug.apk","fullName":"debug","baseName":"debug"},"path":"app-debug.apk","properties":{}}]

+ 40
- 0
Android/app/google-services.json View File

@@ -0,0 +1,40 @@
{
"project_info": {
"project_number": "571227301299",
"firebase_url": "https://whitechat-devx.firebaseio.com",
"project_id": "whitechat-devx",
"storage_bucket": "whitechat-devx.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:571227301299:android:b14440c1fd1a34b8",
"android_client_info": {
"package_name": "kr.devx.whitechat"
}
},
"oauth_client": [
{
"client_id": "571227301299-u0mvudnonp7rh5drtcd28qg5mfkal51f.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyBVvzOSidBRaKhaKByz__yKfOxhwQdDW8g"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "571227301299-u0mvudnonp7rh5drtcd28qg5mfkal51f.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}

+ 21
- 0
Android/app/proguard-rules.pro View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

BIN
Android/app/release/app-release.apk View File


+ 1
- 0
Android/app/release/output.json View File

@@ -0,0 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]

+ 26
- 0
Android/app/src/androidTest/java/kr/devx/whitechat/ExampleInstrumentedTest.java View File

@@ -0,0 +1,26 @@
package kr.devx.whitechat;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("kr.devx.whitechat", appContext.getPackageName());
}
}

+ 44
- 0
Android/app/src/main/AndroidManifest.xml View File

@@ -0,0 +1,44 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="kr.devx.whitechat">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".WhiteApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup,android:icon">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<service
android:name="kr.devx.whitechat.Util.WhiteFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/icon_room" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="WHITECHAT"/>

</application>
</manifest>

+ 15
- 0
Android/app/src/main/java/kr/devx/whitechat/Data/Message.java View File

@@ -0,0 +1,15 @@
package kr.devx.whitechat.Data;

public class Message {
public int message_index;
public int message_owner;
public String message_content;
public String message_created;

public Message(int _index, int _owner, String _content, String _created) {
this.message_index = _index;
this.message_owner = _owner;
this.message_content = _content;
this.message_created = _created;
}
}

+ 21
- 0
Android/app/src/main/java/kr/devx/whitechat/Data/Room.java View File

@@ -0,0 +1,21 @@
package kr.devx.whitechat.Data;

import java.util.ArrayList;

public class Room {
public int room_index;
public String room_name;
public int room_notice;
public int room_count;

public ArrayList<User> participants;
public ArrayList<Message> messages;

public Room(int _index, String _name, int _notice) {
this.room_index = _index;
this.room_name = _name;
this.room_notice = _notice;
participants = new ArrayList<>();
messages = new ArrayList<>();
}
}

+ 30
- 0
Android/app/src/main/java/kr/devx/whitechat/Data/User.java View File

@@ -0,0 +1,30 @@
package kr.devx.whitechat.Data;

import android.util.ArrayMap;

import java.util.ArrayList;

public class User {
public int user_index;
public String user_id;
public String user_nickname;
public int user_rank;

public String user_created;
public String user_thumbnail;
public int user_cash;

public String user_key;

public ArrayList<User> friends;
public ArrayList<Room> rooms;

public User(int _index, String _id, String _nickname, int _rank) {
this.user_index = _index;
this.user_id = _id;
this.user_nickname = _nickname;
this.user_rank = _rank;
friends = new ArrayList<>();
rooms = new ArrayList<>();
}
}

+ 908
- 0
Android/app/src/main/java/kr/devx/whitechat/MainActivity.java View File

@@ -0,0 +1,908 @@
package kr.devx.whitechat;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;

import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URISyntaxException;
import java.util.ArrayList;

import devlight.io.library.ntb.NavigationTabBar;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import kr.devx.whitechat.Data.Message;
import kr.devx.whitechat.Data.Room;
import kr.devx.whitechat.Data.User;
import kr.devx.whitechat.Util.SmartFragmentStatePagerAdapter;
import kr.devx.whitechat.Util.chatAdapter;
import kr.devx.whitechat.Util.roomAdapter;

public class MainActivity extends FragmentActivity {

private WhiteApplication whiteApplication;

public Socket mSocket;

private ViewPager mainViewPager;
private NavigationTabBar mainViewNavigation;
private SmartFragmentStatePagerAdapter mainViewAdapter;

private LinearLayout splashParent, mainParent, chatParent, loaderParent;

// SPLASH SCREEN
private LinearLayout splashLoginParent;
private TextView splashLoginRegister;
private EditText splashLoginInputID, splashLoginInputPW;
private Button splashLoginButton;
private LinearLayout splashRegisterParent;
private TextView splashRegisterLogin;
private EditText splashRegisterInputID, splashRegisterInputPW, splashRegisterInputPW2, splashRegisterInputName;
private Button splashRegisterButton;

private ImageView mainHeaderThumbnail;
private TextView mainHeaderName, mainHeaderRank, mainHeaderCash, mainHeaderCreated;

private SocialFragment socialFragment;
private RoomFragment roomFragment;
private SettingFragment settingFragment;

private int RECENT_ROOM_NUMBER;
private TextView chattingHeader, chattingIndex, chattingLeave;
private RecyclerView chattingListView;
private chatAdapter chattingAdapter;
private EditText chattingEditor;
private Button chattingButton;

@Override
public void onCreate(Bundle onSavedInstanceState) {
super.onCreate(onSavedInstanceState);
setContentView(R.layout.main);

whiteApplication = (WhiteApplication) getApplication();

initializeIDs();
initializeListeners();
initializePager();

try {
mSocket = IO.socket("http://devx.kr:7000");
mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onTimeout);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectionError);
mSocket.on(Socket.EVENT_ERROR, onError);
mSocket.on("login", onLogin);
mSocket.on("register", onRegister);
mSocket.connect();
} catch(URISyntaxException e) {
e.printStackTrace();
}

showSplashScreen();
}

private void initializeIDs() {
splashParent = findViewById(R.id.splash_parent);
mainParent = findViewById(R.id.main_parent);
chatParent = findViewById(R.id.chat_parent);
loaderParent = findViewById(R.id.loader_parent);
splashLoginParent = findViewById(R.id.splash_login);
splashLoginRegister = findViewById(R.id.splash_login_register);
splashLoginInputID = findViewById(R.id.splash_login_input_id);
splashLoginInputPW = findViewById(R.id.splash_login_input_pw);
splashLoginButton = findViewById(R.id.splash_login_button);
splashRegisterParent = findViewById(R.id.splash_register);
splashRegisterLogin = findViewById(R.id.splash_register_login);
splashRegisterInputID = findViewById(R.id.splash_register_input_id);
splashRegisterInputPW = findViewById(R.id.splash_register_input_pw);
splashRegisterInputPW2 = findViewById(R.id.splash_register_input_pw_confirm);
splashRegisterInputName = findViewById(R.id.splash_register_input_nickname);
splashRegisterButton = findViewById(R.id.splash_register_button);
mainViewPager = findViewById(R.id.main_content_holder);
mainViewNavigation = findViewById(R.id.main_content_footer);
mainHeaderThumbnail = findViewById(R.id.main_header_thumbnail);
mainHeaderName = findViewById(R.id.main_header_nickname);
mainHeaderRank = findViewById(R.id.main_header_rank);
mainHeaderCash = findViewById(R.id.main_header_cash);
mainHeaderCreated = findViewById(R.id.main_header_created);
chattingHeader = findViewById(R.id.chat_header);
chattingIndex = findViewById(R.id.chat_header_index);
chattingLeave = findViewById(R.id.chat_header_leave);
chattingListView = findViewById(R.id.chat_listView);
chattingEditor = findViewById(R.id.chat_editor);
chattingButton = findViewById(R.id.chat_send);
}

private void initializeListeners() {
splashLoginRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
splashLoginParent.setVisibility(View.GONE);
splashRegisterParent.setVisibility(View.VISIBLE);
}
});
splashRegisterLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
splashRegisterParent.setVisibility(View.GONE);
splashLoginParent.setVisibility(View.VISIBLE);
}
});
splashLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (splashLoginInputID.getText().toString().length() == 0) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_login_failed_input_id), true,null);
return;
}
if (splashLoginInputPW.getText().toString().length() == 0) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_login_failed_input_password), true,null);
return;
}
try {
JSONObject data = new JSONObject();
data.put("requestID", splashLoginInputID.getText().toString());
data.put("requestPW", splashLoginInputPW.getText().toString());
mSocket.emit("login", data);
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.app_error_json), true,null);
}
}
});
splashRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (splashRegisterInputID.getText().toString().length() == 0) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_id), true,null);
return;
}
if (splashRegisterInputPW.getText().toString().length() == 0) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_password), true,null);
return;
}
if (splashRegisterInputPW2.getText().toString().length() == 0) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_password2), true,null);
return;
}
if (splashRegisterInputName.getText().toString().length() == 0) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_nickname), true,null);
return;
}
String inputID = splashRegisterInputID.getText().toString().trim();
if (inputID.length() < 4) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_id_short), true,null);
return;
}
if (inputID.length() > 20) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_id_long), true,null);
return;
}
String inputPW = splashRegisterInputPW.getText().toString().trim();
if (!inputPW.equals(splashRegisterInputPW2.getText().toString().trim())) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_password_match), true,null);
return;
}
if (inputPW.length() < 4) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_password_short), true,null);
return;
}
if (inputPW.length() > 20) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_password_long), true,null);
return;
}
String inputName = splashRegisterInputName.getText().toString().trim();
if (inputName.length() < 4) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_nickname_short), true,null);
return;
}
if (inputName.length() > 20) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_input_nickname_long), true,null);
return;
}
try {
JSONObject data = new JSONObject();
data.put("requestID", inputID);
data.put("requestPW", inputPW);
data.put("requestName", inputName);
mSocket.emit("register", data);
} catch (Exception e) {
e.printStackTrace();
}
}
});
chattingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (chattingEditor.getText().toString().trim().length() <= 0) return;
try {
mSocket.on("send", new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
JSONObject receivedData = (JSONObject) args[0];
int resultCode = receivedData.getInt("result");
if (resultCode != 0) makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.app_unknown_error), true, null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
JSONObject data = new JSONObject();
data.put("room", RECENT_ROOM_NUMBER);
data.put("message", chattingEditor.getText().toString().trim());
data.put("key", whiteApplication.User.user_key);
mSocket.emit("send", data);
chattingEditor.getText().clear();
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.app_error_json), true,null);
}
}
});
chattingLeave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSocket.on("leave", onLeave);
try {
JSONObject data = new JSONObject();
data.put("room", RECENT_ROOM_NUMBER);
data.put("key", whiteApplication.User.user_key);
mSocket.emit("leave", data);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

private void initializePager() {
mainViewAdapter = new WhitePagerAdapter(getSupportFragmentManager());
mainViewPager.setAdapter(mainViewAdapter);
mainViewPager.setOffscreenPageLimit(2);
final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
models.add(
new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.icon_social), ContextCompat.getColor(this, R.color.colorAccent))
.selectedIcon(getResources().getDrawable(R.drawable.icon_social))
.title(getString(R.string.footer_social))
.build()
);
models.add(
new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.icon_room), ContextCompat.getColor(this, R.color.colorAccent))
.selectedIcon(getResources().getDrawable(R.drawable.icon_room))
.title(getString(R.string.footer_room))
.build()
);
models.add(
new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.icon_setting), ContextCompat.getColor(this, R.color.colorAccent))
.selectedIcon(getResources().getDrawable(R.drawable.icon_setting))
.title(getString(R.string.footer_setting))
.build()
);
mainViewNavigation.setModels(models);
mainViewNavigation.setBgColor(ContextCompat.getColor(this, android.R.color.white));
mainViewNavigation.setViewPager(mainViewPager, 0);
mainViewNavigation.setBehaviorEnabled(true);
}

private void showSplashScreen() {
mainParent.setVisibility(View.GONE);
chatParent.setVisibility(View.GONE);
splashParent.setVisibility(View.VISIBLE);
}

private void showMainScreen() {
splashParent.setVisibility(View.GONE);
chatParent.setVisibility(View.GONE);
mainParent.setVisibility(View.VISIBLE);
}

private void showChatScreen() {
splashParent.setVisibility(View.GONE);
mainParent.setVisibility(View.GONE);
chatParent.setVisibility(View.VISIBLE);
}

private void closeChatScreen() {
showMainScreen();
chattingHeader.setText(null);
chattingListView.setAdapter(null);
try {
JSONObject data = new JSONObject();
data.put("room", RECENT_ROOM_NUMBER);
data.put("key", whiteApplication.User.user_key);
mSocket.emit("exit", data);
} catch (Exception e) {
e.printStackTrace();
}
mSocket.off("receive");
}

private void showLoaderScreen(String message) {
loaderParent.setVisibility(View.VISIBLE);
}

private void hideLoaderScreen() {
loaderParent.setVisibility(View.GONE);
}

public void onSocialFragmentReady(SocialFragment instance) {
this.socialFragment = instance;
socialFragment.onLifeStart();
}

public void onRoomFragmentReady(RoomFragment instance) {
this.roomFragment = instance;
roomFragment.onLifeStart();
}

public void onSettingFragmentReady(SettingFragment instance) {
this.settingFragment = instance;
settingFragment.onLifeStart();
}

public void onLogout() {
showSplashScreen();
mSocket.disconnect();
mSocket.connect();
}

public void onRoomClicked(int clickedRoomIndex) {
showChatScreen();
RECENT_ROOM_NUMBER = clickedRoomIndex;
for (Room room : whiteApplication.User.rooms) {
if (room.room_index == clickedRoomIndex) {
startChatLife(room);
break;
}
}
}

private void startSplashLife() {

}

private void startMainLife() {
Glide.with(this).load(whiteApplication.User.user_thumbnail).apply(new RequestOptions().circleCrop()).into(mainHeaderThumbnail);
mainHeaderName.setText(whiteApplication.User.user_nickname);
mainHeaderRank.setText(String.valueOf(whiteApplication.User.user_rank));
mainHeaderCash.setText(String.valueOf(whiteApplication.User.user_cash));
mainHeaderCreated.setText(getString(R.string.main_header_since).concat(" ").concat(whiteApplication.User.user_created));
if (socialFragment != null) socialFragment.onLifeStart();
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w("WCHAT", "FCM INSTANCE FAILED", task.getException());
return;
}
String token = task.getResult().getToken();
mSocket.emit("clientKey", token);
}
});
}

private void startChatLife(Room targetRoom) {
mSocket.on("retrieve", onRetrieve);
try {
JSONObject data = new JSONObject();
data.put("room", targetRoom.room_index);
data.put("index", -1);
data.put("key", whiteApplication.User.user_key);
mSocket.emit("retrieve", data);
} catch (Exception e) {
e.printStackTrace();
}
}

private void showChatData(final Room targetRoom) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (targetRoom == null) {
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.app_unknown_error), true,null);
return;
}
chattingHeader.setText(targetRoom.room_name);
chattingIndex.setText(getString(R.string.chatting_index) + targetRoom.room_index);
chattingLeave.setText(getString(R.string.chatting_leave));
chattingAdapter = new chatAdapter(MainActivity.this, targetRoom, targetRoom.messages);
chattingListView.setAdapter(chattingAdapter);
chattingListView.post(new Runnable() {
@Override
public void run() {
chattingAdapter.notifyDataSetChanged();
chattingListView.scrollToPosition(chattingAdapter.getItemCount() - 1);
}
});
}
});
mSocket.on("receive", onReceive);
try {
JSONObject data = new JSONObject();
data.put("room", RECENT_ROOM_NUMBER);
data.put("key", whiteApplication.User.user_key);
mSocket.emit("enter", data);
} catch (Exception e) {
e.printStackTrace();
}
}

public void makeAlertDialog(final int ALERT_TYPE, final String message, final boolean isCancelable, final SweetAlertDialog.OnSweetClickListener buttonListener) {
runOnUiThread(new Runnable() {
@Override
public void run() {
SweetAlertDialog sweetAlertDialog = new SweetAlertDialog(MainActivity.this, ALERT_TYPE);
sweetAlertDialog.setTitleText(message);
sweetAlertDialog.setConfirmClickListener(buttonListener);
sweetAlertDialog.setCancelable(isCancelable);
sweetAlertDialog.show();
}
});
}

public void refreshDatas() {
mSocket.on("refresh", onRefresh);
try {
JSONObject data = new JSONObject();
data.put("key", whiteApplication.User.user_key);
mSocket.emit("refresh", data);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onBackPressed() {
if (chatParent.getVisibility() == View.VISIBLE) {
closeChatScreen();
return;
}
super.onBackPressed();
}

private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onConnect");
}
};

private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onDisconnect");
}
};

private Emitter.Listener onConnectionError = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onConnectionError > " + args[0]);
}
};

private Emitter.Listener onTimeout = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onTimeout");
}
};

private Emitter.Listener onError = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onError");
}
};

private Emitter.Listener onLogin = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onLogin");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String error = receivedData.getString("error");
switch (result) {
case 1: // USER NOT EXIST
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_login_failed_account), true,null);
return;
case 2: // USER IS BANNED
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_login_failed_block), true,null);
return;
case 3: // PASSWORD INCORRECT
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_login_failed_password_incorrect), true,null);
return;
case 4: // UNKNOWN ERROR
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_login_unknown) + " " + error, true,null);
return;
}
final int num = receivedData.getInt("num");
final String id = receivedData.getString("id");
final String nickname = receivedData.getString("nickname");
final int rank = receivedData.getInt("rank");
final String thumbnail = receivedData.getString("thumbnail");
String created = receivedData.getString("created");
final int cash = receivedData.getInt("cash");
final String key = receivedData.getString("key");
JSONArray roomArray = receivedData.getJSONArray("rooms");
final ArrayList<Room> rooms = new ArrayList<>();
for (int i = 0; i < roomArray.length(); i ++) {
JSONObject roomObject = roomArray.getJSONObject(i);
int room_index = roomObject.getInt("room_index");
String room_name = roomObject.getString("room_name");
int room_notice = roomObject.getInt("room_notice");
Room room = new Room(room_index, room_name, room_notice);
rooms.add(room);
}
JSONArray socialArray = receivedData.getJSONArray("socials");
final ArrayList<User> socials = new ArrayList<>();
for (int i = 0; i < socialArray.length(); i ++) {
JSONObject socialObject = socialArray.getJSONObject(i);
final int social_num = socialObject.getInt("user_index");
final String social_id = socialObject.getString("user_id");
final String social_nickname = socialObject.getString("user_nickname");
final int social_rank = socialObject.getInt("user_rank");
final String social_thumbnail = socialObject.getString("user_thumbnail");
User friend = new User(social_num, social_id, social_nickname, social_rank);
friend.user_thumbnail = social_thumbnail;
socials.add(friend);
}
if (created.contains("T")) created = created.substring(0, created.indexOf("T"));
final String fixCreated = created;
makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, nickname, false, new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
User user = new User(num, id, nickname, rank);
user.user_cash = cash;
user.user_thumbnail = thumbnail;
user.user_key = key;
user.user_created = fixCreated;
user.friends = socials;
user.rooms = rooms;
whiteApplication.User = user;
showMainScreen();
startMainLife();
sweetAlertDialog.cancel();
}
});
Log.d("WCHAT", "SOCKET > onLogin > " + nickname + " > " + rooms.toString());
Log.d("WCHAT", "SOCKET > onLogin > " + nickname + " > " + socials.toString());
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.ERROR_TYPE, getString(R.string.app_error_json) + e.getMessage(), true,null);
}

}
};

private Emitter.Listener onRegister = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onRegister");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String error = receivedData.getString("error");
switch (result) {
case 1: // ID ALREADY EXISTS
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_failed_account), true,null);
return;
case 2: // UNKNOWN ERROR
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, getString(R.string.splash_message_register_unknown) + " " + error, true,null);
return;
}
makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, getString(R.string.splash_message_register_success), true,null);
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.ERROR_TYPE, getString(R.string.app_error_json) + e.getMessage(), true,null);
}
}
};

private Emitter.Listener onRetrieve = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onRetrieve");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String error = receivedData.getString("error");
switch (result) {
case 2: // UNKNOWN
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, error, true,null);
return;
}
final int room = receivedData.getInt("room");
JSONArray messageArray = receivedData.getJSONArray("messages");
final ArrayList<Message> messages = new ArrayList<>();
for (int i = 0; i < messageArray.length(); i ++) {
JSONObject messageObject = messageArray.getJSONObject(i);
int message_index = messageObject.getInt("message_index");
//int message_room = messageObject.getInt("message_room");
int message_owner = messageObject.getInt("message_owner");
String message_created = messageObject.getString("message_created");
String message_content = messageObject.getString("message_content");
message_created = message_created.substring(0, message_created.lastIndexOf(".")).replace("T"," ").replace("Z","");
Message message = new Message(message_index, message_owner, message_content, message_created);
messages.add(message);
}
JSONArray participantArray = receivedData.getJSONArray("participants");
final ArrayList<User> participants = new ArrayList<>();
for (int i = 0; i < participantArray.length(); i ++) {
JSONObject participantObject = participantArray.getJSONObject(i);
final int participant_num = participantObject.getInt("user_index");
final String participant_id = participantObject.getString("user_id");
final String participant_nickname = participantObject.getString("user_nickname");
final int participant_rank = participantObject.getInt("user_rank");
final String participant_thumbnail = participantObject.getString("user_thumbnail");
User participant = new User(participant_num, participant_id, participant_nickname, participant_rank);
participant.user_thumbnail = participant_thumbnail;
participants.add(participant);
}
Log.d("WCHAT", "SOCKET > onRetrieve > #" + room + " > " + messages.toString());
Log.d("WCHAT", "SOCKET > onRetrieve > #" + room + " > " + participants.toString());
Room targetRoom = null;
for (Room currentRoom : whiteApplication.User.rooms) {
if (currentRoom.room_index == room) {
targetRoom = currentRoom;
break;
}
}
if (targetRoom != null) {
targetRoom.messages.clear();
targetRoom.messages.addAll(messages);
targetRoom.participants.clear();
targetRoom.participants.addAll(participants);
Log.d("WCHAT", "SOCKET > onRetrieve > #" + room + " > Data Updated");
showChatData(targetRoom);
} else {
showChatData(null);
}
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.ERROR_TYPE, getString(R.string.app_error_json) + e.getMessage(), true,null);
showChatData(null);
}
}
};

private Emitter.Listener onReceive = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onReceive");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String error = receivedData.getString("error");
final int room = receivedData.getInt("room");
JSONObject messageObject = receivedData.getJSONObject("message");
int message_index = messageObject.getInt("message_index");
//int message_room = messageObject.getInt("message_room");
int message_owner = messageObject.getInt("message_owner");
String message_created = messageObject.getString("message_created");
String message_content = messageObject.getString("message_content");
message_created = message_created.substring(message_created.lastIndexOf("()") + 5, message_created.lastIndexOf(".")).replace("T"," ").replace("Z","");
Message message = new Message(message_index, message_owner, message_content, message_created);

Log.d("WCHAT", "SOCKET > onReceive > #" + room + " > " + message.toString());
Room targetRoom = null;
for (Room currentRoom : whiteApplication.User.rooms) {
if (currentRoom.room_index == room) {
targetRoom = currentRoom;
break;
}
}
if (targetRoom != null) {
for (Message msg : targetRoom.messages) {
if (msg.message_index == message_index) return;
}
targetRoom.messages.add(message);
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
if (chattingAdapter != null && RECENT_ROOM_NUMBER == room && chattingListView != null) {
chattingAdapter.notifyItemInserted(chattingAdapter.getItemCount()-1);
chattingListView.smoothScrollToPosition(chattingAdapter.getItemCount() - 1);
chattingListView.post(new Runnable() {
@Override
public void run() {
try {
forceRippleAnimation(chattingListView.getLayoutManager().findViewByPosition(chattingAdapter.getItemCount() - 1));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
if (roomFragment != null) roomFragment.notifyAdapterDataChanged(whiteApplication.User.rooms.indexOf(targetRoom));
Log.d("WCHAT", "SOCKET > onReceive > #" + room + " > Data Added");
}
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.ERROR_TYPE, getString(R.string.app_error_json) + e.getMessage(), true,null);
}
}
};

private Emitter.Listener onRefresh = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onRefresh");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String error = receivedData.getString("error");
String message = receivedData.getString("message");
switch (result) {
case 1:
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, message, true,null);
return;
case 2:
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, error, true,null);
return;
}
JSONArray roomArray = receivedData.getJSONArray("rooms");
final ArrayList<Room> rooms = new ArrayList<>();
for (int i = 0; i < roomArray.length(); i ++) {
JSONObject roomObject = roomArray.getJSONObject(i);
int room_index = roomObject.getInt("room_index");
String room_name = roomObject.getString("room_name");
int room_notice = roomObject.getInt("room_notice");
Room room = new Room(room_index, room_name, room_notice);
rooms.add(room);
}
JSONArray socialArray = receivedData.getJSONArray("socials");
final ArrayList<User> socials = new ArrayList<>();
for (int i = 0; i < socialArray.length(); i ++) {
JSONObject socialObject = socialArray.getJSONObject(i);
final int social_num = socialObject.getInt("user_index");
final String social_id = socialObject.getString("user_id");
final String social_nickname = socialObject.getString("user_nickname");
final int social_rank = socialObject.getInt("user_rank");
final String social_thumbnail = socialObject.getString("user_thumbnail");
User friend = new User(social_num, social_id, social_nickname, social_rank);
friend.user_thumbnail = social_thumbnail;
socials.add(friend);
}
whiteApplication.User.friends.clear();
whiteApplication.User.friends.addAll(socials);
whiteApplication.User.rooms.clear();
whiteApplication.User.rooms.addAll(rooms);
if (socialFragment != null) socialFragment.notifyAdapterDataChanged(-1);
if (roomFragment != null) roomFragment.notifyAdapterDataChanged(-1);
Log.d("WCHAT", "SOCKET > onRefresh > " + whiteApplication.User.user_nickname + " > " + rooms.toString());
Log.d("WCHAT", "SOCKET > onRefresh > " + whiteApplication.User.user_nickname + " > " + socials.toString());
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.ERROR_TYPE, getString(R.string.app_error_json) + e.getMessage(), true,null);
}
}
};

private Emitter.Listener onLeave = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onLeave");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String message = receivedData.getString("message");
String error = receivedData.getString("error");
switch (result) {
case 1:
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, message, true,null);
return;
case 2:
makeAlertDialog(SweetAlertDialog.WARNING_TYPE, error, true,null);
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
showMainScreen();
}
});
makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, getString(R.string.chatting_leave_success) + RECENT_ROOM_NUMBER, true,null);
refreshDatas();
} catch (Exception e) {
e.printStackTrace();
makeAlertDialog(SweetAlertDialog.ERROR_TYPE, getString(R.string.app_error_json) + e.getMessage(), true,null);
}
}
};

public static class WhitePagerAdapter extends SmartFragmentStatePagerAdapter {
private static int NUM_ITEMS = 3;

public WhitePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

@Override
public int getCount() {
return NUM_ITEMS;
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SocialFragment.newInstance(0);
case 1:
return RoomFragment.newInstance(1);
case 2:
return SettingFragment.newInstance(2);
default:
return null;
}
}

@Override
public CharSequence getPageTitle(int position) {
return String.valueOf(position);
}

}


protected void forceRippleAnimation(View view)
{
if (view == null) return;

Drawable background = view.getBackground();

if(Build.VERSION.SDK_INT >= 21 && background instanceof RippleDrawable)
{
final RippleDrawable rippleDrawable = (RippleDrawable) background;

rippleDrawable.setState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled});

Handler handler = new Handler();

handler.postDelayed(new Runnable()
{
@Override public void run()
{
rippleDrawable.setState(new int[]{});
}
}, 200);
}
}

}

+ 304
- 0
Android/app/src/main/java/kr/devx/whitechat/RoomFragment.java View File

@@ -0,0 +1,304 @@
package kr.devx.whitechat;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import io.socket.emitter.Emitter;
import kr.devx.whitechat.Data.Message;
import kr.devx.whitechat.Data.Room;
import kr.devx.whitechat.Util.roomAdapter;

public class RoomFragment extends Fragment {

private WhiteApplication whiteApplication;
private int currentPage;
private SharedPreferences appPreferences;

private RecyclerView mainRoomListView;
private roomAdapter mainRoomAdapter;

private LinearLayout roomCreateView, roomJoinView;

public static RoomFragment newInstance(int currentPage) {
RoomFragment fragment = new RoomFragment();
Bundle args = new Bundle();
args.putInt("currentPage", currentPage);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentPage = getArguments().getInt("currentPage", 0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
whiteApplication = (WhiteApplication) getContext().getApplicationContext();
View rootView = inflater.inflate(R.layout.fragment_room, container, false);
initializeId(rootView);

((MainActivity)getActivity()).onRoomFragmentReady(this);
((MainActivity)getActivity()).mSocket.on("create", onCreate);
((MainActivity)getActivity()).mSocket.on("join", onJoin);

return rootView;
}

private void initializeId(View rootView) {
mainRoomListView = rootView.findViewById(R.id.room_listView);
roomCreateView = rootView.findViewById(R.id.room_create);
roomJoinView = rootView.findViewById(R.id.room_join);
}

public void onLifeStart() {
mainRoomAdapter = new roomAdapter(getActivity(), whiteApplication.User.rooms);
mainRoomListView.setAdapter(mainRoomAdapter);
mainRoomAdapter.setOnRoomClickListener(new roomAdapter.OnRoomClickListener() {
@Override
public void onRoomClick(int room_index) {
((MainActivity)getActivity()).onRoomClicked(room_index);
}
});
if (whiteApplication.User.rooms != null && whiteApplication.User.rooms.size() > 0) {
((MainActivity)getActivity()).mSocket.on("retrieve_basics", onRetrieveBasics);
for (Room room: whiteApplication.User.rooms) {
if (((MainActivity)getActivity()).mSocket == null || !((MainActivity)getActivity()).mSocket.connected()) {
return;
}
try {
JSONObject data = new JSONObject();
data.put("room", room.room_index);
data.put("index", -1);
((MainActivity) getActivity()).mSocket.emit("retrieve_basics", data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
roomCreateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCreateRoomDialog();
}
});
roomJoinView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showJoinRoomDialog();
}
});
}

private void showCreateRoomDialog() {
final EditText titleEditor = new EditText(getContext());
titleEditor.setHint(getString(R.string.room_dialog_hint_title));
final EditText passwordEditor = new EditText(getContext());
passwordEditor.setHint(getString(R.string.room_dialog_hint_password));
passwordEditor.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
SweetAlertDialog roomCreateDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.NORMAL_TYPE)
.setTitleText(getString(R.string.room_create))
.setContentText(getString(R.string.room_create_content))
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
if (titleEditor.getText().toString().trim().length() < 1) {
Toast.makeText(getContext(), getString(R.string.room_create_content), Toast.LENGTH_SHORT).show();
return;
}
try {
JSONObject data = new JSONObject();
data.put("title", titleEditor.getText().toString().trim());
data.put("password", passwordEditor.getText().toString().trim());
data.put("key",whiteApplication.User.user_key);
((MainActivity)getActivity()).mSocket.emit("create", data);
} catch (Exception e) {
e.printStackTrace();
}
sweetAlertDialog.dismiss();
}
});
roomCreateDialog.show();
LinearLayout linearLayout = roomCreateDialog.findViewById(R.id.loading);
int index = linearLayout.indexOfChild(linearLayout.findViewById(R.id.content_text));
linearLayout.addView(titleEditor, index + 1);
linearLayout.addView(passwordEditor, index + 2);
}

private void showJoinRoomDialog() {
final EditText indexEditor = new EditText(getContext());
indexEditor.setHint(getString(R.string.room_dialog_hint_index));
indexEditor.setInputType(InputType.TYPE_CLASS_NUMBER);
final EditText passwordEditor = new EditText(getContext());
passwordEditor.setHint(getString(R.string.room_dialog_hint_password));
passwordEditor.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
SweetAlertDialog roomJoinDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.NORMAL_TYPE)
.setTitleText(getString(R.string.room_join))
.setContentText(getString(R.string.room_join_content))
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
if (indexEditor.getText().toString().trim().length() < 1) {
Toast.makeText(getContext(), getString(R.string.room_join_content), Toast.LENGTH_SHORT).show();
return;
}
try {
JSONObject data = new JSONObject();
data.put("room", indexEditor.getText().toString().trim());
data.put("password", passwordEditor.getText().toString().trim());
data.put("key",whiteApplication.User.user_key);
((MainActivity)getActivity()).mSocket.emit("join", data);
} catch (Exception e) {
e.printStackTrace();
}
sweetAlertDialog.dismiss();
}
});
roomJoinDialog.show();
LinearLayout linearLayout = roomJoinDialog.findViewById(R.id.loading);
int index = linearLayout.indexOfChild(linearLayout.findViewById(R.id.content_text));
linearLayout.addView(indexEditor, index + 1);
linearLayout.addView(passwordEditor, index + 2);
}

private Emitter.Listener onRetrieveBasics = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onRetrieveBasics");
try {
JSONObject receivedData = (JSONObject) args[0];
if (receivedData.length() == 0) return;
if (receivedData.getInt("result") != 0) return;
int targetRoomIndex = receivedData.getInt("room");
String participantCountStr = receivedData.getString("participant_count");
int participantCount = -1;
try {
participantCount = Integer.valueOf(participantCountStr.substring(participantCountStr.lastIndexOf(")") + 3, participantCountStr.lastIndexOf("}")));
} catch (Exception e) {
e.printStackTrace();
}
Log.d("WCHAT", "SOCKET > onRetrieveBasics : #" + targetRoomIndex + " has " + participantCount + " participants");
Room targetRoom = null;
for (Room room: whiteApplication.User.rooms) {
if (room.room_index == targetRoomIndex) {
targetRoom = room;
break;
}
}
if (targetRoom == null) return;
try {
JSONArray messageArray = receivedData.getJSONArray("last_message");
JSONObject messageObject = messageArray.getJSONObject(0);
int message_index = messageObject.getInt("message_index");
int message_owner = messageObject.getInt("message_owner");
String message_created = messageObject.getString("message_created");
message_created = message_created.substring(0, message_created.lastIndexOf(".")).replace("T"," ").replace("Z","");
String message_content = messageObject.getString("message_content");
Message newMessage = new Message(message_index, message_owner, message_content, message_created);
targetRoom.messages.add(newMessage);
Log.d("WCHAT", "SOCKET > onRetrieveBasics : #" + targetRoomIndex + " last message is " + message_content);
} catch (Exception e) {
e.printStackTrace();
}
targetRoom.room_count = participantCount;
final int roomIndex = whiteApplication.User.rooms.indexOf(targetRoom);
mainRoomListView.post(new Runnable() {
@Override
public void run() {
mainRoomAdapter.notifyItemChanged(roomIndex);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};

private Emitter.Listener onCreate = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onCreate");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String message = receivedData.getString("message");
String error = receivedData.getString("error");
switch (result) {
case 1:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, message, true, null);
return;
case 2:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, error, true, null);
return;
}
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, getString(R.string.room_create_success), true, null);
((MainActivity)getActivity()).refreshDatas();
} catch (Exception e) {
e.printStackTrace();
}
}
};

private Emitter.Listener onJoin = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onJoin");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String message = receivedData.getString("message");
String error = receivedData.getString("error");
switch (result) {
case 1:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, message, true, null);
return;
case 2:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, error, true, null);
return;
}
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, getString(R.string.room_join_success), true, null);
((MainActivity)getActivity()).refreshDatas();
} catch (Exception e) {
e.printStackTrace();
}
}
};

public void notifyAdapterDataChanged(final int roomIndex) {
if (roomIndex == -1) {
mainRoomListView.post(new Runnable() {
@Override
public void run() {
mainRoomAdapter.notifyDataSetChanged();
}
});
return;
}
mainRoomListView.post(new Runnable() {
@Override
public void run() {
mainRoomAdapter.notifyItemChanged(roomIndex);
}
});
}

}

+ 198
- 0
Android/app/src/main/java/kr/devx/whitechat/SettingFragment.java View File

@@ -0,0 +1,198 @@
package kr.devx.whitechat;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;

import org.json.JSONObject;

import de.psdev.licensesdialog.LicensesDialog;
import de.psdev.licensesdialog.licenses.ApacheSoftwareLicense20;
import de.psdev.licensesdialog.licenses.GnuLesserGeneralPublicLicense21;
import de.psdev.licensesdialog.licenses.MITLicense;
import de.psdev.licensesdialog.model.Notice;
import de.psdev.licensesdialog.model.Notices;
import io.socket.emitter.Emitter;

public class SettingFragment extends Fragment {

private WhiteApplication whiteApplication;
private int currentPage;
private SharedPreferences appPreferences;

private Switch notificationForegroundSwitch, notificationBackgroundSwitch;
private LinearLayout logoutView, withdrawView;
private TextView opensourceView;

public static SettingFragment newInstance(int currentPage) {
SettingFragment fragment = new SettingFragment();
Bundle args = new Bundle();
args.putInt("currentPage", currentPage);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentPage = getArguments().getInt("currentPage", 0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
whiteApplication = (WhiteApplication) getContext().getApplicationContext();
View rootView = inflater.inflate(R.layout.fragment_setting, container, false);
initializeId(rootView);

((MainActivity)getActivity()).onSettingFragmentReady(this);

return rootView;
}

private void initializeId(View rootView) {
notificationForegroundSwitch = rootView.findViewById(R.id.setting_notification_foreground_switch);
notificationBackgroundSwitch = rootView.findViewById(R.id.setting_notification_background_switch);
logoutView = rootView.findViewById(R.id.setting_logout);
withdrawView = rootView.findViewById(R.id.setting_withdraw);
opensourceView = rootView.findViewById(R.id.setting_opensource);
}

public void onLifeStart() {
appPreferences = getContext().getSharedPreferences("DATA", Context.MODE_PRIVATE);
boolean isNotificationForegroundActive = appPreferences.getBoolean("NOTIFICATION_FOREGROUND", true);
boolean isNotificationBackgroundActive = appPreferences.getBoolean("NOTIFICATION_BACKGROUND", true);
notificationForegroundSwitch.setChecked(isNotificationForegroundActive);
notificationBackgroundSwitch.setChecked(isNotificationBackgroundActive);
notificationForegroundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
whiteApplication.IS_NOTIFICATION_FOREGROUND_ACTIVE = isChecked;
appPreferences.edit().putBoolean("NOTIFICATION_FOREGROUND", isChecked).apply();
}
});
notificationBackgroundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
whiteApplication.IS_NOTIFICATION_BACKGROUND_ACTIVE = isChecked;
appPreferences.edit().putBoolean("NOTIFICATION_BACKGROUND", isChecked).apply();
}
});
logoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).onLogout();
}
});
withdrawView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).mSocket.on("withdraw", onWithdraw);
showWithdrawDialog();
}
});
opensourceView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showOpensourceDialog();
}
});
}

private void showWithdrawDialog() {
final EditText idEditor = new EditText(getContext());
idEditor.setHint(getString(R.string.setting_withdraw_dialog_hint_id));
final EditText passwordEditor = new EditText(getContext());
passwordEditor.setHint(getString(R.string.setting_withdraw_dialog_hint_pw));
passwordEditor.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
SweetAlertDialog withdrawDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.NORMAL_TYPE)
.setTitleText(getString(R.string.setting_withdraw_dialog_hint_id))
.setContentText(getString(R.string.setting_withdraw_dialog_content))
.setConfirmText(getString(R.string.setting_withdraw_dialog))
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
if (idEditor.getText().toString().trim().length() < 1 || passwordEditor.getText().toString().trim().length() < 1) {
Toast.makeText(getContext(), getString(R.string.setting_withdraw_dialog_content), Toast.LENGTH_SHORT).show();
return;
}
if (!whiteApplication.User.user_id.equals(idEditor.getText().toString().trim())) {
Toast.makeText(getContext(), getString(R.string.setting_withdraw_dialog_failed_id), Toast.LENGTH_SHORT).show();
return;
}
try {
JSONObject data = new JSONObject();
data.put("confirmPW", passwordEditor.getText().toString().trim());
data.put("key",whiteApplication.User.user_key);
((MainActivity)getActivity()).mSocket.emit("join", data);
} catch (Exception e) {
e.printStackTrace();
}
sweetAlertDialog.dismiss();
}
});
withdrawDialog.show();
LinearLayout linearLayout = withdrawDialog.findViewById(R.id.loading);
int index = linearLayout.indexOfChild(linearLayout.findViewById(R.id.content_text));
linearLayout.addView(idEditor, index + 1);
linearLayout.addView(passwordEditor, index + 2);
}

private void showOpensourceDialog() {
final Notices notices = new Notices();
notices.addNotice(new Notice("SweetAlert Dialog", "https://jitpack.io/p/thomper/sweet-alert-dialog", "Pedant", new MITLicense()));
notices.addNotice(new Notice("NavigationTabBar", "https://github.com/Devlight/NavigationTabBar", "Basil Miller", new ApacheSoftwareLicense20()));
notices.addNotice(new Notice("socket.io-client-java", "https://github.com/socketio/socket.io-client-java", "Naoyuki Kanezawa", new GnuLesserGeneralPublicLicense21()));
notices.addNotice(new Notice("Glide", "https://github.com/bumptech/glide", "Google", new ApacheSoftwareLicense20()));
notices.addNotice(new Notice("Firebase", "http://example.org", "Example Person 2", new ApacheSoftwareLicense20()));
new LicensesDialog.Builder(getActivity())
.setNotices(notices)
.setIncludeOwnLicense(true)
.build()
.show();
}

private Emitter.Listener onWithdraw = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onWithdraw");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String message = receivedData.getString("message");
String error = receivedData.getString("error");
switch (result) {
case 1:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, message, true, null);
return;
case 2:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, error, true, null);
return;
}
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, getString(R.string.setting_withdraw_success), false, new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
((MainActivity)getActivity()).onLogout();
}
});
((MainActivity)getActivity()).refreshDatas();
} catch (Exception e) {
e.printStackTrace();
}
}
};

}

+ 165
- 0
Android/app/src/main/java/kr/devx/whitechat/SocialFragment.java View File

@@ -0,0 +1,165 @@
package kr.devx.whitechat;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;

import org.json.JSONArray;
import org.json.JSONObject;

import io.socket.emitter.Emitter;
import kr.devx.whitechat.Util.socialAdapter;

public class SocialFragment extends Fragment {

private WhiteApplication whiteApplication;
private int currentPage;
private SharedPreferences appPreferences;

private RecyclerView mainSocialListView;
private socialAdapter mainSocialAdapter;

private LinearLayout addFriendView;

public static SocialFragment newInstance(int currentPage) {
SocialFragment fragment = new SocialFragment();
Bundle args = new Bundle();
args.putInt("currentPage", currentPage);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentPage = getArguments().getInt("currentPage", 0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
whiteApplication = (WhiteApplication) getContext().getApplicationContext();
View rootView = inflater.inflate(R.layout.fragment_social, container, false);
initializeId(rootView);

((MainActivity)getActivity()).onSocialFragmentReady(this);
((MainActivity)getActivity()).mSocket.on("relation", onRelation);

return rootView;
}

private void initializeId(View rootView) {
mainSocialListView = rootView.findViewById(R.id.social_listView);
addFriendView = rootView.findViewById(R.id.social_add);
addFriendView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddFriendDialog();
}
});
}

public void onLifeStart() {
mainSocialAdapter = new socialAdapter(getActivity(), whiteApplication.User.friends);
mainSocialListView.setAdapter(mainSocialAdapter);
mainSocialAdapter.setOnSocialClickListener(new socialAdapter.OnSocialClickListener() {
@Override
public void onSocialClick(int position) {

}
});
}

private void showAddFriendDialog() {
final EditText idEditor = new EditText(getContext());
SweetAlertDialog idInputDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.NORMAL_TYPE)
.setTitleText(getString(R.string.social_add_friend))
.setContentText(getString(R.string.social_add_friend_content))
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
if (idEditor.getText().toString().trim().length() < 1) {
Toast.makeText(getContext(), getString(R.string.social_add_friend_content), Toast.LENGTH_SHORT).show();
return;
}
try {
JSONObject data = new JSONObject();
data.put("targetID", idEditor.getText().toString().trim());
data.put("relation", 1);
data.put("key",whiteApplication.User.user_key);
((MainActivity)getActivity()).mSocket.emit("relation", data);
} catch (Exception e) {
e.printStackTrace();
}
sweetAlertDialog.dismiss();
}
});
idInputDialog.show();
LinearLayout linearLayout = idInputDialog.findViewById(R.id.loading);
int index = linearLayout.indexOfChild(linearLayout.findViewById(R.id.content_text));
linearLayout.addView(idEditor, index + 1);
}

private Emitter.Listener onRelation = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d("WCHAT", "SOCKET > onRelation");
try {
JSONObject receivedData = (JSONObject) args[0];
int result = receivedData.getInt("result");
String message = receivedData.getString("message");
String error = receivedData.getString("error");
switch (result) {
case 1:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, message, true, null);
return;
case 2:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.WARNING_TYPE, error, true, null);
return;
}
int relation = receivedData.getInt("relation");
String targetID = receivedData.getString("targetID");
switch (relation) {
case 0:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, targetID + " " + getString(R.string.social_relation_result_0), true, null);
break;
case 1:
((MainActivity)getActivity()).makeAlertDialog(SweetAlertDialog.SUCCESS_TYPE, targetID + " " + getString(R.string.social_relation_result_1), true, null);
break;
}
((MainActivity)getActivity()).refreshDatas();
} catch (Exception e) {
e.printStackTrace();
}
}
};

public void notifyAdapterDataChanged(final int socialIndex) {
if (socialIndex == -1) {
mainSocialListView.post(new Runnable() {
@Override
public void run() {
mainSocialAdapter.notifyDataSetChanged();
}
});
return;
}
mainSocialListView.post(new Runnable() {
@Override
public void run() {
mainSocialAdapter.notifyItemChanged(socialIndex);
}
});
}

}

+ 36
- 0
Android/app/src/main/java/kr/devx/whitechat/Util/SmartFragmentStatePagerAdapter.java View File

@@ -0,0 +1,36 @@
package kr.devx.whitechat.Util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

// Register the fragment when the item is instantiated
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}

// Unregister when the item is inactive
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}

// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}

+ 61
- 0
Android/app/src/main/java/kr/devx/whitechat/Util/WhiteFirebaseMessagingService.java View File

@@ -0,0 +1,61 @@
package kr.devx.whitechat.Util;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import kr.devx.whitechat.MainActivity;
import kr.devx.whitechat.R;
import kr.devx.whitechat.WhiteApplication;

public class WhiteFirebaseMessagingService extends FirebaseMessagingService {

WhiteApplication whiteApplication;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("WCHAT", "FCM > RECEIVED");
whiteApplication = (WhiteApplication) getApplication();
if (remoteMessage != null && remoteMessage.getNotification() != null && whiteApplication.IS_NOTIFICATION_FOREGROUND_ACTIVE) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}

private void sendNotification(String messageTitle, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

String channelId = "WHITECHAT";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.icon_room)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = "WHITECHAT";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}

notificationManager.notify(6753, notificationBuilder.build());
}

}

+ 125
- 0
Android/app/src/main/java/kr/devx/whitechat/Util/chatAdapter.java View File

@@ -0,0 +1,125 @@
package kr.devx.whitechat.Util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import kr.devx.whitechat.Data.Message;
import kr.devx.whitechat.Data.Room;
import kr.devx.whitechat.Data.User;
import kr.devx.whitechat.R;
import kr.devx.whitechat.WhiteApplication;

public class chatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private WhiteApplication whiteApplication;
private Context context;
private Room room;