From c56f61a138e3b16792d0f1890fb70081e695ee23 Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 17 Feb 2015 17:28:56 +0100 Subject: [PATCH] cpp-143: Calculator text size dynamically in Bug Buttons layout Fixes #143 --- .../org/solovyev/android/calculator/App.java | 10 ++ .../android/calculator/CalculatorButtons.java | 8 +- .../calculator/view/ScreenMetrics.java | 120 ++++++++++++++++++ .../src/main/res/values-large-hdpi/dimens.xml | 1 - .../src/main/res/values-large-mdpi/dimens.xml | 1 - .../main/res/values-large-tvdpi/dimens.xml | 1 - .../main/res/values-large-xhdpi/dimens.xml | 1 - .../main/res/values-normal-hdpi/dimens.xml | 1 - .../main/res/values-normal-mdpi/dimens.xml | 1 - .../main/res/values-normal-tvdpi/dimens.xml | 1 - .../main/res/values-normal-xhdpi/dimens.xml | 1 - .../src/main/res/values-small-hdpi/dimens.xml | 1 - .../main/res/values-small-xhdpi/dimens.xml | 1 - .../main/res/values-xlarge-hdpi/dimens.xml | 1 - .../main/res/values-xlarge-mdpi/dimens.xml | 1 - android-app/src/main/res/values/dimens.xml | 1 - 16 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 android-app/src/main/java/org/solovyev/android/calculator/view/ScreenMetrics.java diff --git a/android-app/src/main/java/org/solovyev/android/calculator/App.java b/android-app/src/main/java/org/solovyev/android/calculator/App.java index 0a701b12..f6175b7e 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/App.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/App.java @@ -34,6 +34,7 @@ import android.util.Log; import org.solovyev.android.UiThreadExecutor; import org.solovyev.android.Views; import org.solovyev.android.calculator.ga.Ga; +import org.solovyev.android.calculator.view.ScreenMetrics; import org.solovyev.android.checkout.*; import org.solovyev.android.view.VibratorContainer; import org.solovyev.common.listeners.JEvent; @@ -105,6 +106,9 @@ public final class App { @Nonnull private static volatile Vibrator vibrator; + @Nonnull + private static volatile ScreenMetrics screenMetrics; + private App() { throw new AssertionError(); } @@ -161,6 +165,7 @@ public final class App { } App.broadcaster = new CalculatorBroadcaster(application); App.vibrator = new Vibrator(application, preferences); + App.screenMetrics = new ScreenMetrics(application); App.initialized = true; } else { throw new IllegalStateException("Already initialized!"); @@ -261,4 +266,9 @@ public final class App { public static Vibrator getVibrator() { return vibrator; } + + @Nonnull + public static ScreenMetrics getScreenMetrics() { + return screenMetrics; + } } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java b/android-app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java index cacc9c96..361a7825 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/CalculatorButtons.java @@ -43,6 +43,7 @@ import org.solovyev.android.calculator.view.NumeralBasesButton; import org.solovyev.android.calculator.drag.DragButton; import org.solovyev.android.calculator.drag.DragDirection; import org.solovyev.android.calculator.drag.SimpleDragListener; +import org.solovyev.android.calculator.view.ScreenMetrics; import org.solovyev.common.math.Point2d; import javax.annotation.Nonnull; @@ -64,7 +65,12 @@ public final class CalculatorButtons { @Nonnull View root) { if (!layout.isOptimized()) { - final float textSize = root.getContext().getResources().getDimensionPixelSize(R.dimen.cpp_keyboard_button_text_size_mobile); + final ScreenMetrics metrics = App.getScreenMetrics(); + final boolean portrait = metrics.isInPortraitMode(); + final int buttonsCount = portrait ? 5 : 4; + final int buttonsWeight = portrait ? (2 + 1 + buttonsCount) : (2 + buttonsCount); + final int buttonSize = metrics.getHeightPxs() / buttonsWeight; + final int textSize = 5 * buttonSize / 12; Views.processViewsOfType(root, DragButton.class, new Views.ViewProcessor() { @Override diff --git a/android-app/src/main/java/org/solovyev/android/calculator/view/ScreenMetrics.java b/android-app/src/main/java/org/solovyev/android/calculator/view/ScreenMetrics.java new file mode 100644 index 00000000..256dd3a2 --- /dev/null +++ b/android-app/src/main/java/org/solovyev/android/calculator/view/ScreenMetrics.java @@ -0,0 +1,120 @@ +package org.solovyev.android.calculator.view; + +import android.content.Context; +import android.content.res.Configuration; +import android.os.Build; +import android.util.DisplayMetrics; +import android.view.WindowManager; + +import javax.annotation.Nonnull; + +public class ScreenMetrics { + + @Nonnull + private final android.view.Display display; + @Nonnull + private final DisplayMetrics metrics; + private float xDpi; + private float yDpi; + private float diagonalIns; + private float widthIns; + private float heightIns; + private final int layout; + + public ScreenMetrics(@Nonnull Context context) { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + display = wm.getDefaultDisplay(); + metrics = new DisplayMetrics(); + display.getMetrics(metrics); + initDpi(); + initDimensions(); + layout = context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; + } + + private void initDpi() { + if ((Build.DEVICE.equals("qsd8250_surf") + || Build.MODEL.equals("Dell Streak"))) { + xDpi = 190f; + yDpi = 190f; + } else if (Build.MODEL.equals("VTAB1008")) { + xDpi = 160f; + yDpi = 160f; + } else if (Build.MODEL.equals("Dell Streak 7")) { + xDpi = 150f; + yDpi = 150f; + } else if (Build.MODEL.equals("A1_07")) { + xDpi = 127.5f; + yDpi = 100f; + } else if (Build.MODEL.equals("N12GPS") + || Build.MODEL.equals("MID_Serials")) { + xDpi = 133f; + yDpi = 133f; + } else if (Build.MODEL.startsWith("GT-N710") + || Build.MODEL.equalsIgnoreCase("SCH-N719") + || Build.MODEL.startsWith("SHV-E250")) { + xDpi = 267f; + yDpi = 267f; + } else if (metrics.densityDpi - metrics.xdpi >= 79.0 + || metrics.densityDpi - metrics.ydpi >= 79.0 + || Math.abs(metrics.ydpi - metrics.xdpi) > 40.0) { + xDpi = yDpi = metrics.densityDpi; + } else { + xDpi = metrics.xdpi; + yDpi = metrics.ydpi; + } + } + + private void initDimensions() { + final float w = getWidthPxs() / xDpi; + final float h = getHeightPxs() / yDpi; + diagonalIns = (float) Math.sqrt(w * w + h * h); + widthIns = w; + heightIns = h; + } + + public boolean isInPortraitMode() { + return getWidthPxs() < getHeightPxs(); + } + + @SuppressWarnings("deprecation") + public int getWidthPxs() { + return display.getWidth(); + } + + @SuppressWarnings("deprecation") + public int getHeightPxs() { + return display.getHeight(); + } + + public float getWidthIns() { + return widthIns; + } + + public float getHeightIns() { + return heightIns; + } + + public int getDensityDpi() { + return metrics.densityDpi; + } + + public float getDensity() { + return metrics.density; + } + + public float getXDpi() { + return xDpi; + } + + public float getYDpi() { + return yDpi; + } + + public float getDiagonalIns() { + return diagonalIns; + } + + public int getLayout() { + return layout; + } +} diff --git a/android-app/src/main/res/values-large-hdpi/dimens.xml b/android-app/src/main/res/values-large-hdpi/dimens.xml index 1d43d1f3..009ff17d 100644 --- a/android-app/src/main/res/values-large-hdpi/dimens.xml +++ b/android-app/src/main/res/values-large-hdpi/dimens.xml @@ -28,5 +28,4 @@ 40sp 24dp - 25dp \ No newline at end of file diff --git a/android-app/src/main/res/values-large-mdpi/dimens.xml b/android-app/src/main/res/values-large-mdpi/dimens.xml index 6aa40741..9d05e8cc 100644 --- a/android-app/src/main/res/values-large-mdpi/dimens.xml +++ b/android-app/src/main/res/values-large-mdpi/dimens.xml @@ -28,5 +28,4 @@ 35sp 35dp - 45dp \ No newline at end of file diff --git a/android-app/src/main/res/values-large-tvdpi/dimens.xml b/android-app/src/main/res/values-large-tvdpi/dimens.xml index 7a99ce9d..9ef590ed 100644 --- a/android-app/src/main/res/values-large-tvdpi/dimens.xml +++ b/android-app/src/main/res/values-large-tvdpi/dimens.xml @@ -28,5 +28,4 @@ 35sp 33dp - 30dp \ No newline at end of file diff --git a/android-app/src/main/res/values-large-xhdpi/dimens.xml b/android-app/src/main/res/values-large-xhdpi/dimens.xml index 75ce2f3b..6a89cc24 100644 --- a/android-app/src/main/res/values-large-xhdpi/dimens.xml +++ b/android-app/src/main/res/values-large-xhdpi/dimens.xml @@ -22,5 +22,4 @@ 30dp - 40dp \ No newline at end of file diff --git a/android-app/src/main/res/values-normal-hdpi/dimens.xml b/android-app/src/main/res/values-normal-hdpi/dimens.xml index 8bf9d432..6a89cc24 100644 --- a/android-app/src/main/res/values-normal-hdpi/dimens.xml +++ b/android-app/src/main/res/values-normal-hdpi/dimens.xml @@ -22,5 +22,4 @@ 30dp - 20dp \ No newline at end of file diff --git a/android-app/src/main/res/values-normal-mdpi/dimens.xml b/android-app/src/main/res/values-normal-mdpi/dimens.xml index 9ba50728..0d81d6df 100644 --- a/android-app/src/main/res/values-normal-mdpi/dimens.xml +++ b/android-app/src/main/res/values-normal-mdpi/dimens.xml @@ -22,5 +22,4 @@ 24dp - 25dp \ No newline at end of file diff --git a/android-app/src/main/res/values-normal-tvdpi/dimens.xml b/android-app/src/main/res/values-normal-tvdpi/dimens.xml index f0b1d963..b16279ff 100644 --- a/android-app/src/main/res/values-normal-tvdpi/dimens.xml +++ b/android-app/src/main/res/values-normal-tvdpi/dimens.xml @@ -22,5 +22,4 @@ 33dp - 25dp \ No newline at end of file diff --git a/android-app/src/main/res/values-normal-xhdpi/dimens.xml b/android-app/src/main/res/values-normal-xhdpi/dimens.xml index 75bf8298..780e8926 100644 --- a/android-app/src/main/res/values-normal-xhdpi/dimens.xml +++ b/android-app/src/main/res/values-normal-xhdpi/dimens.xml @@ -22,5 +22,4 @@ 32dp - 15dp \ No newline at end of file diff --git a/android-app/src/main/res/values-small-hdpi/dimens.xml b/android-app/src/main/res/values-small-hdpi/dimens.xml index 777da191..abb3e3b8 100644 --- a/android-app/src/main/res/values-small-hdpi/dimens.xml +++ b/android-app/src/main/res/values-small-hdpi/dimens.xml @@ -22,5 +22,4 @@ 22dp - 14dp \ No newline at end of file diff --git a/android-app/src/main/res/values-small-xhdpi/dimens.xml b/android-app/src/main/res/values-small-xhdpi/dimens.xml index 1316eca1..4394102d 100644 --- a/android-app/src/main/res/values-small-xhdpi/dimens.xml +++ b/android-app/src/main/res/values-small-xhdpi/dimens.xml @@ -22,5 +22,4 @@ 20dp - 10dp \ No newline at end of file diff --git a/android-app/src/main/res/values-xlarge-hdpi/dimens.xml b/android-app/src/main/res/values-xlarge-hdpi/dimens.xml index 77bb4fd0..41c68dc4 100644 --- a/android-app/src/main/res/values-xlarge-hdpi/dimens.xml +++ b/android-app/src/main/res/values-xlarge-hdpi/dimens.xml @@ -29,5 +29,4 @@ 30sp 40dp - 75dp \ No newline at end of file diff --git a/android-app/src/main/res/values-xlarge-mdpi/dimens.xml b/android-app/src/main/res/values-xlarge-mdpi/dimens.xml index 07e9702f..90a6d0fc 100644 --- a/android-app/src/main/res/values-xlarge-mdpi/dimens.xml +++ b/android-app/src/main/res/values-xlarge-mdpi/dimens.xml @@ -29,5 +29,4 @@ 30sp 35dp - 65dp \ No newline at end of file diff --git a/android-app/src/main/res/values/dimens.xml b/android-app/src/main/res/values/dimens.xml index b540125b..ebe13b79 100644 --- a/android-app/src/main/res/values/dimens.xml +++ b/android-app/src/main/res/values/dimens.xml @@ -16,7 +16,6 @@ 30dp 20dp - 20dp 25sp 25sp