Skip to content

Commit c101c85

Browse files
Determine dashboard orientation from window dimensions
Signed-off-by: Keith Conger <keith.conger@blackboxembedded.com>
1 parent edda704 commit c101c85

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

  • app/src/main/java/com/blackboxembedded/WunderLINQ/SVGDashboards

app/src/main/java/com/blackboxembedded/WunderLINQ/SVGDashboards/SVGHelper.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
import static android.content.Context.WINDOW_SERVICE;
44

5+
import android.content.Context;
6+
import android.graphics.Point;
7+
import android.graphics.Rect;
8+
import android.os.Build;
59
import android.preference.PreferenceManager;
610
import android.util.Log;
11+
import android.view.Display;
712
import android.view.Surface;
813
import android.view.WindowManager;
14+
import android.view.WindowMetrics;
915

1016

1117
import com.blackboxembedded.WunderLINQ.hardware.WLQ.MotorcycleData;
@@ -96,24 +102,30 @@ public static String svgFilename(String dashName) {
96102
}
97103

98104
public static boolean isDevicePortrait() {
99-
boolean portrait = false;
100-
101105
try {
102-
WindowManager wm = (WindowManager) MyApplication.getContext().getSystemService(WINDOW_SERVICE);
106+
Context context = MyApplication.getContext();
103107

108+
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
104109
if (wm != null) {
105-
int rotation = wm.getDefaultDisplay().getRotation();
106-
107-
// Determine the screen orientation based on the rotation value
108-
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
109-
portrait = true;
110+
// Get current window metrics (API 30+)
111+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
112+
WindowMetrics metrics = wm.getCurrentWindowMetrics();
113+
Rect bounds = metrics.getBounds();
114+
return bounds.height() >= bounds.width();
115+
} else {
116+
// Fallback for older APIs
117+
Display display = wm.getDefaultDisplay();
118+
Point size = new Point();
119+
display.getSize(size);
120+
return size.y >= size.x;
110121
}
111122
}
112123
} catch (Exception e) {
113124
Log.e("IsPortrait Error", e.toString());
114125
}
115126

116-
return portrait;
127+
// Default: assume landscape if something goes wrong
128+
return false;
117129
}
118130

119131
public String getDataLabel(int infoLine) {

0 commit comments

Comments
 (0)