├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable-hdpi
│ │ │ │ ├── search1.png
│ │ │ │ ├── input_bg_gray.9.png
│ │ │ │ ├── jmui_sending_img.png
│ │ │ │ ├── send_msg_normal.9.png
│ │ │ │ ├── send_msg_press.9.png
│ │ │ │ ├── business_card_bg.9.png
│ │ │ │ ├── receive_msg_normal.9.png
│ │ │ │ ├── receive_msg_press.9.png
│ │ │ │ ├── jmui_send_error_normal.png
│ │ │ │ └── jmui_send_error_press.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ ├── group.png
│ │ │ │ ├── verification.png
│ │ │ │ ├── voip_toast_bg.9.png
│ │ │ │ └── contact_head_icon.png
│ │ │ ├── drawable-xxhdpi
│ │ │ │ ├── back.png
│ │ │ │ ├── icon.png
│ │ │ │ ├── icon_add_nomal.png
│ │ │ │ ├── icon_add_press.png
│ │ │ │ ├── icon_face_nomal.png
│ │ │ │ ├── icon_voice_nomal.png
│ │ │ │ ├── icon_voice_press.png
│ │ │ │ └── jmui_chat_detail.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable
│ │ │ │ ├── jmui_back_btn_bg.xml
│ │ │ │ ├── btn_send_bg_disable.xml
│ │ │ │ ├── jmui_msg_send_bg.xml
│ │ │ │ ├── jmui_msg_receive_bg.xml
│ │ │ │ ├── btn_multi_bg.xml
│ │ │ │ ├── btn_voice_or_text.xml
│ │ │ │ ├── send_message_failed.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── item_chat_receive_text.xml
│ │ │ │ ├── view_keyboard_xhs.xml
│ │ │ │ └── item_chat_send_text.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── yxc
│ │ │ │ └── websocketclientdemo
│ │ │ │ ├── util
│ │ │ │ └── Util.java
│ │ │ │ ├── modle
│ │ │ │ └── ChatMessage.java
│ │ │ │ ├── im
│ │ │ │ ├── JWebSocketClient.java
│ │ │ │ └── JWebSocketClientService.java
│ │ │ │ ├── adapter
│ │ │ │ └── Adapter_ChatMessage.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── yxc
│ │ │ └── websocketclientdemo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── yxc
│ │ └── websocketclientdemo
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── screenshot
├── chat.jpg
├── chatntf.jpg
├── lockmsg.jpg
├── notification.jpg
└── qrcode_chaoyoung.jpg
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── vcs.xml
├── modules.xml
├── misc.xml
├── runConfigurations.xml
└── codeStyles
│ └── Project.xml
├── gradle.properties
├── README.md
├── .gitignore
├── gradlew.bat
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/screenshot/chat.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/chat.jpg
--------------------------------------------------------------------------------
/screenshot/chatntf.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/chatntf.jpg
--------------------------------------------------------------------------------
/screenshot/lockmsg.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/lockmsg.jpg
--------------------------------------------------------------------------------
/screenshot/notification.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/notification.jpg
--------------------------------------------------------------------------------
/screenshot/qrcode_chaoyoung.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/qrcode_chaoyoung.jpg
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | WebSocketClientDemo
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/search1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/search1.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/group.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/group.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/back.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/back.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/input_bg_gray.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/input_bg_gray.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/verification.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/verification.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/jmui_sending_img.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/jmui_sending_img.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/send_msg_normal.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/send_msg_normal.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/send_msg_press.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/send_msg_press.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/voip_toast_bg.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/voip_toast_bg.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/icon_add_nomal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_add_nomal.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/icon_add_press.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_add_press.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/icon_face_nomal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_face_nomal.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/business_card_bg.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/business_card_bg.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/receive_msg_normal.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/receive_msg_normal.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/receive_msg_press.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/receive_msg_press.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/contact_head_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/contact_head_icon.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/icon_voice_nomal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_voice_nomal.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/icon_voice_press.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_voice_press.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/jmui_chat_detail.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/jmui_chat_detail.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/jmui_send_error_normal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/jmui_send_error_normal.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/jmui_send_error_press.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/jmui_send_error_press.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Mar 11 16:34:53 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/jmui_back_btn_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 20sp
4 | 5dp
5 | 28dp
6 | 48.5dp
7 | 40dp
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_send_bg_disable.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yxc/websocketclientdemo/util/Util.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo.util;
2 |
3 | import android.content.Context;
4 | import android.widget.Toast;
5 |
6 | public class Util {
7 | public static final String ws = "ws://echo.websocket.org";//websocket测试地址
8 |
9 | public static void showToast(Context ctx, String msg) {
10 | Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/jmui_msg_send_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/jmui_msg_receive_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/test/java/com/yxc/websocketclientdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 | #F5F5F5
8 | #ffffffff
9 | #ff000000
10 | #3072F3
11 | #2DD0CF
12 | #2DD0CF
13 | @color/white
14 | #0B1816
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_multi_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_voice_or_text.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/send_message_failed.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/yxc/websocketclientdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.yxc.websocketclientdemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WebSocketClient
2 | Android端websocket即时通讯demo
3 |
4 |
5 | #### 主要功能
6 | 1、与websocket建立长连接
7 | 2、与websocket进行即时通讯
8 | 3、Service和Activity之间通讯和UI更新
9 | 4、弹出消息通知(包括锁屏通知)
10 | 5、心跳检测和重连(保证websocket连接稳定性)
11 | 6、服务(Service)保活
12 |
13 | #### 效果图
14 |
15 |

16 |

17 |
18 |

19 |

20 |
21 |
22 | ***
23 | 更多技术干货,欢迎关注我的公众号:ChaoYoung
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yxc/websocketclientdemo/modle/ChatMessage.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo.modle;
2 |
3 | public class ChatMessage {
4 | private String content;
5 | private String time;
6 | private int isMeSend;//0是对方发送 1是自己发送
7 | private int isRead;//是否已读(0未读 1已读)
8 |
9 | public String getContent() {
10 | return content;
11 | }
12 |
13 | public void setContent(String content) {
14 | this.content = content;
15 | }
16 |
17 | public String getTime() {
18 | return time;
19 | }
20 |
21 | public void setTime(String time) {
22 | this.time = time;
23 | }
24 |
25 | public int getIsMeSend() {
26 | return isMeSend;
27 | }
28 |
29 | public void setIsMeSend(int isMeSend) {
30 | this.isMeSend = isMeSend;
31 | }
32 |
33 | public int getIsRead() {
34 | return isRead;
35 | }
36 |
37 | public void setIsRead(int isRead) {
38 | this.isRead = isRead;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yxc/websocketclientdemo/im/JWebSocketClient.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo.im;
2 |
3 | import android.util.Log;
4 |
5 | import org.java_websocket.client.WebSocketClient;
6 | import org.java_websocket.drafts.Draft_6455;
7 | import org.java_websocket.handshake.ServerHandshake;
8 |
9 | import java.net.URI;
10 |
11 | public class JWebSocketClient extends WebSocketClient {
12 | public JWebSocketClient(URI serverUri) {
13 | super(serverUri, new Draft_6455());
14 | }
15 |
16 | @Override
17 | public void onOpen(ServerHandshake handshakedata) {
18 | Log.e("JWebSocketClient", "onOpen()");
19 | }
20 |
21 | @Override
22 | public void onMessage(String message) {
23 | Log.e("JWebSocketClient", "onMessage()");
24 | }
25 |
26 | @Override
27 | public void onClose(int code, String reason, boolean remote) {
28 | Log.e("JWebSocketClient", "onClose()");
29 | }
30 |
31 | @Override
32 | public void onError(Exception ex) {
33 | Log.e("JWebSocketClient", "onError()");
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.yxc.websocketclientdemo"
7 | minSdkVersion 15
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:28.0.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 |
29 | implementation "org.java-websocket:Java-WebSocket:1.4.0"
30 | }
31 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # IntelliJ
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/assetWizardSettings.xml
41 | .idea/dictionaries
42 | .idea/libraries
43 | .idea/caches
44 |
45 | # Keystore files
46 | # Uncomment the following line if you do not want to check your keystore files in.
47 | #*.jks
48 |
49 | # External native build folder generated in Android Studio 2.2 and later
50 | .externalNativeBuild
51 |
52 | # Google Services (e.g. APIs or Firebase)
53 | google-services.json
54 |
55 | # Freeline
56 | freeline.py
57 | freeline/
58 | freeline_project_description.json
59 |
60 | # fastlane
61 | fastlane/report.xml
62 | fastlane/Preview.html
63 | fastlane/screenshots
64 | fastlane/test_output
65 | fastlane/readme.md
66 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
21 |
25 |
30 |
31 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
21 |
22 |
23 |
29 |
30 |
39 |
40 |
47 |
48 |
49 |
50 |
57 |
58 |
59 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
17 |
28 |
36 |
47 |
53 |
61 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yxc/websocketclientdemo/adapter/Adapter_ChatMessage.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo.adapter;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.BaseAdapter;
9 | import android.widget.TextView;
10 |
11 |
12 | import com.yxc.websocketclientdemo.R;
13 | import com.yxc.websocketclientdemo.modle.ChatMessage;
14 |
15 | import java.text.SimpleDateFormat;
16 | import java.util.Date;
17 | import java.util.List;
18 |
19 | public class Adapter_ChatMessage extends BaseAdapter {
20 | List mChatMessageList;
21 | LayoutInflater inflater;
22 | Context context;
23 |
24 | public Adapter_ChatMessage(Context context, List list) {
25 | this.mChatMessageList = list;
26 | this.context = context;
27 | inflater = LayoutInflater.from(context);
28 | }
29 |
30 | @Override
31 | public int getViewTypeCount() {
32 | return 2;
33 | }
34 |
35 | @Override
36 | public int getItemViewType(int position) {
37 | if (mChatMessageList.get(position).getIsMeSend() == 1)
38 | return 0;// 返回的数据位角标
39 | else
40 | return 1;
41 | }
42 |
43 | @Override
44 | public int getCount() {
45 | return mChatMessageList.size();
46 | }
47 |
48 |
49 | @Override
50 | public Object getItem(int i) {
51 | return mChatMessageList.get(i);
52 | }
53 |
54 | @Override
55 | public long getItemId(int i) {
56 | return i;
57 | }
58 |
59 | @Override
60 | public View getView(int i, View view, ViewGroup viewGroup) {
61 | ChatMessage mChatMessage = mChatMessageList.get(i);
62 | String content = mChatMessage.getContent();
63 | String time = formatTime(mChatMessage.getTime());
64 | int isMeSend = mChatMessage.getIsMeSend();
65 | int isRead = mChatMessage.getIsRead();////是否已读(0未读 1已读)
66 | final ViewHolder holder;
67 | if (view == null) {
68 | holder = new ViewHolder();
69 | if (isMeSend == 0) {//对方发送
70 | view = inflater.inflate(R.layout.item_chat_receive_text, viewGroup, false);
71 | holder.tv_content = view.findViewById(R.id.tv_content);
72 | holder.tv_sendtime = view.findViewById(R.id.tv_sendtime);
73 | holder.tv_display_name = view.findViewById(R.id.tv_display_name);
74 | } else {
75 | view = inflater.inflate(R.layout.item_chat_send_text, viewGroup, false);
76 | holder.tv_content = view.findViewById(R.id.tv_content);
77 | holder.tv_sendtime = view.findViewById(R.id.tv_sendtime);
78 | holder.tv_isRead = view.findViewById(R.id.tv_isRead);
79 | }
80 |
81 | view.setTag(holder);
82 | } else {
83 | holder = (ViewHolder) view.getTag();
84 | }
85 |
86 |
87 | holder.tv_sendtime.setText(time);
88 | holder.tv_content.setVisibility(View.VISIBLE);
89 | holder.tv_content.setText(content);
90 |
91 |
92 | //如果是自己发送才显示未读已读
93 | if (isMeSend == 1) {
94 | if (isRead == 0) {
95 | holder.tv_isRead.setText("未读");
96 | holder.tv_isRead.setTextColor(context.getResources().getColor(R.color.jmui_jpush_blue));
97 | } else if (isRead == 1) {
98 | holder.tv_isRead.setText("已读");
99 | holder.tv_isRead.setTextColor(Color.GRAY);
100 | } else {
101 | holder.tv_isRead.setText("");
102 | }
103 | }else{
104 | holder.tv_display_name.setVisibility(View.VISIBLE);
105 | holder.tv_display_name.setText("服务器");
106 | }
107 |
108 | return view;
109 | }
110 |
111 | class ViewHolder {
112 | private TextView tv_content, tv_sendtime, tv_display_name, tv_isRead;
113 | }
114 |
115 |
116 | /**
117 | * 将毫秒数转为日期格式
118 | *
119 | * @param timeMillis
120 | * @return
121 | */
122 | private String formatTime(String timeMillis) {
123 | long timeMillisl=Long.parseLong(timeMillis);
124 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
125 | Date date = new Date(timeMillisl);
126 | return simpleDateFormat.format(date);
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_chat_receive_text.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
10 |
11 |
12 |
16 |
17 |
20 |
21 |
26 |
27 |
33 |
34 |
50 |
51 |
52 |
60 |
61 |
64 |
65 |
74 |
75 |
80 |
81 |
91 |
92 |
102 |
103 |
104 |
105 |
111 |
112 |
121 |
122 |
123 |
124 |
125 |
126 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/view_keyboard_xhs.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
16 |
17 |
18 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
71 |
72 |
73 |
74 |
75 |
83 |
84 |
85 |
92 |
93 |
94 |
101 |
102 |
103 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_chat_send_text.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
9 |
10 |
16 |
17 |
27 |
28 |
36 |
37 |
45 |
46 |
58 |
59 |
67 |
68 |
71 |
72 |
79 |
80 |
85 |
86 |
95 |
96 |
106 |
107 |
108 |
109 |
115 |
116 |
125 |
126 |
127 |
128 |
131 |
132 |
133 |
134 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yxc/websocketclientdemo/im/JWebSocketClientService.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo.im;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.app.KeyguardManager;
5 | import android.app.Notification;
6 | import android.app.NotificationManager;
7 | import android.app.PendingIntent;
8 | import android.app.Service;
9 | import android.content.Context;
10 | import android.content.Intent;
11 | import android.os.Binder;
12 | import android.os.Build;
13 | import android.os.Handler;
14 | import android.os.IBinder;
15 | import android.os.PowerManager;
16 | import android.support.v4.app.NotificationCompat;
17 | import android.util.Log;
18 |
19 | import com.yxc.websocketclientdemo.MainActivity;
20 | import com.yxc.websocketclientdemo.R;
21 | import com.yxc.websocketclientdemo.util.Util;
22 |
23 | import org.java_websocket.handshake.ServerHandshake;
24 |
25 | import java.net.URI;
26 |
27 | import static android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC;
28 |
29 | public class JWebSocketClientService extends Service {
30 | public JWebSocketClient client;
31 | private JWebSocketClientBinder mBinder = new JWebSocketClientBinder();
32 | private final static int GRAY_SERVICE_ID = 1001;
33 | //灰色保活
34 | public static class GrayInnerService extends Service {
35 |
36 | @Override
37 | public int onStartCommand(Intent intent, int flags, int startId) {
38 | startForeground(GRAY_SERVICE_ID, new Notification());
39 | stopForeground(true);
40 | stopSelf();
41 | return super.onStartCommand(intent, flags, startId);
42 | }
43 | @Override
44 | public IBinder onBind(Intent intent) {
45 | return null;
46 | }
47 | }
48 | PowerManager.WakeLock wakeLock;//锁屏唤醒
49 | //获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行
50 | @SuppressLint("InvalidWakeLockTag")
51 | private void acquireWakeLock()
52 | {
53 | if (null == wakeLock)
54 | {
55 | PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
56 | wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "PostLocationService");
57 | if (null != wakeLock)
58 | {
59 | wakeLock.acquire();
60 | }
61 | }
62 | }
63 |
64 | //用于Activity和service通讯
65 | public class JWebSocketClientBinder extends Binder {
66 | public JWebSocketClientService getService() {
67 | return JWebSocketClientService.this;
68 | }
69 | }
70 |
71 | @Override
72 | public IBinder onBind(Intent intent) {
73 | return mBinder;
74 | }
75 |
76 | @Override
77 | public void onCreate() {
78 | super.onCreate();
79 | }
80 |
81 | @Override
82 | public int onStartCommand(Intent intent, int flags, int startId) {
83 | //初始化websocket
84 | initSocketClient();
85 | mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//开启心跳检测
86 |
87 | //设置service为前台服务,提高优先级
88 | if (Build.VERSION.SDK_INT < 18) {
89 | //Android4.3以下 ,隐藏Notification上的图标
90 | startForeground(GRAY_SERVICE_ID, new Notification());
91 | } else if(Build.VERSION.SDK_INT>18 && Build.VERSION.SDK_INT<25){
92 | //Android4.3 - Android7.0,隐藏Notification上的图标
93 | Intent innerIntent = new Intent(this, GrayInnerService.class);
94 | startService(innerIntent);
95 | startForeground(GRAY_SERVICE_ID, new Notification());
96 | }else{
97 | //Android7.0以上app启动后通知栏会出现一条"正在运行"的通知
98 | startForeground(GRAY_SERVICE_ID, new Notification());
99 | }
100 |
101 | acquireWakeLock();
102 | return START_STICKY;
103 | }
104 |
105 |
106 | @Override
107 | public void onDestroy() {
108 | closeConnect();
109 | super.onDestroy();
110 | }
111 |
112 | public JWebSocketClientService() {
113 | }
114 |
115 |
116 | /**
117 | * 初始化websocket连接
118 | */
119 | private void initSocketClient() {
120 | URI uri = URI.create(Util.ws);
121 | client = new JWebSocketClient(uri) {
122 | @Override
123 | public void onMessage(String message) {
124 | Log.e("JWebSocketClientService", "收到的消息:" + message);
125 |
126 | Intent intent = new Intent();
127 | intent.setAction("com.xch.servicecallback.content");
128 | intent.putExtra("message", message);
129 | sendBroadcast(intent);
130 |
131 | checkLockAndShowNotification(message);
132 | }
133 |
134 | @Override
135 | public void onOpen(ServerHandshake handshakedata) {
136 | super.onOpen(handshakedata);
137 | Log.e("JWebSocketClientService", "websocket连接成功");
138 | }
139 | };
140 | connect();
141 | }
142 |
143 | /**
144 | * 连接websocket
145 | */
146 | private void connect() {
147 | new Thread() {
148 | @Override
149 | public void run() {
150 | try {
151 | //connectBlocking多出一个等待操作,会先连接再发送,否则未连接发送会报错
152 | client.connectBlocking();
153 | } catch (InterruptedException e) {
154 | e.printStackTrace();
155 | }
156 | }
157 | }.start();
158 |
159 | }
160 |
161 | /**
162 | * 发送消息
163 | *
164 | * @param msg
165 | */
166 | public void sendMsg(String msg) {
167 | if (null != client) {
168 | Log.e("JWebSocketClientService", "发送的消息:" + msg);
169 | client.send(msg);
170 | }
171 | }
172 |
173 | /**
174 | * 断开连接
175 | */
176 | private void closeConnect() {
177 | try {
178 | if (null != client) {
179 | client.close();
180 | }
181 | } catch (Exception e) {
182 | e.printStackTrace();
183 | } finally {
184 | client = null;
185 | }
186 | }
187 |
188 |
189 | // -----------------------------------消息通知--------------------------------------------------------
190 |
191 | /**
192 | * 检查锁屏状态,如果锁屏先点亮屏幕
193 | *
194 | * @param content
195 | */
196 | private void checkLockAndShowNotification(String content) {
197 | //管理锁屏的一个服务
198 | KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
199 | if (km.inKeyguardRestrictedInputMode()) {//锁屏
200 | //获取电源管理器对象
201 | PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
202 | if (!pm.isScreenOn()) {
203 | @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
204 | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
205 | wl.acquire(); //点亮屏幕
206 | wl.release(); //任务结束后释放
207 | }
208 | sendNotification(content);
209 | } else {
210 | sendNotification(content);
211 | }
212 | }
213 |
214 | /**
215 | * 发送通知
216 | *
217 | * @param content
218 | */
219 | private void sendNotification(String content) {
220 | Intent intent = new Intent();
221 | intent.setClass(this, MainActivity.class);
222 | PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
223 | NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
224 | Notification notification = new NotificationCompat.Builder(this)
225 | .setAutoCancel(true)
226 | // 设置该通知优先级
227 | .setPriority(Notification.PRIORITY_MAX)
228 | .setSmallIcon(R.drawable.icon)
229 | .setContentTitle("服务器")
230 | .setContentText(content)
231 | .setVisibility(VISIBILITY_PUBLIC)
232 | .setWhen(System.currentTimeMillis())
233 | // 向通知添加声音、闪灯和振动效果
234 | .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND)
235 | .setContentIntent(pendingIntent)
236 | .build();
237 | notifyManager.notify(1, notification);//id要保证唯一
238 | }
239 |
240 |
241 | // -------------------------------------websocket心跳检测------------------------------------------------
242 | private static final long HEART_BEAT_RATE = 10 * 1000;//每隔10秒进行一次对长连接的心跳检测
243 | private Handler mHandler = new Handler();
244 | private Runnable heartBeatRunnable = new Runnable() {
245 | @Override
246 | public void run() {
247 | Log.e("JWebSocketClientService", "心跳包检测websocket连接状态");
248 | if (client != null) {
249 | if (client.isClosed()) {
250 | reconnectWs();
251 | }
252 | } else {
253 | //如果client已为空,重新初始化连接
254 | client = null;
255 | initSocketClient();
256 | }
257 | //每隔一定的时间,对长连接进行一次心跳检测
258 | mHandler.postDelayed(this, HEART_BEAT_RATE);
259 | }
260 | };
261 |
262 | /**
263 | * 开启重连
264 | */
265 | private void reconnectWs() {
266 | mHandler.removeCallbacks(heartBeatRunnable);
267 | new Thread() {
268 | @Override
269 | public void run() {
270 | try {
271 | Log.e("JWebSocketClientService", "开启重连");
272 | client.reconnectBlocking();
273 | } catch (InterruptedException e) {
274 | e.printStackTrace();
275 | }
276 | }
277 | }.start();
278 | }
279 | }
280 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yxc/websocketclientdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.yxc.websocketclientdemo;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.AppOpsManager;
5 | import android.content.BroadcastReceiver;
6 | import android.content.ComponentName;
7 | import android.content.Context;
8 | import android.content.DialogInterface;
9 | import android.content.Intent;
10 | import android.content.IntentFilter;
11 | import android.content.ServiceConnection;
12 | import android.content.pm.ApplicationInfo;
13 | import android.net.Uri;
14 | import android.os.Build;
15 | import android.os.IBinder;
16 | import android.provider.Settings;
17 | import android.support.v7.app.AlertDialog;
18 | import android.support.v7.app.AppCompatActivity;
19 | import android.os.Bundle;
20 | import android.text.Editable;
21 | import android.text.TextWatcher;
22 | import android.util.Log;
23 | import android.view.View;
24 | import android.widget.Button;
25 | import android.widget.EditText;
26 | import android.widget.ListView;
27 | import android.widget.TextView;
28 |
29 | import com.yxc.websocketclientdemo.adapter.Adapter_ChatMessage;
30 | import com.yxc.websocketclientdemo.im.JWebSocketClient;
31 | import com.yxc.websocketclientdemo.im.JWebSocketClientService;
32 | import com.yxc.websocketclientdemo.modle.ChatMessage;
33 | import com.yxc.websocketclientdemo.util.Util;
34 |
35 | import java.lang.reflect.Field;
36 | import java.lang.reflect.Method;
37 | import java.util.ArrayList;
38 | import java.util.List;
39 |
40 |
41 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
42 | private Context mContext;
43 | private JWebSocketClient client;
44 | private JWebSocketClientService.JWebSocketClientBinder binder;
45 | private JWebSocketClientService jWebSClientService;
46 | private EditText et_content;
47 | private ListView listView;
48 | private Button btn_send;
49 | private List chatMessageList = new ArrayList<>();//消息列表
50 | private Adapter_ChatMessage adapter_chatMessage;
51 | private ChatMessageReceiver chatMessageReceiver;
52 |
53 | private ServiceConnection serviceConnection = new ServiceConnection() {
54 | @Override
55 | public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
56 | Log.e("MainActivity", "服务与活动成功绑定");
57 | binder = (JWebSocketClientService.JWebSocketClientBinder) iBinder;
58 | jWebSClientService = binder.getService();
59 | client = jWebSClientService.client;
60 | }
61 |
62 | @Override
63 | public void onServiceDisconnected(ComponentName componentName) {
64 | Log.e("MainActivity", "服务与活动成功断开");
65 | }
66 | };
67 |
68 | private class ChatMessageReceiver extends BroadcastReceiver{
69 |
70 | @Override
71 | public void onReceive(Context context, Intent intent) {
72 | String message=intent.getStringExtra("message");
73 | ChatMessage chatMessage=new ChatMessage();
74 | chatMessage.setContent(message);
75 | chatMessage.setIsMeSend(0);
76 | chatMessage.setIsRead(1);
77 | chatMessage.setTime(System.currentTimeMillis()+"");
78 | chatMessageList.add(chatMessage);
79 | initChatMsgListView();
80 | }
81 | }
82 |
83 | @Override
84 | protected void onCreate(Bundle savedInstanceState) {
85 | super.onCreate(savedInstanceState);
86 | getSupportActionBar().hide();
87 | setContentView(R.layout.activity_main);
88 | mContext=MainActivity.this;
89 | //启动服务
90 | startJWebSClientService();
91 | //绑定服务
92 | bindService();
93 | //注册广播
94 | doRegisterReceiver();
95 | //检测通知是否开启
96 | checkNotification(mContext);
97 | findViewById();
98 | initView();
99 | }
100 |
101 | /**
102 | * 绑定服务
103 | */
104 | private void bindService() {
105 | Intent bindIntent = new Intent(mContext, JWebSocketClientService.class);
106 | bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
107 | }
108 | /**
109 | * 启动服务(websocket客户端服务)
110 | */
111 | private void startJWebSClientService() {
112 | Intent intent = new Intent(mContext, JWebSocketClientService.class);
113 | startService(intent);
114 | }
115 | /**
116 | * 动态注册广播
117 | */
118 | private void doRegisterReceiver() {
119 | chatMessageReceiver = new ChatMessageReceiver();
120 | IntentFilter filter = new IntentFilter("com.xch.servicecallback.content");
121 | registerReceiver(chatMessageReceiver, filter);
122 | }
123 |
124 |
125 | private void findViewById() {
126 | listView = findViewById(R.id.chatmsg_listView);
127 | btn_send = findViewById(R.id.btn_send);
128 | et_content = findViewById(R.id.et_content);
129 | btn_send.setOnClickListener(this);
130 | }
131 | private void initView() {
132 | //监听输入框的变化
133 | et_content.addTextChangedListener(new TextWatcher() {
134 | @Override
135 | public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
136 |
137 | }
138 |
139 | @Override
140 | public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
141 | if (et_content.getText().toString().length() > 0) {
142 | btn_send.setVisibility(View.VISIBLE);
143 | } else {
144 | btn_send.setVisibility(View.GONE);
145 | }
146 | }
147 |
148 | @Override
149 | public void afterTextChanged(Editable editable) {
150 |
151 | }
152 | });
153 | }
154 |
155 | @Override
156 | public void onClick(View view) {
157 | switch (view.getId()) {
158 | case R.id.btn_send:
159 | String content = et_content.getText().toString();
160 | if (content.length() <= 0) {
161 | Util.showToast(mContext, "消息不能为空哟");
162 | return;
163 | }
164 |
165 | if (client != null && client.isOpen()) {
166 | jWebSClientService.sendMsg(content);
167 |
168 | //暂时将发送的消息加入消息列表,实际以发送成功为准(也就是服务器返回你发的消息时)
169 | ChatMessage chatMessage=new ChatMessage();
170 | chatMessage.setContent(content);
171 | chatMessage.setIsMeSend(1);
172 | chatMessage.setIsRead(1);
173 | chatMessage.setTime(System.currentTimeMillis()+"");
174 | chatMessageList.add(chatMessage);
175 | initChatMsgListView();
176 | et_content.setText("");
177 | } else {
178 | Util.showToast(mContext, "连接已断开,请稍等或重启App哟");
179 | }
180 | break;
181 | default:
182 | break;
183 | }
184 | }
185 |
186 | private void initChatMsgListView(){
187 | adapter_chatMessage = new Adapter_ChatMessage(mContext, chatMessageList);
188 | listView.setAdapter(adapter_chatMessage);
189 | listView.setSelection(chatMessageList.size());
190 | }
191 |
192 |
193 | /**
194 | * 检测是否开启通知
195 | *
196 | * @param context
197 | */
198 | private void checkNotification(final Context context) {
199 | if (!isNotificationEnabled(context)) {
200 | new AlertDialog.Builder(context).setTitle("温馨提示")
201 | .setMessage("你还未开启系统通知,将影响消息的接收,要去开启吗?")
202 | .setPositiveButton("确定", new DialogInterface.OnClickListener() {
203 | @Override
204 | public void onClick(DialogInterface dialog, int which) {
205 | setNotification(context);
206 | }
207 | }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
208 | @Override
209 | public void onClick(DialogInterface dialog, int which) {
210 |
211 | }
212 | }).show();
213 | }
214 | }
215 | /**
216 | * 如果没有开启通知,跳转至设置界面
217 | *
218 | * @param context
219 | */
220 | private void setNotification(Context context) {
221 | Intent localIntent = new Intent();
222 | //直接跳转到应用通知设置的代码:
223 | if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
224 | localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
225 | localIntent.putExtra("app_package", context.getPackageName());
226 | localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
227 | } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
228 | localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
229 | localIntent.addCategory(Intent.CATEGORY_DEFAULT);
230 | localIntent.setData(Uri.parse("package:" + context.getPackageName()));
231 | } else {
232 | //4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
233 | localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
234 | if (Build.VERSION.SDK_INT >= 9) {
235 | localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
236 | localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
237 | } else if (Build.VERSION.SDK_INT <= 8) {
238 | localIntent.setAction(Intent.ACTION_VIEW);
239 | localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
240 | localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
241 | }
242 | }
243 | context.startActivity(localIntent);
244 | }
245 |
246 | /**
247 | * 获取通知权限,监测是否开启了系统通知
248 | *
249 | * @param context
250 | */
251 | @TargetApi(Build.VERSION_CODES.KITKAT)
252 | private boolean isNotificationEnabled(Context context) {
253 |
254 | String CHECK_OP_NO_THROW = "checkOpNoThrow";
255 | String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
256 |
257 | AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
258 | ApplicationInfo appInfo = context.getApplicationInfo();
259 | String pkg = context.getApplicationContext().getPackageName();
260 | int uid = appInfo.uid;
261 |
262 | Class appOpsClass = null;
263 | try {
264 | appOpsClass = Class.forName(AppOpsManager.class.getName());
265 | Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
266 | String.class);
267 | Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
268 |
269 | int value = (Integer) opPostNotificationValue.get(Integer.class);
270 | return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
271 |
272 | } catch (Exception e) {
273 | e.printStackTrace();
274 | }
275 | return false;
276 | }
277 |
278 | }
279 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------