import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import cn.refineit.common.utils.demo.entity.DeviceSysVersion;
/**
* 应用辅助工具类
* 如:获取设备系统版本,获取唯一设备id,获取versionName或者versionCode,显示Short,
* 格式化文件大小单位,检查是否有新版本,根据appName判断是否已经安装该app
*
*/
public class APPUtils {
/**
* 类型,获取版本信息
*/
public enum TYPE_VERSION {
/**
* 版本号
*/
TYPE_VERSION_CODE
}
/**
* 获取设备系统版本
*
* @return 设备系统版本
*/
public static String getSystemVersion() {
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
//系统版本
String sysVersion = "Android " + currentApiVersion;
//设备型号
String deviceModel = DeviceUtils.getDeviceModel();
DeviceSysVersion deviceSysVersion = new DeviceSysVersion(sysVersion, deviceModel);
//转成Json String
Gson gson = new Gson();
String s = gson.toJson(deviceSysVersion);
Log.i("dInfo", s);
return s;
}
/**
* 获取唯一设备id
*
* @param context 上下文
* @return 唯一设备id
*/
public static final String getUniqueID(Context context) {
if (context == null) {
return null;
}
Context appContent = context.getApplicationContext();
final TelephonyManager tm = (TelephonyManager) appContent.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(appContent.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
String uniqueId = deviceUuid.toString();
Log.i("uniqueId", uniqueId);
return uniqueId;
}
/**
* 获取versionName或者versionCode
*
* @param context 上下文
* @param version_type {@link cn.refineit.common.utils.demo.entity.DeviceSysVersion}
* @return versionName或versionCode
*/
public static final String getAppVersionInfo(Context context, TYPE_VERSION version_type) {
if (context == null) {
return null;
}
String versionCode = null;
String versionName = null;
Context appContent = context.getApplicationContext();
PackageManager pm = appContent.getPackageManager();
PackageInfo pi = null;
try {
pi = pm.getPackageInfo(appContent.getPackageName(), 0);
versionName = pi.versionName;
versionCode = pi.versionCode + "";
} catch (PackageManager.NameNotFoundException e) {
versionName = null;
versionCode = null;
}
if (version_type == TYPE_VERSION.TYPE_VERSION_CODE) {
Log.i("versionCode", versionCode);
return versionCode;
} else {
Log.i("versionName", versionName);
return versionName;
}
}
/**
* 显示Short Toast
*
* @param context 上下文
* @param message 提示语
*/
public static void showToast(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* 自定义显示时间 Toast
*/
public static void showToastOther(final Activity activity, final String word, final long time) {
activity.runOnUiThread(new Runnable() {
public void run() {
final Toast toast = Toast.makeText(activity, word, Toast.LENGTH_LONG);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
toast.cancel();
}
}, time);
}
});
}
/**
* 显示Long Toast
*
* @param context 上下文
* @param message 提示语
*/
public static void showLongToast(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
/**
* 检查是否有新版本
*
* @param localVersionName 当前版本
* @param serviceVersionName 服务器版本
* @return 是否有新版本
*/
public static boolean hasNewVersion(String localVersionName, String serviceVersionName) {
if (localVersionName == null)
return false;
if (serviceVersionName == null)
return false;
localVersionName = localVersionName.replace(".", "_");
serviceVersionName = serviceVersionName.replace(".", "_");
boolean hasNew = false;
if (TextUtils.isEmpty(localVersionName) || TextUtils.isEmpty(serviceVersionName)) {
hasNew = false;
} else {
String localArrays[] = localVersionName.split("_");
String servicesArrays[] = serviceVersionName.split("_");
try {
if (localArrays.length > 0 && servicesArrays.length > 0) {
int localLen = localArrays.length;
int servicesLen = servicesArrays.length;
for (int i = 0; i < servicesLen; i++) {
int li = 0;
if (localLen > i) {
li = Integer.parseInt(localArrays[i]);
}
int si = Integer.parseInt(servicesArrays[i]);
if (si > li) {
hasNew = true;
break;
} else if (si < li) {
hasNew = false;
break;
} else {
continue;
}
}
} else {
hasNew = false;
}
} catch (NumberFormatException e) {
}
}
return hasNew;
}
/**
* 根据appName判断是否已经安装该app
*
* @return 是否已经安装
*/
public static boolean hasApp(Context context, String appName) {
PackageManager pm = context.getApplicationContext().getPackageManager();
List pinfo = pm.getInstalledPackages(0);
List listAppName = new ArrayList<>();
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
PackageInfo info = pinfo.get(i);
String name = info.applicationInfo.loadLabel(pm).toString();
listAppName.add(name);
}
}
return listAppName.contains(appName);
}
/**
* 跳转应用市场
*
* @param context 上下文
*/
public static void toMarket(Context context) {
try {
Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Couldn't launch the market !", Toast.LENGTH_SHORT).show();
}
}
}
/**
* 设备系统版本实体类
*/
public class DeviceSysVersion {
//系统版本
private String sysVersion;
//设备型号
private String deviceModel;
public DeviceSysVersion(String sysVersion, String deviceModel) {
this.sysVersion = sysVersion;
this.deviceModel = deviceModel;
}
public String getSysVersion() {
return sysVersion;
}
public void setSysVersion(String sysVersion) {
this.sysVersion = sysVersion;
}
public String getDeviceModel() {
return deviceModel;
}
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
}
}