Skip to content

Commit

Permalink
Minor refactoring in SystemUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Gramlich committed Mar 17, 2013
1 parent ff7f478 commit 2d76371
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/org/andengine/input/touch/controller/MultiTouch.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ private MultiTouch() {

public static boolean isSupported(final Context pContext) {
if (MultiTouch.sSupported == null) {
MultiTouch.sSupported = SystemUtils.hasSystemFeature(pContext, PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH);
MultiTouch.sSupported = SystemUtils.hasSystemFeature(pContext, PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH, false);
}

return MultiTouch.sSupported;
}

public static boolean isSupportedDistinct(final Context pContext) {
if (MultiTouch.sSupportedDistinct == null) {
MultiTouch.sSupportedDistinct = SystemUtils.hasSystemFeature(pContext, PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT);
MultiTouch.sSupportedDistinct = SystemUtils.hasSystemFeature(pContext, PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT, false);
}

return MultiTouch.sSupportedDistinct;
Expand Down
48 changes: 35 additions & 13 deletions src/org/andengine/util/system/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.andengine.util.StreamUtils;
import org.andengine.util.adt.DataConstants;
import org.andengine.util.exception.MethodNotFoundException;

import android.content.Context;
import android.content.pm.PackageInfo;
Expand Down Expand Up @@ -53,6 +54,14 @@ public class SystemUtils {
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

public static MemoryInfo getMemoryInfo() {
/* Lazy allocation. */
if (SystemUtils.sMemoryInfo == null) {
Expand All @@ -64,18 +73,18 @@ public static MemoryInfo getMemoryInfo() {
return SystemUtils.sMemoryInfo;
}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

public static boolean isGoogleTV(final Context pContext) {
public static boolean isGoogleTV(final Context pContext) throws SystemUtilsException {
return SystemUtils.hasSystemFeature(pContext, "com.google.android.tv");
}

public static boolean isGoogleTV(final Context pContext, final boolean pDefault) {
try {
return SystemUtils.isGoogleTV(pContext);
} catch (final SystemUtilsException e) {
return pDefault;
}
}

public static int getPackageVersionCode(final Context pContext) throws SystemUtilsException {
return SystemUtils.getPackageInfo(pContext).versionCode;
}
Expand Down Expand Up @@ -105,17 +114,30 @@ private static PackageInfo getPackageInfo(final Context pContext) throws SystemU
}
}

public static boolean hasSystemFeature(final Context pContext, final String pFeature) {
public static boolean hasSystemFeature(final Context pContext, final String pFeature) throws SystemUtilsException {
final PackageManager packageManager = pContext.getPackageManager();
try {
try {
return packageManager.hasSystemFeature(pFeature);
} catch (final Throwable t) {
final Method PackageManager_hasSystemFeatures = PackageManager.class.getMethod("hasSystemFeature", new Class[] { String.class });
return (PackageManager_hasSystemFeatures == null) ? false : (Boolean) PackageManager_hasSystemFeatures.invoke(packageManager, pFeature);
final Method PackageManager_hasSystemFeatures = PackageManager.class.getMethod("hasSystemFeature", String.class);
if (PackageManager_hasSystemFeatures == null) {
throw new SystemUtilsException(new MethodNotFoundException(PackageManager.class.getSimpleName() + ".hasSystemFeature(String)"));
} else {
final boolean result = (Boolean) PackageManager_hasSystemFeatures.invoke(packageManager, pFeature);
return result;
}
}
} catch (final Throwable t) {
return false;
throw new SystemUtilsException(t);
}
}

public static boolean hasSystemFeature(final Context pContext, final String pFeature, final boolean pDefault) {
try {
return SystemUtils.hasSystemFeature(pContext, pFeature);
} catch (SystemUtilsException e) {
return pDefault;
}
}

Expand Down

0 comments on commit 2d76371

Please sign in to comment.