×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Ali Lotfi
Added: Dec 15, 2016 3:25 PM
Modified: Dec 22, 2016 12:28 PM
Views: 10
Tags: play services
Calculates and returns play services code for this device.
  1. public static String getPlayServicesCode(@NonNull Context context) {
  2.     char version;
  3.     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  4.         version = '0';
  5.     } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP ||
  6.             Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
  7.         version = '2';
  8.     } else {
  9.         version = '4';
  10.     }
  11.  
  12.     char architecture;
  13.     String abi;
  14.     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  15.         //noinspection deprecation
  16.         abi = Build.CPU_ABI;
  17.     } else {
  18.         abi = Build.SUPPORTED_ABIS[0];
  19.     }
  20.  
  21.     switch (abi) {
  22.         case "armeabi":
  23.             architecture = '1';
  24.             break;
  25.         case "arm64-v8a":
  26.             architecture = '4';
  27.             break;
  28.         case "mips":
  29.         case "mips64":
  30.             architecture = '5';
  31.             break;
  32.         case "x86":
  33.             architecture = '7';
  34.             break;
  35.         case "x86_64":
  36.             architecture = '8';
  37.             break;
  38.         default:
  39.             Timber.w("Architecture %s is unknown.", abi); // Continue to default (armeabi-v7a)
  40.         case "armeabi-v7a":
  41.             architecture = '3';
  42.             break;
  43.     }
  44.  
  45.     char dpi;
  46.     DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  47.     int density = (int) (displayMetrics.density * 160);
  48.     switch (density) {
  49.         case 160:
  50.             dpi = '2';
  51.             break;
  52.         case 240:
  53.             dpi = '4';
  54.             break;
  55.         case 320:
  56.             dpi = '6';
  57.             break;
  58.         case 480:
  59.             dpi = '8';
  60.             break;
  61.         default:
  62.             dpi = '0';
  63.             break;
  64.     }
  65.  
  66.     // Concat characters
  67.     return "" + version + architecture + dpi;
  68. }