Skip to content

Commit 1f65fcc

Browse files
committed
see 09/23 log
1 parent 8891b73 commit 1f65fcc

File tree

9 files changed

+515
-318
lines changed

9 files changed

+515
-318
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.blankj.utilcode.utils;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
7+
/**
8+
* <pre>
9+
* author: Blankj
10+
* blog : http://blankj.com
11+
* time : 2016/9/23
12+
* desc : Activity工具类
13+
* </pre>
14+
*/
15+
public class ActivityUtils {
16+
17+
private ActivityUtils() {
18+
throw new UnsupportedOperationException("u can't fuck me...");
19+
}
20+
21+
/**
22+
* 判断是否存在指定Activity
23+
*
24+
* @param context 上下文
25+
* @param packageName 包名
26+
* @param className activity全路径类名
27+
* @return {@code true}: 是<br>{@code false}: 否
28+
*/
29+
public static boolean isExistActivity(Context context, String packageName, String className) {
30+
Intent intent = new Intent();
31+
intent.setClassName(packageName, className);
32+
return !(context.getPackageManager().resolveActivity(intent, 0) == null ||
33+
intent.resolveActivity(context.getPackageManager()) == null ||
34+
context.getPackageManager().queryIntentActivities(intent, 0).size() == 0);
35+
}
36+
37+
/**
38+
* 打开指定的Activity
39+
*
40+
* @param context 上下文
41+
* @param packageName 包名
42+
* @param className 全类名
43+
*/
44+
public static void launchActivity(Context context, String packageName, String className) {
45+
launchActivity(context, packageName, className, null);
46+
}
47+
48+
/**
49+
* 打开指定的Activity
50+
*
51+
* @param context 上下文
52+
* @param packageName 包名
53+
* @param className 全类名
54+
* @param bundle bundle
55+
*/
56+
public static void launchActivity(Context context, String packageName, String className, Bundle bundle) {
57+
context.startActivity(IntentUtils.getComponentNameIntent(packageName, className, bundle));
58+
}
59+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.blankj.utilcode.utils;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.os.Build;
6+
import android.util.TypedValue;
7+
import android.view.Window;
8+
import android.view.WindowManager;
9+
10+
import java.lang.reflect.Method;
11+
12+
/**
13+
* <pre>
14+
* author: Blankj
15+
* blog : http://blankj.com
16+
* time : 2016/9/23
17+
* desc : 栏相关工具类
18+
* </pre>
19+
*/
20+
public class BarUtils {
21+
private BarUtils() {
22+
throw new UnsupportedOperationException("u can't fuck me...");
23+
}
24+
25+
/**
26+
* 设置透明状态栏(api大于19方可使用)
27+
* <p>可在Activity的onCreat()中调用</p>
28+
* <p>需在顶部控件布局中加入以下属性让内容出现在状态栏之下</p>
29+
* <p>android:clipToPadding="true"</p>
30+
* <p>android:fitsSystemWindows="true"</p>
31+
*
32+
* @param activity activity
33+
*/
34+
public static void setTransparentStatusBar(Activity activity) {
35+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
36+
//透明状态栏
37+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
38+
//透明导航栏
39+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
40+
}
41+
}
42+
43+
/**
44+
* 隐藏状态栏
45+
* <p>也就是设置全屏,一定要在setContentView之前调用,否则报错</p>
46+
* <p>此方法Activity可以继承AppCompatActivity</p>
47+
* <p>启动的时候状态栏会显示一下再隐藏,比如QQ的欢迎界面</p>
48+
* <p>在配置文件中Activity加属性android:theme="@android:style/Theme.NoTitleBar.Fullscreen"</p>
49+
* <p>如加了以上配置Activity不能继承AppCompatActivity,会报错</p>
50+
*
51+
* @param activity activity
52+
*/
53+
public static void hideStatusBar(Activity activity) {
54+
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
55+
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
56+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
57+
}
58+
59+
/**
60+
* 获取状态栏高度
61+
*
62+
* @param context 上下文
63+
* @return 状态栏高度
64+
*/
65+
public static int getStatusBarHeight(Context context) {
66+
int result = 0;
67+
int resourceId = context.getResources()
68+
.getIdentifier("status_bar_height", "dimen", "android");
69+
if (resourceId > 0) {
70+
result = context.getResources().getDimensionPixelSize(resourceId);
71+
}
72+
return result;
73+
}
74+
75+
/**
76+
* 判断状态栏是否存在
77+
*
78+
* @param activity activity
79+
* @return {@code true}: 存在<br>{@code false}: 不存在
80+
*/
81+
public static boolean isStatusBarExists(Activity activity) {
82+
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
83+
return (params.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != WindowManager.LayoutParams.FLAG_FULLSCREEN;
84+
}
85+
86+
/**
87+
* 获取ActionBar高度
88+
*
89+
* @param activity activity
90+
* @return ActionBar高度
91+
*/
92+
public static int getActionBarHeight(Activity activity) {
93+
TypedValue tv = new TypedValue();
94+
if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
95+
return TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
96+
}
97+
return 0;
98+
}
99+
100+
/**
101+
* 显示通知栏
102+
* <p>需添加权限 {@code <uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>}</p>
103+
*
104+
* @param context 上下文
105+
* @param isSettingPanel {@code true}: 打开设置<br>{@code false}: 打开通知
106+
*/
107+
public static void showNotificationBar(Context context, boolean isSettingPanel) {
108+
String methodName = (Build.VERSION.SDK_INT <= 16) ? "expand"
109+
: (isSettingPanel ? "expandSettingsPanel" : "expandNotificationsPanel");
110+
invokePanels(context, methodName);
111+
}
112+
113+
/**
114+
* 隐藏通知栏
115+
* <p>需添加权限 {@code <uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>}</p>
116+
*
117+
* @param context 上下文
118+
*/
119+
public static void hideNotificationBar(Context context) {
120+
String methodName = (Build.VERSION.SDK_INT <= 16) ? "collapse" : "collapsePanels";
121+
invokePanels(context, methodName);
122+
}
123+
124+
/**
125+
* 反射唤醒通知栏
126+
*
127+
* @param context 上下文
128+
* @param methodName 方法名
129+
*/
130+
private static void invokePanels(Context context, String methodName) {
131+
try {
132+
Object service = context.getSystemService("statusbar");
133+
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
134+
Method expand = statusBarManager.getMethod(methodName);
135+
expand.invoke(service);
136+
} catch (Exception e) {
137+
e.printStackTrace();
138+
}
139+
}
140+
}

utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -713,81 +713,6 @@ public static boolean writeFileFromString(File file, String content, boolean app
713713
}
714714
}
715715

716-
/**
717-
* 简单获取文件编码格式
718-
*
719-
* @param filePath 文件路径
720-
* @return 文件编码
721-
*/
722-
public static String getFileCharsetSimple(String filePath) {
723-
return getFileCharsetSimple(getFileByPath(filePath));
724-
}
725-
726-
/**
727-
* 简单获取文件编码格式
728-
*
729-
* @param file 文件
730-
* @return 文件编码
731-
*/
732-
public static String getFileCharsetSimple(File file) {
733-
int p = 0;
734-
InputStream is = null;
735-
try {
736-
is = new BufferedInputStream(new FileInputStream(file));
737-
p = (is.read() << 8) + is.read();
738-
} catch (IOException e) {
739-
e.printStackTrace();
740-
} finally {
741-
closeIO(is);
742-
}
743-
switch (p) {
744-
case 0xefbb:
745-
return "UTF-8";
746-
case 0xfffe:
747-
return "Unicode";
748-
case 0xfeff:
749-
return "UTF-16BE";
750-
default:
751-
return "GBK";
752-
}
753-
}
754-
755-
/**
756-
* 获取文件行数
757-
*
758-
* @param filePath 文件路径
759-
* @return 文件行数
760-
*/
761-
public static int getFileLines(String filePath) {
762-
return getFileLines(getFileByPath(filePath));
763-
}
764-
765-
/**
766-
* 获取文件行数
767-
*
768-
* @param file 文件
769-
* @return 文件行数
770-
*/
771-
public static int getFileLines(File file) {
772-
int count = 1;
773-
InputStream is = null;
774-
try {
775-
is = new BufferedInputStream(new FileInputStream(file));
776-
byte[] buffer = new byte[KB];
777-
int readChars;
778-
while ((readChars = is.read(buffer, 0, KB)) != -1) {
779-
for (int i = 0; i < readChars; ++i) {
780-
if (buffer[i] == '\n') ++count;
781-
}
782-
}
783-
} catch (IOException e) {
784-
e.printStackTrace();
785-
} finally {
786-
closeIO(is);
787-
}
788-
return count;
789-
}
790-
791716
/**
792717
* 指定编码按行读取文件到List
793718
*
@@ -903,7 +828,7 @@ public static String readFile2String(File file, String charsetName) {
903828
}
904829

905830
/**
906-
* 指定编码按行读取文件到字符串中
831+
* 指定编码按行读取文件到字符数组中
907832
*
908833
* @param filePath 文件路径
909834
* @return StringBuilder对象
@@ -913,7 +838,7 @@ public static byte[] readFile2Bytes(String filePath) {
913838
}
914839

915840
/**
916-
* 指定编码按行读取文件到字符串中
841+
* 指定编码按行读取文件到字符数组中
917842
*
918843
* @param file 文件
919844
* @return StringBuilder对象
@@ -928,6 +853,81 @@ public static byte[] readFile2Bytes(File file) {
928853
}
929854
}
930855

856+
/**
857+
* 简单获取文件编码格式
858+
*
859+
* @param filePath 文件路径
860+
* @return 文件编码
861+
*/
862+
public static String getFileCharsetSimple(String filePath) {
863+
return getFileCharsetSimple(getFileByPath(filePath));
864+
}
865+
866+
/**
867+
* 简单获取文件编码格式
868+
*
869+
* @param file 文件
870+
* @return 文件编码
871+
*/
872+
public static String getFileCharsetSimple(File file) {
873+
int p = 0;
874+
InputStream is = null;
875+
try {
876+
is = new BufferedInputStream(new FileInputStream(file));
877+
p = (is.read() << 8) + is.read();
878+
} catch (IOException e) {
879+
e.printStackTrace();
880+
} finally {
881+
closeIO(is);
882+
}
883+
switch (p) {
884+
case 0xefbb:
885+
return "UTF-8";
886+
case 0xfffe:
887+
return "Unicode";
888+
case 0xfeff:
889+
return "UTF-16BE";
890+
default:
891+
return "GBK";
892+
}
893+
}
894+
895+
/**
896+
* 获取文件行数
897+
*
898+
* @param filePath 文件路径
899+
* @return 文件行数
900+
*/
901+
public static int getFileLines(String filePath) {
902+
return getFileLines(getFileByPath(filePath));
903+
}
904+
905+
/**
906+
* 获取文件行数
907+
*
908+
* @param file 文件
909+
* @return 文件行数
910+
*/
911+
public static int getFileLines(File file) {
912+
int count = 1;
913+
InputStream is = null;
914+
try {
915+
is = new BufferedInputStream(new FileInputStream(file));
916+
byte[] buffer = new byte[KB];
917+
int readChars;
918+
while ((readChars = is.read(buffer, 0, KB)) != -1) {
919+
for (int i = 0; i < readChars; ++i) {
920+
if (buffer[i] == '\n') ++count;
921+
}
922+
}
923+
} catch (IOException e) {
924+
e.printStackTrace();
925+
} finally {
926+
closeIO(is);
927+
}
928+
return count;
929+
}
930+
931931
/**
932932
* 获取文件大小
933933
*

0 commit comments

Comments
 (0)