DeviceUtils.java
15.9 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package com.cnlive.goldenline.util;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import com.cnlive.goldenline.R;
import com.orhanobut.logger.Logger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
/**
* 系统版本信息类
*
* @author zuoxian
*/
public class DeviceUtils {
private static final String TAG = DeviceUtils.class.getSimpleName();
// private static final long NO_STORAGE_ERROR = -1L;
private static final long CANNOT_STAT_ERROR = -2L;
/**
* >=2.2
*/
public static boolean hasFroyo() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
}
/**
* >=2.3
*/
public static boolean hasGingerbread() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
}
/**
* >=3.0 LEVEL:11
*/
public static boolean hasHoneycomb() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}
/**
* >=3.1
*/
public static boolean hasHoneycombMR1() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
}
/**
* >=4.0 14
*/
public static boolean hasICS() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
public static int getSDKVersionInt() {
return Build.VERSION.SDK_INT;
}
@SuppressWarnings("deprecation")
public static String getSDKVersion() {
return Build.VERSION.SDK;
}
/**
* 判断是否是平板电脑
*
* @param context
* @return
*/
public static boolean isTablet(@NonNull Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public static boolean isHoneycombTablet(@NonNull Context context) {
return hasHoneycomb() && isTablet(context);
}
/**
* 获得设备型号
*
* @return
*/
public static String getDeviceModel() {
return StringUtils.trim(Build.MODEL);
}
/**
* 检测是否魅族手机
*/
public static boolean isMeizu() {
return getDeviceModel().toLowerCase(Locale.getDefault()).indexOf("meizu") != -1;
}
/**
* 检测是否HTC手机
*/
public static boolean isHTC() {
return getDeviceModel().toLowerCase(Locale.getDefault()).indexOf("htc") != -1;
}
public static boolean isXiaomi() {
return getDeviceModel().toLowerCase(Locale.getDefault()).indexOf("xiaomi") != -1;
}
/**
* 获得设备制造商
*
* @return
*/
public static String getManufacturer() {
return StringUtils.trim(Build.MANUFACTURER);
}
/**
* 获取UUID
* @return 32UUID小写字符串
*/
public static String gainUUID(){
String strUUID = UUID.randomUUID().toString();
strUUID = strUUID.replaceAll("-", "").toLowerCase();
return strUUID;
}
@SuppressWarnings("deprecation")
public static int getScreenHeight(@NonNull Context context) {
Display display = ((WindowManager) context
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
return display.getHeight();
}
/**
* 获取屏幕宽度
*/
@SuppressWarnings("deprecation")
public static int getScreenWidth(@NonNull Context context) {
Display display = ((WindowManager) context
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
return display.getWidth();
}
/**
* 获得设备屏幕密度
*/
public static float getScreenDensity(@NonNull Context context) {
DisplayMetrics metrics = context.getApplicationContext().getResources()
.getDisplayMetrics();
return metrics.density;
}
@NonNull
public static int[] getScreenSize(int w, int h, @NonNull Context context) {
int phoneW = getScreenWidth(context);
int phoneH = getScreenHeight(context);
if (w * phoneH > phoneW * h) {
phoneH = phoneW * h / w;
} else if (w * phoneH < phoneW * h) {
phoneW = phoneH * w / h;
}
return new int[]{phoneW, phoneH};
}
@NonNull
public static int[] getScreenSize(int w, int h, int phoneW, int phoneH) {
if (w * phoneH > phoneW * h) {
phoneH = phoneW * h / w;
} else if (w * phoneH < phoneW * h) {
phoneW = phoneH * w / h;
}
return new int[]{phoneW, phoneH};
}
/**
* 设置屏幕亮度
*/
public static void setBrightness(@NonNull final Activity context, float f) {
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
lp.screenBrightness = f;
if (lp.screenBrightness > 1.0f)
lp.screenBrightness = 1.0f;
else if (lp.screenBrightness < 0.01f)
lp.screenBrightness = 0.01f;
context.getWindow().setAttributes(lp);
}
/**
* 检测磁盘状态
*/
// public static int getStorageStatus(boolean mayHaveSd) {
// long remaining = mayHaveSd ? getAvailableStorage() : NO_STORAGE_ERROR;
// if (remaining == NO_STORAGE_ERROR) {
// return CommonStatus.STORAGE_STATUS_NONE;
// }
// return remaining < CommonConstants.LOW_STORAGE_THRESHOLD ?
// CommonStatus.STORAGE_STATUS_LOW : CommonStatus.STORAGE_STATUS_OK;
// }
public static long getAvailableStorage() {
try {
String storageDirectory = Environment.getExternalStorageDirectory()
.toString();
StatFs stat = new StatFs(storageDirectory);
return (long) stat.getAvailableBlocks()
* (long) stat.getBlockSize();
} catch (RuntimeException ex) {
// if we can't stat the filesystem then we don't know how many
// free bytes exist. It might be zero but just leave it
// blank since we really don't know.
return CANNOT_STAT_ERROR;
}
}
/**
* 隐藏软键盘
*/
public static void hideSoftInput(@Nullable Context ctx) {
if (ctx != null) {
InputMethodManager imm = ((InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE));
View view = ((Activity) ctx).getCurrentFocus();
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
/**
* 显示软键盘
*/
public static void showSoftInput(@NonNull Context ctx, View view) {
// InputMethodManager.SHOW_FORCED);
InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
public static String getCpuInfo() {
String cpuInfo = "";
try {
if (new File("/proc/cpuinfo").exists()) {
FileReader fr = new FileReader("/proc/cpuinfo");
BufferedReader localBufferedReader = new BufferedReader(fr,
8192);
cpuInfo = localBufferedReader.readLine();
localBufferedReader.close();
if (cpuInfo != null) {
cpuInfo = cpuInfo.split(":")[1].trim().split(" ")[0];
}
}
} catch (IOException e) {
} catch (Exception e) {
}
return cpuInfo;
}
public static void startApkActivity(@NonNull final Context ctx, String packageName) {
PackageManager pm = ctx.getPackageManager();
PackageInfo pi;
try {
pi = pm.getPackageInfo(packageName, 0);
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage(pi.packageName);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
String className = ri.activityInfo.name;
intent.setComponent(new ComponentName(packageName, className));
ctx.startActivity(intent);
}
} catch (NameNotFoundException e) {
Logger.e(TAG, e.getMessage(), e);
}
}
/**
* 计算视频宽度
*
* @param ctx
* @return
*/
public static int getVideoWidth(@NonNull Context ctx) {
return Math.min(
ctx.getResources().getDisplayMetrics().widthPixels,
ctx.getResources().getDisplayMetrics().heightPixels);
}
/**
* 计算屏幕高度
*
* @param ctx
* @return
*/
public static int getDeviceWidth(@NonNull Context ctx) {
return Math.max(
ctx.getResources().getDisplayMetrics().widthPixels,
ctx.getResources().getDisplayMetrics().heightPixels);
}
public static int getDeviceHeight(@NonNull Context ctx) {
return Math.min(
ctx.getResources().getDisplayMetrics().widthPixels,
ctx.getResources().getDisplayMetrics().heightPixels);
}
/**
* 计算视频高度 16:9
*
* @param ctx
* @return
*/
public static int getVideoHeight(@NonNull Context ctx) {
return (getVideoWidth(ctx) * 9) / 16;
}
/**
* 软键盘是否已经打开
*
* @return
*/
public static boolean isHardKeyboardOpen(@NonNull Context ctx) {
return ctx.getResources().getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
}
/**
* 显示时间,如果与当前时间差别小于一天,则自动用**秒(分,小时)前,如果大于一天则用format规定的格式显示
*
* @author wxy
* @param ctime
* 时间
* @param format
* 格式 格式描述:例如:yyyy-MM-dd yyyy-MM-dd HH:mm:ss
* @return
*/
public static String showTime(@NonNull Context context, long ctime, @Nullable String format) {
String r = "";
if(ctime == 0) return r;
if(format == null)format = "MM-dd HH:mm";
long nowtime = System.currentTimeMillis();
long result = Math.abs(nowtime - ctime);
if(result < 60000){// 一分钟内
long seconds = result / 1000;
r = context.getString(R.string.time_second);
}else if (result >= 60000 && result < 3600000){// 一小时内
long seconds = result / 60000;
r = seconds + context.getString(R.string.time_minute);
}else if (result >= 3600000 && result < 86400000){// 一天内
long seconds = result / 3600000;
r = seconds + context.getString(R.string.time_hour);
}else if (result >= 86400000 && result < 1702967296){// 三十天内
long seconds = result / 86400000;
r = seconds + context.getString(R.string.time_day);
}else{// 日期格式
SimpleDateFormat df = new SimpleDateFormat(format);
r = df.format(new Date(ctime));
}
return r;
}
/**
* 获取倒计时
* 毫秒转换:X天X时X分X秒
*/
@NonNull
public static String getCountdown(@NonNull Context context, long ctime) {
String r = "";
if(ctime == 0) return r;
long nowtime = System.currentTimeMillis();
long result = ctime - nowtime;
if (result < 1) {
r = "";
} else {
Integer ss = 1000;
Integer mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = result / dd;
Long hour = (result - day * dd) / hh;
Long minute = (result - day * dd - hour * hh) / mi;
Long second = (result - day * dd - hour * hh - minute * mi) / ss;
Long milliSecond = result - day * dd - hour * hh - minute * mi - second * ss;
StringBuffer sb = new StringBuffer();
if(day > 0) {
sb.append(day + context.getString(R.string.date_day));
}
if(hour > 0) {
sb.append(hour + context.getString(R.string.date_hour));
}
if(minute > 0) {
sb.append(minute + context.getString(R.string.date_minute));
}
if(second > 0) {
sb.append(second + context.getString(R.string.date_second));
}
r = sb.toString();
}
return r;
}
/**
* make true current connect service is wifi
* @param context
* @return
*/
public static boolean isWifi(@NonNull Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
public static String getIEMI(@NonNull Context context){
String imei;
try {
imei = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
} catch (Exception e) {
imei = "";
e.printStackTrace();
}
return imei;
}
public static String getIPAddress() {
boolean useIPv4 = true;
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress()) {
String sAddr = addr.getHostAddress();
//boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
boolean isIPv4 = sAddr.indexOf(':')<0;
if (useIPv4) {
if (isIPv4)
return sAddr;
} else {
if (!isIPv4) {
int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
return delim<0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
}
}
}
}
}
} catch (Exception ex) { } // for now eat exceptions
return "";
}
public static String getPhoneNumber(Context context) {
final String KEY_PHONE_NUMBER = "key_phone_number";
SharedPreferencesHelper mSharedPreferencesHelper = new SharedPreferencesHelper(
context);
String phoneNum = mSharedPreferencesHelper.getValue(KEY_PHONE_NUMBER);
if (TextUtils.isEmpty(phoneNum)) {
phoneNum = "";
}
return phoneNum;
}
}