提交 fdf8b3a4 编写于 作者: B Blankj

see 11/01 log

上级 1c36d1ed
......@@ -8,8 +8,6 @@ import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import com.blankj.utilcode.util.ScreenUtils;
/**
* <pre>
* author: Blankj
......
......@@ -400,7 +400,6 @@ public final class LogUtils {
Log.println(type, tag, msg);
return;
}
StringBuilder sb = new StringBuilder();
String[] lines = msg.split(LINE_SEP);
for (String line : lines) {
Log.println(type, tag, LEFT_BORDER + line);
......@@ -577,16 +576,6 @@ public final class LogUtils {
return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
}
private static boolean isSpace(final String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
private static void input2File(final String input, final String filePath) {
EXECUTOR.execute(new Runnable() {
@Override
......
@file:JvmName("StringUtils")
package com.blankj.utilcode.util
/**
* Return whether the string is null or 0-length.
*
* @param s The string.
* @return `true`: yes<br></br> `false`: no
*/
fun isEmpty(s: CharSequence?): Boolean {
return s == null || s.isEmpty()
}
object StringUtils {
/**
* Return whether the string is null or 0-length.
*
* @param s The string.
* @return `true`: yes<br></br> `false`: no
*/
@JvmStatic
fun isEmpty(s: CharSequence?): Boolean {
return s == null || s.isEmpty()
}
/**
* Return whether the string is null or whitespace.
*
* @param s The string.
* @return `true`: yes<br></br> `false`: no
*/
fun isTrimEmpty(s: String?): Boolean {
return s == null || s.trim { it <= ' ' }.isEmpty()
}
/**
* Return whether the string is null or whitespace.
*
* @param s The string.
* @return `true`: yes<br></br> `false`: no
*/
@JvmStatic
fun isTrimEmpty(s: String?): Boolean {
return s == null || s.trim { it <= ' ' }.isEmpty()
}
/**
* Return whether the string is null or white space.
*
* @param s The string.
* @return `true`: yes<br></br> `false`: no
*/
fun isSpace(s: String?): Boolean {
if (s == null) return true
var i = 0
val len = s.length
while (i < len) {
if (!Character.isWhitespace(s[i])) {
return false
/**
* Return whether the string is null or white space.
*
* @param s The string.
* @return `true`: yes<br></br> `false`: no
*/
@JvmStatic
fun isSpace(s: String?): Boolean {
if (s == null) return true
var i = 0
val len = s.length
while (i < len) {
if (!Character.isWhitespace(s[i])) {
return false
}
++i
}
++i
return true
}
return true
}
/**
* Return whether string1 is equals to string2.
*
* @param s1 The first string.
* @param s2 The second string.
* @return `true`: yes<br></br>`false`: no
*/
fun equals(s1: CharSequence?, s2: CharSequence?): Boolean {
if (s1 === s2) return true
if (s1 != null && s2 != null) {
val length: Int = s1.length
if (length == s2.length) {
if (s1 is String && s2 is String) {
return s1 == s2
} else {
for (i in 0 until length) {
if (s1[i] != s2[i]) return false
/**
* Return whether string1 is equals to string2.
*
* @param s1 The first string.
* @param s2 The second string.
* @return `true`: yes<br></br>`false`: no
*/
@JvmStatic
fun equals(s1: CharSequence?, s2: CharSequence?): Boolean {
if (s1 === s2) return true
if (s1 != null && s2 != null) {
val length: Int = s1.length
if (length == s2.length) {
if (s1 is String && s2 is String) {
return s1 == s2
} else {
for (i in 0 until length) {
if (s1[i] != s2[i]) return false
}
return true
}
return true
}
}
return false
}
return false
}
/**
* Return whether string1 is equals to string2, ignoring case considerations..
*
* @param s1 The first string.
* @param s2 The second string.
* @return `true`: yes<br></br>`false`: no
*/
fun equalsIgnoreCase(s1: String?, s2: String?): Boolean {
return s1.equals(s2, ignoreCase = true)
}
/**
* Return whether string1 is equals to string2, ignoring case considerations..
*
* @param s1 The first string.
* @param s2 The second string.
* @return `true`: yes<br></br>`false`: no
*/
@JvmStatic
fun equalsIgnoreCase(s1: String?, s2: String?): Boolean {
return s1.equals(s2, ignoreCase = true)
}
/**
* Return `""` if string equals null.
*
* @param s The string.
* @return `""` if string equals null
*/
fun null2Length0(s: String?): String {
return s ?: ""
}
/**
* Return `""` if string equals null.
*
* @param s The string.
* @return `""` if string equals null
*/
@JvmStatic
fun null2Length0(s: String?): String {
return s ?: ""
}
/**
* Return the length of string.
*
* @param s The string.
* @return the length of string
*/
fun length(s: CharSequence?): Int {
return s?.length ?: 0
}
/**
* Return the length of string.
*
* @param s The string.
* @return the length of string
*/
@JvmStatic
fun length(s: CharSequence?): Int {
return s?.length ?: 0
}
/**
* Set the first letter of string upper.
*
* @param s The string.
* @return the string with first letter upper.
*/
fun upperFirstLetter(s: String?): String {
if (s == null || s.isEmpty()) return ""
return if (!Character.isLowerCase(s[0])) s else (s[0].toInt() - 32).toChar().toString() + s.substring(1)
}
/**
* Set the first letter of string upper.
*
* @param s The string.
* @return the string with first letter upper.
*/
@JvmStatic
fun upperFirstLetter(s: String?): String {
if (s == null || s.isEmpty()) return ""
return if (!Character.isLowerCase(s[0])) s else (s[0].toInt() - 32).toChar().toString() + s.substring(1)
}
/**
* Set the first letter of string lower.
*
* @param s The string.
* @return the string with first letter lower.
*/
fun lowerFirstLetter(s: String?): String {
if (s == null || s.isEmpty()) return ""
return if (!Character.isUpperCase(s[0])) s else (s[0].toInt() + 32).toChar().toString() + s.substring(1)
}
/**
* Set the first letter of string lower.
*
* @param s The string.
* @return the string with first letter lower.
*/
@JvmStatic
fun lowerFirstLetter(s: String?): String {
if (s == null || s.isEmpty()) return ""
return if (!Character.isUpperCase(s[0])) s else (s[0].toInt() + 32).toChar().toString() + s.substring(1)
}
/**
* Reverse the string.
*
* @param s The string.
* @return the reverse string.
*/
fun reverse(s: String?): String {
if (s == null) return ""
val len = s.length
if (len <= 1) return s
val mid = len shr 1
val chars = s.toCharArray()
var c: Char
for (i in 0 until mid) {
c = chars[i]
chars[i] = chars[len - i - 1]
chars[len - i - 1] = c
/**
* Reverse the string.
*
* @param s The string.
* @return the reverse string.
*/
@JvmStatic
fun reverse(s: String?): String {
if (s == null) return ""
val len = s.length
if (len <= 1) return s
val mid = len shr 1
val chars = s.toCharArray()
var c: Char
for (i in 0 until mid) {
c = chars[i]
chars[i] = chars[len - i - 1]
chars[len - i - 1] = c
}
return String(chars)
}
return String(chars)
}
/**
* Convert string to DBC.
*
* @param s The string.
* @return the DBC string
*/
fun toDBC(s: String?): String {
if (s == null || s.isEmpty()) return ""
val chars = s.toCharArray()
var i = 0
val len = chars.size
while (i < len) {
when {
chars[i].toInt() == 12288 -> chars[i] = ' '
chars[i].toInt() in 65281..65374 -> chars[i] = (chars[i].toInt() - 65248).toChar()
else -> chars[i] = chars[i]
/**
* Convert string to DBC.
*
* @param s The string.
* @return the DBC string
*/
@JvmStatic
fun toDBC(s: String?): String {
if (s == null || s.isEmpty()) return ""
val chars = s.toCharArray()
var i = 0
val len = chars.size
while (i < len) {
when {
chars[i].toInt() == 12288 -> chars[i] = ' '
chars[i].toInt() in 65281..65374 -> chars[i] = (chars[i].toInt() - 65248).toChar()
else -> chars[i] = chars[i]
}
i++
}
i++
return String(chars)
}
return String(chars)
}
/**
* Convert string to SBC.
*
* @param s The string.
* @return the SBC string
*/
fun toSBC(s: String?): String {
if (s == null || s.isEmpty()) return ""
val chars = s.toCharArray()
var i = 0
val len = chars.size
while (i < len) {
when {
chars[i] == ' ' -> chars[i] = 12288.toChar()
chars[i].toInt() in 33..126 -> chars[i] = (chars[i].toInt() + 65248).toChar()
else -> chars[i] = chars[i]
/**
* Convert string to SBC.
*
* @param s The string.
* @return the SBC string
*/
@JvmStatic
fun toSBC(s: String?): String {
if (s == null || s.isEmpty()) return ""
val chars = s.toCharArray()
var i = 0
val len = chars.size
while (i < len) {
when {
chars[i] == ' ' -> chars[i] = 12288.toChar()
chars[i].toInt() in 33..126 -> chars[i] = (chars[i].toInt() + 65248).toChar()
else -> chars[i] = chars[i]
}
i++
}
i++
return String(chars)
}
return String(chars)
}
\ No newline at end of file
package com.blankj.utilcode.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.util.DisplayMetrics;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* <pre>
* author:
* ___ ___ ___ ___
* _____ / /\ /__/\ /__/| / /\
* / /::\ / /::\ \ \:\ | |:| / /:/
* / /:/\:\ ___ ___ / /:/\:\ \ \:\ | |:| /__/::\
* / /:/~/::\ /__/\ / /\ / /:/~/::\ _____\__\:\ __| |:| \__\/\:\
* /__/:/ /:/\:| \ \:\ / /:/ /__/:/ /:/\:\ /__/::::::::\ /__/\_|:|____ \ \:\
* \ \:\/:/~/:/ \ \:\ /:/ \ \:\/:/__\/ \ \:\~~\~~\/ \ \:\/:::::/ \__\:\
* \ \::/ /:/ \ \:\/:/ \ \::/ \ \:\ ~~~ \ \::/~~~~ / /:/
* \ \:\/:/ \ \::/ \ \:\ \ \:\ \ \:\ /__/:/
* \ \::/ \__\/ \ \:\ \ \:\ \ \:\ \__\/
* \__\/ \__\/ \__\/ \__\/
* blog : http://blankj.com
* time : 16/12/08
* desc : utils about initialization
* </pre>
*/
public final class Utils {
@SuppressLint("StaticFieldLeak")
private static Application sApplication;
private static final ActivityLifecycleImpl ACTIVITY_LIFECYCLE = new ActivityLifecycleImpl();
private Utils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Init utils.
* <p>Init it in the class of Application.</p>
*
* @param context context
*/
public static void init(final Context context) {
if (context == null) {
init(getApplicationByReflect());
return;
}
init((Application) context.getApplicationContext());
}
/**
* Init utils.
* <p>Init it in the class of Application.</p>
*
* @param app application
*/
public static void init(final Application app) {
if (sApplication == null) {
if (app == null) {
sApplication = getApplicationByReflect();
} else {
sApplication = app;
}
sApplication.registerActivityLifecycleCallbacks(ACTIVITY_LIFECYCLE);
}
}
/**
* Return the context of Application object.
*
* @return the context of Application object
*/
public static Application getApp() {
if (sApplication != null) return sApplication;
Application app = getApplicationByReflect();
init(app);
return app;
}
private static Application getApplicationByReflect() {
try {
@SuppressLint("PrivateApi")
Class<?> activityThread = Class.forName("android.app.ActivityThread");
Object thread = activityThread.getMethod("currentActivityThread").invoke(null);
Object app = activityThread.getMethod("getApplication").invoke(thread);
if (app == null) {
throw new NullPointerException("u should init first");
}
return (Application) app;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
throw new NullPointerException("u should init first");
}
static ActivityLifecycleImpl getActivityLifecycle() {
return ACTIVITY_LIFECYCLE;
}
static LinkedList<Activity> getActivityList() {
return ACTIVITY_LIFECYCLE.mActivityList;
}
static Context getTopActivityOrApp() {
if (isAppForeground()) {
Activity topActivity = ACTIVITY_LIFECYCLE.getTopActivity();
return topActivity == null ? Utils.getApp() : topActivity;
} else {
return Utils.getApp();
}
}
static boolean isAppForeground() {
ActivityManager am =
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
//noinspection ConstantConditions
List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
if (info == null || info.size() == 0) return false;
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return aInfo.processName.equals(Utils.getApp().getPackageName());
}
}
return false;
}
static final AdaptScreenArgs ADAPT_SCREEN_ARGS = new AdaptScreenArgs();
static void restoreAdaptScreen() {
final DisplayMetrics systemDm = Resources.getSystem().getDisplayMetrics();
final DisplayMetrics appDm = Utils.getApp().getResources().getDisplayMetrics();
final Activity activity = ACTIVITY_LIFECYCLE.getTopActivity();
if (activity != null) {
final DisplayMetrics activityDm = activity.getResources().getDisplayMetrics();
if (ADAPT_SCREEN_ARGS.isVerticalSlide) {
activityDm.density = activityDm.widthPixels / (float) ADAPT_SCREEN_ARGS.sizeInPx;
} else {
activityDm.density = activityDm.heightPixels / (float) ADAPT_SCREEN_ARGS.sizeInPx;
}
activityDm.scaledDensity = activityDm.density * (systemDm.scaledDensity / systemDm.density);
activityDm.densityDpi = (int) (160 * activityDm.density);
appDm.density = activityDm.density;
appDm.scaledDensity = activityDm.scaledDensity;
appDm.densityDpi = activityDm.densityDpi;
} else {
if (ADAPT_SCREEN_ARGS.isVerticalSlide) {
appDm.density = appDm.widthPixels / (float) ADAPT_SCREEN_ARGS.sizeInPx;
} else {
appDm.density = appDm.heightPixels / (float) ADAPT_SCREEN_ARGS.sizeInPx;
}
appDm.scaledDensity = appDm.density * (systemDm.scaledDensity / systemDm.density);
appDm.densityDpi = (int) (160 * appDm.density);
}
}
static void cancelAdaptScreen() {
final DisplayMetrics systemDm = Resources.getSystem().getDisplayMetrics();
final DisplayMetrics appDm = Utils.getApp().getResources().getDisplayMetrics();
final Activity activity = ACTIVITY_LIFECYCLE.getTopActivity();
if (activity != null) {
final DisplayMetrics activityDm = activity.getResources().getDisplayMetrics();
activityDm.density = systemDm.density;
activityDm.scaledDensity = systemDm.scaledDensity;
activityDm.densityDpi = systemDm.densityDpi;
}
appDm.density = systemDm.density;
appDm.scaledDensity = systemDm.scaledDensity;
appDm.densityDpi = systemDm.densityDpi;
}
static boolean isAdaptScreen() {
final DisplayMetrics systemDm = Resources.getSystem().getDisplayMetrics();
final DisplayMetrics appDm = Utils.getApp().getResources().getDisplayMetrics();
return systemDm.density != appDm.density;
}
static class AdaptScreenArgs {
int sizeInPx;
boolean isVerticalSlide;
}
static class ActivityLifecycleImpl implements ActivityLifecycleCallbacks {
final LinkedList<Activity> mActivityList = new LinkedList<>();
final HashMap<Object, OnAppStatusChangedListener> mStatusListenerMap = new HashMap<>();
private int mForegroundCount = 0;
private int mConfigCount = 0;
void addListener(final Object object, final OnAppStatusChangedListener listener) {
mStatusListenerMap.put(object, listener);
}
void removeListener(final Object object) {
mStatusListenerMap.remove(object);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
setTopActivity(activity);
}
@Override
public void onActivityStarted(Activity activity) {
setTopActivity(activity);
if (mForegroundCount <= 0) {
postStatus(true);
}
if (mConfigCount < 0) {
++mConfigCount;
} else {
++mForegroundCount;
}
}
@Override
public void onActivityResumed(Activity activity) {
setTopActivity(activity);
}
@Override
public void onActivityPaused(Activity activity) {/**/}
@Override
public void onActivityStopped(Activity activity) {
if (activity.isChangingConfigurations()) {
--mConfigCount;
} else {
--mForegroundCount;
if (mForegroundCount <= 0) {
postStatus(false);
}
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {/**/}
@Override
public void onActivityDestroyed(Activity activity) {
mActivityList.remove(activity);
}
private void postStatus(final boolean isForeground) {
if (mStatusListenerMap.isEmpty()) return;
for (OnAppStatusChangedListener onAppStatusChangedListener : mStatusListenerMap.values()) {
if (onAppStatusChangedListener == null) return;
if (isForeground) {
onAppStatusChangedListener.onForeground();
} else {
onAppStatusChangedListener.onBackground();
}
}
}
private void setTopActivity(final Activity activity) {
if (activity.getClass() == PermissionUtils.PermissionActivity.class) return;
if (mActivityList.contains(activity)) {
if (!mActivityList.getLast().equals(activity)) {
mActivityList.remove(activity);
mActivityList.addLast(activity);
}
} else {
mActivityList.addLast(activity);
}
}
Activity getTopActivity() {
if (!mActivityList.isEmpty()) {
final Activity topActivity = mActivityList.getLast();
if (topActivity != null) {
return topActivity;
}
}
Activity topActivityByReflect = getTopActivityByReflect();
if (topActivityByReflect != null) {
setTopActivity(topActivityByReflect);
}
return topActivityByReflect;
}
private Activity getTopActivityByReflect() {
try {
@SuppressLint("PrivateApi")
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivityList");
activitiesField.setAccessible(true);
Map activities = (Map) activitiesField.get(activityThread);
if (activities == null) return null;
for (Object activityRecord : activities.values()) {
Class activityRecordClass = activityRecord.getClass();
Field pausedField = activityRecordClass.getDeclaredField("paused");
pausedField.setAccessible(true);
if (!pausedField.getBoolean(activityRecord)) {
Field activityField = activityRecordClass.getDeclaredField("activity");
activityField.setAccessible(true);
return (Activity) activityField.get(activityRecord);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return null;
}
}
public static final class FileProvider4UtilCode extends FileProvider {
@Override
public boolean onCreate() {
Utils.init(getContext());
return true;
}
}
///////////////////////////////////////////////////////////////////////////
// interface
///////////////////////////////////////////////////////////////////////////
public interface OnAppStatusChangedListener {
void onForeground();
void onBackground();
}
}
@file:JvmName("Utils")
package com.blankj.utilcode.util
import android.annotation.SuppressLint
import android.app.Activity
import android.app.ActivityManager
import android.app.Application
import android.app.Application.ActivityLifecycleCallbacks
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.support.v4.content.FileProvider
import java.lang.reflect.InvocationTargetException
import java.util.*
@SuppressLint("StaticFieldLeak")
private var sApplication: Application? = null
private val ACTIVITY_LIFECYCLE = ActivityLifecycleImpl()
/**
* Init utils.
*
* Init it in the class of Application.
*
* @param context context
*/
fun init(context: Context?) {
if (context == null) {
init(getApplicationByReflect())
return
}
init(context.applicationContext as Application)
}
/**
* Init utils.
*
* Init it in the class of Application.
*
* @param app application
*/
fun init(app: Application?) {
if (sApplication == null) {
sApplication = app ?: getApplicationByReflect()
sApplication!!.registerActivityLifecycleCallbacks(ACTIVITY_LIFECYCLE)
}
}
/**
* Return the context of Application object.
*
* @return the context of Application object
*/
fun getApp(): Application {
if (sApplication != null) return sApplication as Application
val app = getApplicationByReflect()
init(app)
return app
}
private fun getApplicationByReflect(): Application {
try {
@SuppressLint("PrivateApi")
val activityThread = Class.forName("android.app.ActivityThread")
val thread = activityThread.getMethod("currentActivityThread").invoke(null)
val app = activityThread.getMethod("getApplication").invoke(thread)
?: throw NullPointerException("u should init first")
return app as Application
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: ClassNotFoundException) {
e.printStackTrace()
}
throw NullPointerException("u should init first")
}
internal fun getActivityLifecycle(): ActivityLifecycleImpl {
return ACTIVITY_LIFECYCLE
}
internal fun getActivityList(): LinkedList<Activity> {
return ACTIVITY_LIFECYCLE.mActivityList
}
fun getTopActivityOrApp(): Context {
return if (isAppForeground()) {
val topActivity = ACTIVITY_LIFECYCLE.topActivity
topActivity ?: getApp()
} else {
getApp()
}
}
internal fun isAppForeground(): Boolean {
val am = getApp().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val info = am.runningAppProcesses
if (info == null || info.size == 0) return false
for (aInfo in info) {
if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return aInfo.processName == getApp().packageName
}
}
return false
}
internal val ADAPT_SCREEN_ARGS = AdaptScreenArgs()
internal fun restoreAdaptScreen() {
val systemDm = Resources.getSystem().displayMetrics
val appDm = getApp().resources.displayMetrics
val activity = ACTIVITY_LIFECYCLE.topActivity
if (activity != null) {
val activityDm = activity.resources.displayMetrics
if (ADAPT_SCREEN_ARGS.isVerticalSlide) {
activityDm.density = activityDm.widthPixels / ADAPT_SCREEN_ARGS.sizeInPx.toFloat()
} else {
activityDm.density = activityDm.heightPixels / ADAPT_SCREEN_ARGS.sizeInPx.toFloat()
}
activityDm.scaledDensity = activityDm.density * (systemDm.scaledDensity / systemDm.density)
activityDm.densityDpi = (160 * activityDm.density).toInt()
appDm.density = activityDm.density
appDm.scaledDensity = activityDm.scaledDensity
appDm.densityDpi = activityDm.densityDpi
} else {
if (ADAPT_SCREEN_ARGS.isVerticalSlide) {
appDm.density = appDm.widthPixels / ADAPT_SCREEN_ARGS.sizeInPx.toFloat()
} else {
appDm.density = appDm.heightPixels / ADAPT_SCREEN_ARGS.sizeInPx.toFloat()
}
appDm.scaledDensity = appDm.density * (systemDm.scaledDensity / systemDm.density)
appDm.densityDpi = (160 * appDm.density).toInt()
}
}
internal fun cancelAdaptScreen() {
val systemDm = Resources.getSystem().displayMetrics
val appDm = getApp().resources.displayMetrics
val activity = ACTIVITY_LIFECYCLE.topActivity
if (activity != null) {
val activityDm = activity.resources.displayMetrics
activityDm.density = systemDm.density
activityDm.scaledDensity = systemDm.scaledDensity
activityDm.densityDpi = systemDm.densityDpi
}
appDm.density = systemDm.density
appDm.scaledDensity = systemDm.scaledDensity
appDm.densityDpi = systemDm.densityDpi
}
internal fun isAdaptScreen(): Boolean {
val systemDm = Resources.getSystem().displayMetrics
val appDm = getApp().resources.displayMetrics
return systemDm.density != appDm.density
}
internal class AdaptScreenArgs {
var sizeInPx: Int = 0
var isVerticalSlide: Boolean = false
}
internal class ActivityLifecycleImpl : ActivityLifecycleCallbacks {
val mActivityList: LinkedList<Activity> = LinkedList()
private val mStatusListenerMap: HashMap<Any, OnAppStatusChangedListener> = HashMap()
private var mForegroundCount = 0
private var mConfigCount = 0
var topActivity: Activity?
get() {
if (!mActivityList.isEmpty()) {
val topActivity = mActivityList.last
if (topActivity != null) {
return topActivity
}
}
val topActivityByReflect = topActivityByReflect
if (topActivityByReflect != null) {
topActivity = topActivityByReflect
}
return topActivityByReflect
}
private set(activity) {
if (activity?.javaClass == PermissionUtils.PermissionActivity::class.java) return
if (mActivityList.contains(activity)) {
if (!mActivityList.last.equals(activity)) {
mActivityList.remove(activity)
mActivityList.addLast(activity)
}
} else {
mActivityList.addLast(activity)
}
}
private val topActivityByReflect: Activity?
get() {
try {
@SuppressLint("PrivateApi")
val activityThreadClass = Class.forName("android.app.ActivityThread")
val activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null)
val activitiesField = activityThreadClass.getDeclaredField("mActivityList")
activitiesField.isAccessible = true
val activities = activitiesField.get(activityThread) as Map<*, *>
for (activityRecord in activities.values) {
if (activityRecord == null) continue
val activityRecordClass = activityRecord.javaClass
val pausedField = activityRecordClass.getDeclaredField("paused")
pausedField.isAccessible = true
if (!pausedField.getBoolean(activityRecord)) {
val activityField = activityRecordClass.getDeclaredField("activity")
activityField.isAccessible = true
return activityField.get(activityRecord) as Activity
}
}
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: NoSuchFieldException) {
e.printStackTrace()
}
return null
}
fun addListener(obj: Any, listener: OnAppStatusChangedListener) {
mStatusListenerMap[obj] = listener
}
fun removeListener(obj: Any) {
mStatusListenerMap.remove(obj)
}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle) {
topActivity = activity
}
override fun onActivityStarted(activity: Activity) {
topActivity = activity
if (mForegroundCount <= 0) {
postStatus(true)
}
if (mConfigCount < 0) {
++mConfigCount
} else {
++mForegroundCount
}
}
override fun onActivityResumed(activity: Activity) {
topActivity = activity
}
override fun onActivityPaused(activity: Activity) {/**/
}
override fun onActivityStopped(activity: Activity) {
if (activity.isChangingConfigurations) {
--mConfigCount
} else {
--mForegroundCount
if (mForegroundCount <= 0) {
postStatus(false)
}
}
}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {/**/
}
override fun onActivityDestroyed(activity: Activity) {
mActivityList.remove(activity)
}
private fun postStatus(isForeground: Boolean) {
if (mStatusListenerMap.isEmpty()) return
for (onAppStatusChangedListener in mStatusListenerMap.values) {
if (isForeground) {
onAppStatusChangedListener.onForeground()
} else {
onAppStatusChangedListener.onBackground()
}
}
}
}
class FileProvider4UtilCode : FileProvider() {
override fun onCreate(): Boolean {
init(context)
return true
}
}
///////////////////////////////////////////////////////////////////////////
// interface
///////////////////////////////////////////////////////////////////////////
interface OnAppStatusChangedListener {
fun onForeground()
fun onBackground()
}
\ No newline at end of file
package com.blankj.utilcode.util;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
......@@ -291,12 +293,22 @@ public final class ZipUtils {
if (isSpace(keyword)) {
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (entryName.contains("../")) {
Log.e("ZipUtils", "it's dangerous!");
return files;
}
if (!unzipChildFile(destDir, files, zip, entry)) return files;
}
} else {
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
if (entry.getName().contains(keyword)) {
String entryName = entry.getName();
if (entryName.contains("../")) {
Log.e("ZipUtils", "it's dangerous!");
return files;
}
if (entryName.contains(keyword)) {
if (!unzipChildFile(destDir, files, zip, entry)) return files;
}
}
......
package com.blankj.utilcode.util;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
......@@ -291,12 +293,22 @@ public final class ZipUtils {
if (isSpace(keyword)) {
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (entryName.contains("../")) {
Log.e("ZipUtils", "it's dangerous!");
return files;
}
if (!unzipChildFile(destDir, files, zip, entry)) return files;
}
} else {
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
if (entry.getName().contains(keyword)) {
String entryName = entry.getName();
if (entryName.contains("../")) {
Log.e("ZipUtils", "it's dangerous!");
return files;
}
if (entryName.contains(keyword)) {
if (!unzipChildFile(destDir, files, zip, entry)) return files;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册