|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
1 | 15 | package apijson.demo.ui;
|
2 | 16 |
|
3 | 17 | import android.app.Activity;
|
4 | 18 | import android.app.Application;
|
5 | 19 | import android.content.Context;
|
6 | 20 | import android.content.Intent;
|
| 21 | +import android.content.SharedPreferences; |
7 | 22 | import android.content.pm.ActivityInfo;
|
8 | 23 | import android.content.res.Configuration;
|
9 | 24 | import android.os.Bundle;
|
|
12 | 27 | import android.view.View;
|
13 | 28 | import android.view.Window;
|
14 | 29 | import android.widget.TextView;
|
| 30 | +import android.widget.Toast; |
15 | 31 |
|
16 | 32 | import com.alibaba.fastjson.JSON;
|
17 | 33 | import com.alibaba.fastjson.JSONObject;
|
|
30 | 46 | import apijson.demo.application.DemoApplication;
|
31 | 47 | import apijson.demo.server.MethodUtil;
|
32 | 48 |
|
33 |
| - |
| 49 | +/**自动单元测试,需要用 UnitAuto 发请求到这个设备 |
| 50 | + * https://github.com/TommyLemon/UnitAuto |
| 51 | + * @author Lemon |
| 52 | + */ |
34 | 53 | public class UnitActivity extends Activity implements HttpServerRequestCallback {
|
35 | 54 | private static final String TAG = "UnitActivity";
|
| 55 | + private static final String KEY_PORT = "KEY_PORT"; |
36 | 56 |
|
37 | 57 | /**
|
38 | 58 | * @param context
|
@@ -71,6 +91,10 @@ protected void onCreate(Bundle savedInstanceState) {
|
71 | 91 | etUnitPort = findViewById(R.id.etUnitPort);
|
72 | 92 | pbUnit = findViewById(R.id.pbUnit);
|
73 | 93 |
|
| 94 | + |
| 95 | + SharedPreferences sp = getSharedPreferences(TAG, Context.MODE_PRIVATE); |
| 96 | + port = sp.getString(KEY_PORT, ""); |
| 97 | + |
74 | 98 | etUnitPort.setText(port);
|
75 | 99 | pbUnit.setVisibility(View.GONE);
|
76 | 100 |
|
@@ -113,21 +137,33 @@ public void orient(View v) {
|
113 | 137 | private String port = "8080";
|
114 | 138 | public void start(View v) {
|
115 | 139 | v.setEnabled(false);
|
116 |
| - port = StringUtil.getString(etUnitPort); |
117 |
| - startServer(Integer.valueOf(port)); |
| 140 | + port = StringUtil.getTrimedString(etUnitPort); |
| 141 | + |
| 142 | + try { |
| 143 | + startServer(StringUtil.isEmpty(port, true) ? 8080 : Integer.valueOf(port)); |
118 | 144 |
|
| 145 | + etUnitPort.setEnabled(false); |
| 146 | + pbUnit.setVisibility(View.VISIBLE); |
| 147 | + |
| 148 | + Toast.makeText(context, R.string.please_send_request_with_unit_auto, Toast.LENGTH_LONG).show(); |
| 149 | + } catch (Exception e) { // FIXME 端口异常 catch 不到 |
| 150 | + Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show(); |
| 151 | + } |
119 | 152 | v.setEnabled(true);
|
120 |
| - etUnitPort.setEnabled(false); |
121 |
| - pbUnit.setVisibility(View.VISIBLE); |
122 | 153 | }
|
123 | 154 | public void stop(View v) {
|
124 | 155 | v.setEnabled(false);
|
125 |
| - server.stop(); |
126 |
| - mAsyncServer.stop(); |
127 | 156 |
|
| 157 | + try { |
| 158 | + server.stop(); |
| 159 | + mAsyncServer.stop(); |
| 160 | + |
| 161 | + etUnitPort.setEnabled(true); |
| 162 | + pbUnit.setVisibility(View.GONE); |
| 163 | + } catch (Exception e) { |
| 164 | + Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show(); |
| 165 | + } |
128 | 166 | v.setEnabled(true);
|
129 |
| - etUnitPort.setEnabled(true); |
130 |
| - pbUnit.setVisibility(View.GONE); |
131 | 167 | }
|
132 | 168 |
|
133 | 169 |
|
@@ -268,6 +304,9 @@ public void run() {
|
268 | 304 | protected void onDestroy() {
|
269 | 305 | isAlive = false;
|
270 | 306 | stop(etUnitPort);
|
| 307 | + |
| 308 | + getSharedPreferences(TAG, Context.MODE_PRIVATE).edit().remove(KEY_PORT).putString(KEY_PORT, port).apply(); |
| 309 | + |
271 | 310 | super.onDestroy();
|
272 | 311 | }
|
273 | 312 |
|
|
0 commit comments