cmsdk: Add constraints support for CMHardware features

* This patch will make it easy to hide preferences based on the
   lack of a CMHardware feature. In the preference, specify this:

     cm:requiresFeature="cmhardware:FEATURE_KEY_DISABLE"

   ..and the preference will magically be removed if the feature
   is not supported.

Change-Id: I51699df7b39bb38b9ea69c93ade658736f89b988
This commit is contained in:
Steve Kondik
2016-10-08 05:45:11 -07:00
committed by Gerrit Code Review
parent 009662e24e
commit 08086df9d0
2 changed files with 31 additions and 2 deletions

View File

@@ -27,6 +27,7 @@ import cyanogenmod.hardware.HSIC;
import java.io.UnsupportedEncodingException;
import java.lang.IllegalArgumentException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
@@ -225,6 +226,26 @@ public final class CMHardwareManager {
return feature == (getSupportedFeatures() & feature);
}
/**
* String version for preference constraints
*
* @hide
*/
public boolean isSupported(String feature) {
if (!feature.startsWith("FEATURE_")) {
return false;
}
try {
Field f = getClass().getField(feature);
if (f != null) {
return isSupported((int) f.get(null));
}
} catch (NoSuchFieldException | IllegalAccessException e) {
// ignore
}
return false;
}
/**
* Determine if the given feature is enabled or disabled.
*

View File

@@ -29,6 +29,7 @@ import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import cyanogenmod.hardware.CMHardwareManager;
import cyanogenmod.platform.R;
@@ -115,8 +116,15 @@ public class ConstraintsHelper {
// Check if a system feature is available
String rFeature = a.getString(R.styleable.cm_SelfRemovingPreference_requiresFeature);
if (rFeature != null && !hasSystemFeature(mContext, rFeature)) {
return false;
if (rFeature != null) {
if (rFeature.startsWith("cmhardware:")) {
if (!CMHardwareManager.getInstance(mContext).isSupported(
rFeature.substring("cmhardware:".length()))) {
return false;
}
} else if (!hasSystemFeature(mContext, rFeature)) {
return false;
}
}
// Check a boolean system property