Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
fdf8b3a4
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fdf8b3a4
编写于
11月 01, 2018
作者:
B
Blankj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 11/01 log
上级
1c36d1ed
变更
8
展开全部
显示空白变更内容
内联
并排
Showing
8 changed file
with
1536 addition
and
533 deletion
+1536
-533
app/src/main/java/com/blankj/androidutilcode/base/BaseActivity.java
...in/java/com/blankj/androidutilcode/base/BaseActivity.java
+0
-2
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/LogUtils.java
...tlin/src/main/java/com/blankj/utilcode/util/LogUtils.java
+0
-11
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/LogUtils.kt
...kotlin/src/main/java/com/blankj/utilcode/util/LogUtils.kt
+1021
-0
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/StringUtils.kt
...lin/src/main/java/com/blankj/utilcode/util/StringUtils.kt
+172
-160
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/Utils.java
...-kotlin/src/main/java/com/blankj/utilcode/util/Utils.java
+0
-358
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/Utils.kt
...de-kotlin/src/main/java/com/blankj/utilcode/util/Utils.kt
+317
-0
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/ZipUtils.java
...tlin/src/main/java/com/blankj/utilcode/util/ZipUtils.java
+13
-1
utilcode/src/main/java/com/blankj/utilcode/util/ZipUtils.java
...code/src/main/java/com/blankj/utilcode/util/ZipUtils.java
+13
-1
未找到文件。
app/src/main/java/com/blankj/androidutilcode/base/BaseActivity.java
浏览文件 @
fdf8b3a4
...
...
@@ -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
...
...
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/LogUtils.java
浏览文件 @
fdf8b3a4
...
...
@@ -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
...
...
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/LogUtils.kt
0 → 100644
浏览文件 @
fdf8b3a4
此差异已折叠。
点击以展开。
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/StringUtils.kt
浏览文件 @
fdf8b3a4
@
file
:
JvmName
(
"StringUtils"
)
package
com.blankj.utilcode.util
/**
object
StringUtils
{
/**
* 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
{
@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
{
@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
{
@JvmStatic
fun
isSpace
(
s
:
String
?):
Boolean
{
if
(
s
==
null
)
return
true
var
i
=
0
val
len
=
s
.
length
...
...
@@ -39,16 +41,17 @@ fun isSpace(s: String?): Boolean {
++
i
}
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
{
@JvmStatic
fun
equals
(
s1
:
CharSequence
?,
s2
:
CharSequence
?):
Boolean
{
if
(
s1
===
s2
)
return
true
if
(
s1
!=
null
&&
s2
!=
null
)
{
val
length
:
Int
=
s1
.
length
...
...
@@ -64,68 +67,74 @@ fun equals(s1: CharSequence?, s2: CharSequence?): Boolean {
}
}
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
{
@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
{
@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
{
@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
{
@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
{
@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
{
@JvmStatic
fun
reverse
(
s
:
String
?):
String
{
if
(
s
==
null
)
return
""
val
len
=
s
.
length
if
(
len
<=
1
)
return
s
...
...
@@ -138,15 +147,16 @@ fun reverse(s: String?): String {
chars
[
len
-
i
-
1
]
=
c
}
return
String
(
chars
)
}
}
/**
/**
* Convert string to DBC.
*
* @param s The string.
* @return the DBC string
*/
fun
toDBC
(
s
:
String
?):
String
{
@JvmStatic
fun
toDBC
(
s
:
String
?):
String
{
if
(
s
==
null
||
s
.
isEmpty
())
return
""
val
chars
=
s
.
toCharArray
()
var
i
=
0
...
...
@@ -160,15 +170,16 @@ fun toDBC(s: String?): String {
i
++
}
return
String
(
chars
)
}
}
/**
/**
* Convert string to SBC.
*
* @param s The string.
* @return the SBC string
*/
fun
toSBC
(
s
:
String
?):
String
{
@JvmStatic
fun
toSBC
(
s
:
String
?):
String
{
if
(
s
==
null
||
s
.
isEmpty
())
return
""
val
chars
=
s
.
toCharArray
()
var
i
=
0
...
...
@@ -182,4 +193,5 @@ fun toSBC(s: String?): String {
i
++
}
return
String
(
chars
)
}
}
\ No newline at end of file
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/Utils.java
已删除
100644 → 0
浏览文件 @
1c36d1ed
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
();
}
}
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/Utils.kt
0 → 100644
浏览文件 @
fdf8b3a4
@
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
utilcode-kotlin/src/main/java/com/blankj/utilcode/util/ZipUtils.java
浏览文件 @
fdf8b3a4
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
;
}
}
...
...
utilcode/src/main/java/com/blankj/utilcode/util/ZipUtils.java
浏览文件 @
fdf8b3a4
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录