From 23936a00e8c9f655e208ef2d7881bd8b59f6b002 Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Sun, 16 Aug 2020 17:47:32 +0800 Subject: [PATCH 0001/1250] =?UTF-8?q?Android=EF=BC=9AAPIJSONTest=20?= =?UTF-8?q?=E4=B8=AD=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8A=BD=E5=8F=96=E4=B8=BA=20UnitAuto-Apk=EF=BC=9B?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96UI=E6=B5=8B=E8=AF=95=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E5=9B=9E=E6=94=BE=E5=B4=A9=E6=BA=83=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../APIJSONTest/UnitAuto-Apk/.gitignore | 1 + .../APIJSONTest/UnitAuto-Apk/build.gradle | 28 + .../UnitAuto-Apk/proguard-rules.pro | 23 + .../UnitAuto-Apk/src/main/AndroidManifest.xml | 11 + .../src/main/java/unitauto/JSON.java | 299 ++++++ .../src/main/java/unitauto/Log.java | 74 ++ .../src/main/java/unitauto}/MethodUtil.java | 22 +- .../src/main/java/unitauto/StringUtil.java | 865 ++++++++++++++++++ .../src/main/java/unitauto/apk}/IPUtil.java | 2 +- .../main/java/unitauto/apk}/MethodUtil.java | 37 +- .../java/unitauto/apk}/UnitAutoActivity.java | 46 +- .../main/java/unitauto/apk/UnitAutoApp.java | 113 +++ .../main/res/layout/unit_auto_activity.xml | 10 +- .../src/main/res/values-en/strings.xml | 11 + .../src/main/res/values/dimens.xml | 7 + .../src/main/res/values/strings.xml | 13 + APIJSON-Android/APIJSONTest/app/build.gradle | 3 +- .../java/apijson/demo/ApplicationTest.java | 13 - .../app/src/main/AndroidManifest.xml | 27 +- .../main/java/apijson/demo/StringUtil.java | 19 +- .../demo/application/DemoApplication.java | 29 +- .../java/apijson/demo/ui/SelectActivity.java | 1 + .../apijson/demo/ui/UIAutoListActivity.java | 38 +- .../app/src/main/res/values-en/strings.xml | 8 - .../app/src/main/res/values/strings.xml | 8 - .../java/apijson/demo/ExampleUnitTest.java | 15 - APIJSON-Android/APIJSONTest/settings.gradle | 2 +- 27 files changed, 1596 insertions(+), 129 deletions(-) create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/.gitignore create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/build.gradle create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/proguard-rules.pro create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/AndroidManifest.xml create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/JSON.java create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/Log.java rename APIJSON-Android/APIJSONTest/{app/src/main/java/apijson/demo/server => UnitAuto-Apk/src/main/java/unitauto}/MethodUtil.java (98%) create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/StringUtil.java rename APIJSON-Android/APIJSONTest/{app/src/main/java/apijson/demo => UnitAuto-Apk/src/main/java/unitauto/apk}/IPUtil.java (99%) rename APIJSON-Android/APIJSONTest/{app/src/main/java/apijson/demo => UnitAuto-Apk/src/main/java/unitauto/apk}/MethodUtil.java (65%) rename APIJSON-Android/APIJSONTest/{app/src/main/java/apijson/demo/ui => UnitAuto-Apk/src/main/java/unitauto/apk}/UnitAutoActivity.java (87%) create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoApp.java rename APIJSON-Android/APIJSONTest/{app => UnitAuto-Apk}/src/main/res/layout/unit_auto_activity.xml (92%) create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/res/values-en/strings.xml create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/res/values/dimens.xml create mode 100644 APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/res/values/strings.xml delete mode 100644 APIJSON-Android/APIJSONTest/app/src/androidTest/java/apijson/demo/ApplicationTest.java delete mode 100644 APIJSON-Android/APIJSONTest/app/src/test/java/apijson/demo/ExampleUnitTest.java diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/.gitignore b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/.gitignore new file mode 100644 index 000000000..796b96d1c --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/.gitignore @@ -0,0 +1 @@ +/build diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/build.gradle b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/build.gradle new file mode 100644 index 000000000..a2b4dc169 --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 29 + defaultConfig { + minSdkVersion 21 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + compileOptions { + targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility JavaVersion.VERSION_1_8 + } + buildToolsVersion '29.0.0' +} + +dependencies { + implementation fileTree(include: ['*.jar'], dir: 'libs') + compile 'com.alibaba:fastjson:1.2.61' + compile 'com.koushikdutta.async:androidasync:2.+' +} diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/proguard-rules.pro b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/proguard-rules.pro new file mode 100644 index 000000000..1d9b5ede3 --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/proguard-rules.pro @@ -0,0 +1,23 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile + +-keep public class unitauto.** \ No newline at end of file diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/AndroidManifest.xml b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/AndroidManifest.xml new file mode 100644 index 000000000..54a3c6a2c --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/AndroidManifest.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/JSON.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/JSON.java new file mode 100644 index 000000000..353014062 --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/JSON.java @@ -0,0 +1,299 @@ +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License.*/ + +package unitauto; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.parser.Feature; +import com.alibaba.fastjson.serializer.SerializerFeature; + +import java.util.List; + + +/**阿里FastJSON封装类 防止解析时异常 + * @author Lemon + */ +public class JSON { + private static final String TAG = "JSON"; + + /**判断json格式是否正确 + * @param s + * @return + */ + public static boolean isJsonCorrect(String s) { + //太长 Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>"); + if (s == null + // || s.equals("[]") + // || s.equals("{}") + || s.equals("") + || s.equals("[null]") + || s.equals("{null}") + || s.equals("null")) { + return false; + } + return true; + } + + /**获取有效的json + * @param s + * @return + */ + public static String getCorrectJson(String s) { + return getCorrectJson(s, false); + } + /**获取有效的json + * @param s + * @param isArray + * @return + */ + public static String getCorrectJson(String s, boolean isArray) { + s = StringUtil.getTrimedString(s); + // if (isArray) { + // while (s.startsWith("\"")) { + // s = s.substring(1); + // } + // while (s.endsWith("\"")) { + // s = s.substring(0, s.length() - 1); + // } + // } + return s;//isJsonCorrect(s) ? s : null; + } + + /** + * @param obj + * @return + */ + public static Object parse(Object obj) { + int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE; + features |= Feature.OrderedField.getMask(); + try { + return com.alibaba.fastjson.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), features); + } catch (Exception e) { + Log.i(TAG, "parse catch \n" + e.getMessage()); + } + return null; + } + /**obj转JSONObject + * @param obj + * @return + */ + public static JSONObject parseObject(Object obj) { + if (obj instanceof JSONObject) { + return (JSONObject) obj; + } + return parseObject(toJSONString(obj)); + } + /**json转JSONObject + * @param json + * @return + */ + public static JSONObject parseObject(String json) { + int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE; + features |= Feature.OrderedField.getMask(); + return parseObject(json, features); + } + /**json转JSONObject + * @param json + * @param features + * @return + */ + public static JSONObject parseObject(String json, int features) { + try { + return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features); + } catch (Exception e) { + Log.i(TAG, "parseObject catch \n" + e.getMessage()); + } + return null; + } + + /**JSONObject转实体类 + * @param object + * @param clazz + * @return + */ + public static T parseObject(JSONObject object, Class clazz) { + return parseObject(toJSONString(object), clazz); + } + /**json转实体类 + * @param json + * @param clazz + * @return + */ + public static T parseObject(String json, Class clazz) { + if (clazz == null) { + Log.e(TAG, "parseObject clazz == null >> return null;"); + } else { + try { + int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE; + features |= Feature.OrderedField.getMask(); + return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features); + } catch (Exception e) { + Log.i(TAG, "parseObject catch \n" + e.getMessage()); + } + } + return null; + } + + /**list转JSONArray + * @param list + * @return + */ + public static JSONArray parseArray(List list) { + return new JSONArray(list); + } + /**obj转JSONArray + * @param obj + * @return + */ + public static JSONArray parseArray(Object obj) { + if (obj instanceof JSONArray) { + return (JSONArray) obj; + } + return parseArray(toJSONString(obj)); + } + /**json转JSONArray + * @param json + * @return + */ + public static JSONArray parseArray(String json) { + try { + return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true)); + } catch (Exception e) { + Log.i(TAG, "parseArray catch \n" + e.getMessage()); + } + return null; + } + /**JSONArray转实体类列表 + * @param array + * @param clazz + * @return + */ + public static List parseArray(JSONArray array, Class clazz) { + return parseArray(toJSONString(array), clazz); + } + /**json转实体类列表 + * @param json + * @param clazz + * @return + */ + public static List parseArray(String json, Class clazz) { + if (clazz == null) { + Log.e(TAG, "parseArray clazz == null >> return null;"); + } else { + try { + return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz); + } catch (Exception e) { + Log.i(TAG, "parseArray catch \n" + e.getMessage()); + } + } + return null; + } + + /**实体类转json + * @param obj + * @return + */ + public static String toJSONString(Object obj) { + if (obj instanceof String) { + return (String) obj; + } + try { + return com.alibaba.fastjson.JSON.toJSONString(obj); + } catch (Exception e) { + Log.e(TAG, "toJSONString catch \n" + e.getMessage()); + } + return null; + } + + /**实体类转json + * @param obj + * @param features + * @return + */ + public static String toJSONString(Object obj, SerializerFeature... features) { + if (obj instanceof String) { + return (String) obj; + } + try { + return com.alibaba.fastjson.JSON.toJSONString(obj, features); + } catch (Exception e) { + Log.e(TAG, "parseArray catch \n" + e.getMessage()); + } + return null; + } + + /**格式化,显示更好看 + * @param json + * @return + */ + public static String format(String json) { + return format(parse(json)); + } + /**格式化,显示更好看 + * @param object + * @return + */ + public static String format(Object object) { + return toJSONString(object, SerializerFeature.PrettyFormat); + } + + /**判断是否为JSONObject + * @param obj instanceof String ? parseObject + * @return + */ + public static boolean isJSONObject(Object obj) { + if (obj instanceof JSONObject) { + return true; + } + if (obj instanceof String) { + try { + JSONObject json = parseObject((String) obj); + return json != null && json.isEmpty() == false; + } catch (Exception e) { + Log.e(TAG, "isJSONObject catch \n" + e.getMessage()); + } + } + + return false; + } + /**判断是否为JSONArray + * @param obj instanceof String ? parseArray + * @return + */ + public static boolean isJSONArray(Object obj) { + if (obj instanceof JSONArray) { + return true; + } + if (obj instanceof String) { + try { + JSONArray json = parseArray((String) obj); + return json != null && json.isEmpty() == false; + } catch (Exception e) { + Log.e(TAG, "isJSONArray catch \n" + e.getMessage()); + } + } + + return false; + } + + /**判断是否为 Boolean,Number,String 中的一种 + * @param obj + * @return + */ + public static boolean isBooleanOrNumberOrString(Object obj) { + return obj instanceof Boolean || obj instanceof Number || obj instanceof String; + } + +} diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/Log.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/Log.java new file mode 100644 index 000000000..96cf7bc22 --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/Log.java @@ -0,0 +1,74 @@ +/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License.*/ + +package unitauto; + +/**测试用Log + * @modifier Lemon + */ +public class Log { + + public static boolean DEBUG = true; + + /** + * @param TAG + * @param msg + */ + public static void d(String TAG, String msg) { + if (DEBUG) { + System.err.println(TAG + ".DEBUG: " + msg); + } + } + + /** + * @param TAG + * @param msg + */ + public static void v(String TAG, String msg) { + if (DEBUG) { + System.out.println(TAG + ".VERBOSE: " + msg); + } + } + + /** + * @param TAG + * @param msg + */ + public static void i(String TAG, String msg) { + if (DEBUG) { + System.out.println(TAG + ".INFO: " + msg); + } + } + + /** + * @param TAG + * @param msg + */ + public static void e(String TAG, String msg) { + if (DEBUG) { + System.err.println(TAG + ".ERROR: " + msg); + } + } + + /** + * @param TAG + * @param msg + */ + public static void w(String TAG, String msg) { + if (DEBUG) { + System.err.println(TAG + ".WARN: " + msg); + } + } + +} diff --git a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/server/MethodUtil.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/MethodUtil.java similarity index 98% rename from APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/server/MethodUtil.java rename to APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/MethodUtil.java index 88194d5fb..33a039c51 100644 --- a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/server/MethodUtil.java +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/MethodUtil.java @@ -1,4 +1,18 @@ -package apijson.demo.server; +/*Copyright ©2020 TommyLemon(https://github.com/TommyLemon) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License.*/ + +package unitauto; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; @@ -30,8 +44,6 @@ import java.util.Objects; import java.util.Set; -import zuo.biao.apijson.StringUtil; - import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.CONSTRUCTOR; import static java.lang.annotation.ElementType.FIELD; @@ -39,6 +51,10 @@ import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/**方法/函数的工具类 + * @author Lemon + */ public class MethodUtil { diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/StringUtil.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/StringUtil.java new file mode 100644 index 000000000..607c171af --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/StringUtil.java @@ -0,0 +1,865 @@ +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License.*/ + +package unitauto; + +import java.io.File; +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.util.regex.Pattern; + +/**通用字符串(String)相关类,为null时返回"" + * @author Lemon + * @use StringUtil. + */ +public class StringUtil { + private static final String TAG = "StringUtil"; + + public StringUtil() { + } + + public static final String UTF_8 = "utf-8"; + + public static final String EMPTY = "无"; + public static final String UNKNOWN = "未知"; + public static final String UNLIMITED = "不限"; + + public static final String I = "我"; + public static final String YOU = "你"; + public static final String HE = "他"; + public static final String SHE = "她"; + public static final String IT = "它"; + + public static final String MALE = "男"; + public static final String FEMALE = "女"; + + public static final String TODO = "未完成"; + public static final String DONE = "已完成"; + + public static final String FAIL = "失败"; + public static final String SUCCESS = "成功"; + + public static final String SUNDAY = "日"; + public static final String MONDAY = "一"; + public static final String TUESDAY = "二"; + public static final String WEDNESDAY = "三"; + public static final String THURSDAY = "四"; + public static final String FRIDAY = "五"; + public static final String SATURDAY = "六"; + + public static final String YUAN = "元"; + + + private static String currentString = ""; + /**获取刚传入处理后的string + * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 + * @return + */ + public static String getCurrentString() { + return currentString == null ? "" : currentString; + } + + //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**获取string,为null则返回"" + * @param object + * @return + */ + public static String getString(Object object) { + return object == null ? "" : object.toString(); + } + /**获取string,为null则返回"" + * @param cs + * @return + */ + public static String getString(CharSequence cs) { + return cs == null ? "" : cs.toString(); + } + /**获取string,为null则返回"" + * @param s + * @return + */ + public static String getString(String s) { + return s == null ? "" : s; + } + /**获取string,为null则返回"" + * ignoreEmptyItem = false; + * split = "," + * @param array + * @return {@link #getString(Object[], boolean)} + */ + public static String getString(Object[] array) { + return getString(array, false); + } + /**获取string,为null则返回"" + * split = "," + * @param array + * @param ignoreEmptyItem + * @return {@link #getString(Object[], boolean)} + */ + public static String getString(Object[] array, boolean ignoreEmptyItem) { + return getString(array, null, ignoreEmptyItem); + } + /**获取string,为null则返回"" + * ignoreEmptyItem = false; + * @param array + * @param split + * @return {@link #getString(Object[], String, boolean)} + */ + public static String getString(Object[] array, String split) { + return getString(array, split, false); + } + /**获取string,为null则返回"" + * @param array + * @param split + * @param ignoreEmptyItem + * @return + */ + public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { + String s = ""; + if (array != null) { + if (split == null) { + split = ","; + } + for (int i = 0; i < array.length; i++) { + if (ignoreEmptyItem && isEmpty(array[i], true)) { + continue; + } + s += ((i > 0 ? split : "") + array[i]); + } + } + return getString(s); + } + + //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**获取去掉前后空格后的string,为null则返回"" + * @param object + * @return + */ + public static String getTrimedString(Object object) { + return getTrimedString(getString(object)); + } + /**获取去掉前后空格后的string,为null则返回"" + * @param cs + * @return + */ + public static String getTrimedString(CharSequence cs) { + return getTrimedString(getString(cs)); + } + /**获取去掉前后空格后的string,为null则返回"" + * @param s + * @return + */ + public static String getTrimedString(String s) { + return getString(s).trim(); + } + + //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + + //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**获取去掉所有空格后的string,为null则返回"" + * @param object + * @return + */ + public static String getNoBlankString(Object object) { + return getNoBlankString(getString(object)); + } + /**获取去掉所有空格后的string,为null则返回"" + * @param cs + * @return + */ + public static String getNoBlankString(CharSequence cs) { + return getNoBlankString(getString(cs)); + } + /**获取去掉所有空格后的string,为null则返回"" + * @param s + * @return + */ + public static String getNoBlankString(String s) { + return getString(s).replaceAll("\\s", ""); + } + + //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + + //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**获取string的长度,为null则返回0 + * @param object + * @param trim + * @return + */ + public static int getLength(Object object, boolean trim) { + return getLength(getString(object), trim); + } + /**获取string的长度,为null则返回0 + * @param cs + * @param trim + * @return + */ + public static int getLength(CharSequence cs, boolean trim) { + return getLength(getString(cs), trim); + } + /**获取string的长度,为null则返回0 + * @param s + * @param trim + * @return + */ + public static int getLength(String s, boolean trim) { + s = trim ? getTrimedString(s) : s; + return getString(s).length(); + } + + //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + + //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**判断字符是否为空 + * @param object + * @param trim + * @return + */ + public static boolean isEmpty(Object object, boolean trim) { + return isEmpty(getString(object), trim); + } + /**判断字符是否为空 + * @param cs + * @param trim + * @return + */ + public static boolean isEmpty(CharSequence cs, boolean trim) { + return isEmpty(getString(cs), trim); + } + /**判断字符是否为空 + * @param s + * @param trim + * @return + */ + public static boolean isEmpty(String s, boolean trim) { + // Log.i(TAG, "getTrimedString s = " + s); + if (s == null) { + return true; + } + if (trim) { + s = s.trim(); + } + if (s.isEmpty()) { + return true; + } + + currentString = s; + + return false; + } + + //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**判断字符是否非空 + * @param object + * @param trim + * @return + */ + public static boolean isNotEmpty(Object object, boolean trim) { + return isNotEmpty(getString(object), trim); + } + /**判断字符是否非空 + * @param cs + * @param trim + * @return + */ + public static boolean isNotEmpty(CharSequence cs, boolean trim) { + return isNotEmpty(getString(cs), trim); + } + /**判断字符是否非空 + * @param s + * @param trim + * @return + */ + public static boolean isNotEmpty(String s, boolean trim) { + return ! isEmpty(s, trim); + } + + //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + + //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + public static final Pattern PATTERN_NUMBER; + public static final Pattern PATTERN_PHONE; + public static final Pattern PATTERN_EMAIL; + public static final Pattern PATTERN_ID_CARD; + public static final Pattern PATTERN_ALPHA; + public static final Pattern PATTERN_PASSWORD; //TODO + public static final Pattern PATTERN_NAME; + public static final Pattern PATTERN_ALPHA_BIG; + public static final Pattern PATTERN_ALPHA_SMALL; + static { + PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); + PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); + PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); + PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); + PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_]+$");//已用55个中英字符测试通过 + PATTERN_PHONE = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-2,5-9])|(17[0-9]))\\d{8}$"); + PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); + PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); + PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); + } + + /**判断手机格式是否正确 + * @param phone + * @return + */ + public static boolean isPhone(String phone) { + if (isNotEmpty(phone, true) == false) { + return false; + } + + currentString = phone; + return PATTERN_PHONE.matcher(phone).matches(); + } + /**判断手机格式是否正确 + * @param s + * @return + */ + public static boolean isPassword(String s) { + return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); + } + /**判断是否全是数字密码 + * @param s + * @return + */ + public static boolean isNumberPassword(String s) { + return getLength(s, false) == 6 && isNumer(s); + } + /**判断email格式是否正确 + * @param email + * @return + */ + public static boolean isEmail(String email) { + if (isNotEmpty(email, true) == false) { + return false; + } + + currentString = email; + return PATTERN_EMAIL.matcher(email).matches(); + } + + + /**判断是否全是验证码 + * @param s + * @return + */ + public static boolean isVerify(String s) { + return getLength(s, false) >= 4 && isNumer(s); + } + /**判断是否全是数字 + * @param s + * @return + */ + public static boolean isNumer(String s) { + if (isNotEmpty(s, true) == false) { + return false; + } + + currentString = s; + return PATTERN_NUMBER.matcher(s).matches(); + } + /**判断是否全是字母 + * @param s + * @return + */ + public static boolean isAlpha(String s) { + if (isEmpty(s, true)) { + return false; + } + + currentString = s; + return PATTERN_ALPHA.matcher(s).matches(); + } + /**判断是否全是数字或字母 + * @param s + * @return + */ + public static boolean isNumberOrAlpha(String s) { + return isNumer(s) || isAlpha(s); + } + + /**判断是否为代码名称,只能包含字母,数字或下划线 + * @param s + * @return + */ + public static boolean isName(String s) { + return s != null && PATTERN_NAME.matcher(s).matches(); + } + /**判断是否为首字母大写的代码名称 + * @param s + * @return + */ + public static boolean isBigName(String s) { + s = getString(s); + if (s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { + return false; + } + return s.length() <= 1 ? true : isName(s.substring(1)); + } + /**判断是否为首字母小写的代码名称 + * @param s + * @return + */ + public static boolean isSmallName(String s) { + s = getString(s); + if (s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { + return false; + } + return s.length() <= 1 ? true : isName(s.substring(1)); + } + + + /**判断字符类型是否是身份证号 + * @param number + * @return + */ + public static boolean isIDCard(String number) { + if (isNumberOrAlpha(number) == false) { + return false; + } + number = getString(number); + if (number.length() == 15) { + Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); + currentString = number; + return true; + } + if (number.length() == 18) { + currentString = number; + return true; + } + + return false; + } + + public static final String HTTP = "http"; + public static final String URL_PREFIX = "http://"; + public static final String URL_PREFIXs = "https://"; + public static final String URL_STAFFIX = URL_PREFIX; + public static final String URL_STAFFIXs = URL_PREFIXs; + /**判断字符类型是否是网址 + * @param url + * @return + */ + public static boolean isUrl(String url) { + if (isNotEmpty(url, true) == false) { + return false; + } else if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { + return false; + } + + currentString = url; + return true; + } + + public static final String FILE_PATH_PREFIX = "file://"; + /**判断文件路径是否存在 + * @param path + * @return + */ + public static boolean isFilePathExist(String path) { + return StringUtil.isFilePath(path) && new File(path).exists(); + } + + public static final String SEPARATOR = "/"; + /**判断是否为路径 + * @param path + * @return + */ + public static boolean isPath(String path) { + return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) + && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; + } + + /**判断字符类型是否是路径 + * @param path + * @return + */ + public static boolean isFilePath(String path) { + if (isNotEmpty(path, true) == false) { + return false; + } + + if (! path.contains(".") || path.endsWith(".")) { + return false; + } + + currentString = path; + + return true; + } + + //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + + //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**去掉string内所有非数字类型字符 + * @param object + * @return + */ + public static String getNumber(Object object) { + return getNumber(getString(object)); + } + /**去掉string内所有非数字类型字符 + * @param cs + * @return + */ + public static String getNumber(CharSequence cs) { + return getNumber(getString(cs)); + } + /**去掉string内所有非数字类型字符 + * @param s + * @return + */ + public static String getNumber(String s) { + return getNumber(s, false); + } + /**去掉string内所有非数字类型字符 + * @param s + * @param onlyStart 中间有非数字时只获取前面的数字 + * @return + */ + public static String getNumber(String s, boolean onlyStart) { + if (isNotEmpty(s, true) == false) { + return ""; + } + + String numberString = ""; + String single; + for (int i = 0; i < s.length(); i++) { + single = s.substring(i, i + 1); + if (isNumer(single)) { + numberString += single; + } else { + if (onlyStart) { + return numberString; + } + } + } + + return numberString; + } + + //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + /**获取网址,自动补全 + * @param url + * @return + */ + public static String getCorrectUrl(String url) { + Log.i(TAG, "getCorrectUrl : \n" + url); + if (isNotEmpty(url, true) == false) { + return ""; + } + + // if (! url.endsWith("/") && ! url.endsWith(".html")) { + // url = url + "/"; + // } + + if (isUrl(url) == false) { + return URL_PREFIX + url; + } + return url; + } + + /**获取去掉所有 空格 、"-" 、"+86" 后的phone + * @param phone + * @return + */ + public static String getCorrectPhone(String phone) { + if (isNotEmpty(phone, true) == false) { + return ""; + } + + phone = getNoBlankString(phone); + phone = phone.replaceAll("-", ""); + if (phone.startsWith("+86")) { + phone = phone.substring(3); + } + return phone; + } + + + /**获取邮箱,自动补全 + * @param email + * @return + */ + public static String getCorrectEmail(String email) { + if (isNotEmpty(email, true) == false) { + return ""; + } + + email = getNoBlankString(email); + if (isEmail(email) == false && ! email.endsWith(".com")) { + email += ".com"; + } + + return email; + } + + + public static final int PRICE_FORMAT_DEFAULT = 0; + public static final int PRICE_FORMAT_PREFIX = 1; + public static final int PRICE_FORMAT_SUFFIX = 2; + public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; + public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; + public static final String[] PRICE_FORMATS = { + "", "¥", "元", "¥ ", " 元" + }; + + /**获取价格,保留两位小数 + * @param price + * @return + */ + public static String getPrice(String price) { + return getPrice(price, PRICE_FORMAT_DEFAULT); + } + /**获取价格,保留两位小数 + * @param price + * @param formatType 添加单位(元) + * @return + */ + public static String getPrice(String price, int formatType) { + if (isNotEmpty(price, true) == false) { + return getPrice(0, formatType); + } + + //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< + String correctPrice = ""; + String s; + for (int i = 0; i < price.length(); i++) { + s = price.substring(i, i + 1); + if (".".equals(s) || isNumer(s)) { + correctPrice += s; + } + } + //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> + + Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); + if (correctPrice.contains(".")) { + // if (correctPrice.startsWith(".")) { + // correctPrice = 0 + correctPrice; + // } + if (correctPrice.endsWith(".")) { + correctPrice = correctPrice.replaceAll(".", ""); + } + } + + Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); + return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); + } + /**获取价格,保留两位小数 + * @param price + * @return + */ + public static String getPrice(BigDecimal price) { + return getPrice(price, PRICE_FORMAT_DEFAULT); + } + /**获取价格,保留两位小数 + * @param price + * @return + */ + public static String getPrice(double price) { + return getPrice(price, PRICE_FORMAT_DEFAULT); + } + /**获取价格,保留两位小数 + * @param price + * @param formatType 添加单位(元) + * @return + */ + public static String getPrice(BigDecimal price, int formatType) { + return getPrice(price == null ? 0 : price.doubleValue(), formatType); + } + /**获取价格,保留两位小数 + * @param price + * @param formatType 添加单位(元) + * @return + */ + public static String getPrice(double price, int formatType) { + String s = new DecimalFormat("#########0.00").format(price); + switch (formatType) { + case PRICE_FORMAT_PREFIX: + return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; + case PRICE_FORMAT_SUFFIX: + return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; + case PRICE_FORMAT_PREFIX_WITH_BLANK: + return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; + case PRICE_FORMAT_SUFFIX_WITH_BLANK: + return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; + default: + return s; + } + } + + + /**分割路径 + * @param path + * @return + */ + public static String[] splitPath(String path) { + if (StringUtil.isNotEmpty(path, true) == false) { + return null; + } + return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; + } + /**将s分割成String[] + * @param s + * @return + */ + public static String[] split(String s) { + return split(s, null); + } + /**将s用split分割成String[] + * trim = true; + * @param s + * @param split + * @return + */ + public static String[] split(String s, String split) { + return split(s, split, true); + } + /**将s用split分割成String[] + * @param s + * @param split + * @param trim 去掉前后两端的split + * @return + */ + public static String[] split(String s, String split, boolean trim) { + s = getString(s); + if (s.isEmpty()) { + return null; + } + if (isNotEmpty(split, false) == false) { + split = ","; + } + if (trim) { + while (s.startsWith(split)) { + s = s.substring(split.length()); + } + while (s.endsWith(split)) { + s = s.substring(0, s.length() - split.length()); + } + } + return s.contains(split) ? s.split(split) : new String[]{s}; + } + + /** + * @param key + * @param suffix + * @return key + suffix,第一个字母小写 + */ + public static String addSuffix(String key, String suffix) { + key = getNoBlankString(key); + if (key.isEmpty()) { + return firstCase(suffix); + } + return firstCase(key) + firstCase(suffix, true); + } + /** + * @param key + */ + public static String firstCase(String key) { + return firstCase(key, false); + } + /** + * @param key + * @param upper + * @return + */ + public static String firstCase(String key, boolean upper) { + key = getString(key); + if (key.isEmpty()) { + return ""; + } + + String first = key.substring(0, 1); + key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); + + return key; + } + + /**全部大写 + * @param s + * @return + */ + public static String toUpperCase(String s) { + return toUpperCase(s, false); + } + /**全部大写 + * @param s + * @param trim + * @return + */ + public static String toUpperCase(String s, boolean trim) { + s = trim ? getTrimedString(s) : getString(s); + return s.toUpperCase(); + } + /**全部小写 + * @param s + * @return + */ + public static String toLowerCase(String s) { + return toLowerCase(s, false); + } + /**全部小写 + * @param s + * @return + */ + public static String toLowerCase(String s, boolean trim) { + s = trim ? getTrimedString(s) : getString(s); + return s.toLowerCase(); + } + + public static String concat(String left, String right) { + return concat(left, right, null); + } + public static String concat(String left, String right, String split) { + return concat(left, right, split, true); + } + public static String concat(String left, String right, boolean trim) { + return concat(left, right, null, trim); + } + public static String concat(String left, String right, String split, boolean trim) { + if (isEmpty(left, trim)) { + return right; + } + if (isEmpty(right, trim)) { + return left; + } + + if (split == null) { + split = ","; + } + return left + split + right; + } + + //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + +} diff --git a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/IPUtil.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/IPUtil.java similarity index 99% rename from APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/IPUtil.java rename to APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/IPUtil.java index 0e363e1d7..c3d3442f4 100644 --- a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/IPUtil.java +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/IPUtil.java @@ -1,4 +1,4 @@ -package apijson.demo; +package unitauto.apk; import android.content.Context; import android.net.wifi.WifiInfo; diff --git a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/MethodUtil.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/MethodUtil.java similarity index 65% rename from APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/MethodUtil.java rename to APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/MethodUtil.java index db66fcb4b..785e88ae2 100644 --- a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/MethodUtil.java +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/MethodUtil.java @@ -1,16 +1,31 @@ -package apijson.demo; +/*Copyright ©2020 TommyLemon(https://github.com/TommyLemon) -import com.alibaba.fastjson.JSONObject; +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License.*/ + +package unitauto.apk; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; -import apijson.demo.application.DemoApplication; import dalvik.system.DexFile; -public class MethodUtil extends apijson.demo.server.MethodUtil { + +/**针对 Apk 文件的方法/函数的工具类 + * @author Lemon + */ +public class MethodUtil extends unitauto.MethodUtil { static { CLASS_LOADER_CALLBACK = new ClassLoaderCallback() { @@ -36,7 +51,7 @@ public List> loadClassList(String packageOrFileName, String className, ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - DexFile dex = new DexFile(DemoApplication.getInstance().getPackageResourcePath()); + DexFile dex = new DexFile(UnitAutoApp.getApp().getPackageResourcePath()); Enumeration entries = dex.entries(); while (entries.hasMoreElements()) { String entryName = entries.nextElement(); @@ -54,16 +69,4 @@ public List> loadClassList(String packageOrFileName, String className, }; } - public static JSONObject listMethod(String request) { - return apijson.demo.server.MethodUtil.listMethod(request); - } - - public static void invokeMethod(String request, Object instance, Listener listener) throws Exception { - apijson.demo.server.MethodUtil.invokeMethod(request, instance, listener); - } - - public static void invokeMethod(JSONObject request, Object instance, Listener listener) throws Exception { - apijson.demo.server.MethodUtil.invokeMethod(request, instance, listener); - } - } \ No newline at end of file diff --git a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/UnitAutoActivity.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoActivity.java similarity index 87% rename from APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/UnitAutoActivity.java rename to APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoActivity.java index 8cc5e8189..0f42ba30d 100755 --- a/APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/UnitAutoActivity.java +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoActivity.java @@ -1,4 +1,4 @@ -/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon) +/*Copyright ©2020 TommyLemon(https://github.com/TommyLemon) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -12,10 +12,12 @@ See the License for the specific language governing permissions and limitations under the License.*/ -package apijson.demo.ui; +package unitauto.apk; import android.app.Activity; import android.app.Application; +import android.content.ClipData; +import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -29,7 +31,6 @@ import android.widget.TextView; import android.widget.Toast; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.koushikdutta.async.AsyncServer; import com.koushikdutta.async.http.Headers; @@ -41,17 +42,15 @@ import java.lang.reflect.Method; -import apijson.demo.IPUtil; -import apijson.demo.MethodUtil; -import apijson.demo.R; -import apijson.demo.StringUtil; -import apijson.demo.application.DemoApplication; +import unitauto.JSON; +import unitauto.StringUtil; + /**自动单元测试,需要用 UnitAuto 发请求到这个设备 * https://github.com/TommyLemon/UnitAuto * @author Lemon */ -public class UnitAutoActivity extends UIAutoBaseActivity implements HttpServerRequestCallback { +public class UnitAutoActivity extends Activity implements HttpServerRequestCallback { public static final String TAG = "UnitAutoActivity"; private static final String KEY_PORT = "KEY_PORT"; @@ -128,7 +127,7 @@ public void onConfigurationChanged(Configuration newConfig) { public void copy(View v) { - StringUtil.copyText(context, StringUtil.getString((TextView) v)); + copyText(context, StringUtil.getString(((TextView) v).getText())); } public void orient(View v) { @@ -139,7 +138,22 @@ public void ip(View v) { String ip = IPUtil.getIpAddress(context); tvUnitIP.setText(ip + ":"); - StringUtil.copyText(context, "http://" + ip + ":" + getPort()); + copyText(context, "http://" + ip + ":" + getPort()); + } + + + /** + * @param value + */ + public static void copyText(Context context, String value) { + if (context == null || StringUtil.isEmpty(value, true)) { + Log.e("StringUtil", "copyText context == null || StringUtil.isNotEmpty(value, true) == false >> return;"); + return; + } + ClipData cd = ClipData.newPlainText("simple text", value); + ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + clipboardManager.setPrimaryClip(cd); + Toast.makeText(context, "已复制\n" + value, Toast.LENGTH_SHORT).show(); } @@ -149,7 +163,7 @@ private boolean isLandscape() { private String port = "8080"; private String getPort() { - String p = StringUtil.getTrimedString(etUnitPort); + String p = StringUtil.getTrimedString(etUnitPort.getText()); if (StringUtil.isEmpty(p, true)) { p = StringUtil.getTrimedString(etUnitPort.getHint()); } @@ -230,7 +244,7 @@ public void onRequest(final AsyncHttpServerRequest asyncHttpServerRequest, final public void run() { if (isAlive) { //TODO 改为 ListView 展示,保证每次请求都能对齐 Request 和 Response 的显示 try { - tvUnitRequest.setText(StringUtil.getString(asyncHttpServerRequest) + "Content:\n" + zuo.biao.apijson.JSON.format(request)); //批量跑测试容易卡死,也没必要显示所有的,专注更好 + "\n\n\n\n\n" + StringUtil.getString(tvUnitRequest)); + tvUnitRequest.setText(StringUtil.getString(asyncHttpServerRequest) + "Content:\n" + JSON.format(request)); //批量跑测试容易卡死,也没必要显示所有的,专注更好 + "\n\n\n\n\n" + StringUtil.getString(tvUnitRequest)); } catch (Exception e) { e.printStackTrace(); @@ -283,10 +297,10 @@ public void complete(JSONObject data, Method method, MethodUtil.InterfaceProxy p if (req.getBooleanValue("static") == false) { if (Activity.class.isAssignableFrom(clazz) || Context.class.isAssignableFrom(clazz)) { - instance = DemoApplication.getInstance().getCurrentActivity(); + instance = UnitAutoApp.getInstance().getCurrentActivity(); } else if (Application.class.isAssignableFrom(clazz)) { - instance = DemoApplication.getInstance(); + instance = UnitAutoApp.getApp(); } } } @@ -328,7 +342,7 @@ private void send(AsyncHttpServerResponse asyncHttpServerResponse, String json) public void run() { if (isAlive) { try { - tvUnitResponse.setText(StringUtil.getString(asyncHttpServerResponse) + "Content:\n" + zuo.biao.apijson.JSON.format(json)); //批量跑测试容易卡死,也没必要显示所有的,专注更好 + "\n\n\n\n\n" + StringUtil.getString(tvUnitResponse)); + tvUnitResponse.setText(StringUtil.getString(asyncHttpServerResponse) + "Content:\n" + JSON.format(json)); //批量跑测试容易卡死,也没必要显示所有的,专注更好 + "\n\n\n\n\n" + StringUtil.getString(tvUnitResponse)); } catch (Exception e) { e.printStackTrace(); diff --git a/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoApp.java b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoApp.java new file mode 100644 index 000000000..c7ddc1f45 --- /dev/null +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/java/unitauto/apk/UnitAutoApp.java @@ -0,0 +1,113 @@ +/*Copyright ©2020 TommyLemon(https://github.com/TommyLemon) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License.*/ + +package unitauto.apk; + +import android.app.Activity; +import android.app.Application; +import android.os.Bundle; +import android.util.Log; + +import java.lang.ref.WeakReference; +import java.util.LinkedList; +import java.util.List; + +/**Application + * @author Lemon + */ +public class UnitAutoApp extends Application { + private static final String TAG = "UnitAutoApp"; + + private static UnitAutoApp INSTANCE; + public static UnitAutoApp getInstance() { + return INSTANCE; + } + + private static Application APP; + public static Application getApp() { + return APP; + } + public static void init(Application app) { + APP = app; + + app.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { + + @Override + public void onActivityStarted(Activity activity) { + Log.v(TAG, "onActivityStarted activity = " + activity.getClass().getName()); + } + + @Override + public void onActivityStopped(Activity activity) { + Log.v(TAG, "onActivityStopped activity = " + activity.getClass().getName()); + } + + @Override + public void onActivitySaveInstanceState(Activity activity, Bundle outState) { + Log.v(TAG, "onActivitySaveInstanceState activity = " + activity.getClass().getName()); + } + + @Override + public void onActivityResumed(Activity activity) { + Log.v(TAG, "onActivityResumed activity = " + activity.getClass().getName()); + setCurrentActivity(activity); + } + + @Override + public void onActivityPaused(Activity activity) { + Log.v(TAG, "onActivityPaused activity = " + activity.getClass().getName()); + setCurrentActivity(ACTIVITY_LIST.isEmpty() ? null : ACTIVITY_LIST.get(ACTIVITY_LIST.size() - 1)); + } + + @Override + public void onActivityCreated(Activity activity, Bundle savedInstanceState) { + Log.v(TAG, "onActivityCreated activity = " + activity.getClass().getName()); + ACTIVITY_LIST.add(activity); + } + + @Override + public void onActivityDestroyed(Activity activity) { + Log.v(TAG, "onActivityDestroyed activity = " + activity.getClass().getName()); + ACTIVITY_LIST.remove(activity); + } + + }); + + } + + + private static List ACTIVITY_LIST = new LinkedList<>(); + + private static WeakReference CURRENT_ACTIVITY_REF; + public static Activity getCurrentActivity() { + return CURRENT_ACTIVITY_REF == null ? null : CURRENT_ACTIVITY_REF.get(); + } + public static void setCurrentActivity(Activity activity) { + if (CURRENT_ACTIVITY_REF == null || ! activity.equals(CURRENT_ACTIVITY_REF.get())) { + CURRENT_ACTIVITY_REF = new WeakReference<>(activity); + } + } + + + + @Override + public void onCreate() { + super.onCreate(); + INSTANCE = this; + init(this); + } + + + +} diff --git a/APIJSON-Android/APIJSONTest/app/src/main/res/layout/unit_auto_activity.xml b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/res/layout/unit_auto_activity.xml similarity index 92% rename from APIJSON-Android/APIJSONTest/app/src/main/res/layout/unit_auto_activity.xml rename to APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/res/layout/unit_auto_activity.xml index dd43c9407..3d399ca1c 100755 --- a/APIJSON-Android/APIJSONTest/app/src/main/res/layout/unit_auto_activity.xml +++ b/APIJSON-Android/APIJSONTest/UnitAuto-Apk/src/main/res/layout/unit_auto_activity.xml @@ -4,15 +4,17 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - tools:context=".ui.UnitAutoActivity" + tools:context="unitauto.apk.UnitAutoActivity" > + android:text="@string/horizontal" />