Fix bidi direction of dialog and bidi-wrap service name

Previously, the name of the service being in a different direction
from the locale could cause bidi artifacts.

Now, we explicitly set the direction of the dialog to the locale, and
bidi-wrap the service name.

Test: Manual
Change-Id: I563299866b24a4ce90fa2d09781aa269d8de2edd
Fixes: 37761152
diff --git a/res/layout/enable_accessibility_service_dialog_content.xml b/res/layout/enable_accessibility_service_dialog_content.xml
index 3ca40ac..f212eb1 100644
--- a/res/layout/enable_accessibility_service_dialog_content.xml
+++ b/res/layout/enable_accessibility_service_dialog_content.xml
@@ -18,6 +18,7 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
+    android:textDirection="locale"
     android:scrollbarStyle="outsideOverlay"
     android:gravity="top">
 
diff --git a/src/com/android/settings/accessibility/AccessibilityServiceWarning.java b/src/com/android/settings/accessibility/AccessibilityServiceWarning.java
index 9f89bc1..e0c58db 100644
--- a/src/com/android/settings/accessibility/AccessibilityServiceWarning.java
+++ b/src/com/android/settings/accessibility/AccessibilityServiceWarning.java
@@ -23,6 +23,7 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.os.storage.StorageManager;
+import android.text.BidiFormatter;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
@@ -34,6 +35,7 @@
 import com.android.settings.R;
 
 import java.util.List;
+import java.util.Locale;
 
 /**
  * Utility class for creating the dialog that asks users for explicit permission to grant
@@ -44,7 +46,7 @@
             AccessibilityServiceInfo info, DialogInterface.OnClickListener listener) {
         final AlertDialog ad = new AlertDialog.Builder(parentActivity)
                 .setTitle(parentActivity.getString(R.string.enable_service_title,
-                        info.getResolveInfo().loadLabel(parentActivity.getPackageManager())))
+                        getServiceName(parentActivity, info)))
                 .setView(createEnableDialogContentView(parentActivity, info))
                 .setCancelable(true)
                 .setPositiveButton(android.R.string.ok, listener)
@@ -97,7 +99,7 @@
                 R.id.encryption_warning);
         if (isFullDiskEncrypted()) {
             String text = context.getString(R.string.enable_service_encryption_warning,
-                    info.getResolveInfo().loadLabel(context.getPackageManager()));
+                    getServiceName(context, info));
             encryptionWarningView.setText(text);
             encryptionWarningView.setVisibility(View.VISIBLE);
         } else {
@@ -107,7 +109,7 @@
         TextView capabilitiesHeaderView = (TextView) content.findViewById(
                 R.id.capabilities_header);
         capabilitiesHeaderView.setText(context.getString(R.string.capabilities_list_title,
-                info.getResolveInfo().loadLabel(context.getPackageManager())));
+                getServiceName(context, info)));
 
         LinearLayout capabilitiesView = (LinearLayout) content.findViewById(R.id.capabilities);
 
@@ -161,4 +163,12 @@
 
         return content;
     }
+
+    // Get the service name and bidi wrap it to protect from bidi side effects.
+    private static CharSequence getServiceName(Context context, AccessibilityServiceInfo info) {
+        final Locale locale = context.getResources().getConfiguration().getLocales().get(0);
+        final CharSequence label =
+                info.getResolveInfo().loadLabel(context.getPackageManager());
+        return BidiFormatter.getInstance(locale).unicodeWrap(label);
+    }
 }