253 lines
9.1 KiB
Java
253 lines
9.1 KiB
Java
package com.aoleyun.sn.utils;
|
||
|
||
import android.content.Context;
|
||
import android.content.pm.ApplicationInfo;
|
||
import android.content.pm.PackageInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.content.pm.PackageManager.NameNotFoundException;
|
||
import android.net.ConnectivityManager;
|
||
import android.net.NetworkInfo;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.os.Environment;
|
||
import android.os.Looper;
|
||
import android.os.storage.StorageManager;
|
||
import android.telephony.TelephonyManager;
|
||
import android.text.TextUtils;
|
||
|
||
import java.io.File;
|
||
import java.lang.reflect.Array;
|
||
import java.lang.reflect.InvocationTargetException;
|
||
import java.lang.reflect.Method;
|
||
import java.text.DecimalFormat;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
|
||
|
||
public class ExampleUtil {
|
||
public static final String PREFS_NAME = "JPUSH_EXAMPLE";
|
||
public static final String PREFS_DAYS = "JPUSH_EXAMPLE_DAYS";
|
||
public static final String PREFS_START_TIME = "PREFS_START_TIME";
|
||
public static final String PREFS_END_TIME = "PREFS_END_TIME";
|
||
public static final String KEY_APP_KEY = "JPUSH_APPKEY";
|
||
|
||
public static boolean isEmpty(String s) {
|
||
if (null == s)
|
||
return true;
|
||
if (s.length() == 0)
|
||
return true;
|
||
if (s.trim().length() == 0)
|
||
return true;
|
||
return false;
|
||
}
|
||
/**
|
||
* 只能以 “+” 或者 数字开头;后面的内容只能包含 “-” 和 数字。
|
||
* */
|
||
private final static String MOBILE_NUMBER_CHARS = "^[+0-9][-0-9]{1,}$";
|
||
public static boolean isValidMobileNumber(String s) {
|
||
if(TextUtils.isEmpty(s)) return true;
|
||
Pattern p = Pattern.compile(MOBILE_NUMBER_CHARS);
|
||
Matcher m = p.matcher(s);
|
||
return m.matches();
|
||
}
|
||
// 校验Tag Alias 只能是数字,英文字母和中文
|
||
public static boolean isValidTagAndAlias(String s) {
|
||
Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$");
|
||
Matcher m = p.matcher(s);
|
||
return m.matches();
|
||
}
|
||
|
||
// 取得AppKey
|
||
public static String getAppKey(Context context) {
|
||
Bundle metaData = null;
|
||
String appKey = null;
|
||
try {
|
||
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
|
||
context.getPackageName(), PackageManager.GET_META_DATA);
|
||
if (null != ai)
|
||
metaData = ai.metaData;
|
||
if (null != metaData) {
|
||
appKey = metaData.getString(KEY_APP_KEY);
|
||
if ((null == appKey) || appKey.length() != 24) {
|
||
appKey = null;
|
||
}
|
||
}
|
||
} catch (NameNotFoundException e) {
|
||
|
||
}
|
||
return appKey;
|
||
}
|
||
|
||
// 取得版本号
|
||
public static String GetVersion(Context context) {
|
||
try {
|
||
PackageInfo manager = context.getPackageManager().getPackageInfo(
|
||
context.getPackageName(), 0);
|
||
return manager.versionName;
|
||
} catch (NameNotFoundException e) {
|
||
return "Unknown";
|
||
}
|
||
}
|
||
|
||
public static void showToast(final String toast, final Context context)
|
||
{
|
||
new Thread(new Runnable() {
|
||
|
||
@Override
|
||
public void run() {
|
||
Looper.prepare();
|
||
// Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
|
||
Looper.loop();
|
||
}
|
||
}).start();
|
||
}
|
||
|
||
public static boolean isConnected(Context context) {
|
||
ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||
NetworkInfo info = conn.getActiveNetworkInfo();
|
||
return (info != null && info.isConnected());
|
||
}
|
||
|
||
public static String getImei(Context context, String imei) {
|
||
String ret = null;
|
||
try {
|
||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||
ret = telephonyManager.getDeviceId();
|
||
} catch (Exception e) {
|
||
Logger.e(ExampleUtil.class.getSimpleName(), e.getMessage());
|
||
}
|
||
if (isReadableASCII(ret)){
|
||
return ret;
|
||
} else {
|
||
return imei;
|
||
}
|
||
}
|
||
|
||
private static boolean isReadableASCII(CharSequence string){
|
||
if (TextUtils.isEmpty(string)) return false;
|
||
try {
|
||
Pattern p = Pattern.compile("[\\x20-\\x7E]+");
|
||
return p.matcher(string).matches();
|
||
} catch (Throwable e){
|
||
return true;
|
||
}
|
||
}
|
||
|
||
public static String getDeviceId(Context context) {
|
||
// return JPushInterface.getUdid(context);
|
||
return "0";
|
||
}
|
||
|
||
public static class StorageUtils {
|
||
|
||
|
||
// 获取存储空间 false 内置sd卡路径,true 外置sd卡路径
|
||
public static File getStoragePath(Context mContext, boolean isSDcard) {
|
||
StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
|
||
Class<?> storageVolumeClazz = null;
|
||
try {
|
||
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
|
||
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
|
||
Method getPath = storageVolumeClazz.getMethod("getPath");
|
||
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
|
||
Object result = getVolumeList.invoke(mStorageManager);
|
||
final int length = Array.getLength(result);
|
||
for (int i = 0; i < length; i++) {
|
||
Object storageVolumeElement = Array.get(result, i);
|
||
String path = (String) getPath.invoke(storageVolumeElement);
|
||
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
|
||
if (isSDcard == removable) {
|
||
File file = new File(path);
|
||
if (file.exists()) {
|
||
return file;
|
||
}else {
|
||
if (!isSDcard) {
|
||
return Environment.getExternalStorageDirectory();
|
||
} else {
|
||
File file1 = new File("root/storage/sdcard1");
|
||
if (file1.exists()){
|
||
return file1;
|
||
}else {
|
||
File file2 = new File("storage/sdcard1");
|
||
if (file2.exists()){
|
||
return file2;
|
||
}else {
|
||
File file3 = new File("/storage/emulated/0");
|
||
if (file3.exists()){
|
||
return file3;
|
||
}else {
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (ClassNotFoundException e) {
|
||
e.printStackTrace();
|
||
} catch (InvocationTargetException e) {
|
||
e.printStackTrace();
|
||
} catch (NoSuchMethodException e) {
|
||
e.printStackTrace();
|
||
} catch (IllegalAccessException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
|
||
public static String getFileCache(Context context, String s) {
|
||
return getExternalFilesDir(context, s).getAbsolutePath();
|
||
}
|
||
|
||
public static String getFileRoot(Context context) {
|
||
return getExternalFilesDir(context, "Download").getAbsolutePath();
|
||
}
|
||
|
||
public static File getExternalFilesDir(Context context, String s) {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
|
||
File path = context.getExternalFilesDir(s);
|
||
if (path != null) {
|
||
return path;
|
||
}
|
||
}
|
||
final String cacheDir = "/Android/data/" + context.getPackageName() + "/files/" + s;
|
||
return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
|
||
}
|
||
|
||
|
||
// 检测文件是否存在
|
||
public static boolean isFileIsExists(String filePath){
|
||
try{
|
||
File f=new File(filePath);
|
||
if(!f.exists()){
|
||
return false;
|
||
}
|
||
}catch (Exception e) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
public static String size(long size) {
|
||
if (size / (1024 * 1024) > 0) {
|
||
float tmpSize = (float) (size) / (float) (1024 * 1024 );
|
||
DecimalFormat df = new DecimalFormat("#.##");
|
||
return "" + df.format(tmpSize) + "GB";
|
||
} else if (size / 1024 > 0) {
|
||
float tmpSize = (float) (size) / (float) 1024 ;
|
||
DecimalFormat df = new DecimalFormat("#.##");
|
||
return "" + df.format(tmpSize) + "MB";
|
||
} else if (size > 0) {
|
||
return "" + (size ) + "KB";
|
||
} else {
|
||
return "" + (size) + "KB";
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|