use TypedValueCompat for dp <-> px conversion

This commit is contained in:
Helium314 2024-03-06 20:55:47 +01:00
parent 1874e11d30
commit a0c7b28b91
8 changed files with 18 additions and 11 deletions

View file

@ -19,6 +19,7 @@ import android.view.inputmethod.InputMethodSubtype;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.util.TypedValueCompat;
import helium314.keyboard.keyboard.internal.BatchInputArbiter; import helium314.keyboard.keyboard.internal.BatchInputArbiter;
import helium314.keyboard.keyboard.internal.BatchInputArbiter.BatchInputArbiterListener; import helium314.keyboard.keyboard.internal.BatchInputArbiter.BatchInputArbiterListener;
@ -84,7 +85,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
// Parameters for pointer handling. // Parameters for pointer handling.
private static PointerTrackerParams sParams; private static PointerTrackerParams sParams;
private static final int sPointerStep = (int)(10.0 * Resources.getSystem().getDisplayMetrics().density); private static final int sPointerStep = (int)TypedValueCompat.dpToPx(10, Resources.getSystem().getDisplayMetrics());
private static GestureStrokeRecognitionParams sGestureStrokeRecognitionParams; private static GestureStrokeRecognitionParams sGestureStrokeRecognitionParams;
private static GestureStrokeDrawingParams sGestureStrokeDrawingParams; private static GestureStrokeDrawingParams sGestureStrokeDrawingParams;
private static boolean sNeedsPhantomSuddenMoveEventHack; private static boolean sNeedsPhantomSuddenMoveEventHack;

View file

@ -6,6 +6,7 @@ import android.content.SharedPreferences
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.core.util.TypedValueCompat
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.TwoStatePreference import androidx.preference.TwoStatePreference
@ -80,8 +81,8 @@ class AppearanceSettingsFragment : SubScreenFragment() {
} }
} }
val metrics = requireContext().resources.displayMetrics val metrics = requireContext().resources.displayMetrics
val widthDp = metrics.widthPixels / metrics.density val widthDp = TypedValueCompat.pxToDp(metrics.widthPixels.toFloat(), metrics)
val heightDp = metrics.heightPixels / metrics.density val heightDp = TypedValueCompat.pxToDp(metrics.heightPixels.toFloat(), metrics)
if ((min(widthDp, heightDp) < 600 && max(widthDp, heightDp) < 720)) { if ((min(widthDp, heightDp) < 600 && max(widthDp, heightDp) < 720)) {
removePreference(Settings.PREF_ENABLE_SPLIT_KEYBOARD) removePreference(Settings.PREF_ENABLE_SPLIT_KEYBOARD)
removePreference(Settings.PREF_SPLIT_SPACER_SCALE) removePreference(Settings.PREF_SPLIT_SPACER_SCALE)

View file

@ -326,7 +326,7 @@ class LanguageSettingsDialog(
val attrs = context.obtainStyledAttributes(R.style.PreferenceSubtitleText, intArrayOf(android.R.attr.textSize)) val attrs = context.obtainStyledAttributes(R.style.PreferenceSubtitleText, intArrayOf(android.R.attr.textSize))
setTextSize(TypedValue.COMPLEX_UNIT_PX, attrs.getDimension(0, 20f)) setTextSize(TypedValue.COMPLEX_UNIT_PX, attrs.getDimension(0, 20f))
attrs.recycle() attrs.recycle()
setPadding((context.resources.displayMetrics.scaledDensity * 16).toInt(), 0, 0, 0) setPadding(ResourceUtils.toPx(16, context.resources), 0, 0, 0)
isEnabled = userDicts.none { it.name == "${DictionaryInfoUtils.MAIN_DICT_PREFIX}${USER_DICTIONARY_SUFFIX}" } isEnabled = userDicts.none { it.name == "${DictionaryInfoUtils.MAIN_DICT_PREFIX}${USER_DICTIONARY_SUFFIX}" }
}) })
} }

View file

@ -15,6 +15,7 @@ import android.view.inputmethod.InputMethodSubtype;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.util.TypedValueCompat;
import helium314.keyboard.compat.ConfigurationCompatKt; import helium314.keyboard.compat.ConfigurationCompatKt;
import helium314.keyboard.keyboard.internal.keyboard_parser.LocaleKeyboardInfosKt; import helium314.keyboard.keyboard.internal.keyboard_parser.LocaleKeyboardInfosKt;
@ -167,7 +168,7 @@ public class SettingsValues {
mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res); mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res);
mDoubleSpacePeriodTimeout = res.getInteger(R.integer.config_double_space_period_timeout); mDoubleSpacePeriodTimeout = res.getInteger(R.integer.config_double_space_period_timeout);
mHasHardwareKeyboard = Settings.readHasHardwareKeyboard(res.getConfiguration()); mHasHardwareKeyboard = Settings.readHasHardwareKeyboard(res.getConfiguration());
final float displayWidthDp = res.getDisplayMetrics().widthPixels / res.getDisplayMetrics().density; final float displayWidthDp = TypedValueCompat.pxToDp(res.getDisplayMetrics().widthPixels, res.getDisplayMetrics());
mIsSplitKeyboardEnabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD, false) && displayWidthDp > 600; // require display width of 600 dp for split mIsSplitKeyboardEnabled = prefs.getBoolean(Settings.PREF_ENABLE_SPLIT_KEYBOARD, false) && displayWidthDp > 600; // require display width of 600 dp for split
// determine spacerWidth from display width and scale setting // determine spacerWidth from display width and scale setting
mSplitKeyboardSpacerRelativeWidth = mIsSplitKeyboardEnabled mSplitKeyboardSpacerRelativeWidth = mIsSplitKeyboardEnabled

