Created
March 21, 2015 16:53
-
-
Save manadream/3c8fb861f8eb35a59e85 to your computer and use it in GitHub Desktop.
Java method for determining if android device is a tablet
This file contains 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 static boolean isDeviceTablet(String deviceId, int screenHeight, int screenWidth) { | |
try { | |
Process getDensity; | |
if (deviceId != null) { | |
getDensity = new ProcessBuilder("adb", "-s", deviceId, "shell", "getprop", "ro.sf.lcd_density").start(); | |
} else { | |
getDensity = new ProcessBuilder("adb", "shell", "getprop", "ro.sf.lcd_density").start(); | |
} | |
getDensity.waitFor(); | |
int deviceDpi = Integer.parseInt(convertStreamToString(getDensity.getInputStream()).replaceAll("[^0-9]", "")); | |
float minDp = Math.min(screenHeight, screenWidth) / (deviceDpi / 160); | |
return minDp >= 600; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
public static String convertStreamToString(InputStream stream) throws Exception { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); | |
StringBuilder stringBuilder = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
stringBuilder.append(line).append("\n"); | |
} | |
line = stringBuilder.toString(); | |
reader.close(); | |
stream.close(); | |
return line; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment