├── .gitignore
├── DreamSocket.jar
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── android
│ │ └── network
│ │ └── http
│ │ └── sample
│ │ ├── MainActivity.java
│ │ ├── MyApplication.java
│ │ ├── TestFragmentActivity.java
│ │ ├── TestView.java
│ │ └── UploadActivity.java
│ └── res
│ ├── layout
│ ├── activity_fragment_test.xml
│ ├── activity_main.xml
│ └── activity_upload.xml
│ ├── menu
│ └── menu_main.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ └── android
│ └── network
│ └── http
│ ├── Http.java
│ ├── HttpProgress.java
│ ├── InputStreamCallback.java
│ └── URLFormat.java
├── settings.gradle
└── socket
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
└── main
├── AndroidManifest.xml
├── aidl
└── android
│ └── network
│ ├── binder
│ └── remote
│ │ ├── IRemoteBinder.aidl
│ │ └── IRemoteCallback.aidl
│ └── protocol
│ └── Message.aidl
├── java
└── android
│ └── network
│ ├── invoke
│ ├── LoopInvoke.java
│ ├── RemoteBinderInvoke.java
│ └── RemoteCallbackBinder.java
│ ├── model
│ └── Status.java
│ ├── protocol
│ ├── Body.java
│ ├── Message.java
│ └── Protocol.java
│ ├── remote
│ ├── RemoteService.java
│ ├── RemoteServiceConnection.java
│ ├── codec
│ │ └── MessageCodec.java
│ └── logger
│ │ └── DreamSocketLogger.java
│ └── sdk
│ ├── DreamManager.java
│ ├── Receiver.java
│ ├── Sender.java
│ ├── body
│ ├── BodyType.java
│ └── StringBody.java
│ └── listener
│ └── OnReceiverMessage.java
└── res
└── values
└── strings.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | .idea
3 | build
4 | captures
5 | local.properties
6 | *.iml
7 | .DS_Store
--------------------------------------------------------------------------------
/DreamSocket.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sopage/android-network/95b485ddbce8229d284e590d66dc183732bc5ca9/DreamSocket.jar
--------------------------------------------------------------------------------
/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 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # android-network [](https://996.icu)
2 | Android网络通信相关库
3 | ### 简介
4 | 封装:
5 | TCP连接
6 | UDP连接
7 | Http连接
8 |
9 | # [](https://github.com/996icu/996.ICU/blob/master/LICENSE)
10 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion "27.0.3"
6 |
7 | defaultConfig {
8 | applicationId "android.network.http.sample"
9 | minSdkVersion 15
10 | targetSdkVersion 15
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | repositories {
23 | flatDir {
24 | dirs 'libs'
25 | }
26 | }
27 |
28 | dependencies {
29 | compile fileTree(include: ['*.jar'], dir: 'libs')
30 | compile project(':library')
31 | compile project(':socket')
32 | }
33 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/sanders/Developer/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
15 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/java/android/network/http/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package android.network.http.sample;
2 |
3 | import android.app.Activity;
4 | import android.network.protocol.Body;
5 | import android.network.protocol.Message;
6 | import android.network.sdk.DreamManager;
7 | import android.network.sdk.body.StringBody;
8 | import android.network.sdk.listener.OnReceiverMessage;
9 | import android.os.Bundle;
10 | import android.support.annotation.Nullable;
11 | import android.view.View;
12 | import android.widget.TextView;
13 |
14 | public class MainActivity extends Activity implements View.OnClickListener, OnReceiverMessage {
15 |
16 | public int index = 1;
17 | private TextView tv_message;
18 |
19 | @Override
20 | protected void onCreate(@Nullable Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_main);
23 | DreamManager.getReceiver().register(this);
24 | findViewById(R.id.btn_login).setOnClickListener(this);
25 | findViewById(R.id.btn_logout).setOnClickListener(this);
26 | findViewById(R.id.btn_send).setOnClickListener(this);
27 | tv_message = findViewById(R.id.tv_message);
28 | }
29 |
30 | @Override
31 | public void onClick(View view) {
32 | int id = view.getId();
33 | if (id == R.id.btn_login) {
34 | DreamManager.getSender().login(1, "token");
35 | } else if (id == R.id.btn_logout) {
36 | DreamManager.getSender().logout();
37 | } else if (id == R.id.btn_send) {
38 | StringBody body = new StringBody();
39 | body.setString("message -> " + index);
40 | Message message = new Message(100000, 200000, body);
41 | DreamManager.getSender().send(message);
42 | index++;
43 | }
44 | }
45 |
46 | @Override
47 | public void onMessage(Message message) {
48 | Body body = message.getBody();
49 | if(body instanceof StringBody){
50 | tv_message.append(((StringBody) body).getString());
51 | tv_message.append("\n");
52 | }
53 | }
54 |
55 | @Override
56 | public void onStatus(int status) {
57 |
58 | }
59 |
60 | @Override
61 | protected void onDestroy() {
62 | DreamManager.getReceiver().unregister(this);
63 | super.onDestroy();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/android/network/http/sample/MyApplication.java:
--------------------------------------------------------------------------------
1 | package android.network.http.sample;
2 |
3 | import android.app.Application;
4 | import android.network.sdk.DreamManager;
5 |
6 | /**
7 | * @author Mr.Huang
8 | * @date 2017/8/21
9 | */
10 | public class MyApplication extends Application {
11 |
12 | @Override
13 | public void onCreate() {
14 | super.onCreate();
15 | DreamManager.register(this);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/main/java/android/network/http/sample/TestFragmentActivity.java:
--------------------------------------------------------------------------------
1 | package android.network.http.sample;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.FragmentActivity;
6 |
7 | /**
8 | *
9 | */
10 | public class TestFragmentActivity extends FragmentActivity {
11 |
12 | @Override
13 | protected void onCreate(@Nullable Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_fragment_test);
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/java/android/network/http/sample/TestView.java:
--------------------------------------------------------------------------------
1 | package android.network.http.sample;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Paint;
7 | import android.support.annotation.Nullable;
8 | import android.util.AttributeSet;
9 | import android.util.Log;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.TextView;
13 |
14 | /**
15 | *
16 | */
17 | public class TestView extends View {
18 | public TestView(Context context) {
19 | super(context);
20 | }
21 |
22 | public TestView(Context context, @Nullable AttributeSet attrs) {
23 | super(context, attrs);
24 | }
25 |
26 | public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
27 | super(context, attrs, defStyleAttr);
28 | }
29 |
30 | public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
31 | super(context, attrs, defStyleAttr, defStyleRes);
32 | }
33 |
34 | public void onDraw(Canvas canvas) {
35 | View view = View.inflate(getContext(), R.layout.activity_upload, null);
36 | view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
37 | Log.e("ESA", "" + view.getMeasuredWidth());
38 | view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
39 | view.draw(canvas);
40 | // Paint paint = new Paint();
41 | // paint.setColor(Color.BLACK);
42 | // paint.setTextSize(120f);
43 | // paint.setAntiAlias(true);
44 | // canvas.drawText("test", 100, 300, paint);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/android/network/http/sample/UploadActivity.java:
--------------------------------------------------------------------------------
1 | package android.network.http.sample;
2 |
3 | import android.app.Activity;
4 | import android.network.http.Http;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.util.Log;
8 | import android.view.View;
9 |
10 | import java.io.File;
11 | import java.util.HashMap;
12 | import java.util.Map;
13 |
14 | /**
15 | *
16 | */
17 | public class UploadActivity extends Activity implements View.OnClickListener {
18 |
19 | @Override
20 | protected void onCreate(@Nullable Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_upload);
23 | findViewById(R.id.btn_upload).setOnClickListener(this);
24 | }
25 |
26 | @Override
27 | public void onClick(View view) {
28 | new Thread() {
29 | @Override
30 | public void run() {
31 | try {
32 | Map params = new HashMap<>();
33 | params.put("参数1", "参数值1");
34 | params.put("参数2", "参数值2");
35 | params.put("参数3", "参数值3");
36 | File file = new File("/mnt/sdcard/image.jpg");
37 | Map fileMap = new HashMap<>();
38 | fileMap.put("file", new File[]{file});
39 | String s = Http.get("http://10.0.2.2:8080/get", null, params);
40 | Log.e("ESA", s);
41 | } catch (Exception e) {
42 | e.printStackTrace();
43 | }
44 | }
45 | }.start();
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_fragment_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 |
12 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
15 |
16 |
21 |
22 |
27 |
28 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_upload.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sopage/android-network/95b485ddbce8229d284e590d66dc183732bc5ca9/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sopage/android-network/95b485ddbce8229d284e590d66dc183732bc5ca9/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sopage/android-network/95b485ddbce8229d284e590d66dc183732bc5ca9/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Sopage/android-network/95b485ddbce8229d284e590d66dc183732bc5ca9/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | android-network
3 |
4 | Hello world!
5 | Settings
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | ext.kotlin_version = '1.2.21'
4 | repositories {
5 | jcenter()
6 | maven{
7 | url 'https://oss.sonatype.org/content/groups/public'
8 | }
9 | google()
10 | }
11 | dependencies {
12 | classpath 'com.android.tools.build:gradle:3.0.1'
13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14 |
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | }
19 | allprojects {
20 | repositories {
21 | jcenter()
22 | maven{
23 | url 'https://oss.sonatype.org/content/groups/public'
24 | }
25 | google()
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 |
4 | android {
5 | compileSdkVersion 27
6 | buildToolsVersion "27.0.3"
7 |
8 | defaultConfig {
9 | minSdkVersion 15
10 | targetSdkVersion 15
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
24 | }
25 | repositories {
26 | mavenCentral()
27 | }
28 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/sanders/Developer/android-sdk-macosx/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | å
5 |
6 |
--------------------------------------------------------------------------------
/library/src/main/java/android/network/http/Http.java:
--------------------------------------------------------------------------------
1 | package android.network.http;
2 |
3 | import android.util.Log;
4 |
5 | import java.io.BufferedReader;
6 | import java.io.File;
7 | import java.io.FileInputStream;
8 | import java.io.IOException;
9 | import java.io.InputStream;
10 | import java.io.InputStreamReader;
11 | import java.io.OutputStream;
12 | import java.net.HttpURLConnection;
13 | import java.net.URL;
14 | import java.security.KeyManagementException;
15 | import java.security.NoSuchAlgorithmException;
16 | import java.security.cert.CertificateException;
17 | import java.security.cert.X509Certificate;
18 | import java.util.HashMap;
19 | import java.util.Map;
20 |
21 | import javax.net.ssl.HostnameVerifier;
22 | import javax.net.ssl.HttpsURLConnection;
23 | import javax.net.ssl.SSLContext;
24 | import javax.net.ssl.SSLSession;
25 | import javax.net.ssl.TrustManager;
26 | import javax.net.ssl.X509TrustManager;
27 |
28 | public class Http {
29 |
30 | private static Map cookieCache = new HashMap<>();
31 | private static SSLContext sslcontext;
32 | private static HostnameVerifier hostnameVerifier;
33 |
34 | public static String get(String url, Map headers, Map params) throws IOException {
35 | url = URLFormat.getFormatUrl(url, params);
36 | URL _url = new URL(url);
37 | String host = _url.getHost();
38 | HttpURLConnection conn = createHttpURLConnection(_url);
39 | conn.setConnectTimeout(3000);
40 | conn.setRequestMethod("GET");
41 | conn.setRequestProperty("Connection", "Keep-Alive");
42 | conn.setRequestProperty("Accept-Charset", "UTF-8");
43 | String cookie = cookieCache.get(host);
44 | if (cookie != null) {
45 | conn.setRequestProperty("Cookie", cookie);
46 | }
47 | addHeaders(conn, headers);
48 | int responseCode = conn.getResponseCode();
49 | StringBuffer body = null;
50 | if (responseCode == 200) {
51 | BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
52 | body = new StringBuffer();
53 | String line;
54 | while ((line = reader.readLine()) != null) {
55 | body.append(line);
56 | }
57 | reader.close();
58 | }
59 | cookie = conn.getHeaderField("Set-Cookie");
60 | if (cookie != null) {
61 | cookieCache.put(host, cookie);
62 | }
63 | conn.disconnect();
64 | return body != null ? body.toString() : null;
65 | }
66 |
67 | public static String post(String url, Map headers, Map params) throws IOException {
68 | url = URLFormat.getFormatUrl(url, null);
69 | URL _url = new URL(url);
70 | String host = _url.getHost();
71 | HttpURLConnection conn = createHttpURLConnection(_url);
72 | conn.setConnectTimeout(3000);
73 | conn.setRequestMethod("POST");
74 | conn.setRequestProperty("Connection", "Keep-Alive");
75 | conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
76 | conn.setRequestProperty("Accept-Charset", "UTF-8");
77 | String cookie = cookieCache.get(host);
78 | if (cookie != null) {
79 | conn.setRequestProperty("Cookie", cookie);
80 | }
81 | addHeaders(conn, headers);
82 | conn.setDoInput(true);
83 | conn.setDoOutput(true);
84 | StringBuilder body = new StringBuilder();
85 | if (params != null && params.size() > 0) {
86 | boolean isAppendAt = false;
87 | for (Map.Entry entry : params.entrySet()) {
88 | if (isAppendAt) {
89 | body.append("&");
90 | }
91 | body.append(URLFormat.encode(entry.getKey())).append("=").append(URLFormat.encode(entry.getValue()));
92 | isAppendAt = true;
93 | }
94 | }
95 | OutputStream out = conn.getOutputStream();
96 | out.write(body.toString().getBytes("UTF-8"));
97 | out.flush();
98 | out.close();
99 | body.delete(0, body.length());
100 | int responseCode = conn.getResponseCode();
101 | if (responseCode == 200) {
102 | BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
103 | String line;
104 | while ((line = reader.readLine()) != null) {
105 | body.append(line);
106 | }
107 | reader.close();
108 | } else {
109 | body = null;
110 | }
111 | cookie = conn.getHeaderField("Set-Cookie");
112 | if (cookie != null) {
113 | cookieCache.put(host, cookie);
114 | }
115 | conn.disconnect();
116 | return body != null ? body.toString() : null;
117 | }
118 |
119 | public static String postJson(String url, Map headers, String json) throws IOException {
120 | url = URLFormat.getFormatUrl(url, null);
121 | URL _url = new URL(url);
122 | String host = _url.getHost();
123 | HttpURLConnection conn = createHttpURLConnection(_url);
124 | conn.setConnectTimeout(3000);
125 | conn.setRequestMethod("POST");
126 | conn.setRequestProperty("Connection", "Keep-Alive");
127 | conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
128 | conn.setRequestProperty("Accept-Charset", "UTF-8");
129 | String cookie = cookieCache.get(host);
130 | if (cookie != null) {
131 | conn.setRequestProperty("Cookie", cookie);
132 | }
133 | addHeaders(conn, headers);
134 | conn.setDoInput(true);
135 | conn.setDoOutput(true);
136 | OutputStream out = conn.getOutputStream();
137 | out.write(json.getBytes("UTF-8"));
138 | out.flush();
139 | out.close();
140 | StringBuilder body = new StringBuilder();
141 | int responseCode = conn.getResponseCode();
142 | if (responseCode == 200) {
143 | BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
144 | String line;
145 | while ((line = reader.readLine()) != null) {
146 | body.append(line);
147 | }
148 | reader.close();
149 | } else {
150 | body = null;
151 | }
152 | cookie = conn.getHeaderField("Set-Cookie");
153 | if (cookie != null) {
154 | cookieCache.put(host, cookie);
155 | }
156 | conn.disconnect();
157 | return body != null ? body.toString() : null;
158 | }
159 |
160 | /**
161 | * https://developer.android.com/reference/java/net/HttpURLConnection.html
162 | * To upload data to a web server, configure the connection for output using setDoOutput(true).
163 | * For best performance, you should call either setFixedLengthStreamingMode(int) when the body length is known in advance,
164 | * or setChunkedStreamingMode(int) when it is not.
165 | * Otherwise HttpURLConnection will be forced to buffer the complete request body in memory before it is transmitted,
166 | * wasting (and possibly exhausting) heap and increasing latency.
167 | */
168 | public static String upload(String url, Map headers, Map params, Map fileMap, HttpProgress progress) throws IOException {
169 | String boundary = "---------------------------";
170 | byte[] boundarys = boundary.getBytes("UTF-8");
171 | byte[] nextLine = "\r\n".getBytes("UTF-8");
172 | byte[] block = "--".getBytes("UTF-8");
173 | byte[] colon = "\"".getBytes("UTF-8");
174 | byte[] name = "Content-Disposition: form-data; name=".getBytes("UTF-8");
175 | byte[] fileName = "; filename=".getBytes("UTF-8");
176 | byte[] type = "Content-Type: application/octet-stream".getBytes("UTF-8");
177 |
178 | url = URLFormat.getFormatUrl(url, null);
179 | URL _url = new URL(url);
180 | String host = _url.getHost();
181 | HttpURLConnection conn = createHttpURLConnection(_url);
182 | conn.setConnectTimeout(3000);
183 | conn.setDoInput(true);
184 | conn.setDoOutput(true);
185 | conn.setRequestMethod("POST");
186 | String cookie = cookieCache.get(host);
187 | if (cookie != null) {
188 | conn.setRequestProperty("Cookie", cookie);
189 | }
190 | conn.setRequestProperty("Connection", "Keep-Alive");
191 | conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
192 | conn.setRequestProperty("Accept-Charset", "UTF-8");
193 | addHeaders(conn, headers);
194 |
195 | int contentLength = 0;
196 | if (params != null && params.size() > 0) {
197 | for (Map.Entry entry : params.entrySet()) {
198 | String value = entry.getValue();
199 | if (value == null || value.trim().length() == 0) {
200 | continue;
201 | }
202 | byte[] keyBytes = entry.getKey().getBytes("UTF-8");
203 | byte[] valueBytes = value.getBytes("UTF-8");
204 | contentLength += (block.length + boundarys.length + nextLine.length);
205 | contentLength += (name.length + colon.length + keyBytes.length + colon.length + nextLine.length);
206 | contentLength += nextLine.length;
207 | contentLength += valueBytes.length;
208 | contentLength += nextLine.length;
209 | }
210 | }
211 | if (fileMap != null && fileMap.size() > 0) {
212 | for (Map.Entry entry : fileMap.entrySet()) {
213 | File[] files = entry.getValue();
214 | if (files == null || files.length == 0) {
215 | continue;
216 | }
217 | byte[] fieldNameBytes = entry.getKey().getBytes("UTF-8");
218 | for (File file : files) {
219 | byte[] fileNameBytes = file.getName().getBytes("UTF-8");
220 | contentLength += (block.length + boundarys.length + nextLine.length);
221 | contentLength += (name.length + colon.length + fieldNameBytes.length + colon.length + fileName.length + +colon.length + fileNameBytes.length + colon.length + nextLine.length);
222 | contentLength += type.length + nextLine.length;
223 | contentLength += nextLine.length;
224 | contentLength += file.length();
225 | contentLength += nextLine.length;
226 | }
227 | }
228 | }
229 | contentLength += block.length + boundarys.length + block.length;
230 | conn.setFixedLengthStreamingMode(contentLength);
231 |
232 | int writeLength = 0;
233 |
234 | OutputStream out = conn.getOutputStream();
235 | if (params != null && params.size() > 0) {
236 | for (Map.Entry entry : params.entrySet()) {
237 | String value = entry.getValue();
238 | if (value == null || value.trim().length() == 0) {
239 | continue;
240 | }
241 | byte[] keyBytes = entry.getKey().getBytes("UTF-8");
242 | byte[] valueBytes = value.getBytes("UTF-8");
243 | out.write(block);
244 | out.write(boundarys);
245 | out.write(nextLine);
246 | writeLength += (block.length + boundarys.length + nextLine.length);
247 | out.write(name);
248 | out.write(colon);
249 | out.write(keyBytes);
250 | out.write(colon);
251 | out.write(nextLine);
252 | writeLength += (name.length + colon.length + keyBytes.length + colon.length + nextLine.length);
253 | out.write(nextLine);
254 | writeLength += nextLine.length;
255 | out.write(valueBytes);
256 | writeLength += valueBytes.length;
257 | out.write(nextLine);
258 | writeLength += nextLine.length;
259 | out.flush();
260 | progress(contentLength, writeLength, progress);
261 | }
262 | }
263 | byte[] buffer = new byte[10240];
264 | if (fileMap != null && fileMap.size() > 0) {
265 | for (Map.Entry entry : fileMap.entrySet()) {
266 | File[] files = entry.getValue();
267 | if (files == null || files.length == 0) {
268 | continue;
269 | }
270 | byte[] fieldNameBytes = entry.getKey().getBytes("UTF-8");
271 | for (File file : files) {
272 | byte[] fileNameBytes = file.getName().getBytes("UTF-8");
273 | out.write(block);
274 | out.write(boundarys);
275 | out.write(nextLine);
276 | writeLength += (block.length + boundarys.length + nextLine.length);
277 | out.write(name);
278 | out.write(colon);
279 | out.write(fieldNameBytes);
280 | out.write(colon);
281 | out.write(fileName);
282 | out.write(colon);
283 | out.write(fileNameBytes);
284 | out.write(colon);
285 | out.write(nextLine);
286 | writeLength += (name.length + colon.length + fieldNameBytes.length + colon.length + fileName.length + +colon.length + fileNameBytes.length + colon.length + nextLine.length);
287 | out.write(type);
288 | out.write(nextLine);
289 | writeLength += type.length + nextLine.length;
290 | out.write(nextLine);
291 | writeLength += nextLine.length;
292 | progress(contentLength, writeLength, progress);
293 | FileInputStream fis = new FileInputStream(file);
294 | int len;
295 | while ((len = fis.read(buffer)) != -1) {
296 | out.write(buffer, 0, len);
297 | writeLength += len;
298 | progress(contentLength, writeLength, progress);
299 | }
300 | out.write(nextLine);
301 | writeLength += nextLine.length;
302 | out.flush();
303 | fis.close();
304 | progress(contentLength, writeLength, progress);
305 | }
306 | }
307 | }
308 | out.write(block);
309 | out.write(boundarys);
310 | out.write(block);
311 | out.flush();
312 | writeLength += block.length + boundarys.length + block.length;
313 | progress(contentLength, writeLength, progress);
314 |
315 | StringBuilder body = new StringBuilder();
316 | int responseCode = conn.getResponseCode();
317 | if (responseCode == 200) {
318 | BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
319 | String line;
320 | while ((line = reader.readLine()) != null) {
321 | body.append(line);
322 | }
323 | reader.close();
324 | }
325 | cookie = conn.getHeaderField("Set-Cookie");
326 | if (cookie != null) {
327 | cookieCache.put(host, cookie);
328 | }
329 | out.close();
330 | conn.disconnect();
331 | return body.length() > 0 ? body.toString() : null;
332 | }
333 |
334 | public static boolean download(String url, Map headers, int range, InputStreamCallback callback) throws IOException {
335 | url = URLFormat.getFormatUrl(url, null);
336 | URL _url = new URL(url);
337 | String host = _url.getHost();
338 | HttpURLConnection conn = createHttpURLConnection(_url);
339 | conn.setConnectTimeout(3000);
340 | conn.setRequestMethod("GET");
341 | conn.setRequestProperty("Connection", "Keep-Alive");
342 | String cookie = cookieCache.get(host);
343 | if (cookie != null) {
344 | conn.setRequestProperty("Cookie", cookie);
345 | }
346 | if (range > 0) {
347 | conn.addRequestProperty("Range", "bytes=" + range + "-");
348 | }
349 | addHeaders(conn, headers);
350 | int responseCode = conn.getResponseCode();
351 | boolean result = false;
352 | if (responseCode == 200 || responseCode == 206) {
353 | int contentLength = conn.getContentLength() + range;
354 | InputStream stream = conn.getInputStream();
355 | callback.stream(stream, contentLength);
356 | stream.close();
357 | result = true;
358 | }
359 | cookie = conn.getHeaderField("Set-Cookie");
360 | if (cookie != null) {
361 | cookieCache.put(host, cookie);
362 | }
363 | conn.disconnect();
364 | return result;
365 | }
366 |
367 | private static void addHeaders(HttpURLConnection conn, Map headers) {
368 | if (headers == null || headers.size() == 0) {
369 | return;
370 | }
371 | for (Map.Entry entry : headers.entrySet()) {
372 | conn.addRequestProperty(entry.getKey(), entry.getValue());
373 | }
374 | }
375 |
376 | private static void progress(int contentLength, int writeLength, HttpProgress progress) {
377 | Log.e("ESA", String.valueOf(((float) writeLength) / ((float) contentLength)));
378 | if (progress != null) {
379 | progress.progress(contentLength, writeLength);
380 | }
381 | }
382 |
383 | private static HttpURLConnection createHttpURLConnection(URL url) throws IOException {
384 | HttpURLConnection conn;
385 | if (url.toString().startsWith("https")) {
386 | HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
387 | connection.setSSLSocketFactory(getSSLContext().getSocketFactory());
388 | connection.setHostnameVerifier(getHostnameVerifier());
389 | conn = connection;
390 | } else {
391 | conn = (HttpURLConnection) url.openConnection();
392 | }
393 | return conn;
394 | }
395 |
396 | private static SSLContext getSSLContext() {
397 | if (sslcontext == null) {
398 | TrustManager trustManager = new X509TrustManager() {
399 | @Override
400 | public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
401 |
402 | }
403 |
404 | @Override
405 | public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
406 |
407 | }
408 |
409 | @Override
410 | public X509Certificate[] getAcceptedIssuers() {
411 | return null;
412 | }
413 | };
414 | try {
415 | sslcontext = SSLContext.getInstance("TLS");
416 | sslcontext.init(null, new TrustManager[]{trustManager}, new java.security.SecureRandom());
417 | } catch (NoSuchAlgorithmException | KeyManagementException e) {
418 | e.printStackTrace();
419 | }
420 | }
421 | return sslcontext;
422 | }
423 |
424 | private static HostnameVerifier getHostnameVerifier() {
425 | if (hostnameVerifier == null) {
426 | hostnameVerifier = new HostnameVerifier() {
427 | @Override
428 | public boolean verify(String s, SSLSession sslSession) {
429 | return true;
430 | }
431 | };
432 | }
433 | return hostnameVerifier;
434 | }
435 |
436 | }
437 |
--------------------------------------------------------------------------------
/library/src/main/java/android/network/http/HttpProgress.java:
--------------------------------------------------------------------------------
1 | package android.network.http;
2 |
3 | /**
4 | *
5 | */
6 | public interface HttpProgress {
7 |
8 | void progress(int contentLength, int currentLength);
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/library/src/main/java/android/network/http/InputStreamCallback.java:
--------------------------------------------------------------------------------
1 | package android.network.http;
2 |
3 | import java.io.InputStream;
4 |
5 | /**
6 | * @author Mr.Huang
7 | * @date 2017/11/17
8 | */
9 | public interface InputStreamCallback {
10 |
11 | void stream(InputStream stream, int contentLength);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/library/src/main/java/android/network/http/URLFormat.java:
--------------------------------------------------------------------------------
1 | package android.network.http;
2 |
3 | import java.io.UnsupportedEncodingException;
4 | import java.net.URL;
5 | import java.net.URLEncoder;
6 | import java.util.Map;
7 |
8 | /**
9 | * @author Mr.Huang
10 | * @date 2017/11/17
11 | */
12 | public class URLFormat {
13 |
14 | public static String getFormatUrl(String urlString, Map params) {
15 | try {
16 | URL url = new URL(urlString);
17 | StringBuilder sb = new StringBuilder();
18 | sb.append(url.getProtocol()).append("://").append(url.getHost());
19 | int port = url.getPort();
20 | if (port > 0) {
21 | sb.append(":").append(port);
22 | }
23 | String path = fixPathStart(url.getPath());
24 | if (path != null) {
25 | sb.append("/").append(path);
26 | }
27 | fixPathEnd(sb);
28 | String query = url.getQuery();
29 | boolean isQuery = (query != null && query.trim().length() > 0);
30 | if (isQuery) {
31 | sb.append("?");
32 | String[] ps = query.split("&");
33 | int len = ps.length;
34 | for (int i = 0; i < len; i++) {
35 | String p = ps[i];
36 | String[] kv = p.split("=");
37 | if (kv.length == 2) {
38 | if ((i + 1) != len) {
39 | sb.append("&");
40 | }
41 | sb.append(encode(kv[0])).append("=").append(encode(kv[1]));
42 | }
43 | }
44 | }
45 | if (params != null && !params.isEmpty()) {
46 | for (Map.Entry param : params.entrySet()) {
47 | String value = param.getValue();
48 | if (value == null || value.trim().length() == 0) {
49 | continue;
50 | }
51 | String key = param.getKey();
52 | if (isQuery) {
53 | sb.append("&");
54 | } else {
55 | sb.append("?");
56 | isQuery = true;
57 | }
58 | sb.append(encode(key)).append("=").append(encode(value));
59 | }
60 | }
61 | return sb.append(isQuery ? "&" : "?").append("timestamp=").append(System.currentTimeMillis()).toString();
62 | } catch (Exception e) {
63 | e.printStackTrace();
64 | return urlString;
65 | }
66 | }
67 |
68 | private static void fixPathEnd(StringBuilder sb) {
69 | int len = sb.length();
70 | if (sb.charAt(len - 1) == '/') {
71 | sb.deleteCharAt(len - 1);
72 | fixPathEnd(sb);
73 | }
74 | }
75 |
76 | private static String fixPathStart(String path) {
77 | if (path == null) {
78 | return null;
79 | }
80 | path = path.trim();
81 | if (path.length() == 0 || "/".equals(path)) {
82 | return null;
83 | }
84 | if (path.startsWith("/")) {
85 | path = path.substring(1);
86 | } else {
87 | return path;
88 | }
89 | return fixPathStart(path);
90 | }
91 |
92 | public static String encode(String params) {
93 | try {
94 | if (params == null) {
95 | return "";
96 | }
97 | return URLEncoder.encode(params, "UTF-8");
98 | } catch (UnsupportedEncodingException e) {
99 | e.printStackTrace();
100 | return params;
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library', ':socket'
2 |
--------------------------------------------------------------------------------
/socket/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/socket/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion "27.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 15
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | configurations.all{
23 | resolutionStrategy.cacheChangingModulesFor 1, 'seconds'
24 | }
25 |
26 | dependencies {
27 | compile fileTree(include: ['*.jar'], dir: 'libs')
28 | compile 'com.android.support:appcompat-v7:27.0.2'
29 | testCompile 'junit:junit:4.12'
30 | compile 'com.github.spacetimeme:dream-socket:3.0.0'
31 | }
32 |
--------------------------------------------------------------------------------
/socket/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/socket/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
11 |
12 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/socket/src/main/aidl/android/network/binder/remote/IRemoteBinder.aidl:
--------------------------------------------------------------------------------
1 | package android.network.binder.remote;
2 |
3 | import android.network.binder.remote.IRemoteCallback;
4 | import android.network.protocol.Message;
5 |
6 | interface IRemoteBinder {
7 |
8 | boolean register(IRemoteCallback cb);
9 |
10 | boolean unregister(IRemoteCallback cb);
11 |
12 | void login(int uid, String token);
13 |
14 | void logout();
15 |
16 | boolean send(in Message message);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/socket/src/main/aidl/android/network/binder/remote/IRemoteCallback.aidl:
--------------------------------------------------------------------------------
1 | package android.network.binder.remote;
2 |
3 | import android.network.protocol.Message;
4 |
5 | interface IRemoteCallback {
6 |
7 | void onMessage(in Message message);
8 |
9 | void onStatus(int status);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/socket/src/main/aidl/android/network/protocol/Message.aidl:
--------------------------------------------------------------------------------
1 | package android.network.protocol;
2 |
3 | parcelable Message;
4 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/invoke/LoopInvoke.java:
--------------------------------------------------------------------------------
1 | package android.network.invoke;
2 |
3 | import android.os.Handler;
4 |
5 | /**
6 | * @author Mr.Huang
7 | * @date 2017/8/22
8 | */
9 | public abstract class LoopInvoke implements Runnable {
10 |
11 | private static final int LOOP_TIME = 100;
12 | private Handler handler;
13 | private int num;
14 |
15 | public LoopInvoke(Handler handler) {
16 | this.handler = handler;
17 | }
18 |
19 | @Override
20 | public void run() {
21 | if (num < 100 && !invoke() && handler != null) {
22 | handler.postDelayed(this, LOOP_TIME);
23 | }
24 | num++;
25 | }
26 |
27 | public boolean start() {
28 | if (handler != null) {
29 | handler.postDelayed(this, LOOP_TIME);
30 | return true;
31 | }
32 | return false;
33 | }
34 |
35 | protected abstract boolean invoke();
36 | }
37 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/invoke/RemoteBinderInvoke.java:
--------------------------------------------------------------------------------
1 | package android.network.invoke;
2 |
3 | import android.network.binder.remote.IRemoteBinder;
4 | import android.network.binder.remote.IRemoteCallback;
5 | import android.network.protocol.Message;
6 | import android.network.remote.RemoteServiceConnection;
7 | import android.os.Handler;
8 | import android.os.RemoteCallbackList;
9 |
10 | /**
11 | * @author Mr.Huang
12 | * @date 2017/8/21
13 | */
14 | public class RemoteBinderInvoke {
15 |
16 | public static void loopInvokeRegister(Handler handler, final RemoteServiceConnection connection, final IRemoteCallback callback) {
17 | if (!RemoteBinderInvoke.register(connection.getBinder(), callback)) {
18 | new LoopInvoke(handler) {
19 | @Override
20 | protected boolean invoke() {
21 | return RemoteBinderInvoke.register(connection.getBinder(), callback);
22 | }
23 | }.start();
24 | }
25 | }
26 |
27 | public static void loopInvokeLogin(Handler handler, final RemoteServiceConnection connection, final int uid, final String token) {
28 | if (!RemoteBinderInvoke.login(connection.getBinder(), uid, token)) {
29 | new LoopInvoke(handler) {
30 | @Override
31 | protected boolean invoke() {
32 | return RemoteBinderInvoke.login(connection.getBinder(), uid, token);
33 | }
34 | }.start();
35 | }
36 | }
37 |
38 | public static void loopInvokeLogout(Handler handler, final RemoteServiceConnection connection) {
39 | if (!RemoteBinderInvoke.logout(connection.getBinder())) {
40 | new LoopInvoke(handler) {
41 | @Override
42 | protected boolean invoke() {
43 | return RemoteBinderInvoke.logout(connection.getBinder());
44 | }
45 | }.start();
46 | }
47 | }
48 |
49 | public static void loopInvokeSend(Handler handler, final RemoteServiceConnection connection, final Message message) {
50 | if (!RemoteBinderInvoke.send(connection.getBinder(), message)) {
51 | new LoopInvoke(handler) {
52 | @Override
53 | protected boolean invoke() {
54 | return RemoteBinderInvoke.send(connection.getBinder(), message);
55 | }
56 | }.start();
57 | }
58 | }
59 |
60 | private static boolean register(IRemoteBinder remote, IRemoteCallback cb) {
61 | try {
62 | if (remote != null && cb != null) {
63 | return remote.register(cb);
64 | }
65 | } catch (Exception e) {
66 | e.printStackTrace();
67 | }
68 | return false;
69 | }
70 |
71 | private static boolean unregister(IRemoteBinder remote, IRemoteCallback cb) {
72 | try {
73 | if (remote != null && cb != null) {
74 | return remote.unregister(cb);
75 | }
76 | } catch (Exception e) {
77 | e.printStackTrace();
78 | }
79 | return false;
80 | }
81 |
82 | private static boolean login(IRemoteBinder remote, int uid, String token) {
83 | try {
84 | if (remote != null) {
85 | remote.login(uid, token);
86 | return true;
87 | }
88 | } catch (Exception e) {
89 | e.printStackTrace();
90 | }
91 | return false;
92 | }
93 |
94 | private static boolean logout(IRemoteBinder remote) {
95 | try {
96 | if (remote != null) {
97 | remote.logout();
98 | return true;
99 | }
100 | } catch (Exception e) {
101 | e.printStackTrace();
102 | }
103 | return false;
104 | }
105 |
106 | private static boolean send(IRemoteBinder remote, Message message) {
107 | try {
108 | if (remote != null && message != null) {
109 | return remote.send(message);
110 | }
111 | } catch (Exception e) {
112 | e.printStackTrace();
113 | }
114 | return false;
115 | }
116 |
117 | public static void onStatusCallback(RemoteCallbackList callbackList, int status) {
118 | try {
119 | int n = callbackList.beginBroadcast();
120 | for (int i = 0; i < n; i++) {
121 | callbackList.getBroadcastItem(i).onStatus(status);
122 | }
123 | callbackList.finishBroadcast();
124 | } catch (Exception e) {
125 | e.printStackTrace();
126 | }
127 | }
128 |
129 | public static void onMessageCallback(RemoteCallbackList callbackList, Message message) {
130 | try {
131 | int n = callbackList.beginBroadcast();
132 | for (int i = 0; i < n; i++) {
133 | callbackList.getBroadcastItem(i).onMessage(message);
134 | }
135 | callbackList.finishBroadcast();
136 | } catch (Exception e) {
137 | e.printStackTrace();
138 | }
139 | }
140 |
141 | }
142 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/invoke/RemoteCallbackBinder.java:
--------------------------------------------------------------------------------
1 | package android.network.invoke;
2 |
3 | import android.network.binder.remote.IRemoteCallback;
4 | import android.network.protocol.Message;
5 | import android.os.RemoteException;
6 |
7 | /**
8 | *
9 | */
10 | public class RemoteCallbackBinder extends IRemoteCallback.Stub {
11 |
12 | @Override
13 | public void onMessage(Message message) throws RemoteException {
14 |
15 | }
16 |
17 | @Override
18 | public void onStatus(int status) throws RemoteException {
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/model/Status.java:
--------------------------------------------------------------------------------
1 | package android.network.model;
2 |
3 | /**
4 | * @author Mr.Huang
5 | * @date 2017/8/21
6 | */
7 | public interface Status {
8 |
9 | byte FAIL = -1;
10 |
11 | byte CONNECTED = 0;
12 |
13 | byte DISCONNECTED = 1;
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/protocol/Body.java:
--------------------------------------------------------------------------------
1 | package android.network.protocol;
2 |
3 | /**
4 | *
5 | */
6 | public abstract class Body {
7 |
8 | protected abstract int getType();
9 |
10 | protected abstract byte[] toArray();
11 |
12 | protected abstract void source(byte[] body);
13 | }
14 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/protocol/Message.java:
--------------------------------------------------------------------------------
1 | package android.network.protocol;
2 |
3 | import android.network.sdk.body.BodyType;
4 | import android.network.sdk.body.StringBody;
5 | import android.os.Parcel;
6 |
7 | import java.util.UUID;
8 |
9 | /**
10 | * @author Mr.Huang
11 | * @date 2017/11/29
12 | */
13 | public class Message extends com.dream.socket.codec.Message implements android.os.Parcelable, Protocol {
14 |
15 | private String id;
16 | private int type;
17 | private int sender;
18 | private int recipient;
19 | private byte[] body;
20 |
21 | public Message(int sender, Body body) {
22 | this(id(), sender, -1, body);
23 | }
24 |
25 | public Message(int sender, int recipient, Body body) {
26 | this(id(), sender, recipient, body);
27 | }
28 |
29 | public Message(String id, int sender, int recipient, Body body) {
30 | this.id = id;
31 | this.type = body.getType();
32 | this.sender = sender;
33 | this.recipient = recipient;
34 | this.body = body.toArray();
35 | }
36 |
37 | public Message(int type, int sender, int recipient, byte[] body) {
38 | this(id(), type, sender, recipient, body);
39 | }
40 |
41 | public Message(String id, int type, int sender, int recipient, byte[] body) {
42 | this.id = id;
43 | this.type = type;
44 | this.sender = sender;
45 | this.recipient = recipient;
46 | this.body = body;
47 | }
48 |
49 | public final String getId() {
50 | return id;
51 | }
52 |
53 | public final int getType() {
54 | return type;
55 | }
56 |
57 | public final int getSender() {
58 | return sender;
59 | }
60 |
61 | public final int getRecipient() {
62 | return recipient;
63 | }
64 |
65 | public final byte[] getBodyArray() {
66 | if (this.body == null) {
67 | return new byte[0];
68 | }
69 | return body;
70 | }
71 |
72 | private Message(Parcel in) {
73 | id = in.readString();
74 | type = in.readInt();
75 | sender = in.readInt();
76 | recipient = in.readInt();
77 | int length = in.readInt();
78 | if (length > 0) {
79 | byte[] body = new byte[length];
80 | in.readByteArray(body);
81 | this.body = body;
82 | }
83 | }
84 |
85 | public static final Creator CREATOR = new Creator() {
86 | @Override
87 | public Message createFromParcel(Parcel in) {
88 | return new Message(in);
89 | }
90 |
91 | @Override
92 | public Message[] newArray(int size) {
93 | return new Message[size];
94 | }
95 | };
96 |
97 | @Override
98 | public final int describeContents() {
99 | return 0;
100 | }
101 |
102 | @Override
103 | public final void writeToParcel(Parcel parcel, int i) {
104 | parcel.writeString(id);
105 | parcel.writeInt(type);
106 | parcel.writeInt(sender);
107 | parcel.writeInt(recipient);
108 | if (body != null && body.length > 0) {
109 | parcel.writeInt(body.length);
110 | parcel.writeByteArray(body);
111 | }
112 | }
113 |
114 | private static String id() {
115 | return UUID.randomUUID().toString().replace("-", "");
116 | }
117 |
118 | public Body getBody() {
119 | Body body = null;
120 | if (type == 0 || type == BodyType.STRING) {
121 | body = new StringBody();
122 | }
123 | if (body != null) {
124 | body.source(this.body);
125 | }
126 | return body;
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/protocol/Protocol.java:
--------------------------------------------------------------------------------
1 | package android.network.protocol;
2 |
3 | public interface Protocol {
4 |
5 | //{起始标记 -byte - 1}
6 | //{包总长度 -int - 4}
7 | //{包的类型 -int - 4}
8 | //{发送者ID -int - 4}
9 | //{接受者ID -int - 4}
10 | //{消息ID -byte[32] - 32}
11 | //{包体内容 -byte[n] - n}
12 | //{结束标记 -byte - 1}
13 |
14 | /**
15 | * 包头长度(算上头和最后一个结束标记)
16 | */
17 | int HEADER_LENGTH = 50;
18 |
19 | int ID_LENGTH = 32;
20 |
21 | /**
22 | * 起始标记
23 | */
24 | byte START_TAG = '<';
25 |
26 | /**
27 | * 结束标记
28 | */
29 | byte END_TAG = '>';
30 |
31 | }
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/remote/RemoteService.java:
--------------------------------------------------------------------------------
1 | package android.network.remote;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.network.binder.remote.IRemoteBinder;
6 | import android.network.binder.remote.IRemoteCallback;
7 | import android.network.invoke.RemoteBinderInvoke;
8 | import android.network.model.Status;
9 | import android.network.protocol.Message;
10 | import android.network.remote.codec.MessageCodec;
11 | import android.network.remote.logger.DreamSocketLogger;
12 | import android.os.IBinder;
13 | import android.os.RemoteCallbackList;
14 | import android.os.RemoteException;
15 |
16 | import com.dream.socket.DreamSocket;
17 | import com.dream.socket.DreamTCPSocket;
18 | import com.dream.socket.codec.MessageHandle;
19 |
20 | /**
21 | * @author Mr.Huang
22 | * @date 2017/8/16
23 | */
24 | public class RemoteService extends Service {
25 |
26 | private RemoteBinder binder = new RemoteBinder();
27 | private DreamSocket socket;
28 | private int uid = -1;
29 | private String token;
30 |
31 | @Override
32 | public IBinder onBind(Intent intent) {
33 | return binder;
34 | }
35 |
36 | @Override
37 | public int onStartCommand(Intent intent, int flags, int startId) {
38 | if (intent != null) {
39 | int uid = intent.getIntExtra("uid", -1);
40 | if (uid > 0) {
41 | this.uid = uid;
42 | token = intent.getStringExtra("token");
43 | }
44 | }
45 | return START_STICKY;
46 | }
47 |
48 | @Override
49 | public void onCreate() {
50 | socket = new DreamTCPSocket("10.0.2.2", 6969);
51 | socket.setLogger(new DreamSocketLogger());
52 | socket.codec(new MessageCodec());
53 | socket.handle(handle);
54 | }
55 |
56 | private MessageHandle handle = new MessageHandle() {
57 | @Override
58 | public void onStatus(int status) {
59 | if (binder != null) {
60 | binder.onStatusCallback(status);
61 | }
62 | switch (status) {
63 | case Status.CONNECTED:
64 | if (uid > 0) {
65 | socket.send(getLoginBody(uid, token));
66 | }
67 | break;
68 | case Status.DISCONNECTED:
69 | break;
70 | case Status.FAIL:
71 | break;
72 | default:
73 | break;
74 | }
75 | }
76 |
77 | @Override
78 | public void onMessage(Message message) {
79 | if (binder != null) {
80 | binder.onMessageCallback(message);
81 | }
82 | }
83 | };
84 |
85 | @Override
86 | public void onDestroy() {
87 | if (socket != null && socket.isConnected()) {
88 | socket.stop();
89 | }
90 | Intent intent = new Intent(this, RemoteService.class);
91 | if (uid > 0) {
92 | intent.putExtra("uid", uid);
93 | intent.putExtra("token", token);
94 | }
95 | startService(intent);
96 | }
97 |
98 | private final class RemoteBinder extends IRemoteBinder.Stub {
99 |
100 | private final RemoteCallbackList callbackList = new RemoteCallbackList<>();
101 |
102 | @Override
103 | public boolean register(IRemoteCallback cb) throws RemoteException {
104 | return callbackList.register(cb);
105 | }
106 |
107 | @Override
108 | public boolean unregister(IRemoteCallback cb) throws RemoteException {
109 | return callbackList.unregister(cb);
110 | }
111 |
112 | @Override
113 | public void login(int uid, String token) throws RemoteException {
114 | RemoteService.this.uid = uid;
115 | RemoteService.this.token = token;
116 | if (socket != null) {
117 | if (!socket.isConnected()) {
118 | socket.start();
119 | } else {
120 | send(getLoginBody(uid, token));
121 | }
122 | }
123 | }
124 |
125 | @Override
126 | public void logout() throws RemoteException {
127 | uid = -1;
128 | token = null;
129 | if (socket != null && socket.isConnected()) {
130 | socket.stop();
131 | }
132 | }
133 |
134 | @Override
135 | public boolean send(Message message) throws RemoteException {
136 | if (socket != null && socket.isConnected()) {
137 | return socket.send(message);
138 | }
139 | return false;
140 | }
141 |
142 | public void onStatusCallback(int status) {
143 | RemoteBinderInvoke.onStatusCallback(callbackList, status);
144 | }
145 |
146 | public void onMessageCallback(Message message) {
147 | RemoteBinderInvoke.onMessageCallback(callbackList, message);
148 | }
149 | }
150 |
151 | private static Message getLoginBody(int uid, String token) {
152 | return new Message(0, uid, 0, token.getBytes());
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/remote/RemoteServiceConnection.java:
--------------------------------------------------------------------------------
1 | package android.network.remote;
2 |
3 | import android.content.ComponentName;
4 | import android.content.ServiceConnection;
5 | import android.network.binder.remote.IRemoteBinder;
6 | import android.network.binder.remote.IRemoteCallback;
7 | import android.network.invoke.RemoteBinderInvoke;
8 | import android.os.Handler;
9 | import android.os.IBinder;
10 |
11 | /**
12 | *
13 | */
14 | public class RemoteServiceConnection implements ServiceConnection {
15 |
16 | private IRemoteBinder mBinder;
17 | private IRemoteCallback mRemoteCallback;
18 | private Handler mHandler;
19 |
20 | public RemoteServiceConnection(IRemoteCallback callback, Handler handler){
21 | this.mRemoteCallback = callback;
22 | this.mHandler = handler;
23 | }
24 |
25 | @Override
26 | public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
27 | mBinder = IRemoteBinder.Stub.asInterface(iBinder);
28 | RemoteBinderInvoke.loopInvokeRegister(mHandler, this, mRemoteCallback);
29 | }
30 |
31 | @Override
32 | public void onServiceDisconnected(ComponentName componentName) {
33 | mBinder = null;
34 | }
35 |
36 | public IRemoteBinder getBinder() {
37 | return mBinder;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/remote/codec/MessageCodec.java:
--------------------------------------------------------------------------------
1 | package android.network.remote.codec;
2 |
3 | import android.network.protocol.Message;
4 | import android.network.protocol.Protocol;
5 |
6 | import java.net.SocketAddress;
7 | import java.nio.ByteBuffer;
8 |
9 | /**
10 | * @author Mr.Huang
11 | * @date 2017/8/17
12 | */
13 | public class MessageCodec implements com.dream.socket.codec.MessageCodec {
14 |
15 | //{起始标记 -byte - 1}
16 | //{包总长度 -int - 4}
17 | //{包的类型 -int - 4}
18 | //{发送者ID -int - 4}
19 | //{接受者ID -int - 4}
20 | //{消息ID -byte[32] - 32}
21 | //{包体内容 -byte[n] - n}
22 | //{结束标记 -byte - 1}
23 | @Override
24 | public Message decode(SocketAddress address, ByteBuffer buffer) {
25 | int limit = buffer.limit();
26 | if (limit < Protocol.HEADER_LENGTH) {
27 | return null;
28 | }
29 | byte start = buffer.get();
30 | if (start != '<') {
31 | buffer.clear();
32 | return null;
33 | }
34 | int length = buffer.getInt();
35 | if (length > limit) {
36 | return null;
37 | }
38 | int type = buffer.getInt();
39 | int sender = buffer.getInt();
40 | int recipient = buffer.getInt();
41 | byte[] idBytes = new byte[Protocol.ID_LENGTH];
42 | buffer.get(idBytes);
43 | byte[] bodyBytes = new byte[length - Protocol.HEADER_LENGTH];
44 | buffer.get(bodyBytes);
45 | byte end = buffer.get();
46 | if (end != '>') {
47 | buffer.clear();
48 | return null;
49 | }
50 | return new Message(new String(idBytes), type, sender, recipient, bodyBytes);
51 | }
52 |
53 | //{起始标记 -byte - 1}
54 | //{包总长度 -int - 4}
55 | //{包的类型 -int - 4}
56 | //{发送者ID -int - 4}
57 | //{接受者ID -int - 4}
58 | //{消息ID -byte[32] - 32}
59 | //{包体内容 -byte[n] - n}
60 | //{结束标记 -byte - 1}
61 | @Override
62 | public void encode(Message message, ByteBuffer buffer) {
63 | byte[] body = message.getBodyArray();
64 | buffer.put(Protocol.START_TAG);
65 | buffer.putInt(body.length + Protocol.HEADER_LENGTH);
66 | buffer.putInt(message.getType());
67 | buffer.putInt(message.getSender());
68 | buffer.putInt(message.getRecipient());
69 | buffer.put(id(message.getId()));
70 | buffer.put(body);
71 | buffer.put(Protocol.END_TAG);
72 | }
73 |
74 | private static byte[] id(String id) {
75 | if (id != null) {
76 | byte[] bytes = id.getBytes();
77 | if (bytes.length == Protocol.ID_LENGTH) {
78 | return bytes;
79 | }
80 | byte[] idBytes = new byte[Protocol.ID_LENGTH];
81 | if (bytes.length > Protocol.ID_LENGTH) {
82 | System.arraycopy(bytes, 0, idBytes, 0, idBytes.length);
83 | } else {
84 | System.arraycopy(bytes, 0, idBytes, 0, bytes.length);
85 | }
86 | return idBytes;
87 | }
88 | return new byte[Protocol.ID_LENGTH];
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/remote/logger/DreamSocketLogger.java:
--------------------------------------------------------------------------------
1 | package android.network.remote.logger;
2 |
3 | import android.util.Log;
4 |
5 | import com.dream.socket.logger.Logger;
6 |
7 | /**
8 | * @author Mr.Huang
9 | * @date 2017/8/23
10 | */
11 | public class DreamSocketLogger implements Logger{
12 | @Override
13 | public void debug(String log) {
14 | Log.d("ESA", log);
15 | }
16 |
17 | @Override
18 | public void info(String log) {
19 | Log.i("ESA", log);
20 | }
21 |
22 | @Override
23 | public void warn(String log) {
24 | Log.w("ESA", log);
25 | }
26 |
27 | @Override
28 | public void error(String log) {
29 | Log.e("ESA", log);
30 | }
31 |
32 | @Override
33 | public void error(String log, Throwable throwable) {
34 | Log.e("ESA", log, throwable);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/sdk/DreamManager.java:
--------------------------------------------------------------------------------
1 | package android.network.sdk;
2 |
3 | import android.app.Application;
4 | import android.app.Service;
5 | import android.content.Intent;
6 | import android.network.remote.RemoteService;
7 | import android.network.remote.RemoteServiceConnection;
8 | import android.os.Handler;
9 | import android.os.HandlerThread;
10 | import android.os.Looper;
11 |
12 | /**
13 | * @author Mr.Huang
14 | * @date 2017/8/21
15 | */
16 | public class DreamManager {
17 |
18 | private static HandlerThread mHandlerThread = new HandlerThread("invoke remote binder");
19 | private static RemoteServiceConnection mRemoteServiceConnection;
20 | private static Receiver mReceiver;
21 | private static Sender mSender;
22 |
23 | public static void register(Application application) {
24 | mHandlerThread.start();
25 | Handler handler = new Handler(mHandlerThread.getLooper());
26 | Handler mainHandler = new Handler(Looper.getMainLooper());
27 | mReceiver = new Receiver(mainHandler);
28 | mRemoteServiceConnection = new RemoteServiceConnection(mReceiver, handler);
29 | mSender = new Sender(mRemoteServiceConnection, handler);
30 |
31 | Intent service = new Intent(application, RemoteService.class);
32 | application.startService(service);
33 | application.bindService(service, mRemoteServiceConnection, Service.BIND_AUTO_CREATE);
34 | }
35 |
36 | public static void unregister(Application application) {
37 | application.unbindService(mRemoteServiceConnection);
38 | }
39 |
40 | public static Sender getSender() {
41 | return mSender;
42 | }
43 |
44 | public static Receiver getReceiver() {
45 | return mReceiver;
46 | }
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/sdk/Receiver.java:
--------------------------------------------------------------------------------
1 | package android.network.sdk;
2 |
3 | import android.network.binder.remote.IRemoteCallback;
4 | import android.network.protocol.Message;
5 | import android.network.sdk.listener.OnReceiverMessage;
6 | import android.os.Handler;
7 | import android.os.RemoteException;
8 | import android.util.Log;
9 |
10 | import java.util.ArrayList;
11 | import java.util.Iterator;
12 | import java.util.List;
13 |
14 | /**
15 | * @author Mr.Huang
16 | * @date 2017/8/21
17 | */
18 | public class Receiver extends IRemoteCallback.Stub {
19 |
20 | private List receiverMessageList = new ArrayList<>();
21 | private Handler handler;
22 |
23 | public Receiver(Handler handler) {
24 | this.handler = handler;
25 | }
26 |
27 | public void register(OnReceiverMessage receiverMessage) {
28 | receiverMessageList.add(receiverMessage);
29 | }
30 |
31 | public void unregister(OnReceiverMessage receiverMessage) {
32 | receiverMessageList.remove(receiverMessage);
33 | }
34 |
35 | @Override
36 | public final void onMessage(final Message message) throws RemoteException {
37 | handler.post(new Runnable() {
38 | @Override
39 | public void run() {
40 | Iterator iterator = receiverMessageList.iterator();
41 | while (iterator.hasNext()) {
42 | iterator.next().onMessage(message);
43 | }
44 | }
45 | });
46 | }
47 |
48 | @Override
49 | public final void onStatus(final int status) throws RemoteException {
50 | handler.post(new Runnable() {
51 | @Override
52 | public void run() {
53 | Iterator iterator = receiverMessageList.iterator();
54 | while (iterator.hasNext()) {
55 | iterator.next().onStatus(status);
56 | }
57 | }
58 | });
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/sdk/Sender.java:
--------------------------------------------------------------------------------
1 | package android.network.sdk;
2 |
3 | import android.network.invoke.RemoteBinderInvoke;
4 | import android.network.protocol.Message;
5 | import android.network.remote.RemoteServiceConnection;
6 | import android.os.Handler;
7 |
8 | /**
9 | * @author Mr.Huang
10 | * @date 2017/8/21
11 | */
12 | public class Sender {
13 |
14 | private RemoteServiceConnection mConnection;
15 | private Handler mHandler;
16 |
17 | public Sender(RemoteServiceConnection connection, Handler handler) {
18 | this.mConnection = connection;
19 | this.mHandler = handler;
20 | }
21 |
22 | public void login(int uid, String token) {
23 | RemoteBinderInvoke.loopInvokeLogin(mHandler, mConnection, uid, token);
24 | }
25 |
26 | public void logout() {
27 | RemoteBinderInvoke.loopInvokeLogout(mHandler, mConnection);
28 | }
29 |
30 | public void send(Message message) {
31 | RemoteBinderInvoke.loopInvokeSend(mHandler, mConnection, message);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/sdk/body/BodyType.java:
--------------------------------------------------------------------------------
1 | package android.network.sdk.body;
2 |
3 | /**
4 | *
5 | */
6 | public interface BodyType {
7 |
8 | int STRING = 1000;
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/sdk/body/StringBody.java:
--------------------------------------------------------------------------------
1 | package android.network.sdk.body;
2 |
3 | import android.network.protocol.Body;
4 |
5 | /**
6 | * @author Mr.Huang
7 | * @date 2017/12/1
8 | */
9 | public class StringBody extends Body {
10 |
11 | private String string;
12 |
13 | public String getString() {
14 | return string;
15 | }
16 |
17 | public void setString(String string) {
18 | this.string = string;
19 | }
20 |
21 | @Override
22 | public byte[] toArray() {
23 | return string != null ? string.getBytes() : new byte[0];
24 | }
25 |
26 | @Override
27 | protected void source(byte[] body) {
28 | string = new String(body);
29 | }
30 |
31 | @Override
32 | protected int getType() {
33 | return BodyType.STRING;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/socket/src/main/java/android/network/sdk/listener/OnReceiverMessage.java:
--------------------------------------------------------------------------------
1 | package android.network.sdk.listener;
2 |
3 |
4 | import android.network.protocol.Message;
5 |
6 | /**
7 | * @author Mr.Huang
8 | * @date 2017/8/23
9 | */
10 | public interface OnReceiverMessage {
11 |
12 | void onMessage(Message message);
13 |
14 | void onStatus(int status);
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/socket/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | socket
3 |
4 |
--------------------------------------------------------------------------------