diff --git a/.gitignore b/.gitignore index 2816b58..17a48cf 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.iml .DS_Store /build +.watchmanconfig diff --git a/yearclass/src/main/java/com/facebook/device/yearclass/YearClass.java b/yearclass/src/main/java/com/facebook/device/yearclass/YearClass.java index b57e5e5..e572d9f 100644 --- a/yearclass/src/main/java/com/facebook/device/yearclass/YearClass.java +++ b/yearclass/src/main/java/com/facebook/device/yearclass/YearClass.java @@ -7,11 +7,11 @@ */ package com.facebook.device.yearclass; +import android.content.Context; + import java.util.ArrayList; import java.util.Collections; -import android.content.Context; - public class YearClass { // Year definitions public static final int CLASS_UNKNOWN = -1; @@ -22,6 +22,7 @@ public class YearClass { public static final int CLASS_2012 = 2012; public static final int CLASS_2013 = 2013; public static final int CLASS_2014 = 2014; + public static final int CLASS_2015 = 2015; private static final long MB = 1024 * 1024; private static final int MHZ_IN_KHZ = 1000; @@ -40,7 +41,7 @@ public static int get(Context c) { if (mYearCategory == null) { synchronized(YearClass.class) { if (mYearCategory == null) { - mYearCategory = categorizeByYear(c); + mYearCategory = categorizeByYear2016Method(c); } } } @@ -53,6 +54,27 @@ private static void conditionallyAdd(ArrayList list, int value) { } } + private static int categorizeByYear2016Method(Context c) { + long totalRam = DeviceInfo.getTotalMemory(c); + if (totalRam == DeviceInfo.DEVICEINFO_UNKNOWN) { + return categorizeByYear2014Method(c); + } + + if (totalRam <= 768 * MB) { + return DeviceInfo.getNumberOfCPUCores() <= 1 ? CLASS_2009 : CLASS_2010; + } + if (totalRam <= 1024 * MB) { + return DeviceInfo.getCPUMaxFreqKHz() < 1280 * MHZ_IN_KHZ ? CLASS_2011 : CLASS_2012; + } + if (totalRam < 1536 * MB) { + return DeviceInfo.getCPUMaxFreqKHz() < 1780 * MHZ_IN_KHZ ? CLASS_2012 : CLASS_2013; + } + if (totalRam <= 2048 * MB) { + return CLASS_2013; + } + return totalRam < 3 * 1024 * MB ? CLASS_2014 : CLASS_2015; + } + /** * Calculates the "best-in-class year" of the device. This represents the top-end or flagship * devices of that year, not the actual release year of the phone. For example, the Galaxy Duos @@ -61,7 +83,7 @@ private static void conditionallyAdd(ArrayList list, int value) { * * @return The year when this device would have been considered top-of-the-line. */ - private static int categorizeByYear(Context c) { + private static int categorizeByYear2014Method(Context c) { ArrayList componentYears = new ArrayList(); conditionallyAdd(componentYears, getNumCoresYear()); conditionallyAdd(componentYears, getClockSpeedYear()); diff --git a/yearclass/src/test/java/com/facebook/device/yearclass/YearClassTest.java b/yearclass/src/test/java/com/facebook/device/yearclass/YearClassTest.java index ae88cb7..5328ff7 100644 --- a/yearclass/src/test/java/com/facebook/device/yearclass/YearClassTest.java +++ b/yearclass/src/test/java/com/facebook/device/yearclass/YearClassTest.java @@ -30,6 +30,22 @@ public void testGetYearCategory() { assertEquals(YearClass.CLASS_2013, yearClass); } + @PrepareForTest(DeviceInfo.class) + @Test + public void testGetYearCategoryGalaxyJ1() { + // CPU, frequency, RAM, and YearClass values from Samsung Galaxy J1. + int yearClass = getYearClass(2, 1248000, 768L*1024*1024); + assertEquals(YearClass.CLASS_2010, yearClass); + } + + @PrepareForTest(DeviceInfo.class) + @Test + public void testGetYearCategoryP8lite() { + // CPU, frequency, RAM, and YearClass values from Huawei P8lite. + int yearClass = getYearClass(8, 1200000, 2048L*1024*1024); + assertEquals(YearClass.CLASS_2013, yearClass); + } + @PrepareForTest(DeviceInfo.class) @Test public void testEmptyCase() {