Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
张涛 committed Oct 12, 2016
1 parent cc892fc commit 0e7e13b
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 42 deletions.
42 changes: 42 additions & 0 deletions Common/common/src/main/java/com/kymjs/common/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.kymjs.common;

import android.app.Application;
import android.widget.Toast;

/**
* Created by ZhangTao on 10/12/16.
*/
public class App {

public static final Application INSTANCE;

static {
Application app = null;
try {
app = (Application) Class.forName("android.app.AppGlobals").getMethod("getInitialApplication").invoke(null);
if (app == null)
throw new IllegalStateException("Static initialization of Applications must be on main thread.");
} catch (final Exception e) {
LogUtils.e("Failed to get current application from AppGlobals." + e.getMessage());
try {
app = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null);
} catch (final Exception ex) {
LogUtils.e("Failed to get current application from ActivityThread." + e.getMessage());
}
} finally {
INSTANCE = app;
}
}

public static void toast(String msg) {
Toast.makeText(INSTANCE, msg, Toast.LENGTH_SHORT).show();
}

public static void toast(int msgId) {
Toast.makeText(INSTANCE, msgId, Toast.LENGTH_SHORT).show();
}

public static void longToast(String msg) {
Toast.makeText(INSTANCE, msg, Toast.LENGTH_LONG).show();
}
}
32 changes: 12 additions & 20 deletions Common/common/src/main/java/com/kymjs/common/DensityUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.kymjs.common;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;
Expand All @@ -14,58 +12,52 @@ public class DensityUtils {
/**
* 获取屏幕高度
*
* @param activity Activity
* @return 屏幕高度
*/
public static int getScreenH(Activity activity) {
DisplayMetrics dm = activity.getResources().getDisplayMetrics();
public static int getScreenH() {
DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
return dm.heightPixels;
}

/**
* 获取屏幕宽度
*
* @param activity Activity
* @return 屏幕宽度
*/
public static int getScreenW(Activity activity) {
DisplayMetrics dm = activity.getResources().getDisplayMetrics();
public static int getScreenW() {
DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
return dm.widthPixels;
}

/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
Resources r = context.getResources();
public static int dip2px(float dpValue) {
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpValue, r.getDisplayMetrics());
dpValue, Resources.getSystem().getDisplayMetrics());
return (int) px;
}

/**
* 根据手机的分辨率从 px 的单位 转成为 dp
*/
public static int px2dip(Context context, float px) {
Resources r = context.getResources();
return (int) (px / r.getDisplayMetrics().density);
public static int px2dip(float px) {
return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

/**
* 根据手机的分辨率从 px 的单位 转成为 sp
*/
public static int px2sp(Context context, float px) {
Resources r = context.getResources();
return (int) (px / r.getDisplayMetrics().scaledDensity);
public static int px2sp(float px) {
return (int) (px / Resources.getSystem().getDisplayMetrics().scaledDensity);
}

/**
* 根据手机的分辨率从 sp 的单位 转成为 px(像素)
*/
public static int sp2px(Context context, float spValue) {
Resources r = context.getResources();
public static int sp2px(float spValue) {
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spValue, r.getDisplayMetrics());
spValue, Resources.getSystem().getDisplayMetrics());
return (int) px;
}

Expand Down
151 changes: 151 additions & 0 deletions Common/common/src/main/java/com/kymjs/common/LogUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.kymjs.common;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import android.util.Log;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
* Created by ZhangTao on 10/12/16.
*/
public class LogUtils {
public static boolean mLogEnable = true;
private static String mClassname = LogUtils.class.getName();
private static ArrayList<String> mMethods = new ArrayList<>();

public static void setEnable(boolean logEnable) {
mLogEnable = logEnable;
}

public static void d(String tag, String msg) {
if (mLogEnable) {
Log.d(tag, getMsgWithLineNumber(msg));
}

}

public static void e(String tag, String msg) {
if (mLogEnable) {
Log.e(tag, getMsgWithLineNumber(msg));
}

}

public static void i(String tag, String msg) {
if (mLogEnable) {
Log.i(tag, getMsgWithLineNumber(msg));
}

}

public static void w(String tag, String msg) {
if (mLogEnable) {
Log.w(tag, getMsgWithLineNumber(msg));
}

}

public static void v(String tag, String msg) {
if (mLogEnable) {
Log.v(tag, getMsgWithLineNumber(msg));
}

}

public static void d(String msg) {
if (mLogEnable) {
String[] content = getMsgAndTagWithLineNumber(msg);
Log.d(content[0], content[1]);
}

}

public static void e(String msg) {
if (mLogEnable) {
String[] content = getMsgAndTagWithLineNumber(msg);
Log.e(content[0], content[1]);
}

}

public static void i(String msg) {
if (mLogEnable) {
String[] content = getMsgAndTagWithLineNumber(msg);
Log.i(content[0], content[1]);
}

}

public static void i() {
if (mLogEnable) {
String[] content = getMsgAndTagWithLineNumber("");
Log.i(content[0], content[1]);
}

}

public static void w(String msg) {
if (mLogEnable) {
String[] content = getMsgAndTagWithLineNumber(msg);
Log.w(content[0], content[1]);
}

}

public static void v(String msg) {
if (mLogEnable) {
String[] content = getMsgAndTagWithLineNumber(msg);
Log.v(content[0], content[1]);
}

}

public static String getMsgWithLineNumber(String msg) {
try {
StackTraceElement[] e = (new Throwable()).getStackTrace();
int var2 = e.length;

for (StackTraceElement st : e) {
if (!mClassname.equals(st.getClassName()) && !mMethods.contains(st.getMethodName())) {
int b = st.getClassName().lastIndexOf(".") + 1;
String TAG = st.getClassName().substring(b);
return TAG + "->" + st.getMethodName() + "():" + st.getLineNumber() + "->" + msg;
}
}
} catch (Exception var8) {
}

return msg;
}

public static String[] getMsgAndTagWithLineNumber(String msg) {
try {
StackTraceElement[] e = (new Throwable()).getStackTrace();
int var2 = e.length;

for (StackTraceElement st : e) {
if (!mClassname.equals(st.getClassName()) && !mMethods.contains(st.getMethodName())) {
int b = st.getClassName().lastIndexOf(".") + 1;
String TAG = st.getClassName().substring(b);
String message = st.getMethodName() + "():" + st.getLineNumber() + "->" + msg;
return new String[]{TAG, message};
}
}
} catch (Exception var9) {
}

return new String[]{"universal tag", msg};
}

static {
Method[] ms = LogUtils.class.getDeclaredMethods();
for (Method m : ms) {
mMethods.add(m.getName());
}
}
}
16 changes: 0 additions & 16 deletions Common/common/src/main/java/com/kymjs/common/Logger.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kymjs.common;
package com.kymjs.common.function;

import android.os.Handler;
import android.os.Looper;
Expand All @@ -12,20 +12,25 @@
* Created by ZhangTao on 9/1/16.
*/
public class ThreadSwitch extends Thread {
private static final int DEFAULT_SIZE = 8;

private final BlockingQueue<Runnable> mPoolWorkQueue = new LinkedBlockingQueue<>(200);
private final BlockingQueue<Runnable> mPoolWorkQueue;
private Handler handler = new Handler(Looper.getMainLooper());

private static class Holder {
private static final ThreadSwitch INSTANCE = new ThreadSwitch();
private static final ThreadSwitch INSTANCE = new ThreadSwitch(200);
}

public static ThreadSwitch singleton() {
return Holder.INSTANCE;
}

public static ThreadSwitch get() {
return new ThreadSwitch();
return new ThreadSwitch(DEFAULT_SIZE);
}

public static ThreadSwitch get(int size) {
return new ThreadSwitch(size);
}

////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -39,8 +44,9 @@ public interface IO extends Runnable {
public interface Break extends Runnable {
}

private ThreadSwitch() {
private ThreadSwitch(int size) {
this.start();
mPoolWorkQueue = new LinkedBlockingQueue<>(size);
}

public ThreadSwitch io(final IO func) {
Expand Down
2 changes: 1 addition & 1 deletion Common/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ DEVELOPER_NAME=ZhangTao
DEVELOPER_EMAIL=[email protected]

COMMON_MODULE_NAME=common
COMMON_MODULE_VERSION=1.0.2
COMMON_MODULE_VERSION=1.0.4

KOTLIN_COMMON_MODULE_NAME=kotlin-common
KOTLIN_COMMON_MODULE_VERSION=1.0.0

0 comments on commit 0e7e13b

Please sign in to comment.