723 lines
26 KiB
Java
723 lines
26 KiB
Java
|
|
package com.tfq.library.utils;
|
|||
|
|
|
|||
|
|
import android.app.Activity;
|
|||
|
|
import android.app.ActivityManager;
|
|||
|
|
import android.app.AlertDialog;
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.content.ContextWrapper;
|
|||
|
|
import android.content.DialogInterface;
|
|||
|
|
import android.content.Intent;
|
|||
|
|
import android.content.pm.ActivityInfo;
|
|||
|
|
import android.content.pm.PackageInfo;
|
|||
|
|
import android.content.pm.PackageManager;
|
|||
|
|
import android.net.ConnectivityManager;
|
|||
|
|
import android.net.NetworkCapabilities;
|
|||
|
|
import android.net.Uri;
|
|||
|
|
import android.os.Build;
|
|||
|
|
import android.os.Environment;
|
|||
|
|
import android.os.StatFs;
|
|||
|
|
import android.text.TextUtils;
|
|||
|
|
import android.view.View;
|
|||
|
|
import android.view.WindowManager;
|
|||
|
|
import android.view.inputmethod.InputMethodManager;
|
|||
|
|
import android.widget.EditText;
|
|||
|
|
import android.widget.TextView;
|
|||
|
|
|
|||
|
|
import androidx.core.content.FileProvider;
|
|||
|
|
|
|||
|
|
import com.gyf.immersionbar.BarHide;
|
|||
|
|
import com.gyf.immersionbar.ImmersionBar;
|
|||
|
|
import com.tfq.library.app.LibraryApp;
|
|||
|
|
|
|||
|
|
import java.io.ByteArrayInputStream;
|
|||
|
|
import java.io.File;
|
|||
|
|
import java.lang.reflect.Field;
|
|||
|
|
import java.security.cert.CertificateFactory;
|
|||
|
|
import java.security.cert.X509Certificate;
|
|||
|
|
import java.util.Iterator;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Locale;
|
|||
|
|
import java.util.Map;
|
|||
|
|
|
|||
|
|
public class AppUtil {
|
|||
|
|
/**
|
|||
|
|
* 获取屏幕分辨率?
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static int[] getScreenDispaly(Context context) {
|
|||
|
|
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
|||
|
|
@SuppressWarnings("deprecation") int width = windowManager.getDefaultDisplay().getWidth();
|
|||
|
|
@SuppressWarnings("deprecation") int height = windowManager.getDefaultDisplay().getHeight();
|
|||
|
|
int[] result = {width, height};
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的名称
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getAppName(Context context) {
|
|||
|
|
String appName = "";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = context.getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
|||
|
|
int labelRes = packageInfo.applicationInfo.labelRes;
|
|||
|
|
appName = context.getResources().getString(labelRes);
|
|||
|
|
} catch (Throwable e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return appName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的名称
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getAppVersionNameCode(Context context) {
|
|||
|
|
String appCode = "";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = context.getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
|||
|
|
String versionName = packageInfo.versionName;
|
|||
|
|
String versionCode = packageInfo.versionCode + "";
|
|||
|
|
appCode = "versionName=" + versionName + " versionCode=" + versionCode;
|
|||
|
|
} catch (Throwable e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return appCode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的名称
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getPackageName(Context context) {
|
|||
|
|
String name = "";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = context.getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
|||
|
|
name = packageInfo.applicationInfo.packageName;
|
|||
|
|
} catch (Throwable e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return name;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的名称
|
|||
|
|
*
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getPackageName() {
|
|||
|
|
String name = "";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = LibraryApp.getContext().getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(LibraryApp.getContext().getPackageName(), 0);
|
|||
|
|
name = packageInfo.applicationInfo.packageName;
|
|||
|
|
} catch (Throwable e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return name;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @param mContext 上下文
|
|||
|
|
* @param title title
|
|||
|
|
* @param message message
|
|||
|
|
* @param ok 确定按钮
|
|||
|
|
* @param cancel 返回按钮
|
|||
|
|
* @param orNull 是否可以为空,false设置为默认title message ok cancel
|
|||
|
|
*/
|
|||
|
|
public static void securitySD(Activity mContext, String title, String message, String ok, String cancel, boolean orNull) {
|
|||
|
|
if (mContext != null) {
|
|||
|
|
mContext.runOnUiThread(new Runnable() {
|
|||
|
|
@Override
|
|||
|
|
public void run() {
|
|||
|
|
String s_title = title;
|
|||
|
|
String s_message = message;
|
|||
|
|
String s_ok = ok;
|
|||
|
|
String s_cancel = cancel;
|
|||
|
|
if (!orNull) {
|
|||
|
|
s_title = "温馨提示";
|
|||
|
|
if (!TextUtils.isEmpty(title)) {
|
|||
|
|
s_title = title;
|
|||
|
|
}
|
|||
|
|
s_message = "请在应用详情页面权限列表进行设置。";
|
|||
|
|
if (!TextUtils.isEmpty(message)) {
|
|||
|
|
s_message = message;
|
|||
|
|
}
|
|||
|
|
s_ok = "确定";
|
|||
|
|
if (!TextUtils.isEmpty(ok)) {
|
|||
|
|
s_ok = ok;
|
|||
|
|
}
|
|||
|
|
s_cancel = "返回";
|
|||
|
|
if (!TextUtils.isEmpty(cancel)) {
|
|||
|
|
s_cancel = cancel;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle(s_title).setMessage(s_message).setPositiveButton(s_ok, new DialogInterface.OnClickListener() {
|
|||
|
|
@Override
|
|||
|
|
public void onClick(DialogInterface dialog, int which) {
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}).setNegativeButton(s_cancel, new DialogInterface.OnClickListener() {
|
|||
|
|
@Override
|
|||
|
|
public void onClick(DialogInterface dialog, int which) {
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}).setCancelable(false).create();
|
|||
|
|
alertDialog.show();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void securitySD(Activity mContext, String title, String message, String ok, String cancel) {
|
|||
|
|
if (mContext != null) {
|
|||
|
|
mContext.runOnUiThread(new Runnable() {
|
|||
|
|
@Override
|
|||
|
|
public void run() {
|
|||
|
|
String s_title = "温馨提示";
|
|||
|
|
if (!TextUtils.isEmpty(title)) {
|
|||
|
|
s_title = title;
|
|||
|
|
}
|
|||
|
|
String s_message = "请在应用详情页面权限列表进行设置。";
|
|||
|
|
if (!TextUtils.isEmpty(message)) {
|
|||
|
|
s_message = message;
|
|||
|
|
}
|
|||
|
|
String s_ok = "确定";
|
|||
|
|
if (!TextUtils.isEmpty(ok)) {
|
|||
|
|
s_ok = ok;
|
|||
|
|
}
|
|||
|
|
String s_cancel = "返回";
|
|||
|
|
if (!TextUtils.isEmpty(cancel)) {
|
|||
|
|
s_cancel = cancel;
|
|||
|
|
}
|
|||
|
|
AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle(s_title).setMessage(s_message).setPositiveButton(s_ok, new DialogInterface.OnClickListener() {
|
|||
|
|
@Override
|
|||
|
|
public void onClick(DialogInterface dialog, int which) {
|
|||
|
|
Intent intent = new Intent();
|
|||
|
|
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
|
|||
|
|
intent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
|
|||
|
|
mContext.startActivity(intent);
|
|||
|
|
}
|
|||
|
|
}).setNegativeButton(s_cancel, new DialogInterface.OnClickListener() {
|
|||
|
|
@Override
|
|||
|
|
public void onClick(DialogInterface dialog, int which) {
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}).setCancelable(false).create();
|
|||
|
|
alertDialog.show();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void securitySD(Activity mContext, String title, String message, String ok) {
|
|||
|
|
if (mContext != null) {
|
|||
|
|
mContext.runOnUiThread(new Runnable() {
|
|||
|
|
@Override
|
|||
|
|
public void run() {
|
|||
|
|
String s_title = "温馨提示";
|
|||
|
|
if (!TextUtils.isEmpty(title)) {
|
|||
|
|
s_title = title;
|
|||
|
|
}
|
|||
|
|
String s_message = "";
|
|||
|
|
if (!TextUtils.isEmpty(message)) {
|
|||
|
|
s_message = message;
|
|||
|
|
}
|
|||
|
|
String s_ok = "";
|
|||
|
|
if (!TextUtils.isEmpty(ok)) {
|
|||
|
|
s_ok = ok;
|
|||
|
|
}
|
|||
|
|
AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle(s_title).setMessage(s_message).setPositiveButton(s_ok, new DialogInterface.OnClickListener() {
|
|||
|
|
@Override
|
|||
|
|
public void onClick(DialogInterface dialog, int which) {
|
|||
|
|
mContext.finish();
|
|||
|
|
}
|
|||
|
|
}).setCancelable(false).create();
|
|||
|
|
alertDialog.show();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void installApk(Context mContext, String downloadApk) {
|
|||
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|||
|
|
File file = new File(downloadApk);
|
|||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|||
|
|
//这里要注意FileProvider一定要在AndroidManifest中去声明哦
|
|||
|
|
//同时com.mc.mcplatform代表你自己的包名
|
|||
|
|
Uri apkUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);
|
|||
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|||
|
|
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|||
|
|
} else {
|
|||
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|||
|
|
Uri uri = Uri.fromFile(file);
|
|||
|
|
intent.setDataAndType(uri, "application/vnd.android.package-archive");
|
|||
|
|
}
|
|||
|
|
mContext.startActivity(intent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static boolean connectStatus(Context context) {
|
|||
|
|
boolean isConnect = true;
|
|||
|
|
try {
|
|||
|
|
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|||
|
|
NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
|
|||
|
|
if (networkCapabilities != null) {
|
|||
|
|
isConnect = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return isConnect;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static boolean connectStatus() {
|
|||
|
|
boolean isConnect = true;
|
|||
|
|
try {
|
|||
|
|
ConnectivityManager manager = (ConnectivityManager) LibraryApp.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|||
|
|
NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
|
|||
|
|
if (networkCapabilities != null) {
|
|||
|
|
isConnect = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return isConnect;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static boolean isRunningForeground(Context mContext) {
|
|||
|
|
try {
|
|||
|
|
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
|
|||
|
|
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
|
|||
|
|
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
|
|||
|
|
if (appProcess.processName.equals(mContext.getPackageName())) {
|
|||
|
|
return appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static ActivityInfo[] getAllActivity(Context mContext) {
|
|||
|
|
ActivityInfo[] activities = null;
|
|||
|
|
PackageManager packageManager = mContext.getPackageManager();
|
|||
|
|
PackageInfo packageInfo = null;
|
|||
|
|
try {
|
|||
|
|
packageInfo = packageManager.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
|
|||
|
|
//所有的Activity
|
|||
|
|
activities = packageInfo.activities;
|
|||
|
|
|
|||
|
|
// for (ActivityInfo activity : activities) {
|
|||
|
|
// Class<?> aClass = Class.forName(activity.name);
|
|||
|
|
// }
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return activities;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void exitActivity(Context mContext) {
|
|||
|
|
ActivityInfo[] activities = getAllActivity(mContext);
|
|||
|
|
if (activities != null && activities.length > 0) {
|
|||
|
|
for (int i = 0; i < activities.length; i++) {
|
|||
|
|
ActivityInfo activity = activities[i];
|
|||
|
|
String name = activity.name;
|
|||
|
|
LogK.e("mContext.getPackageName()=" + mContext.getPackageName());
|
|||
|
|
if (name.startsWith(mContext.getPackageName())) {
|
|||
|
|
Activity currentActivity = getCurrentActivity(name);
|
|||
|
|
if (currentActivity != null) currentActivity.finish();
|
|||
|
|
LogK.e("name=" + name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static Activity getCurrentActivity(String name) {
|
|||
|
|
try {
|
|||
|
|
Class activityThreadClass = Class.forName(name);
|
|||
|
|
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
|
|||
|
|
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
|
|||
|
|
activitiesField.setAccessible(true);
|
|||
|
|
Map activities = (Map) activitiesField.get(activityThread);
|
|||
|
|
|
|||
|
|
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);
|
|||
|
|
Activity activity = (Activity) activityField.get(activityRecord);
|
|||
|
|
return activity;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的VersionName
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getAppVersionName(Context context) {
|
|||
|
|
String appVersionName = "1";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = context.getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
|||
|
|
appVersionName = packageInfo.versionName;
|
|||
|
|
} catch (Throwable e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return appVersionName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的VersionName
|
|||
|
|
*
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getAppVersionName() {
|
|||
|
|
String appVersionName = "1";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = LibraryApp.getContext().getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(LibraryApp.getContext().getPackageName(), 0);
|
|||
|
|
appVersionName = packageInfo.versionName;
|
|||
|
|
} catch (Throwable e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return appVersionName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的VersionCode
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getAppVersionCode(Context context) {
|
|||
|
|
String appVersionCode = "1";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = context.getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
|||
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
|||
|
|
appVersionCode = packageInfo.versionCode + "";
|
|||
|
|
} else {
|
|||
|
|
appVersionCode = packageInfo.getLongVersionCode() + "";
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return appVersionCode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取app的VersionCode
|
|||
|
|
*
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getAppVersionCode() {
|
|||
|
|
String appVersionCode = "1";
|
|||
|
|
try {
|
|||
|
|
PackageManager packageManager = LibraryApp.getContext().getPackageManager();
|
|||
|
|
PackageInfo packageInfo = packageManager.getPackageInfo(LibraryApp.getContext().getPackageName(), 0);
|
|||
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
|||
|
|
appVersionCode = packageInfo.versionCode + "";
|
|||
|
|
} else {
|
|||
|
|
appVersionCode = packageInfo.getLongVersionCode() + "";
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return appVersionCode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static boolean regexPhoneNum(String phone) {
|
|||
|
|
String telRegex = "^1[3-9]\\d{9}$";
|
|||
|
|
if (TextUtils.isEmpty(phone)) {
|
|||
|
|
return false;
|
|||
|
|
} else {
|
|||
|
|
return phone.matches(telRegex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 正则是不是纯数字
|
|||
|
|
*
|
|||
|
|
* @param string
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static boolean regexIsNumber(String string) {
|
|||
|
|
String str = "[0-9]*";
|
|||
|
|
boolean matches = string.matches(str);
|
|||
|
|
return matches;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 判断字符串是否为整数或小数(含正负数)
|
|||
|
|
* @param input 待检测字符串
|
|||
|
|
* @return true-是数字,false-不是数字
|
|||
|
|
*/
|
|||
|
|
public static boolean isNumeric(String input) {
|
|||
|
|
if (input == null || input.isEmpty()) return false;
|
|||
|
|
// 正则解释:
|
|||
|
|
// ^-? : 可选负号开头
|
|||
|
|
// \\d+ : 至少一位整数
|
|||
|
|
// (\\.\\d+)?: 可选小数部分
|
|||
|
|
return input.matches("^-?\\d+(\\.\\d+)?$");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 扩展方法:区分整数和小数
|
|||
|
|
public static boolean isInteger(String input) {
|
|||
|
|
return input != null && input.matches("^-?\\d+$");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static boolean isDecimal(String input) {
|
|||
|
|
return input != null && input.matches("^-?\\d+\\.\\d+$");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public static void getActivityName(Context context) {
|
|||
|
|
Activity activityByContext = getActivityByContext(context);
|
|||
|
|
if (activityByContext != null) {
|
|||
|
|
String localClassName = activityByContext.getLocalClassName();
|
|||
|
|
LogK.e("localClassName=" + localClassName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static Activity getActivityByContext(Context context) {
|
|||
|
|
while (context instanceof ContextWrapper) {
|
|||
|
|
if (context instanceof Activity) {
|
|||
|
|
return (Activity) context;
|
|||
|
|
}
|
|||
|
|
context = ((ContextWrapper) context).getBaseContext();
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static String getTopActivityName(Context context) {
|
|||
|
|
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|||
|
|
List<ActivityManager.RunningTaskInfo> listTask = activityManager.getRunningTasks(0);
|
|||
|
|
String activityName = "";
|
|||
|
|
if (listTask != null && !listTask.isEmpty()) {
|
|||
|
|
ActivityManager.RunningTaskInfo runningTaskInfo = listTask.get(1);
|
|||
|
|
activityName = runningTaskInfo.topActivity.getClassName();
|
|||
|
|
}
|
|||
|
|
return activityName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static String getTopActivity(Context context) {
|
|||
|
|
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|||
|
|
List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
|
|||
|
|
|
|||
|
|
if (runningTaskInfos != null) {
|
|||
|
|
return (runningTaskInfos.get(0).topActivity.getClassName());
|
|||
|
|
} else return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static String getPublicKey(byte[] signature) {
|
|||
|
|
try {
|
|||
|
|
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
|
|||
|
|
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature));
|
|||
|
|
LogK.i("监听2" + " 22");
|
|||
|
|
String publickey = cert.getPublicKey().toString();
|
|||
|
|
LogK.d("-------密码TRACK " + publickey);
|
|||
|
|
// publickey = publickey.substring(publickey.indexOf("modulus:") + 9,publickey.indexOf("\n", publickey.indexOf("modulus:")));
|
|||
|
|
|
|||
|
|
LogK.d("-------密码TRACK " + publickey);
|
|||
|
|
return publickey;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static byte[] getSign(Context context) {
|
|||
|
|
PackageManager pm = context.getPackageManager();
|
|||
|
|
List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);
|
|||
|
|
Iterator<PackageInfo> iter = apps.iterator();
|
|||
|
|
LogK.i("签名1 " + iter + "");
|
|||
|
|
LogK.i("监听1 " + "----------");
|
|||
|
|
while (iter.hasNext()) {
|
|||
|
|
PackageInfo info = iter.next();
|
|||
|
|
String packageName = info.packageName;
|
|||
|
|
//按包名 取签名
|
|||
|
|
if (packageName.equals(AppUtil.getPackageName(context))) {
|
|||
|
|
LogK.i("监听1 " + info.signatures[0].toByteArray() + "----------");
|
|||
|
|
return info.signatures[0].toByteArray();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 格式化时间
|
|||
|
|
*/
|
|||
|
|
public static String stringForTime(int timeMs) {
|
|||
|
|
int totalSeconds = timeMs / 1000;
|
|||
|
|
|
|||
|
|
int seconds = totalSeconds % 60;
|
|||
|
|
int minutes = (totalSeconds / 60) % 60;
|
|||
|
|
int hours = totalSeconds / 3600;
|
|||
|
|
|
|||
|
|
if (hours > 0) {
|
|||
|
|
return String.format(Locale.getDefault(), "%d:%02d:%02d", hours, minutes, seconds);
|
|||
|
|
} else {
|
|||
|
|
return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根据给定的大小返回合适的单位(KB、MB、GB)
|
|||
|
|
*
|
|||
|
|
* @param sizeInBytes 字节数
|
|||
|
|
* @return 格式化后的字符串
|
|||
|
|
*/
|
|||
|
|
public static String getSizeInUnit(long sizeInBytes) {
|
|||
|
|
if (sizeInBytes < 1024) {
|
|||
|
|
return sizeInBytes + " B";
|
|||
|
|
} else if (sizeInBytes < 1024 * 1024) {
|
|||
|
|
return String.format("%.2f KB", sizeInBytes / 1024.0);
|
|||
|
|
} else if (sizeInBytes < 1024 * 1024 * 1024) {
|
|||
|
|
return String.format("%.2f MB", sizeInBytes / (1024.0 * 1024.0));
|
|||
|
|
} else {
|
|||
|
|
return String.format("%.2f GB", sizeInBytes / (1024.0 * 1024.0 * 1024.0));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 清除缓存
|
|||
|
|
*
|
|||
|
|
* @param context
|
|||
|
|
*/
|
|||
|
|
public static void clearApplicationCache(Context context) {
|
|||
|
|
File cache = context.getCacheDir();
|
|||
|
|
if (cache != null && cache.isDirectory()) {
|
|||
|
|
deleteDir(cache);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 执行循环清除
|
|||
|
|
*
|
|||
|
|
* @param dir
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
private static boolean deleteDir(File dir) {
|
|||
|
|
if (dir != null && dir.isDirectory()) {
|
|||
|
|
String[] children = dir.list();
|
|||
|
|
for (int i = 0; i < children.length; i++) {
|
|||
|
|
boolean success = deleteDir(new File(dir, children[i]));
|
|||
|
|
if (!success) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// The directory is now empty so delete it
|
|||
|
|
return dir.delete();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 手机存储百分比
|
|||
|
|
*/
|
|||
|
|
public static void queryStorage(TextView textView) {
|
|||
|
|
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
|
|||
|
|
//存储块总数量
|
|||
|
|
long blockCount = statFs.getBlockCount();
|
|||
|
|
//块大小
|
|||
|
|
long blockSize = statFs.getBlockSize();
|
|||
|
|
//可用块数量
|
|||
|
|
long availableCount = statFs.getAvailableBlocks();
|
|||
|
|
//剩余块数量,注:这个包含保留块(including reserved blocks)即应用无法使用的空间
|
|||
|
|
long freeBlocks = statFs.getFreeBlocks();
|
|||
|
|
//这两个方法是直接输出总内存和可用空间,也有getFreeBytes//API level 18(JELLY_BEAN_MR2)引入
|
|||
|
|
long totalSize = statFs.getTotalBytes();
|
|||
|
|
long availableSize = statFs.getAvailableBytes();
|
|||
|
|
long all = totalSize + availableSize;
|
|||
|
|
int percentage = (int) (totalSize * 100 / (totalSize + availableSize));
|
|||
|
|
textView.setText(getUnit(totalSize) + "/" + getUnit(all));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*** 单位转换*/
|
|||
|
|
private static String getUnit(float size) {
|
|||
|
|
String[] units = {"B", "KB", "MB", "GB", "TB"};
|
|||
|
|
int index = 0;
|
|||
|
|
while (size > 1024 && index < 4) {
|
|||
|
|
size = size / 1024;
|
|||
|
|
index++;
|
|||
|
|
}
|
|||
|
|
return String.format(Locale.getDefault(), " %.2f %s", size, units[index]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 隐藏状态栏和导航栏
|
|||
|
|
*/
|
|||
|
|
public static void setHideBar_StatusAndNavigation(Context mContext) {
|
|||
|
|
ImmersionBar.with((Activity) mContext)
|
|||
|
|
.hideBar(BarHide.FLAG_HIDE_STATUS_BAR)
|
|||
|
|
.hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
|
|||
|
|
.init();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 关闭软键盘
|
|||
|
|
*/
|
|||
|
|
public static void closeKeyBoard(View currentFocus) {
|
|||
|
|
try {
|
|||
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|||
|
|
InputMethodManager imm = (InputMethodManager) LibraryApp.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|||
|
|
imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 关闭软键盘
|
|||
|
|
*/
|
|||
|
|
public static void closeKeyBoard(Activity activity) {
|
|||
|
|
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
|
|||
|
|
View focusView = activity.getCurrentFocus();
|
|||
|
|
// 优先使用当前焦点视图的窗口令牌
|
|||
|
|
if (focusView != null) {
|
|||
|
|
imm.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
|
|||
|
|
} else {
|
|||
|
|
// 无焦点视图时使用Activity的窗口令牌
|
|||
|
|
imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|