Skip to content

Instantly share code, notes, and snippets.

@manadream
Created March 21, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manadream/3c8fb861f8eb35a59e85 to your computer and use it in GitHub Desktop.
Save manadream/3c8fb861f8eb35a59e85 to your computer and use it in GitHub Desktop.
Java method for determining if android device is a tablet
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