forked from robisaks/rockonnggl
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix backwards-compatibility to API level 3 for haptic feedback
Added singleton using reflection to check for "Build.VERSION.SDK_INT", as this didn't exist yet in API level 3 (Android 1.6), the minimum supported by the app.
- Loading branch information
Showing
3 changed files
with
60 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.abrantix.rockon.rockonnggl; | ||
|
||
import java.lang.reflect.Field; | ||
import android.os.Build; | ||
import android.view.HapticFeedbackConstants; | ||
import android.view.View; | ||
|
||
public class APILevelChecker { | ||
//Singleton pattern implementation taken from Android Developers Blog: | ||
//http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html | ||
|
||
//Reflection-based API Level Int check, with package-level "3" minimum, taken from "Android 1" blog entry: | ||
//http://doandroids.com/blogs/2010/5/8/backwards-compatibility/ | ||
|
||
public int SDK_INT; | ||
|
||
// Private constructor prevents instantiation from other classes | ||
private APILevelChecker() { | ||
try { | ||
// works for level 4 and up | ||
Field SDK_INT_field = Build.VERSION.class.getField("SDK_INT"); | ||
SDK_INT = (Integer) SDK_INT_field.get(null); | ||
} catch (NoSuchFieldException e) { | ||
// Must be level 3 (since the app doesn't support lower levels) | ||
SDK_INT=3; | ||
} catch (IllegalAccessException e) { | ||
//Shouldn't happen, let's assume 0 is a suitable invalid value, | ||
// we don't want to add a throws clause. | ||
SDK_INT=0; | ||
} | ||
} | ||
|
||
/** | ||
* SingletonHolder is loaded on the first execution of Singleton.getInstance() | ||
* or the first access to SingletonHolder.INSTANCE, not before. | ||
*/ | ||
private static class SingletonHolder { | ||
private static final APILevelChecker INSTANCE = new APILevelChecker(); | ||
} | ||
|
||
public static APILevelChecker getInstance() { | ||
return SingletonHolder.INSTANCE; | ||
} | ||
|
||
public void hapticFeedback(View v) | ||
{ | ||
if (SDK_INT >= 5) | ||
v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters