package com.basic.security.utils;
|
|
import android.app.Activity;
|
import android.bluetooth.BluetoothAdapter;
|
import android.content.Context;
|
import android.content.pm.PackageManager;
|
import android.content.res.Resources;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.os.Build;
|
import android.text.TextUtils;
|
import android.widget.Toast;
|
|
import com.basic.security.base.BaseApplication;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.UnsupportedEncodingException;
|
|
/**
|
* Created by EN on 2016/8/18.
|
*/
|
public class CommonUtils {
|
private static final int FAIL = -1;
|
private static final int EMPTY = 0;
|
private static final int SUCCESS = 1;
|
|
public static Resources getResource() {
|
return BaseApplication.getApplication().getResources();
|
}
|
|
public static void shortToast(String content) {
|
Toast.makeText(BaseApplication.getApplication(), content, Toast.LENGTH_SHORT).show();
|
}
|
|
public static void longToast(String content) {
|
Toast.makeText(BaseApplication.getApplication(), content, Toast.LENGTH_LONG).show();
|
}
|
|
public static Bitmap isToBitmap(InputStream is) {
|
return BitmapFactory.decodeStream(is);
|
}
|
|
public static String isToString(InputStream is) throws IOException, UnsupportedEncodingException {
|
byte[] b = new byte[4096];
|
int len;
|
int sum = 0;
|
StringBuffer sb = new StringBuffer();
|
try {
|
if (is == null) {
|
return null;
|
} else if (is.available() == 0) {
|
return "";
|
} else {
|
while ((len = is.read(b)) != -1) {
|
sb.append(new String(b, 0, len));
|
}
|
}
|
//result = new String(sb);
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
// Reader reader = null;
|
// reader = new InputStreamReader(stream, "UTF-8");
|
// char[] buffer = new char[len];
|
// reader.read(buffer);
|
// return new String(buffer);
|
return sb.toString();
|
}
|
|
/**
|
* dip转换px
|
*/
|
public static int dip2px(int dip) {
|
final float scale = getResource().getDisplayMetrics().density;
|
return (int) (dip * scale + 0.5f);
|
}
|
|
/**
|
* px转换dip
|
*/
|
|
public static int px2dip(int px) {
|
final float scale = getResource().getDisplayMetrics().density;
|
return (int) (px / scale + 0.5f);
|
}
|
|
// public static void parseJosn(String json, List<Object> list) {
|
// Gson gson = new Gson();
|
// }
|
//
|
//// public static boolean checkPermission(Context context, String permName, String pkgName){
|
//// PackageManager pm = context.getPackageManager();
|
//// if(PackageManager.PERMISSION_GRANTED == pm.checkPermission(permName, pkgName)){
|
//// return true;
|
//// }else{
|
//// //PackageManager.PERMISSION_DENIED == pm.checkPermission(permName, pkgName)
|
//// System.out.println(pkgName + "not has permission : " + permName);
|
//// return false;
|
//// }
|
//// }
|
|
public static boolean checkPermission(Context context, String permission) {
|
int perm = context.checkCallingOrSelfPermission(permission);
|
return perm == PackageManager.PERMISSION_GRANTED;
|
}
|
|
public static boolean checkAndGetPermission(Activity activity, String permission, int code) {
|
int perm = activity.checkCallingOrSelfPermission(permission);
|
if (perm != PackageManager.PERMISSION_GRANTED) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
activity.requestPermissions(new String[]{permission}, code);
|
}
|
}
|
return perm == PackageManager.PERMISSION_GRANTED;
|
}
|
|
public static boolean hasQEmuFiles() {
|
String[] known_files = {
|
"/system/lib/libc_malloc_debug_qemu.so",
|
"/sys/qemu_trace",
|
"/system/bin/qemu-props"
|
};
|
|
for (String pipe : known_files) {
|
File qemu_file = new File(pipe);
|
if (qemu_file.exists())
|
return true;
|
}
|
return false;
|
|
}
|
|
/**
|
* 判断蓝牙是否有效来判断是否为模拟器
|
*
|
* @return true 为模拟器
|
*/
|
public boolean notHasBlueTooth() {
|
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
|
if (ba == null) {
|
return true;
|
} else {
|
// 如果有蓝牙不一定是有效的。获取蓝牙名称,若为null 则默认为模拟器
|
String name = ba.getName();
|
if (TextUtils.isEmpty(name)) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
}
|
|
}
|