Created
October 25, 2014 20:01
-
-
Save snadjafi/89d48a2426a1080c9df9 to your computer and use it in GitHub Desktop.
helper class to get device device info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Device { | |
| public static final boolean GTE_HC = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; | |
| private static String emailAddress = null; | |
| private static Boolean isTablet = null; | |
| private static DisplayMetrics displayMetrics = null; | |
| public static void onConfigurationChanged(Configuration newConfig) { | |
| displayMetrics = new DisplayMetrics(); | |
| ((WindowManager) Application.systemService(Context.WINDOW_SERVICE)) | |
| .getDefaultDisplay().getMetrics(displayMetrics); | |
| } | |
| public static boolean isTablet() { | |
| if (isTablet == null) { | |
| int screenSize = (Application.configuration().screenLayout & SCREENLAYOUT_SIZE_MASK); | |
| isTablet = screenSize >= SCREENLAYOUT_SIZE_LARGE; | |
| } | |
| return isTablet; | |
| } | |
| public static void hideKeyboard(View v) { | |
| InputMethodManager imm = (InputMethodManager) Application.systemService(Context.INPUT_METHOD_SERVICE); | |
| imm.hideSoftInputFromWindow(v.getWindowToken(), 0); | |
| } | |
| public static void showKeyboard() { | |
| InputMethodManager imm = (InputMethodManager) Application.systemService(Context.INPUT_METHOD_SERVICE); | |
| imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); | |
| } | |
| public static boolean isLandscape() { | |
| return Application.configuration().orientation == ORIENTATION_LANDSCAPE; | |
| } | |
| public static boolean isPortrait() { | |
| return Application.configuration().orientation == ORIENTATION_PORTRAIT; | |
| } | |
| public static int getScreenWidth() { | |
| return getDisplayMetrics().widthPixels; | |
| } | |
| public static int getScreenHeight() { | |
| return getDisplayMetrics().heightPixels; | |
| } | |
| public static float getDPI() { return getDisplayMetrics().density; } | |
| public static String getID() { return Secure.getString(Application.context().getContentResolver(), Secure.ANDROID_ID); } | |
| public static DisplayMetrics getDisplayMetrics() { | |
| if (displayMetrics == null) { | |
| onConfigurationChanged(null); | |
| } | |
| return displayMetrics; | |
| } | |
| public static boolean hasInternet() { | |
| ConnectivityManager connectivityManager = | |
| (ConnectivityManager) Application.systemService(Context.CONNECTIVITY_SERVICE); | |
| NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); | |
| return activeNetworkInfo != null; | |
| } | |
| public static String getEmailAddress() { | |
| if (emailAddress != null) return emailAddress; | |
| Account[] accounts = AccountManager.get(Application.context()).getAccounts(); | |
| for (Account account : accounts) { | |
| if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) { | |
| emailAddress = account.name; | |
| return emailAddress; | |
| } | |
| } | |
| if (StringUtils.isBlank(emailAddress)) emailAddress = ""; | |
| return emailAddress; | |
| } | |
| public static String getInfo() { | |
| return "Device model: " | |
| + Build.MODEL + "\n" | |
| + "Android version: " | |
| + Build.VERSION.RELEASE + "\n" | |
| + "Carrier: " | |
| + getCarrier(); | |
| } | |
| public static String getCarrier() { | |
| TelephonyManager tm = (TelephonyManager) Application.systemService((Context.TELEPHONY_SERVICE)); | |
| return tm.getNetworkOperatorName(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment