-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTDevice.java
More file actions
716 lines (643 loc) · 25.1 KB
/
TDevice.java
File metadata and controls
716 lines (643 loc) · 25.1 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package net.oschina.app.util;
import java.io.File;
import java.lang.reflect.Field;
import java.text.NumberFormat;
import java.util.List;
import java.util.UUID;
import net.oschina.app.AppContext;
import net.oschina.app.R;
import net.oschina.app.base.BaseApplication;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class TDevice {
// 手机网络类型
public static final int NETTYPE_WIFI = 0x01;
public static final int NETTYPE_CMWAP = 0x02;
public static final int NETTYPE_CMNET = 0x03;
public static boolean GTE_HC;
public static boolean GTE_ICS;
public static boolean PRE_HC;
private static Boolean _hasBigScreen = null;
private static Boolean _hasCamera = null;
private static Boolean _isTablet = null;
private static Integer _loadFactor = null;
private static int _pageSize = -1;
public static float displayDensity = 0.0F;
static {
GTE_ICS = Build.VERSION.SDK_INT >= 14;
GTE_HC = Build.VERSION.SDK_INT >= 11;
PRE_HC = Build.VERSION.SDK_INT < 11;
}
public TDevice() {
}
public static float dpToPixel(float dp) {
return dp * (getDisplayMetrics().densityDpi / 160F);
}
public static int getDefaultLoadFactor() {
if (_loadFactor == null) {
Integer integer = Integer.valueOf(0xf & BaseApplication.context()
.getResources().getConfiguration().screenLayout);
_loadFactor = integer;
_loadFactor = Integer.valueOf(Math.max(integer.intValue(), 1));
}
return _loadFactor.intValue();
}
public static float getDensity() {
if (displayDensity == 0.0)
displayDensity = getDisplayMetrics().density;
return displayDensity;
}
public static DisplayMetrics getDisplayMetrics() {
DisplayMetrics displaymetrics = new DisplayMetrics();
((WindowManager) BaseApplication.context().getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(
displaymetrics);
return displaymetrics;
}
public static float getScreenHeight() {
return getDisplayMetrics().heightPixels;
}
public static float getScreenWidth() {
return getDisplayMetrics().widthPixels;
}
public static int[] getRealScreenSize(Activity activity) {
int[] size = new int[2];
int screenWidth = 0, screenHeight = 0;
WindowManager w = activity.getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try {
screenWidth = (Integer) Display.class.getMethod("getRawWidth")
.invoke(d);
screenHeight = (Integer) Display.class
.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17)
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d,
realSize);
screenWidth = realSize.x;
screenHeight = realSize.y;
} catch (Exception ignored) {
}
size[0] = screenWidth;
size[1] = screenHeight;
return size;
}
public static int getStatusBarHeight() {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
return BaseApplication.context().getResources()
.getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static String getUdid() {
String udid = BaseApplication.getPreferences().getString("udid", "");
if (udid.length() == 0) {
SharedPreferences.Editor editor = BaseApplication.getPreferences()
.edit();
udid = String.format("%s", UUID.randomUUID());
editor.putString("udid", udid);
editor.commit();
}
return udid;
}
public static boolean hasBigScreen() {
boolean flag = true;
if (_hasBigScreen == null) {
boolean flag1;
if ((0xf & BaseApplication.context().getResources()
.getConfiguration().screenLayout) >= 3)
flag1 = flag;
else
flag1 = false;
Boolean boolean1 = Boolean.valueOf(flag1);
_hasBigScreen = boolean1;
if (!boolean1.booleanValue()) {
if (getDensity() <= 1.5F)
flag = false;
_hasBigScreen = Boolean.valueOf(flag);
}
}
return _hasBigScreen.booleanValue();
}
public static final boolean hasCamera() {
if (_hasCamera == null) {
PackageManager pckMgr = BaseApplication.context()
.getPackageManager();
boolean flag = pckMgr
.hasSystemFeature("android.hardware.camera.front");
boolean flag1 = pckMgr.hasSystemFeature("android.hardware.camera");
boolean flag2;
if (flag || flag1)
flag2 = true;
else
flag2 = false;
_hasCamera = Boolean.valueOf(flag2);
}
return _hasCamera.booleanValue();
}
public static boolean hasHardwareMenuKey(Context context) {
boolean flag = false;
if (PRE_HC)
flag = true;
else if (GTE_ICS) {
flag = ViewConfiguration.get(context).hasPermanentMenuKey();
} else
flag = false;
return flag;
}
public static boolean hasInternet() {
boolean flag;
if (((ConnectivityManager) BaseApplication.context().getSystemService(
"connectivity")).getActiveNetworkInfo() != null)
flag = true;
else
flag = false;
return flag;
}
public static boolean gotoGoogleMarket(Activity activity, String pck) {
try {
Intent intent = new Intent();
intent.setPackage("com.android.vending");
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + pck));
activity.startActivity(intent);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean isPackageExist(String pckName) {
try {
PackageInfo pckInfo = BaseApplication.context().getPackageManager()
.getPackageInfo(pckName, 0);
if (pckInfo != null)
return true;
} catch (NameNotFoundException e) {
TLog.error(e.getMessage());
}
return false;
}
public static void hideAnimatedView(View view) {
if (PRE_HC && view != null)
view.setPadding(view.getWidth(), 0, 0, 0);
}
public static void hideSoftKeyboard(View view) {
if (view == null)
return;
((InputMethodManager) BaseApplication.context().getSystemService(
Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
view.getWindowToken(), 0);
}
public static boolean isLandscape() {
boolean flag;
if (BaseApplication.context().getResources().getConfiguration().orientation == 2)
flag = true;
else
flag = false;
return flag;
}
public static boolean isPortrait() {
boolean flag = true;
if (BaseApplication.context().getResources().getConfiguration().orientation != 1)
flag = false;
return flag;
}
public static boolean isTablet() {
if (_isTablet == null) {
boolean flag;
if ((0xf & BaseApplication.context().getResources()
.getConfiguration().screenLayout) >= 3)
flag = true;
else
flag = false;
_isTablet = Boolean.valueOf(flag);
}
return _isTablet.booleanValue();
}
public static float pixelsToDp(float f) {
return f / (getDisplayMetrics().densityDpi / 160F);
}
public static void showAnimatedView(View view) {
if (PRE_HC && view != null)
view.setPadding(0, 0, 0, 0);
}
public static void showSoftKeyboard(Dialog dialog) {
dialog.getWindow().setSoftInputMode(4);
}
public static void showSoftKeyboard(View view) {
((InputMethodManager) BaseApplication.context().getSystemService(
Context.INPUT_METHOD_SERVICE)).showSoftInput(view,
InputMethodManager.SHOW_FORCED);
}
public static void toogleSoftKeyboard(View view) {
((InputMethodManager) BaseApplication.context().getSystemService(
Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0,
InputMethodManager.HIDE_NOT_ALWAYS);
}
public static boolean isSdcardReady() {
return Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState());
}
public static String getCurCountryLan() {
return BaseApplication.context().getResources().getConfiguration().locale
.getLanguage()
+ "-"
+ BaseApplication.context().getResources().getConfiguration().locale
.getCountry();
}
public static boolean isZhCN() {
String lang = BaseApplication.context().getResources()
.getConfiguration().locale.getCountry();
if (lang.equalsIgnoreCase("CN")) {
return true;
}
return false;
}
public static String percent(double p1, double p2) {
String str;
double p3 = p1 / p2;
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(2);
str = nf.format(p3);
return str;
}
public static String percent2(double p1, double p2) {
String str;
double p3 = p1 / p2;
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(0);
str = nf.format(p3);
return str;
}
public static void gotoMarket(Context context, String pck) {
if (!isHaveMarket(context)) {
AppContext.showToast("你手机中没有安装应用市场!");
return;
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + pck));
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
public static boolean isHaveMarket(Context context) {
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.APP_MARKET");
PackageManager pm = context.getPackageManager();
List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
return infos.size() > 0;
}
public static void openAppInMarket(Context context) {
if (context != null) {
String pckName = context.getPackageName();
try {
gotoMarket(context, pckName);
} catch (Exception ex) {
try {
String otherMarketUri = "http://market.android.com/details?id="
+ pckName;
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(otherMarketUri));
context.startActivity(intent);
} catch (Exception e) {
}
}
}
}
public static void setFullScreen(Activity activity) {
WindowManager.LayoutParams params = activity.getWindow()
.getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
activity.getWindow().setAttributes(params);
activity.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
public static void cancelFullScreen(Activity activity) {
WindowManager.LayoutParams params = activity.getWindow()
.getAttributes();
params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().setAttributes(params);
activity.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
public static PackageInfo getPackageInfo(String pckName) {
try {
return BaseApplication.context().getPackageManager()
.getPackageInfo(pckName, 0);
} catch (NameNotFoundException e) {
TLog.error(e.getMessage());
}
return null;
}
public static int getVersionCode() {
int versionCode = 0;
try {
versionCode = BaseApplication
.context()
.getPackageManager()
.getPackageInfo(BaseApplication.context().getPackageName(),
0).versionCode;
} catch (PackageManager.NameNotFoundException ex) {
versionCode = 0;
}
return versionCode;
}
public static int getVersionCode(String packageName) {
int versionCode = 0;
try {
versionCode = BaseApplication.context().getPackageManager()
.getPackageInfo(packageName, 0).versionCode;
} catch (PackageManager.NameNotFoundException ex) {
versionCode = 0;
}
return versionCode;
}
public static String getVersionName() {
String name = "";
try {
name = BaseApplication
.context()
.getPackageManager()
.getPackageInfo(BaseApplication.context().getPackageName(),
0).versionName;
} catch (PackageManager.NameNotFoundException ex) {
name = "";
}
return name;
}
public static boolean isScreenOn() {
PowerManager pm = (PowerManager) BaseApplication.context()
.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
public static void installAPK(Context context, File file) {
if (file == null || !file.exists())
return;
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
public static Intent getInstallApkIntent(File file) {
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
return intent;
}
public static void openDial(Context context, String number) {
Uri uri = Uri.parse("tel:" + number);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
context.startActivity(it);
}
public static void openSMS(Context context, String smsBody, String tel) {
Uri uri = Uri.parse("smsto:" + tel);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", smsBody);
context.startActivity(it);
}
public static void openDail(Context context) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public static void openSendMsg(Context context) {
Uri uri = Uri.parse("smsto:");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public static void openCamera(Context context) {
Intent intent = new Intent(); // 调用照相机
intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
intent.setFlags(0x34c40000);
context.startActivity(intent);
}
public static String getIMEI() {
TelephonyManager tel = (TelephonyManager) BaseApplication.context()
.getSystemService(Context.TELEPHONY_SERVICE);
return tel.getDeviceId();
}
public static String getPhoneType() {
return android.os.Build.MODEL;
}
public static void openApp(Context context, String packageName) {
Intent mainIntent = BaseApplication.context().getPackageManager()
.getLaunchIntentForPackage(packageName);
if (mainIntent == null) {
mainIntent = new Intent(packageName);
} else {
TLog.log("Action:" + mainIntent.getAction());
}
context.startActivity(mainIntent);
}
public static boolean openAppActivity(Context context, String packageName,
String activityName) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, activityName);
intent.setComponent(cn);
try {
context.startActivity(intent);
return true;
} catch (Exception e) {
return false;
}
}
public static boolean isWifiOpen() {
boolean isWifiConnect = false;
ConnectivityManager cm = (ConnectivityManager) BaseApplication
.context().getSystemService(Context.CONNECTIVITY_SERVICE);
// check the networkInfos numbers
NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
for (int i = 0; i < networkInfos.length; i++) {
if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
if (networkInfos[i].getType() == ConnectivityManager.TYPE_MOBILE) {
isWifiConnect = false;
}
if (networkInfos[i].getType() == ConnectivityManager.TYPE_WIFI) {
isWifiConnect = true;
}
}
}
return isWifiConnect;
}
public static void uninstallApk(Context context, String packageName) {
if (isPackageExist(packageName)) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
packageURI);
context.startActivity(uninstallIntent);
}
}
@SuppressWarnings("deprecation")
public static void copyTextToBoard(String string) {
if (TextUtils.isEmpty(string))
return;
ClipboardManager clip = (ClipboardManager) BaseApplication.context()
.getSystemService(Context.CLIPBOARD_SERVICE);
clip.setText(string);
AppContext.showToast(R.string.copy_success);
}
/**
* 发送邮件
*
* @param context
* @param subject 主题
* @param content 内容
* @param emails 邮件地址
*/
public static void sendEmail(Context context, String subject,
String content, String... emails) {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
// 模拟器
// intent.setType("text/plain");
intent.setType("message/rfc822"); // 真机
intent.putExtra(android.content.Intent.EXTRA_EMAIL, emails);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
public static int getStatuBarHeight() {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 38;// 默认为38,貌似大部分是这样的
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar = BaseApplication.context().getResources()
.getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return sbar;
}
public static int getActionBarHeight(Context context) {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize,
tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
context.getResources().getDisplayMetrics());
if (actionBarHeight == 0
&& context.getTheme().resolveAttribute(R.attr.actionBarSize,
tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
context.getResources().getDisplayMetrics());
}
return actionBarHeight;
}
public static boolean hasStatusBar(Activity activity) {
WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
if ((attrs.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
return false;
} else {
return true;
}
}
/**
* 调用系统安装了的应用分享
*
* @param context
* @param title
* @param url
*/
public static void showSystemShareOption(Activity context,
final String title, final String url) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享:" + title);
intent.putExtra(Intent.EXTRA_TEXT, title + " " + url);
context.startActivity(Intent.createChooser(intent, "选择分享"));
}
/**
* 获取当前网络类型
*
* @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
*/
public static int getNetworkType() {
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) AppContext
.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if (!StringUtils.isEmpty(extraInfo)) {
if (extraInfo.toLowerCase().equals("cmnet")) {
netType = NETTYPE_CMNET;
} else {
netType = NETTYPE_CMWAP;
}
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = NETTYPE_WIFI;
}
return netType;
}
}