Skip to content

Commit

Permalink
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
TaoK committed Jan 8, 2011
1 parent 15e6d3e commit e56fb4f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
50 changes: 50 additions & 0 deletions 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);
}
}
11 changes: 3 additions & 8 deletions src/org/abrantix/rockon/rockonnggl/LockScreen.java
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
*/
Expand Down
20 changes: 7 additions & 13 deletions src/org/abrantix/rockon/rockonnggl/RockOnNextGenGL.java
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit e56fb4f

Please sign in to comment.