View file

@ -6,8 +6,6 @@
package helium314.keyboard.latin.setup; package helium314.keyboard.latin.setup;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
import android.content.Intent; import android.content.Intent;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.content.res.Resources; import android.content.res.Resources;
@ -15,7 +13,6 @@ import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.os.Message; import android.os.Message;
import android.provider.Settings; import android.provider.Settings;
import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.TextView; import android.widget.TextView;
@ -33,6 +30,7 @@ import helium314.keyboard.latin.settings.SettingsActivity;
import helium314.keyboard.latin.utils.ActivityThemeUtils; import helium314.keyboard.latin.utils.ActivityThemeUtils;
import helium314.keyboard.latin.utils.JniUtils; import helium314.keyboard.latin.utils.JniUtils;
import helium314.keyboard.latin.utils.LeakGuardHandlerWrapper; import helium314.keyboard.latin.utils.LeakGuardHandlerWrapper;
import helium314.keyboard.latin.utils.ResourceUtils;
import helium314.keyboard.latin.utils.UncachedInputMethodManagerUtils; import helium314.keyboard.latin.utils.UncachedInputMethodManagerUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -387,7 +385,7 @@ public final class SetupWizardActivity extends AppCompatActivity implements View
final int paddingEnd = mActionLabel.getPaddingEnd(); final int paddingEnd = mActionLabel.getPaddingEnd();
mActionLabel.setPaddingRelative(paddingEnd, 0, paddingEnd, 0); mActionLabel.setPaddingRelative(paddingEnd, 0, paddingEnd, 0);
} else { } else {
final int size = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, 24f, res.getDisplayMetrics()); final int size = ResourceUtils.toPx(24, res);
actionIconDrawable.setBounds(0,0, size, size); actionIconDrawable.setBounds(0,0, size, size);
mActionLabel.setCompoundDrawablesRelative(actionIconDrawable, null, null, null); mActionLabel.setCompoundDrawablesRelative(actionIconDrawable, null, null, null);
} }

View file

@ -58,7 +58,7 @@ fun loadCustomLayout(layoutContent: String, layoutName: String, languageTag: Str
.setView(EditText(context).apply { .setView(EditText(context).apply {
setText(name) setText(name)
doAfterTextChanged { name = it.toString() } doAfterTextChanged { name = it.toString() }
val padding = (8 * context.resources.displayMetrics.density).toInt() val padding = ResourceUtils.toPx(8, context.resources)
setPadding(3 * padding, padding, 3 * padding, padding) setPadding(3 * padding, padding, 3 * padding, padding)
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_NORMAL inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_NORMAL
}) })

View file

@ -117,7 +117,7 @@ fun reorderPopupKeysDialog(context: Context, key: String, defaultSetting: String
both.first() to both.last().toBoolean() both.first() to both.last().toBoolean()
} }
val rv = RecyclerView(context) val rv = RecyclerView(context)
val padding = (8 * context.resources.displayMetrics.density).toInt() val padding = ResourceUtils.toPx(8, context.resources)
rv.setPadding(3 * padding, padding, padding, padding) rv.setPadding(3 * padding, padding, padding, padding)
rv.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) rv.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
val callback = object : DiffUtil.ItemCallback<Pair<String, Boolean>>() { val callback = object : DiffUtil.ItemCallback<Pair<String, Boolean>>() {

View file

@ -14,6 +14,8 @@ import android.text.TextUtils;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import androidx.core.util.TypedValueCompat;
import helium314.keyboard.latin.R; import helium314.keyboard.latin.R;
import helium314.keyboard.latin.settings.SettingsValues; import helium314.keyboard.latin.settings.SettingsValues;
@ -259,4 +261,8 @@ public final class ResourceUtils {
public static boolean isNight(final Resources res) { public static boolean isNight(final Resources res) {
return (res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; return (res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
} }
public static int toPx(final int dp, final Resources res) {
return (int) TypedValueCompat.dpToPx(dp, res.getDisplayMetrics());
}
} }