-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct ActionBar subtitle on locale change
- Loading branch information
Showing
3 changed files
with
102 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/zegoggles/smssync/activity/PreferenceTitles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.zegoggles.smssync.activity; | ||
|
||
import android.content.res.Resources; | ||
import android.content.res.XmlResourceParser; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.StringRes; | ||
import android.support.annotation.XmlRes; | ||
import org.xmlpull.v1.XmlPullParser; | ||
import org.xmlpull.v1.XmlPullParserException; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Standard Android preferences don't expose the title resource id. Reparse preferences XML, | ||
* key title resources by preference key. | ||
*/ | ||
class PreferenceTitles { | ||
private static final String NS = "http://schemas.android.com/apk/res/android"; | ||
private static final String PREFERENCE_SCREEN = "PreferenceScreen"; | ||
private Map<String, Integer> titleResources = new HashMap<String, Integer>(); | ||
|
||
PreferenceTitles(@NonNull Resources resources, @XmlRes int preferenceRes) { | ||
final XmlResourceParser parser = resources.getXml(preferenceRes); | ||
try { | ||
while (true) { | ||
int type; | ||
do { | ||
type = parser.next(); | ||
} while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT); | ||
|
||
if (type == XmlPullParser.END_DOCUMENT) { | ||
break; | ||
} else if (PREFERENCE_SCREEN.equals(parser.getName()) && parser.getAttributeCount() > 0) { | ||
final @StringRes int titleRes = parser.getAttributeResourceValue(NS, "title", 0); | ||
final String key = parser.getAttributeValue(NS, "key"); | ||
if (titleRes > 0 && key != null) { | ||
titleResources.put(key, titleRes); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} catch (XmlPullParserException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* @return the string resource id or 0 if not found | ||
*/ | ||
@StringRes int getTitleRes(String preferenceKey) { | ||
final Integer res = titleResources.get(preferenceKey); | ||
return res == null ? 0 : res; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/test/java/com/zegoggles/smssync/activity/PreferenceTitlesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.zegoggles.smssync.activity; | ||
|
||
import com.zegoggles.smssync.R; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.RuntimeEnvironment; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class PreferenceTitlesTest { | ||
private PreferenceTitles subject; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
subject = new PreferenceTitles(RuntimeEnvironment.application.getResources(), R.xml.preferences); | ||
} | ||
|
||
@Test public void testParseValidKey() { | ||
final int res = subject.getTitleRes("com.zegoggles.smssync.activity.fragments.AutoBackupSettings"); | ||
assertThat(res).isGreaterThan(0); | ||
String resolved = RuntimeEnvironment.application.getString(res); | ||
assertThat(resolved).isEqualTo("Auto backup settings"); | ||
} | ||
|
||
@Test public void testInvalidKeyReturnsZero() { | ||
final int res = subject.getTitleRes("foo.bar.not.found"); | ||
assertThat(res).isEqualTo(0); | ||
} | ||
} |