Skip to content

Commit

Permalink
Merge pull request #3 from ppoffice/master
Browse files Browse the repository at this point in the history
Translate resource ID strings to actual strings
  • Loading branch information
XhinLiang committed Mar 21, 2016
2 parents e6608cc + 9ef57fc commit 845ecb9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.github.XhinLiang.MDPreference:mdpreference:0.2.3@aar'
compile 'com.github.XhinLiang.MDPreference:material:0.2.3@aar'
compile 'com.github.XhinLiang.MDPreference:mdpreference:0.3.0@aar'
compile 'com.github.XhinLiang.MDPreference:material:0.3.0@aar'
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ public abstract class DialogPreference extends Preference {
protected Dialog mDialog;

private void init(Context context, AttributeSet attrs) {
mDialogTitle = getAndroidAttribute(attrs, "dialogTitle", (String) getTitle());
mDialogMessage = getAndroidAttribute(attrs, "dialogMessage", null);
mPositiveButtonText = getAndroidAttribute(attrs, "positiveButtonText",
(String) context.getText(R.string.confirm));
mNegativeButtonText = getAndroidAttribute(attrs, "negativeButtonText",
(String) context.getText(R.string.cancel));
mDialogTitle = getStringAttribute(attrs, "dialogTitle", (String) getTitle());
mDialogMessage = getStringAttribute(attrs, "dialogMessage", null);
mPositiveButtonText = getStringAttribute(attrs, "positiveButtonText", context.getString(android.R.string.ok));
mNegativeButtonText = getStringAttribute(attrs, "negativeButtonText", context.getString(android.R.string.cancel));
}

public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,37 @@ public void setIcon(Drawable icon) {
this.icon = icon;
}

public String getAndroidAttribute(AttributeSet attrs, String name, String defaultValue) {
if (attrs == null || attrs.getAttributeValue("http://schemas.android.com/apk/res/android", name) == null)
public String getAndroidAttribute(AttributeSet attrs, String name) {
if (attrs.getAttributeValue("http://schemas.android.com/apk/res/android", name) != null)
return attrs.getAttributeValue("http://schemas.android.com/apk/res/android", name);
return null;
}


/**
* Get a resource ID from a string
* @link https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/ResourceTypes.cpp#L62
*/
public int formatResourceId(String value) {
if (value != null && value.matches("^@[0-9]+$")) {
// A valid resource ID string starts with "@" and is followed by an integer whose first two bytes are either 0x01 or 0x07
int resId = Integer.parseInt(value.substring(1, value.length()), 10);
if (Integer.toHexString(resId).length() != 8 || resId >> 24 != 0x01 || resId >> 24 != 0x7f) {
return resId;
}
}
return -1;
}

public String getStringAttribute(AttributeSet attrs, String name, String defaultValue) {
String value = getAndroidAttribute(attrs, name);
if (value == null) {
return defaultValue;
return attrs.getAttributeValue("http://schemas.android.com/apk/res/android", name);
}
int resId = formatResourceId(value);
if (resId == -1) {
return value;
}
return getContext().getString(resId);
}
}
2 changes: 0 additions & 2 deletions mdpreference/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<resources>
<string name="app_name">MaterialPreference</string>
<string name="cancel">Cancel</string>
<string name="confirm">Confirm</string>
</resources>

0 comments on commit 845ecb9

Please sign in to comment.