support wrist orientation in recovery/fastbootd

Add support for wrist orientation in recovery. Feature controlled by
the property "config.enable_wristorientation".

Read 'ro.boot.wrist_orientation' property to determine if screen
orientation should be flipped and swipes inverted.

Bug: 257123026
Bug: 309982093

Test: build boot/recovery
Test: recovery/fastbootd uses same orientation as android

Change-Id: I29e0e682cca81d302dae502382b6270afe105d72
diff --git a/recovery_ui/wear_ui.cpp b/recovery_ui/wear_ui.cpp
index 7b9ecf8..552f0cf 100644
--- a/recovery_ui/wear_ui.cpp
+++ b/recovery_ui/wear_ui.cpp
@@ -22,6 +22,7 @@
 #include <string>
 #include <vector>
 
+#include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/strings.h>
 
@@ -44,6 +45,50 @@
   touch_screen_allowed_ = true;
 }
 
+static void FlipOrientation() {
+  auto rotation = gr_get_rotation();
+  if (rotation == GRRotation::NONE) {
+    gr_rotate(GRRotation::DOWN);
+  } else if (rotation == GRRotation::DOWN) {
+    gr_rotate(GRRotation::NONE);
+  } else {
+    LOG(WARNING) << "Unsupported rotation for wrist orientation" << static_cast<int>(rotation);
+  }
+}
+
+// Match values in
+// frameworks/opt/wear/src/com/android/clockwork/wristorientation/WristOrientationService.java
+enum class WristOrientation : unsigned {
+  LEFT_WRIST_ROTATION_0 = 0,
+  LEFT_WRIST_ROTATION_180 = 1,
+  RIGHT_WRIST_ROTATION_0 = 2,
+  RIGHT_WRIST_ROTATION_180 = 3,
+};
+
+static void InitWristOrientation() {
+  auto prop = android::base::GetUintProperty("ro.boot.wrist_orientation", 0u);
+  WristOrientation orientation{ prop };
+  if (orientation == WristOrientation::LEFT_WRIST_ROTATION_180 ||
+      orientation == WristOrientation::RIGHT_WRIST_ROTATION_180) {
+    LOG(INFO)
+        << "InitWristOrientation(): flipping orientation because, 'ro.boot.wrist_orientation'="
+        << prop;
+
+    FlipOrientation();
+  }
+}
+
+bool WearRecoveryUI::Init(const std::string& locale) {
+  auto result = ScreenRecoveryUI::Init(locale);
+  auto wrist_orientation_enabled =
+      android::base::GetBoolProperty("config.enable_wristorientation", false);
+  LOG(INFO) << "WearRecoveryUI::Init(): enable_wristorientation=" << wrist_orientation_enabled;
+  if (wrist_orientation_enabled) {
+    InitWristOrientation();
+  }
+  return result;
+}
+
 // Draw background frame on the screen.  Does not flip pages.
 // Should only be called with updateMutex locked.
 // TODO merge drawing routines with screen_ui