Skip to content

Commit 3e6b45c

Browse files
fix!: correct pixels for android auto padding
1 parent fcf583f commit 3e6b45c

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

android/src/main/java/com/google/android/react/navsdk/AndroidAutoBaseScreen.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.graphics.Point;
2121
import android.hardware.display.DisplayManager;
2222
import android.hardware.display.VirtualDisplay;
23+
import android.util.DisplayMetrics;
2324
import androidx.annotation.NonNull;
2425
import androidx.annotation.Nullable;
2526
import androidx.car.app.AppManager;
@@ -62,6 +63,7 @@ public abstract class AndroidAutoBaseScreen extends Screen
6263
protected GoogleMap mGoogleMap;
6364
protected boolean mNavigationInitialized = false;
6465
private MapViewController mMapViewController;
66+
private float mDisplayDensity = 1f;
6567

6668
private boolean mAndroidAutoModuleInitialized = false;
6769
private boolean mNavModuleInitialized = false;
@@ -163,6 +165,11 @@ private void unRegisterControllersForAndroidAutoModule() {
163165
}
164166
}
165167

168+
/** Returns the density of the Android Auto surface currently used. */
169+
public float getDisplayDensity() {
170+
return mDisplayDensity;
171+
}
172+
166173
private boolean isSurfaceReady(SurfaceContainer surfaceContainer) {
167174
return surfaceContainer.getSurface() != null
168175
&& surfaceContainer.getDpi() != 0
@@ -175,6 +182,7 @@ public void onSurfaceAvailable(@NonNull SurfaceContainer surfaceContainer) {
175182
if (!isSurfaceReady(surfaceContainer)) {
176183
return;
177184
}
185+
mDisplayDensity = surfaceContainer.getDpi() / (float) DisplayMetrics.DENSITY_DEFAULT;
178186
mVirtualDisplay =
179187
getCarContext()
180188
.getSystemService(DisplayManager.class)

android/src/main/java/com/google/android/react/navsdk/NavAutoModule.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.facebook.react.bridge.UiThreadUtil;
2626
import com.facebook.react.bridge.WritableArray;
2727
import com.facebook.react.bridge.WritableMap;
28-
import com.facebook.react.uimanager.PixelUtil;
2928
import com.google.android.gms.maps.UiSettings;
3029
import com.google.android.gms.maps.model.CameraPosition;
3130
import com.google.android.gms.maps.model.Circle;
@@ -620,20 +619,28 @@ public void isAutoScreenAvailable(final Promise promise) {
620619

621620
@Override
622621
public void setMapPadding(double top, double left, double bottom, double right) {
623-
int topInt = Math.round(PixelUtil.toPixelFromDIP(top));
624-
int leftInt = Math.round(PixelUtil.toPixelFromDIP(left));
625-
int bottomInt = Math.round(PixelUtil.toPixelFromDIP(bottom));
626-
int rightInt = Math.round(PixelUtil.toPixelFromDIP(right));
627622
UiThreadUtil.runOnUiThread(
628623
() -> {
629624
if (mMapViewController == null) {
630625
return;
631626
}
632627

633-
mMapViewController.setPadding(topInt, leftInt, bottomInt, rightInt);
628+
float density = getAutoDisplayDensity();
629+
mMapViewController.setPadding(
630+
Math.round((float) (top * density)),
631+
Math.round((float) (left * density)),
632+
Math.round((float) (bottom * density)),
633+
Math.round((float) (right * density)));
634634
});
635635
}
636636

637+
private float getAutoDisplayDensity() {
638+
if (mAutoScreen != null) {
639+
return mAutoScreen.getDisplayDensity();
640+
}
641+
return reactContext.getResources().getDisplayMetrics().density;
642+
}
643+
637644
@Override
638645
public void getMarkers(final Promise promise) {
639646
UiThreadUtil.runOnUiThread(

0 commit comments

Comments
 (0)