|
2 | 2 |
|
3 | 3 | import static android.content.Context.WINDOW_SERVICE; |
4 | 4 |
|
| 5 | +import android.content.Context; |
| 6 | +import android.graphics.Point; |
| 7 | +import android.graphics.Rect; |
| 8 | +import android.os.Build; |
5 | 9 | import android.preference.PreferenceManager; |
6 | 10 | import android.util.Log; |
| 11 | +import android.view.Display; |
7 | 12 | import android.view.Surface; |
8 | 13 | import android.view.WindowManager; |
| 14 | +import android.view.WindowMetrics; |
9 | 15 |
|
10 | 16 |
|
11 | 17 | import com.blackboxembedded.WunderLINQ.hardware.WLQ.MotorcycleData; |
@@ -96,24 +102,30 @@ public static String svgFilename(String dashName) { |
96 | 102 | } |
97 | 103 |
|
98 | 104 | public static boolean isDevicePortrait() { |
99 | | - boolean portrait = false; |
100 | | - |
101 | 105 | try { |
102 | | - WindowManager wm = (WindowManager) MyApplication.getContext().getSystemService(WINDOW_SERVICE); |
| 106 | + Context context = MyApplication.getContext(); |
103 | 107 |
|
| 108 | + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
104 | 109 | 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; |
110 | 121 | } |
111 | 122 | } |
112 | 123 | } catch (Exception e) { |
113 | 124 | Log.e("IsPortrait Error", e.toString()); |
114 | 125 | } |
115 | 126 |
|
116 | | - return portrait; |
| 127 | + // Default: assume landscape if something goes wrong |
| 128 | + return false; |
117 | 129 | } |
118 | 130 |
|
119 | 131 | public String getDataLabel(int infoLine) { |
|
0 commit comments