Developer option to force RTL layout direction

Sets a global setting and system property and forces a config change
when developer selects to force RTL screen layout direction.

Bug: 10244047
Change-Id: Id91e52be5e492515ff1eef3cbccfb08e944bfc92
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 6b9cf74..a4e51e0 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4155,6 +4155,11 @@
     <!-- UI debug setting: show layout bounds information summary [CHAR LIMIT=50] -->
     <string name="debug_layout_summary">Show clip bounds, margins, etc.</string>
 
+    <!-- UI debug setting: force right to left layout [CHAR LIMIT=25] -->
+    <string name="force_rtl_layout_all_locales">Force RTL layout direction</string>
+    <!-- UI debug setting: force right to left layout summary [CHAR LIMIT=50] -->
+    <string name="force_rtl_layout_all_locales_summary">Force screen layout direction to RTL for all locales</string>
+
     <!-- UI debug setting: show how CPU is being used? [CHAR LIMIT=25] -->
     <string name="show_cpu_usage">Show CPU usage</string>
     <!-- UI debug setting: show cpu usage summary [CHAR LIMIT=50] -->
diff --git a/res/xml/development_prefs.xml b/res/xml/development_prefs.xml
index db003d3..3a473bb 100644
--- a/res/xml/development_prefs.xml
+++ b/res/xml/development_prefs.xml
@@ -130,6 +130,11 @@
             android:title="@string/debug_layout"
             android:summary="@string/debug_layout_summary"/>
 
+        <CheckBoxPreference
+            android:key="force_rtl_layout_all_locales"
+            android:title="@string/force_rtl_layout_all_locales"
+            android:summary="@string/force_rtl_layout_all_locales_summary"/>
+
         <ListPreference
             android:key="window_animation_scale"
             android:title="@string/window_animation_scale_title"
diff --git a/src/com/android/settings/DevelopmentSettings.java b/src/com/android/settings/DevelopmentSettings.java
index c727bcc..66f024e 100644
--- a/src/com/android/settings/DevelopmentSettings.java
+++ b/src/com/android/settings/DevelopmentSettings.java
@@ -74,6 +74,7 @@
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Locale;
 
 /*
  * Displays preferences for application developers.
@@ -128,6 +129,7 @@
     private static final String SHOW_HW_LAYERS_UPDATES_KEY = "show_hw_layers_udpates";
     private static final String DEBUG_HW_OVERDRAW_KEY = "debug_hw_overdraw";
     private static final String DEBUG_LAYOUT_KEY = "debug_layout";
+    private static final String FORCE_RTL_LAYOUT_KEY = "force_rtl_layout_all_locales";
     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
     private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale";
     private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale";
@@ -189,6 +191,7 @@
     private CheckBoxPreference mShowHwScreenUpdates;
     private CheckBoxPreference mShowHwLayersUpdates;
     private CheckBoxPreference mDebugLayout;
+    private CheckBoxPreference mForceRtlLayout;
     private ListPreference mDebugHwOverdraw;
     private ListPreference mTrackFrameTime;
     private ListPreference mShowNonRectClip;
@@ -302,6 +305,7 @@
         mShowHwScreenUpdates = findAndInitCheckboxPref(SHOW_HW_SCREEN_UPDATES_KEY);
         mShowHwLayersUpdates = findAndInitCheckboxPref(SHOW_HW_LAYERS_UPDATES_KEY);
         mDebugLayout = findAndInitCheckboxPref(DEBUG_LAYOUT_KEY);
+        mForceRtlLayout = findAndInitCheckboxPref(FORCE_RTL_LAYOUT_KEY);
         mDebugHwOverdraw = addListPreference(DEBUG_HW_OVERDRAW_KEY);
         mWindowAnimationScale = addListPreference(WINDOW_ANIMATION_SCALE_KEY);
         mTransitionAnimationScale = addListPreference(TRANSITION_ANIMATION_SCALE_KEY);
@@ -522,6 +526,7 @@
         updateExperimentalWebViewOptions();
         updateVerifyAppsOverUsbOptions();
         updateBugreportOptions();
+        updateForceRtlOptions();
     }
 
     private void resetDangerousOptions() {
@@ -958,6 +963,19 @@
         pokeSystemProperties();
     }
 
+    private void updateForceRtlOptions() {
+        updateCheckBox(mForceRtlLayout, Settings.Global.getInt(getActivity().getContentResolver(),
+                Settings.Global.DEVELOPMENT_FORCE_RTL, 0) != 0);
+    }
+
+    private void writeForceRtlOptions() {
+        boolean value = mForceRtlLayout.isChecked();
+        Settings.Global.putInt(getActivity().getContentResolver(),
+                Settings.Global.DEVELOPMENT_FORCE_RTL, value ? 1 : 0);
+        SystemProperties.set(Settings.Global.DEVELOPMENT_FORCE_RTL, value ? "1" : "0");
+        LocalePicker.updateLocale(getActivity().getResources().getConfiguration().locale);
+    }
+
     private void updateCpuUsageOptions() {
         updateCheckBox(mShowCpuUsage, Settings.Global.getInt(getActivity().getContentResolver(),
                 Settings.Global.SHOW_PROCESSES, 0) != 0);
@@ -1263,6 +1281,8 @@
             writeShowHwLayersUpdatesOptions();
         } else if (preference == mDebugLayout) {
             writeDebugLayoutOptions();
+        } else if (preference == mForceRtlLayout) {
+            writeForceRtlOptions();
         }
 
         return false;