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

Commit

Permalink
update the sample app and add methods to get the int value for purpos…
Browse files Browse the repository at this point in the history
…es such as saving to shared preferences
  • Loading branch information
trevor-e committed Mar 29, 2015
1 parent 754f9dc commit d57ccf7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,30 @@ protected void onCreate(Bundle savedInstanceState) {
mYearClass = (TextView) findViewById(R.id.year_class);
}

private class GetOrComputeYearClass extends AsyncTask<Void, Void, Integer> {
private class GetOrComputeYearClass extends AsyncTask<Void, Void, YearClass> {

@Override
protected Integer doInBackground(Void... voids) {
int yearClass = YearClass.CLASS_UNKNOWN;
protected YearClass doInBackground(Void... voids) {
YearClass yearClass = YearClass.CLASS_UNKNOWN;
SharedPreferences prefs = getSharedPreferences(PREF_FILE, 0);
if (prefs.contains(PREF_NAME)) {
yearClass = prefs.getInt(PREF_NAME, YearClass.CLASS_UNKNOWN);
yearClass = YearClass.fromIntValue(prefs.getInt(PREF_NAME,
YearClass.CLASS_UNKNOWN.getIntValue()));
}
//Try again if device was previously unknown.
if (yearClass == YearClass.CLASS_UNKNOWN) {
yearClass = YearClass.get(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(PREF_NAME, yearClass);
editor.putInt(PREF_NAME, yearClass.getIntValue());
editor.apply();
}
return yearClass;
}

@Override
protected void onPostExecute(Integer yearClass) {
protected void onPostExecute(YearClass yearClass) {
//update UI
mYearClass.setText(Integer.toString(yearClass));
mYearClass.setText(Integer.toString(yearClass.getIntValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum YearClass {
/**
* @return a YearClass constructed from the integer value, or CLASS_UNKNOWN if not found.
*/
private static YearClass fromIntValue(int intValue) {
public static YearClass fromIntValue(int intValue) {
for (YearClass yearClass : YearClass.values()) {
if (intValue == yearClass.mIntValue) {
return yearClass;
Expand All @@ -38,6 +38,13 @@ private static YearClass fromIntValue(int intValue) {
return CLASS_UNKNOWN;
}

/**
* @return a integer value associated with the YearClass. For example, CLASS_2008 will return 2008.
*/
public int getIntValue() {
return mIntValue;
}

/**
* Calculates the YearClass class by the number of processor cores the phone has.
* Evaluations are based off the table below:
Expand All @@ -54,7 +61,7 @@ private static YearClass fromIntValue(int intValue) {
*
* @return the YearClass in which top-of-the-line phones had the same number of processors as this phone.
*/
public static YearClass fromNumberOfCores() {
private static YearClass fromNumberOfCores() {
int cores = DeviceInfo.getNumberOfCPUCores();
if (cores < 1) return CLASS_UNKNOWN;
if (cores == 1) return CLASS_2008;
Expand Down

0 comments on commit d57ccf7

Please sign in to comment.