-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from sergio-sastre/#issue1/not_working_from_api26+
#issue1/not working from api26+
- Loading branch information
Showing
8 changed files
with
146 additions
and
36 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
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
30 changes: 0 additions & 30 deletions
30
rules/fontsize/src/main/java/sergio/sastre/fontsize/FontScaleSetting.kt
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
...ontsize/src/main/java/sergio/sastre/fontsize/activityscenario/MockFontSizeActivityFile.kt
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,49 @@ | ||
package sergio.sastre.fontsize | ||
|
||
import android.content.Context | ||
import android.content.res.Configuration | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.test.core.app.ActivityScenario | ||
|
||
object FontSizeActivityScenario { | ||
fun launchWith(fontScale: FontScale): ActivityScenario<out MockFontSizeActivity> { | ||
val clazz = when (fontScale){ | ||
FontScale.SMALL -> SmallFontSizeActivity::class.java | ||
FontScale.NORMAL -> NormalFontSizeActivity::class.java | ||
FontScale.LARGE -> LargeFontSizeActivity::class.java | ||
FontScale.HUGE -> HugeFontSizeActivity::class.java | ||
} | ||
return ActivityScenario.launch(clazz) | ||
} | ||
} | ||
|
||
abstract class MockFontSizeActivity : AppCompatActivity() { | ||
abstract val fontScale: FontScale | ||
|
||
override fun attachBaseContext(newBase: Context?) { | ||
val newConfig = Configuration(newBase!!.resources.configuration) | ||
newConfig.fontScale = fontScale.value.toFloat() | ||
applyOverrideConfiguration(newConfig) | ||
super.attachBaseContext(newBase) | ||
} | ||
} | ||
|
||
class SmallFontSizeActivity : MockFontSizeActivity() { | ||
override val fontScale: FontScale | ||
get() = FontScale.SMALL | ||
} | ||
|
||
class NormalFontSizeActivity : MockFontSizeActivity() { | ||
override val fontScale: FontScale | ||
get() = FontScale.NORMAL | ||
} | ||
|
||
class LargeFontSizeActivity : MockFontSizeActivity() { | ||
override val fontScale: FontScale | ||
get() = FontScale.LARGE | ||
} | ||
|
||
class HugeFontSizeActivity : MockFontSizeActivity() { | ||
override val fontScale: FontScale | ||
get() = FontScale.HUGE | ||
} |
2 changes: 1 addition & 1 deletion
2
.../java/sergio/sastre/fontsize/Condition.kt → ...gio/sastre/fontsize/testrule/Condition.kt
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
3 changes: 2 additions & 1 deletion
3
.../sergio/sastre/fontsize/FontScaleRules.kt → ...astre/fontsize/testrule/FontScaleRules.kt
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
44 changes: 44 additions & 0 deletions
44
rules/fontsize/src/main/java/sergio/sastre/fontsize/testrule/FontScaleSetting.kt
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,44 @@ | ||
package sergio.sastre.fontsize.testrule | ||
|
||
/** | ||
* Concept taken from : https://github.com/novoda/espresso-support | ||
*/ | ||
import android.content.res.Resources | ||
import android.os.Build | ||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation | ||
import sergio.sastre.fontsize.FontScale | ||
|
||
class FontScaleSetting internal constructor(private val resources: Resources) { | ||
|
||
fun get(): FontScale { | ||
return FontScale.from(resources.configuration.fontScale) | ||
} | ||
|
||
fun set(scale: FontScale) { | ||
try { | ||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){ | ||
changeFontScaleFromApi25(scale) | ||
} else { | ||
changeFontScalePreApi25(scale) | ||
} | ||
|
||
} catch (e: Exception) { | ||
throw saveFontScaleError(scale) | ||
} | ||
} | ||
|
||
private fun changeFontScalePreApi25(scale: FontScale){ | ||
resources.configuration.fontScale = java.lang.Float.parseFloat(scale.value) | ||
val metrics = Resources.getSystem().displayMetrics | ||
metrics.scaledDensity = resources.configuration.fontScale * metrics.density | ||
resources.updateConfiguration(resources.configuration, metrics) | ||
} | ||
|
||
private fun changeFontScaleFromApi25(scale: FontScale){ | ||
getInstrumentation().uiAutomation.executeShellCommand("settings put system font_scale " + scale.value) | ||
} | ||
|
||
private fun saveFontScaleError(scale: FontScale): RuntimeException { | ||
return RuntimeException("Unable to save font size " + scale.name) | ||
} | ||
} |
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