Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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...
commit e56fb4f36a0d98e4bc805f6bf1bb5a95f9a2c5e0 1 parent 15e6d3e
@TaoK TaoK authored
View
50 src/org/abrantix/rockon/rockonnggl/APILevelChecker.java
@@ -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);
+ }
+}
View
11 src/org/abrantix/rockon/rockonnggl/LockScreen.java
@@ -380,19 +380,19 @@ private void updateFields()
public void onClick(View v) {
if(v.getId() == R.id.control_next_lock)
{
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
doNext();
updateFields();
}
else if(v.getId() == R.id.control_play_lock)
{
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
doPlayPause();
updateFields();
}
else if(v.getId() == R.id.control_prev_lock)
{
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
doPrevious();
updateFields();
}
@@ -449,11 +449,6 @@ private void doPrevious()
}
}
- private void hapticFeedback(View v)
- {
- if (android.os.Build.VERSION.SDK_INT >= 5)
- v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
- }
/**
* attachBroadcastReceivers
*/
View
20 src/org/abrantix/rockon/rockonnggl/RockOnNextGenGL.java
@@ -2487,7 +2487,7 @@ public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
@Override
public void onClick(View v) {
if(!mPlayPauseClickHandler.hasMessages(0)){
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
try{
if(mService.isPlaying()){
setPlayButton();
@@ -2578,7 +2578,7 @@ public void handleMessage(Message msg){
public void onClick(View v) {
if(!mNextClickHandler.hasMessages(0))
{
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
mNextClickHandler.sendEmptyMessage(0);
// mNextClickHandler.sendEmptyMessageDelayed(0, Constants.CLICK_ACTION_DELAY);
}
@@ -2604,7 +2604,7 @@ public void handleMessage(Message msg){
public void onClick(View v) {
if(!mPreviousClickHandler.hasMessages(0))
{
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
mPreviousClickHandler.sendEmptyMessage(0);
// mPreviousClickHandler.sendEmptyMessageDelayed(0, Constants.CLICK_ACTION_DELAY);
}
@@ -2628,7 +2628,7 @@ public void handleMessage(Message msg){
public void onClick(View v) {
if(!mRepeatClickHandler.hasMessages(0))
{
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
mRepeatClickHandler.sendEmptyMessage(0);
// mRepeatClickHandler.sendEmptyMessageDelayed(0, Constants.CLICK_ACTION_DELAY);
}
@@ -2662,7 +2662,7 @@ public void handleMessage(Message msg){
public void onClick(View v) {
if(!mShuffleClickHandler.hasMessages(0))
{
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
mShuffleClickHandler.sendEmptyMessage(0);
// mShuffleClickHandler.sendEmptyMessageDelayed(0, Constants.CLICK_ACTION_DELAY);
}
@@ -2694,7 +2694,7 @@ public void handleMessage(Message msg){
@Override
public void onClick(View v) {
if(!mSearchClickHandler.hasMessages(0)){
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
if(findViewById(R.id.search_container) == null)
showSearch();
else
@@ -2718,7 +2718,7 @@ public void handleMessage(Message msg){
@Override
public void onClick(View v) {
if(!mPlayQueueClickHandler.hasMessages(0)){
- hapticFeedback(v);
+ APILevelChecker.getInstance().hapticFeedback(v);
mPlayQueueClickHandler.sendEmptyMessage(0);
// mPlayQueueClickHandler.sendEmptyMessageDelayed(
// 0,
@@ -3837,12 +3837,6 @@ private boolean updateNavigatorToCurrent(IRockOnNextGenService service)
}
}
- private void hapticFeedback(View v)
- {
- if (android.os.Build.VERSION.SDK_INT >= 5)
- v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
- }
-
/**
* Broadcast Receivers
*/
Please sign in to comment.
Something went wrong with that request. Please try again.