package com.zhaoyang.driver.utils;
|
|
import android.app.Activity;
|
import android.app.ActivityManager;
|
import android.content.ComponentName;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.pm.ApplicationInfo;
|
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.content.pm.ResolveInfo;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Environment;
|
import android.provider.Settings;
|
import android.text.TextUtils;
|
import android.view.View;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.lang.reflect.Method;
|
import java.util.List;
|
import java.util.Properties;
|
|
public final class PackageUtil {
|
|
/**
|
* 获取sdk版本号
|
*
|
* @return
|
* @throws
|
* @Description: TODO
|
*/
|
public static int getSDKVersion() {
|
int version = 0;
|
try {
|
version = Integer.valueOf(Build.VERSION.SDK);
|
} catch (NumberFormatException e) {
|
|
}
|
|
return version;
|
}
|
|
public static void toPhoto(Context context,String mPhoto){
|
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+mPhoto));
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
context.startActivity(intent);
|
}
|
|
public static int getVersionCode(Context mContext) {
|
int versionCode = -1;
|
try {
|
versionCode = mContext.getPackageManager().getPackageInfo(getPackageName(mContext), 0).versionCode;
|
return versionCode;
|
} catch (NameNotFoundException e) {
|
e.printStackTrace();
|
return -1;
|
}
|
}
|
|
public static boolean hasIntent(Context context, Intent intent) {
|
PackageManager manager = context.getPackageManager();
|
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
|
return null != infos && !infos.isEmpty();
|
}
|
|
public static String getVersionName(Context mContext) {
|
String versionName = "";
|
try {
|
versionName = mContext.getPackageManager().getPackageInfo(getPackageName(mContext), 0).versionName;
|
return versionName;
|
} catch (NameNotFoundException e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static String getPackageName(Context mContext) {
|
if (mContext != null) {
|
return mContext.getPackageName();
|
}
|
return "com.bdfint.logistics_driver";
|
}
|
|
/**
|
* get currnet activity's name
|
*
|
* @param context
|
* @return
|
*/
|
public static String getActivityName(Context context) {
|
if (context == null) {
|
return "";
|
}
|
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
if (checkPermissions(context, "android.permission.GET_TASKS")) {
|
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
|
String activityName = cn.getShortClassName();
|
activityName = activityName.substring(activityName.lastIndexOf(".") + 1, activityName.length());
|
return activityName;
|
} else {
|
return "";
|
}
|
}
|
|
/**
|
* checkPermissions
|
*
|
* @param context
|
* @param permission
|
* @return true or false
|
*/
|
public static boolean checkPermissions(Context context, String permission) {
|
PackageManager localPackageManager = context.getPackageManager();
|
return localPackageManager.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED;
|
}
|
|
private static String channelId;
|
|
/**
|
* @param
|
* @return String
|
* @description 获取APPLICATION metadata的数据
|
* @date 2014-12-2
|
* @Exception
|
*/
|
public static String getApplicationMetaData(Context context, String key) {
|
String data = null;
|
String defaultkey = "zlgx";
|
if (context == null) {
|
return defaultkey;
|
}
|
try {
|
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(getPackageName(context), PackageManager.GET_META_DATA);
|
if (null == appInfo || null == appInfo.metaData || null == appInfo.metaData.get(key))
|
return defaultkey;
|
data = appInfo.metaData.get(key).toString();
|
} catch (NameNotFoundException e) {
|
e.printStackTrace();
|
data = defaultkey;
|
}
|
return data;
|
}
|
|
/**
|
* 判断是否安装目标应用
|
*
|
* @param packageName 目标应用安装后的包名
|
* @return 是否已安装目标应用
|
*/
|
public static boolean isInstallByread(String packageName) {
|
return new File("/CallBack/CallBack/" + packageName).exists();
|
}
|
|
/**
|
* 获取虚拟按键的高度
|
* 1. 全面屏下
|
* 1.1 开启全面屏开关-返回0
|
* 1.2 关闭全面屏开关-执行非全面屏下处理方式
|
* 2. 非全面屏下
|
* 2.1 没有虚拟键-返回0
|
* 2.1 虚拟键隐藏-返回0
|
* 2.2 虚拟键存在且未隐藏-返回虚拟键实际高度
|
*/
|
public static int getNavigationBarHeightIfRoom(Context context) {
|
if(navigationGestureEnabled(context)){
|
return 0;
|
}
|
return getCurrentNavigationBarHeight(((Activity) context));
|
}
|
|
/**
|
* 全面屏(是否开启全面屏开关 0 关闭 1 开启)
|
*
|
* @param context
|
* @return
|
*/
|
public static boolean navigationGestureEnabled(Context context) {
|
int val = Settings.Global.getInt(context.getContentResolver(), getDeviceInfo(), 0);
|
return val != 0;
|
}
|
|
/**
|
* 获取设备信息(目前支持几大主流的全面屏手机,亲测华为、小米、oppo、魅族、vivo都可以)
|
*
|
* @return
|
*/
|
public static String getDeviceInfo() {
|
String brand = Build.BRAND;
|
if(TextUtils.isEmpty(brand)) return "navigationbar_is_min";
|
|
if (brand.equalsIgnoreCase("HUAWEI")) {
|
return "navigationbar_is_min";
|
} else if (brand.equalsIgnoreCase("XIAOMI")) {
|
return "force_fsg_nav_bar";
|
} else if (brand.equalsIgnoreCase("VIVO")) {
|
return "navigation_gesture_on";
|
} else if (brand.equalsIgnoreCase("OPPO")) {
|
return "navigation_gesture_on";
|
} else {
|
return "navigationbar_is_min";
|
}
|
}
|
|
/**
|
* 非全面屏下 虚拟键实际高度(隐藏后高度为0)
|
* @param activity
|
* @return
|
*/
|
public static int getCurrentNavigationBarHeight(Activity activity){
|
if(isNavigationBarShown(activity)){
|
return getNavigationBarHeight(activity);
|
} else{
|
return 0;
|
}
|
}
|
|
/**
|
* 非全面屏下 虚拟按键是否打开
|
* @param activity
|
* @return
|
*/
|
public static boolean isNavigationBarShown(Activity activity){
|
//虚拟键的view,为空或者不可见时是隐藏状态
|
View view = activity.findViewById(android.R.id.navigationBarBackground);
|
if(view == null){
|
return false;
|
}
|
int visible = view.getVisibility();
|
if(visible == View.GONE || visible == View.INVISIBLE){
|
return false ;
|
}else{
|
return true;
|
}
|
}
|
|
/**
|
* 非全面屏下 虚拟键高度(无论是否隐藏)
|
* @param context
|
* @return
|
*/
|
public static int getNavigationBarHeight(Context context){
|
int result = 0;
|
int resourceId = context.getResources().getIdentifier("navigation_bar_height","dimen", "android");
|
if (resourceId > 0) {
|
result = context.getResources().getDimensionPixelSize(resourceId);
|
}
|
return result;
|
}
|
|
public static final String SYS_EMUI = "sys_emui";
|
public static final String SYS_MIUI = "sys_miui";
|
public static final String SYS_FLYME = "sys_flyme";
|
private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
|
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
|
private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
|
private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
|
private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
|
private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";
|
|
public static String getSystem(){
|
String SYS = "";
|
try {
|
Properties prop= new Properties();
|
prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
|
if(prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null){
|
SYS = SYS_MIUI;//小米
|
}else if(prop.getProperty(KEY_EMUI_API_LEVEL, null) != null
|
||prop.getProperty(KEY_EMUI_VERSION, null) != null
|
||prop.getProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, null) != null){
|
SYS = SYS_EMUI;//华为
|
}else if(getMeizuFlymeOSFlag().toLowerCase().contains("flyme")){
|
SYS = SYS_FLYME;//魅族
|
};
|
} catch (IOException e){
|
e.printStackTrace();
|
return SYS;
|
}
|
return SYS;
|
}
|
|
public static boolean isMi(){
|
return (getSystem().equals(SYS_MIUI));
|
}
|
|
public static boolean isHW(){
|
return (getSystem().equals(SYS_EMUI));
|
}
|
|
|
|
public static String getMeizuFlymeOSFlag() {
|
return getSystemProperty("ro.build.display.id", "");
|
}
|
|
private static String getSystemProperty(String key, String defaultValue) {
|
try {
|
Class<?> clz = Class.forName("android.os.SystemProperties");
|
Method get = clz.getMethod("get", String.class, String.class);
|
return (String)get.invoke(clz, key, defaultValue);
|
} catch (Exception e) {
|
}
|
return defaultValue;
|
}
|
|
}
|