From 5a4a5144e4501b89f2a7bd03290045f9a707b0e7 Mon Sep 17 00:00:00 2001 From: Vamsee Lakamsani Date: Mon, 20 Feb 2017 01:47:08 -0800 Subject: [PATCH 1/5] 1. Made it work with https://socketio-chat.now.sh/ from https://github.com/socketio/socket.io/issues/2823 2. Android Studio 2.3.0-beta4, Api25, gradle 3.3 changes. 3. works with Pixel XL emulator, Api23 --- app/build.gradle | 31 ++++++++----------- .../socketio/androidchat/ApplicationTest.java | 13 -------- .../androidchat/ExampleInstrumentedTest.java | 27 ++++++++++++++++ .../socketio/androidchat/Constants.java | 2 +- .../socketio/androidchat/MainActivity.java | 4 +-- .../socketio/androidchat/MainFragment.java | 16 ++++++++-- build.gradle | 2 +- gradle.properties | 1 + gradle/wrapper/gradle-wrapper.properties | 4 +-- 9 files changed, 61 insertions(+), 39 deletions(-) delete mode 100644 app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ApplicationTest.java create mode 100644 app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ExampleInstrumentedTest.java diff --git a/app/build.gradle b/app/build.gradle index b920e3e..c6c1e10 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,24 +1,14 @@ -buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'com.android.tools.build:gradle:1.2.3' - } -} apply plugin: 'com.android.application' -repositories { - jcenter() -} + android { - compileSdkVersion 21 - buildToolsVersion "21.1.2" + compileSdkVersion 25 + buildToolsVersion "25.0.2" defaultConfig { applicationId 'com.github.nkzawa.socketio.androidchat' - minSdkVersion 15 - targetSdkVersion 21 + minSdkVersion 17 + targetSdkVersion 25 versionCode 1 versionName "1.0" } @@ -34,9 +24,14 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:21.0.+' - compile 'com.android.support:recyclerview-v7:21.0.+' - compile ('io.socket:socket.io-client:0.8.1') { + compile 'com.android.support:appcompat-v7:25.0.+' + compile 'com.android.support:recyclerview-v7:25.0.+' + compile ('io.socket:socket.io-client:0.8.3') { exclude group: 'org.json', module: 'json' } + + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + testCompile 'junit:junit:4.12' } diff --git a/app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ApplicationTest.java b/app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ApplicationTest.java deleted file mode 100644 index c22c369..0000000 --- a/app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.github.nkzawa.socketio.androidchat; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ExampleInstrumentedTest.java new file mode 100644 index 0000000..b9fd700 --- /dev/null +++ b/app/src/androidTest/java/com/github/nkzawa/socketio/androidchat/ExampleInstrumentedTest.java @@ -0,0 +1,27 @@ +package com.github.nkzawa.socketio.androidchat; + +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 junit.framework.Assert.assertEquals; + + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.github.nkzawa.socketio.androidchat", appContext.getPackageName()); + } +} diff --git a/app/src/main/java/com/github/nkzawa/socketio/androidchat/Constants.java b/app/src/main/java/com/github/nkzawa/socketio/androidchat/Constants.java index 24627b2..84b0ba0 100644 --- a/app/src/main/java/com/github/nkzawa/socketio/androidchat/Constants.java +++ b/app/src/main/java/com/github/nkzawa/socketio/androidchat/Constants.java @@ -1,5 +1,5 @@ package com.github.nkzawa.socketio.androidchat; public class Constants { - public static final String CHAT_SERVER_URL = "http://chat.socket.io"; + public static final String CHAT_SERVER_URL = "https://socketio-chat.now.sh/"; } diff --git a/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainActivity.java b/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainActivity.java index 1da8b27..a5e010c 100644 --- a/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainActivity.java +++ b/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainActivity.java @@ -1,10 +1,10 @@ package com.github.nkzawa.socketio.androidchat; import android.os.Bundle; -import android.support.v7.app.ActionBarActivity; +import android.support.v7.app.AppCompatActivity; -public class MainActivity extends ActionBarActivity { +public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java b/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java index abf5349..69720b6 100644 --- a/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java +++ b/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java @@ -10,6 +10,7 @@ import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; +import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; @@ -22,20 +23,24 @@ import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; -import io.socket.client.Socket; -import io.socket.emitter.Emitter; + import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; +import io.socket.client.Socket; +import io.socket.emitter.Emitter; + /** * A chat fragment containing messages view and input form. */ public class MainFragment extends Fragment { + private static final String TAG = "MainFragment"; + private static final int REQUEST_LOGIN = 0; private static final int TYPING_TIMER_LENGTH = 600; @@ -290,6 +295,7 @@ public void call(Object... args) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { + Log.i(TAG, "diconnected"); isConnected = false; Toast.makeText(getActivity().getApplicationContext(), R.string.disconnect, Toast.LENGTH_LONG).show(); @@ -304,6 +310,7 @@ public void call(Object... args) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { + Log.e(TAG, "Error connecting"); Toast.makeText(getActivity().getApplicationContext(), R.string.error_connect, Toast.LENGTH_LONG).show(); } @@ -324,6 +331,7 @@ public void run() { username = data.getString("username"); message = data.getString("message"); } catch (JSONException e) { + Log.e(TAG, e.getMessage()); return; } @@ -347,6 +355,7 @@ public void run() { username = data.getString("username"); numUsers = data.getInt("numUsers"); } catch (JSONException e) { + Log.e(TAG, e.getMessage()); return; } @@ -370,6 +379,7 @@ public void run() { username = data.getString("username"); numUsers = data.getInt("numUsers"); } catch (JSONException e) { + Log.e(TAG, e.getMessage()); return; } @@ -392,6 +402,7 @@ public void run() { try { username = data.getString("username"); } catch (JSONException e) { + Log.e(TAG, e.getMessage()); return; } addTyping(username); @@ -411,6 +422,7 @@ public void run() { try { username = data.getString("username"); } catch (JSONException e) { + Log.e(TAG, e.getMessage()); return; } removeTyping(username); diff --git a/build.gradle b/build.gradle index 9405f3f..16f9930 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.2.3' + classpath 'com.android.tools.build:gradle:2.3.0-beta4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle.properties b/gradle.properties index 1d3591c..4e7c65d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,6 +11,7 @@ # The setting is particularly useful for tweaking memory settings. # Default value: -Xmx10248m -XX:MaxPermSize=256m # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +org.gradle.jvmargs=-Xmx1536m # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index af155f8..95ab04d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Dec 23 22:20:48 JST 2015 +#Mon Feb 20 01:28:27 PST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip From 690e79ae555123ec321a7c1ae5a06052f8c0290c Mon Sep 17 00:00:00 2001 From: Vamsee Lakamsani Date: Mon, 20 Feb 2017 01:52:06 -0800 Subject: [PATCH 2/5] updated README.md to use https://socketio-chat.now.sh/ --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a3169fa..4a0e861 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # socket.io-android-chat -This is a simple chat demo for socket.io and Android. You can connect to [chat.socket.io](http://socket.io/demos/chat/) using this app. +This is a simple chat demo for socket.io and Android. You can connect to [chat.socket.io](https://socketio-chat.now.sh/) using this app. ## Installation From aa3687bffcb0e7697daca8bbdfaf562a857a639f Mon Sep 17 00:00:00 2001 From: Vamsee Lakamsani Date: Mon, 20 Feb 2017 01:56:04 -0800 Subject: [PATCH 3/5] updated README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a0e861..0d07c53 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # socket.io-android-chat -This is a simple chat demo for socket.io and Android. You can connect to [chat.socket.io](https://socketio-chat.now.sh/) using this app. +This is a simple chat demo for socket.io and Android. You can connect to [https://socketio-chat.now.sh/](https://socketio-chat.now.sh/) using this app. ## Installation @@ -12,6 +12,9 @@ This is a simple chat demo for socket.io and Android. You can connect to [chat.s http://socket.io/blog/native-socket-io-and-android/ +## Misc +https://github.com/socketio/socket.io/issues/2823 + ## License MIT From 8ebe096f8581beac58d8a6ffc0235ec4b524696c Mon Sep 17 00:00:00 2001 From: Vamsee Lakamsani Date: Mon, 20 Feb 2017 09:34:59 -0800 Subject: [PATCH 4/5] moved to gradle 3.4 --- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 95ab04d..2a61779 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Feb 20 01:28:27 PST 2017 +#Mon Feb 20 09:34:25 PST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.4-all.zip From 931efc20c959d79f0fcd4a4399769a58e30854bc Mon Sep 17 00:00:00 2001 From: Vamsee Lakamsani Date: Mon, 20 Feb 2017 11:15:00 -0800 Subject: [PATCH 5/5] updated onAttach signature. --- .../nkzawa/socketio/androidchat/MainFragment.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java b/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java index 69720b6..4060468 100644 --- a/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java +++ b/app/src/main/java/com/github/nkzawa/socketio/androidchat/MainFragment.java @@ -1,6 +1,7 @@ package com.github.nkzawa.socketio.androidchat; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; @@ -60,12 +61,20 @@ public MainFragment() { super(); } + + // This event fires 1st, before creation of fragment or any views + // The onAttach method is called when the Fragment instance is associated with an Activity. + // This does not mean the Activity is fully initialized. @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - mAdapter = new MessageAdapter(activity, mMessages); + public void onAttach(Context context) { + super.onAttach(context); + mAdapter = new MessageAdapter(context, mMessages); + if (context instanceof Activity){ + //this.listener = (MainActivity) context; + } } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);