Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Convert to new model definition
Browse files Browse the repository at this point in the history
  • Loading branch information
zlern2k committed Jun 30, 2016
1 parent 98cb181 commit a61efe9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.iml
.DS_Store
/build
.watchmanconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}
}
Expand All @@ -53,6 +54,27 @@ private static void conditionallyAdd(ArrayList<Integer> 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
Expand All @@ -61,7 +83,7 @@ private static void conditionallyAdd(ArrayList<Integer> 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<Integer> componentYears = new ArrayList<Integer>();
conditionallyAdd(componentYears, getNumCoresYear());
conditionallyAdd(componentYears, getClockSpeedYear());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit a61efe9

Please sign in to comment.