zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
            }
        }
    }
 
}