ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/util/CommonUtil.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.util; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.app.AlertDialog; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.app.AlertDialog.Builder; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import android.content.Context; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import android.os.Build; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import android.os.Bundle; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import android.text.TextUtils; |
| TMBOY | 45:2aa9f933c8d2 | 24 | |
| TMBOY | 45:2aa9f933c8d2 | 25 | import org.json.JSONArray; |
| TMBOY | 45:2aa9f933c8d2 | 26 | import org.json.JSONObject; |
| TMBOY | 45:2aa9f933c8d2 | 27 | |
| TMBOY | 45:2aa9f933c8d2 | 28 | import java.io.Closeable; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import java.io.IOException; |
| TMBOY | 45:2aa9f933c8d2 | 30 | import java.net.URLDecoder; |
| TMBOY | 45:2aa9f933c8d2 | 31 | import java.net.URLEncoder; |
| TMBOY | 45:2aa9f933c8d2 | 32 | import java.text.SimpleDateFormat; |
| TMBOY | 45:2aa9f933c8d2 | 33 | import java.util.Date; |
| TMBOY | 45:2aa9f933c8d2 | 34 | import java.util.Locale; |
| TMBOY | 45:2aa9f933c8d2 | 35 | import java.util.UUID; |
| TMBOY | 45:2aa9f933c8d2 | 36 | |
| TMBOY | 45:2aa9f933c8d2 | 37 | /** |
| TMBOY | 45:2aa9f933c8d2 | 38 | * 辅助工具类 |
| TMBOY | 45:2aa9f933c8d2 | 39 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 40 | * Created by guxiuzhong@baidu.com on 2017/5/17. |
| TMBOY | 45:2aa9f933c8d2 | 41 | */ |
| TMBOY | 45:2aa9f933c8d2 | 42 | public class CommonUtil { |
| TMBOY | 45:2aa9f933c8d2 | 43 | private static final int JSON_INDENT = 4; |
| TMBOY | 45:2aa9f933c8d2 | 44 | |
| TMBOY | 45:2aa9f933c8d2 | 45 | public static String getCurrentTime() { |
| TMBOY | 45:2aa9f933c8d2 | 46 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", |
| TMBOY | 45:2aa9f933c8d2 | 47 | Locale.CHINESE); |
| TMBOY | 45:2aa9f933c8d2 | 48 | return format.format(new Date()); |
| TMBOY | 45:2aa9f933c8d2 | 49 | } |
| TMBOY | 45:2aa9f933c8d2 | 50 | |
| TMBOY | 45:2aa9f933c8d2 | 51 | /** |
| TMBOY | 45:2aa9f933c8d2 | 52 | * 将毫秒格式转化为yyyy-MM-dd HH:mm:ss |
| TMBOY | 45:2aa9f933c8d2 | 53 | * |
| TMBOY | 45:2aa9f933c8d2 | 54 | * @param milliSeconds 毫秒 |
| TMBOY | 45:2aa9f933c8d2 | 55 | * @return 格式化后的字符串结果 |
| TMBOY | 45:2aa9f933c8d2 | 56 | */ |
| TMBOY | 45:2aa9f933c8d2 | 57 | public static String formatToDataTime(long milliSeconds) { |
| TMBOY | 45:2aa9f933c8d2 | 58 | SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); |
| TMBOY | 45:2aa9f933c8d2 | 59 | return sd.format(milliSeconds); |
| TMBOY | 45:2aa9f933c8d2 | 60 | } |
| TMBOY | 45:2aa9f933c8d2 | 61 | |
| TMBOY | 45:2aa9f933c8d2 | 62 | /** |
| TMBOY | 45:2aa9f933c8d2 | 63 | * 将key1=value1&key2=value2格式的query转换成key-value形式的参数串 |
| TMBOY | 45:2aa9f933c8d2 | 64 | * |
| TMBOY | 45:2aa9f933c8d2 | 65 | * @param query key1=value1&key2=value2格式的query |
| TMBOY | 45:2aa9f933c8d2 | 66 | * @return key-value形式的bundle |
| TMBOY | 45:2aa9f933c8d2 | 67 | */ |
| TMBOY | 45:2aa9f933c8d2 | 68 | public static Bundle decodeUrl(String query) { |
| TMBOY | 45:2aa9f933c8d2 | 69 | Bundle ret = new Bundle(); |
| TMBOY | 45:2aa9f933c8d2 | 70 | if (query != null) { |
| TMBOY | 45:2aa9f933c8d2 | 71 | String[] pairs = query.split("&"); |
| TMBOY | 45:2aa9f933c8d2 | 72 | for (String pair : pairs) { |
| TMBOY | 45:2aa9f933c8d2 | 73 | String[] keyAndValues = pair.split("="); |
| TMBOY | 45:2aa9f933c8d2 | 74 | if (keyAndValues != null && keyAndValues.length == 2) { |
| TMBOY | 45:2aa9f933c8d2 | 75 | String key = keyAndValues[0]; |
| TMBOY | 45:2aa9f933c8d2 | 76 | String value = keyAndValues[1]; |
| TMBOY | 45:2aa9f933c8d2 | 77 | if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) { |
| TMBOY | 45:2aa9f933c8d2 | 78 | ret.putString(URLDecoder.decode(key), URLDecoder.decode(value)); |
| TMBOY | 45:2aa9f933c8d2 | 79 | } |
| TMBOY | 45:2aa9f933c8d2 | 80 | } |
| TMBOY | 45:2aa9f933c8d2 | 81 | } |
| TMBOY | 45:2aa9f933c8d2 | 82 | } |
| TMBOY | 45:2aa9f933c8d2 | 83 | return ret; |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | |
| TMBOY | 45:2aa9f933c8d2 | 86 | /** |
| TMBOY | 45:2aa9f933c8d2 | 87 | * 将key-value形式的参数串,转换成key1=value1&key2=value2格式的query |
| TMBOY | 45:2aa9f933c8d2 | 88 | * |
| TMBOY | 45:2aa9f933c8d2 | 89 | * @param params key-value参数 |
| TMBOY | 45:2aa9f933c8d2 | 90 | * @return key1=value1&key2=value2格式的query |
| TMBOY | 45:2aa9f933c8d2 | 91 | */ |
| TMBOY | 45:2aa9f933c8d2 | 92 | public static String encodeUrl(Bundle params) { |
| TMBOY | 45:2aa9f933c8d2 | 93 | if (params == null || params.isEmpty()) { |
| TMBOY | 45:2aa9f933c8d2 | 94 | return null; |
| TMBOY | 45:2aa9f933c8d2 | 95 | } |
| TMBOY | 45:2aa9f933c8d2 | 96 | boolean first = true; |
| TMBOY | 45:2aa9f933c8d2 | 97 | StringBuilder sb = new StringBuilder(); |
| TMBOY | 45:2aa9f933c8d2 | 98 | for (String key : params.keySet()) { |
| TMBOY | 45:2aa9f933c8d2 | 99 | String paramValue = params.getString(key); |
| TMBOY | 45:2aa9f933c8d2 | 100 | if (paramValue == null) { |
| TMBOY | 45:2aa9f933c8d2 | 101 | continue; |
| TMBOY | 45:2aa9f933c8d2 | 102 | } |
| TMBOY | 45:2aa9f933c8d2 | 103 | if (first) { |
| TMBOY | 45:2aa9f933c8d2 | 104 | first = false; |
| TMBOY | 45:2aa9f933c8d2 | 105 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 106 | sb.append("&"); |
| TMBOY | 45:2aa9f933c8d2 | 107 | } |
| TMBOY | 45:2aa9f933c8d2 | 108 | sb.append(URLEncoder.encode(key)).append("=").append(URLEncoder.encode(paramValue)); |
| TMBOY | 45:2aa9f933c8d2 | 109 | } |
| TMBOY | 45:2aa9f933c8d2 | 110 | return sb.toString(); |
| TMBOY | 45:2aa9f933c8d2 | 111 | } |
| TMBOY | 45:2aa9f933c8d2 | 112 | |
| TMBOY | 45:2aa9f933c8d2 | 113 | /** |
| TMBOY | 45:2aa9f933c8d2 | 114 | * 展示一个通用的弹出框UI |
| TMBOY | 45:2aa9f933c8d2 | 115 | * |
| TMBOY | 45:2aa9f933c8d2 | 116 | * @param context 展示弹出框的上下文环境 |
| TMBOY | 45:2aa9f933c8d2 | 117 | * @param title 警告的title信息 |
| TMBOY | 45:2aa9f933c8d2 | 118 | * @param text 警告信息 |
| TMBOY | 45:2aa9f933c8d2 | 119 | */ |
| TMBOY | 45:2aa9f933c8d2 | 120 | public static void showAlert(Context context, String title, String text) { |
| TMBOY | 45:2aa9f933c8d2 | 121 | AlertDialog alertDialog = new Builder(context).create(); |
| TMBOY | 45:2aa9f933c8d2 | 122 | alertDialog.setTitle(title); |
| TMBOY | 45:2aa9f933c8d2 | 123 | alertDialog.setMessage(text); |
| TMBOY | 45:2aa9f933c8d2 | 124 | alertDialog.setCanceledOnTouchOutside(true); |
| TMBOY | 45:2aa9f933c8d2 | 125 | alertDialog.show(); |
| TMBOY | 45:2aa9f933c8d2 | 126 | } |
| TMBOY | 45:2aa9f933c8d2 | 127 | |
| TMBOY | 45:2aa9f933c8d2 | 128 | private static long lastClickTime; |
| TMBOY | 45:2aa9f933c8d2 | 129 | |
| TMBOY | 45:2aa9f933c8d2 | 130 | /** |
| TMBOY | 45:2aa9f933c8d2 | 131 | * 是否是双击 |
| TMBOY | 45:2aa9f933c8d2 | 132 | * |
| TMBOY | 45:2aa9f933c8d2 | 133 | * @return true 是,false 否 |
| TMBOY | 45:2aa9f933c8d2 | 134 | */ |
| TMBOY | 45:2aa9f933c8d2 | 135 | public static boolean isFastDoubleClick() { |
| TMBOY | 45:2aa9f933c8d2 | 136 | long time = System.currentTimeMillis(); |
| TMBOY | 45:2aa9f933c8d2 | 137 | if (time - lastClickTime < 500) { |
| TMBOY | 45:2aa9f933c8d2 | 138 | return true; |
| TMBOY | 45:2aa9f933c8d2 | 139 | } |
| TMBOY | 45:2aa9f933c8d2 | 140 | lastClickTime = time; |
| TMBOY | 45:2aa9f933c8d2 | 141 | return false; |
| TMBOY | 45:2aa9f933c8d2 | 142 | } |
| TMBOY | 45:2aa9f933c8d2 | 143 | |
| TMBOY | 45:2aa9f933c8d2 | 144 | /** |
| TMBOY | 45:2aa9f933c8d2 | 145 | * json 格式化输出 |
| TMBOY | 45:2aa9f933c8d2 | 146 | * |
| TMBOY | 45:2aa9f933c8d2 | 147 | * @param json json字符串 |
| TMBOY | 45:2aa9f933c8d2 | 148 | * @return 用四个空格缩进后的json字符串结果 |
| TMBOY | 45:2aa9f933c8d2 | 149 | */ |
| TMBOY | 45:2aa9f933c8d2 | 150 | public static String formatJson(String json) { |
| TMBOY | 45:2aa9f933c8d2 | 151 | String formatted = ""; |
| TMBOY | 45:2aa9f933c8d2 | 152 | if (json == null || json.length() == 0) { |
| TMBOY | 45:2aa9f933c8d2 | 153 | return formatted; |
| TMBOY | 45:2aa9f933c8d2 | 154 | } |
| TMBOY | 45:2aa9f933c8d2 | 155 | try { |
| TMBOY | 45:2aa9f933c8d2 | 156 | if (json.startsWith("{")) { |
| TMBOY | 45:2aa9f933c8d2 | 157 | JSONObject jo = new JSONObject(json); |
| TMBOY | 45:2aa9f933c8d2 | 158 | formatted = jo.toString(JSON_INDENT); |
| TMBOY | 45:2aa9f933c8d2 | 159 | } else if (json.startsWith("[")) { |
| TMBOY | 45:2aa9f933c8d2 | 160 | JSONArray ja = new JSONArray(json); |
| TMBOY | 45:2aa9f933c8d2 | 161 | formatted = ja.toString(JSON_INDENT); |
| TMBOY | 45:2aa9f933c8d2 | 162 | } |
| TMBOY | 45:2aa9f933c8d2 | 163 | } catch (Exception e) { |
| TMBOY | 45:2aa9f933c8d2 | 164 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 165 | } |
| TMBOY | 45:2aa9f933c8d2 | 166 | return formatted; |
| TMBOY | 45:2aa9f933c8d2 | 167 | } |
| TMBOY | 45:2aa9f933c8d2 | 168 | |
| TMBOY | 45:2aa9f933c8d2 | 169 | /** |
| TMBOY | 45:2aa9f933c8d2 | 170 | * 关闭流 |
| TMBOY | 45:2aa9f933c8d2 | 171 | * |
| TMBOY | 45:2aa9f933c8d2 | 172 | * @param closeables closeables |
| TMBOY | 45:2aa9f933c8d2 | 173 | */ |
| TMBOY | 45:2aa9f933c8d2 | 174 | public static void closeQuietly(Closeable... closeables) { |
| TMBOY | 45:2aa9f933c8d2 | 175 | for (Closeable c : closeables) { |
| TMBOY | 45:2aa9f933c8d2 | 176 | try { |
| TMBOY | 45:2aa9f933c8d2 | 177 | if (c != null) { |
| TMBOY | 45:2aa9f933c8d2 | 178 | c.close(); |
| TMBOY | 45:2aa9f933c8d2 | 179 | } |
| TMBOY | 45:2aa9f933c8d2 | 180 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 181 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 182 | } |
| TMBOY | 45:2aa9f933c8d2 | 183 | } |
| TMBOY | 45:2aa9f933c8d2 | 184 | } |
| TMBOY | 45:2aa9f933c8d2 | 185 | |
| TMBOY | 45:2aa9f933c8d2 | 186 | /** |
| TMBOY | 45:2aa9f933c8d2 | 187 | * Android设备物理唯一标识符 |
| TMBOY | 45:2aa9f933c8d2 | 188 | * |
| TMBOY | 45:2aa9f933c8d2 | 189 | * @return String 设备唯一标识 |
| TMBOY | 45:2aa9f933c8d2 | 190 | */ |
| TMBOY | 45:2aa9f933c8d2 | 191 | public static String getDeviceUniqueID() { |
| TMBOY | 45:2aa9f933c8d2 | 192 | String devIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10); |
| TMBOY | 45:2aa9f933c8d2 | 193 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| TMBOY | 45:2aa9f933c8d2 | 194 | devIDShort += (Build.SUPPORTED_ABIS[0].length() % 10); |
| TMBOY | 45:2aa9f933c8d2 | 195 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 196 | devIDShort += (Build.CPU_ABI.length() % 10); |
| TMBOY | 45:2aa9f933c8d2 | 197 | } |
| TMBOY | 45:2aa9f933c8d2 | 198 | devIDShort += (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) |
| TMBOY | 45:2aa9f933c8d2 | 199 | + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10); |
| TMBOY | 45:2aa9f933c8d2 | 200 | String serial; |
| TMBOY | 45:2aa9f933c8d2 | 201 | try { |
| TMBOY | 45:2aa9f933c8d2 | 202 | serial = Build.class.getField("SERIAL").get(null).toString(); |
| TMBOY | 45:2aa9f933c8d2 | 203 | return new UUID(devIDShort.hashCode(), serial.hashCode()).toString(); |
| TMBOY | 45:2aa9f933c8d2 | 204 | } catch (Exception e) { |
| TMBOY | 45:2aa9f933c8d2 | 205 | serial = "Dueros000"; |
| TMBOY | 45:2aa9f933c8d2 | 206 | } |
| TMBOY | 45:2aa9f933c8d2 | 207 | return new UUID(devIDShort.hashCode(), serial.hashCode()).toString(); |
| TMBOY | 45:2aa9f933c8d2 | 208 | } |
| TMBOY | 45:2aa9f933c8d2 | 209 | } |
