提交 358cfa77 编写于 作者: B Blankj

see 01/04 log

上级 fe70516b
......@@ -702,6 +702,16 @@ create : 创建样式字符串
* ### SP 相关 -> [SPUtils.java][sp.java] -> [Demo][sp.demo]
```
putStatic : SP 中写入数据
getStringStatic : SP 中读取 String
getIntStatic : SP 中读取 int
getLongStatic : SP 中读取 long
getFloatStatic : SP 中读取 float
getBooleanStatic : SP 中读取 boolean
getAllStatic : SP 中获取所有键值对
containsStatic : SP 中是否存在该 key
removeStatic : SP 中移除该 key
clearStatic : SP 中清除所有数据
getInstance : 获取 SP 实例
Instance.put : SP 中写入数据
Instance.getString : SP 中读取 String
......
......@@ -12,7 +12,7 @@ import android.view.View;
* desc : utils about anti shake
* </pre>
*/
public class AntiShakeUtils {
public final class AntiShakeUtils {
private static final long DEFAULT_DURATION = 200;
private static final int TAG_KEY = 0x7EFFFFFF;
......
......@@ -1070,4 +1070,10 @@ public final class CacheDiskUtils implements CacheConstants {
}
return true;
}
///////////////////////////////////////////////////////////////////////////
// static
///////////////////////////////////////////////////////////////////////////
}
\ No newline at end of file
......@@ -1533,7 +1533,10 @@ public final class ImageUtils {
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isImage(final File file) {
return file != null && isImage(file.getPath());
if (file == null || !file.exists()) {
return false;
}
return isImage(file.getPath());
}
/**
......@@ -1543,10 +1546,14 @@ public final class ImageUtils {
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isImage(final String filePath) {
String path = filePath.toUpperCase();
return path.endsWith(".PNG") || path.endsWith(".JPG")
|| path.endsWith(".JPEG") || path.endsWith(".BMP")
|| path.endsWith(".GIF") || path.endsWith(".WEBP");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
return options.outWidth != -1 && options.outHeight != -1;
} catch (Exception e) {
return false;
}
}
/**
......
//package com.blankj.utilcode.util;
//
//
//import org.json.JSONException;
//import org.json.JSONObject;
//
//public final class JsonUtils {
//
// private static byte TYPE_LONG = 0x01;
// private static byte TYPE_INT = 0x02;
//
// private JsonUtils() {
// throw new UnsupportedOperationException("u can't instantiate me...");
// }
//
// public static long getLong(final JSONObject jsonObject,
// final String key,
// final long defaultValue) {
// if (jsonObject == null || key == null || key.length() == 0) {
// return defaultValue;
// }
// try {
// return jsonObject.getLong(key);
// } catch (JSONException e) {
// return defaultValue;
// }
// }
//
// public static long getLong(final String json,
// final String key,
// final long defaultValue) {
// if (json == null || json.length() == 0
// || key == null || key.length() == 0) {
// return defaultValue;
// }
//
// try {
// return getLong(new JSONObject(json), key, defaultValue);
// } catch (JSONException e) {
// e.printStackTrace();
// return defaultValue;
// }
// }
//
// public static Object getType(final JSONObject jsonObject,
// final String key,
// final Object defaultValue,
// final byte type) {
// if (jsonObject == null || key == null || key.length() == 0) {
// return defaultValue;
// }
// try {
// if (type == TYPE_LONG) {
// return jsonObject.getLong(key);
// } else if (type == TYPE_INT) {
// return jsonObject.getInt(key);
// }
// } catch (JSONException e) {
// return defaultValue;
// }
// }
//
// public static long getLong(final String json,
// final String key,
// final long defaultValue) {
// if (json == null || json.length() == 0
// || key == null || key.length() == 0) {
// return defaultValue;
// }
//
// try {
// return getLong(new JSONObject(json), key, defaultValue);
// } catch (JSONException e) {
// e.printStackTrace();
// return defaultValue;
// }
// }
//
//}
......@@ -797,7 +797,11 @@ public final class LogUtils {
if (object instanceof CharSequence) {
return formatJson(object.toString());
}
return GSON.toJson(object);
try {
return GSON.toJson(object);
} catch (Throwable t) {
return object.toString();
}
}
static String formatXml(String xml) {
......
......@@ -442,4 +442,328 @@ public final class SPUtils {
}
return true;
}
///////////////////////////////////////////////////////////////////////////
// static
///////////////////////////////////////////////////////////////////////////
/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void putStatic(@NonNull final String key, final String value) {
SPUtils.getInstance().put(key, value);
}
/**
* Put the string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void putStatic(@NonNull final String key, final String value, final boolean isCommit) {
SPUtils.getInstance().put(key, value, isCommit);
}
/**
* Return the string value in sp.
*
* @param key The key of sp.
* @return the string value if sp exists or {@code ""} otherwise
*/
public static String getStringStatic(@NonNull final String key) {
return SPUtils.getInstance().getString(key);
}
/**
* Return the string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn't exist.
* @return the string value if sp exists or {@code defaultValue} otherwise
*/
public static String getStringStatic(@NonNull final String key, final String defaultValue) {
return SPUtils.getInstance().getString(key, defaultValue);
}
/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void putStatic(@NonNull final String key, final int value) {
SPUtils.getInstance().put(key, value);
}
/**
* Put the int value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void putStatic(@NonNull final String key, final int value, final boolean isCommit) {
SPUtils.getInstance().put(key, value, isCommit);
}
/**
* Return the int value in sp.
*
* @param key The key of sp.
* @return the int value if sp exists or {@code -1} otherwise
*/
public static int getIntStatic(@NonNull final String key) {
return SPUtils.getInstance().getInt(key);
}
/**
* Return the int value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn't exist.
* @return the int value if sp exists or {@code defaultValue} otherwise
*/
public static int getIntStatic(@NonNull final String key, final int defaultValue) {
return SPUtils.getInstance().getInt(key, defaultValue);
}
/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void putStatic(@NonNull final String key, final long value) {
SPUtils.getInstance().put(key, value);
}
/**
* Put the long value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void putStatic(@NonNull final String key, final long value, final boolean isCommit) {
SPUtils.getInstance().put(key, value, isCommit);
}
/**
* Return the long value in sp.
*
* @param key The key of sp.
* @return the long value if sp exists or {@code -1} otherwise
*/
public static long getLongStatic(@NonNull final String key) {
return SPUtils.getInstance().getLong(key);
}
/**
* Return the long value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn't exist.
* @return the long value if sp exists or {@code defaultValue} otherwise
*/
public static long getLongStatic(@NonNull final String key, final long defaultValue) {
return SPUtils.getInstance().getLong(key, defaultValue);
}
/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void putStatic(@NonNull final String key, final float value) {
SPUtils.getInstance().put(key, value);
}
/**
* Put the float value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void putStatic(@NonNull final String key, final float value, final boolean isCommit) {
SPUtils.getInstance().put(key, value, isCommit);
}
/**
* Return the float value in sp.
*
* @param key The key of sp.
* @return the float value if sp exists or {@code -1f} otherwise
*/
public static float getFloatStatic(@NonNull final String key) {
return SPUtils.getInstance().getFloat(key);
}
/**
* Return the float value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn't exist.
* @return the float value if sp exists or {@code defaultValue} otherwise
*/
public static float getFloatStatic(@NonNull final String key, final float defaultValue) {
return SPUtils.getInstance().getFloat(key, defaultValue);
}
/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void putStatic(@NonNull final String key, final boolean value) {
SPUtils.getInstance().put(key, value);
}
/**
* Put the boolean value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void putStatic(@NonNull final String key, final boolean value, final boolean isCommit) {
SPUtils.getInstance().put(key, value, isCommit);
}
/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @return the boolean value if sp exists or {@code false} otherwise
*/
public static boolean getBooleanStatic(@NonNull final String key) {
return SPUtils.getInstance().getBoolean(key);
}
/**
* Return the boolean value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn't exist.
* @return the boolean value if sp exists or {@code defaultValue} otherwise
*/
public static boolean getBooleanStatic(@NonNull final String key, final boolean defaultValue) {
return SPUtils.getInstance().getBoolean(key, defaultValue);
}
/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
*/
public static void putStatic(@NonNull final String key, final Set<String> value) {
SPUtils.getInstance().put(key, value);
}
/**
* Put the set of string value in sp.
*
* @param key The key of sp.
* @param value The value of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void putStatic(@NonNull final String key,
final Set<String> value,
final boolean isCommit) {
SPUtils.getInstance().put(key, value, isCommit);
}
/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @return the set of string value if sp exists
* or {@code Collections.<String>emptySet()} otherwise
*/
public static Set<String> getStringSetStatic(@NonNull final String key) {
return SPUtils.getInstance().getStringSet(key);
}
/**
* Return the set of string value in sp.
*
* @param key The key of sp.
* @param defaultValue The default value if the sp doesn't exist.
* @return the set of string value if sp exists or {@code defaultValue} otherwise
*/
public static Set<String> getStringSetStatic(@NonNull final String key,
final Set<String> defaultValue) {
return SPUtils.getInstance().getStringSet(key, defaultValue);
}
/**
* Return all values in sp.
*
* @return all values in sp
*/
public static Map<String, ?> getAllStatic() {
return SPUtils.getInstance().getAll();
}
/**
* Return whether the sp contains the preference.
*
* @param key The key of sp.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean containsStatic(@NonNull final String key) {
return SPUtils.getInstance().contains(key);
}
/**
* Remove the preference in sp.
*
* @param key The key of sp.
*/
public static void removeStatic(@NonNull final String key) {
SPUtils.getInstance().remove(key);
}
/**
* Remove the preference in sp.
*
* @param key The key of sp.
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void removeStatic(@NonNull final String key, final boolean isCommit) {
SPUtils.getInstance().remove(key, isCommit);
}
/**
* Remove all preferences in sp.
*/
public static void clearStatic() {
SPUtils.getInstance().clear();
}
/**
* Remove all preferences in sp.
*
* @param isCommit True to use {@link SharedPreferences.Editor#commit()},
* false to use {@link SharedPreferences.Editor#apply()}
*/
public static void clearStatic(final boolean isCommit) {
SPUtils.getInstance().clear(isCommit);
}
}
package com.blankj.utilcode.util;
import android.app.Application;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -7,6 +9,7 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
......@@ -24,7 +27,7 @@ public class LogUtilsTest extends BaseTest {
private static final String XML = "<books><book><author>Jack Herrington</author><title>PHP Hacks</title><publisher>O'Reilly</publisher></book><book><author>Jack Herrington</author><title>Podcasting Hacks</title><publisher>O'Reilly</publisher></book></books>";
private static final int[] ONE_D_ARRAY = new int[]{1, 2, 3};
private static final int[][] TWO_D_ARRAY = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
private static final ArrayList<String> LIST = new ArrayList<>();
private static final ArrayList<Object> LIST = new ArrayList<>();
private static final Map<String, String> MAP = new HashMap<>();
@Test
......@@ -163,17 +166,30 @@ public class LogUtilsTest extends BaseTest {
LIST.add("hello");
LIST.add("log");
LIST.add("utils");
LIST.add(new Application());
MAP.put("name", "AndroidUtilCode");
MAP.put("class", "LogUtils");
LogUtils.d((Object) ONE_D_ARRAY);
LogUtils.d((Object) TWO_D_ARRAY);
LogUtils.d(LIST);
LogUtils.d(formatJson(collection2String(LIST)));
LogUtils.d(MAP);
LogUtils.d(formatJson(array2String(TWO_D_ARRAY)));
LogUtils.d(array2String(TWO_D_ARRAY));
}
static String collection2String(Collection collection) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("size", collection.size());
jsonObject.put("data", collection);
return jsonObject.toString();
} catch (JSONException ignore) {
return collection.toString();
}
}
private static String formatJson(String json) {
try {
if (json.startsWith("{")) {
......
package com.blankj.utilcode.pkg.data;
import com.blankj.utilcode.util.SPUtils;
import java.util.Map;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/01/08
* desc : manager about data
* </pre>
*/
public class DataManager {
private static final SPUtils SP_UTILS = SPUtils.getInstance("demo");
public static void putString() {
SP_UTILS.put("STRING", "string");
}
public static String getString() {
return SP_UTILS.getString("STRING");
}
public static void putInt() {
SP_UTILS.put("INT", 21);
}
public static String getInt() {
return String.valueOf(SP_UTILS.getInt("INT"));
}
public static void putLong() {
SP_UTILS.put("LONG", Long.MAX_VALUE);
}
public static String getLong() {
return String.valueOf(SP_UTILS.getLong("LONG"));
}
public static void putFloat() {
SP_UTILS.put("FLOAT", (float) Math.PI);
}
public static String getFloat() {
return String.valueOf(SP_UTILS.getFloat("FLOAT"));
}
public static void putBoolean() {
SP_UTILS.put("BOOLEAN", true);
}
public static String getBoolean() {
return String.valueOf(SP_UTILS.getBoolean("BOOLEAN"));
}
public static void clear() {
SP_UTILS.clear();
}
public static String sp2String() {
StringBuilder sb = new StringBuilder();
Map<String, ?> map = SP_UTILS.getAll();
for (Map.Entry<String, ?> entry : map.entrySet()) {
sb.append(entry.getKey())
.append(": ")
.append(entry.getValue())
.append("\n");
}
return sb.toString();
}
}
......@@ -10,7 +10,9 @@ import android.widget.TextView;
import com.blankj.lib.base.BaseBackActivity;
import com.blankj.utilcode.pkg.R;
import com.blankj.utilcode.pkg.data.DataManager;
import com.blankj.utilcode.util.SPUtils;
import java.util.Map;
/**
* <pre>
......@@ -59,28 +61,40 @@ public class SPActivity extends BaseBackActivity {
public void onWidgetClick(@NonNull View view) {
int i = view.getId();
if (i == R.id.btn_sp_put_string) {
DataManager.putString();
SPUtils.putStatic("STRING", "string");
} else if (i == R.id.btn_sp_put_int) {
DataManager.putInt();
SPUtils.putStatic("INT", 21);
} else if (i == R.id.btn_sp_put_long) {
DataManager.putLong();
SPUtils.putStatic("LONG", Long.MAX_VALUE);
} else if (i == R.id.btn_sp_put_float) {
DataManager.putFloat();
SPUtils.putStatic("FLOAT", (float) Math.PI);
} else if (i == R.id.btn_sp_put_boolean) {
DataManager.putBoolean();
SPUtils.putStatic("BOOLEAN", true);
} else if (i == R.id.btn_sp_clear) {
DataManager.clear();
SPUtils.clearStatic();
}
updateAboutSp();
}
private void updateAboutSp() {
tvAboutSp.setText(DataManager.sp2String());
tvAboutSp.setText(sp2String());
}
public static String sp2String() {
StringBuilder sb = new StringBuilder();
Map<String, ?> map = SPUtils.getAllStatic();
for (Map.Entry<String, ?> entry : map.entrySet()) {
sb.append(entry.getKey())
.append(": ")
.append(entry.getValue())
.append("\n");
}
return sb.toString();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册