fix:App should respect device font-size (#4306)

* * Add adjuctFontScale function  to fix issue
* Add Javadoc

* reformed the codechange

* rearrange the codechange

* make metric and wm variable final
This commit is contained in:
Prince kushwaha 2021-04-06 14:17:38 +05:30 committed by GitHub
parent 881239dc59
commit 12547fd34a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,10 +1,11 @@
package fr.free.nrw.commons.theme;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import javax.inject.Inject;
import javax.inject.Named;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.di.CommonsDaggerAppCompatActivity;
import fr.free.nrw.commons.kvstore.JsonKvStore;
@ -27,6 +28,11 @@ public abstract class BaseActivity extends CommonsDaggerAppCompatActivity {
super.onCreate(savedInstanceState);
wasPreviouslyDarkTheme = systemThemeUtils.isDeviceInNightMode();
setTheme(wasPreviouslyDarkTheme ? R.style.DarkAppTheme : R.style.LightAppTheme);
float fontScale = android.provider.Settings.System.getFloat(
getBaseContext().getContentResolver(),
android.provider.Settings.System.FONT_SCALE,
1f);
adjustFontScale(getResources().getConfiguration(), fontScale);
}
@Override
@ -44,4 +50,17 @@ public abstract class BaseActivity extends CommonsDaggerAppCompatActivity {
super.onDestroy();
compositeDisposable.clear();
}
/**
* Apply fontScale on device
*/
public void adjustFontScale(Configuration configuration, float scale) {
configuration.fontScale = scale;
final DisplayMetrics metrics = getResources().getDisplayMetrics();
final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
}