Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f2ce877c authored by Fabrice Di Meglio's avatar Fabrice Di Meglio
Browse files

Fix bug #4584320 Single Line EditText not drawing correctly with spans applied (ICS)

- use correct 0 index for computing advance thru the char buffer (the buffer is created from TextUtils.getChars())
- udpate unit tests

Change-Id: Iaeb07658b79ecdf5e17395d55afb7c84965bb0fc
parent fd0b623c
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -1602,7 +1602,7 @@ public class Paint {
        int len = end - start;
        int len = end - start;
        char[] buf = TemporaryBuffer.obtain(contextLen);
        char[] buf = TemporaryBuffer.obtain(contextLen);
        TextUtils.getChars(text, start, end, buf, 0);
        TextUtils.getChars(text, start, end, buf, 0);
        float result = getTextRunAdvances(buf, start - contextStart, len,
        float result = getTextRunAdvances(buf, 0, len,
                0, contextLen, flags, advances, advancesIndex, reserved);
                0, contextLen, flags, advances, advancesIndex, reserved);
        TemporaryBuffer.recycle(buf);
        TemporaryBuffer.recycle(buf);
        return result;
        return result;
+34 −22
Original line number Original line Diff line number Diff line
@@ -46,6 +46,7 @@
                      android:layout_width="match_parent"
                      android:layout_width="match_parent"
                      android:textSize="32dip"
                      android:textSize="32dip"
                      android:textDirection="ltr"
                      android:textDirection="ltr"
                      android:text="@string/url"
                    />
                    />


            <TextView android:id="@+id/textview_rtl"
            <TextView android:id="@+id/textview_rtl"
@@ -70,7 +71,16 @@
                android:text="@string/button_alert_dialog_text"
                android:text="@string/button_alert_dialog_text"
                android:textSize="32dip"
                android:textSize="32dip"
                />
                />
    </LinearLayout>

        <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/edittext_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:textSize="32dip"
            android:scrollHorizontally="true"
            android:ellipsize="end"
            android:text="@string/url"
            />


        <LinearLayout
        <LinearLayout
            android:layout_width="600dip"
            android:layout_width="600dip"
@@ -100,4 +110,6 @@
            </LinearLayout>
            </LinearLayout>
        </LinearLayout>
        </LinearLayout>


    </LinearLayout>

</FrameLayout>
</FrameLayout>
+1 −0
Original line number Original line Diff line number Diff line
@@ -47,5 +47,6 @@
    <string name="ltr">Left to right text"</string>
    <string name="ltr">Left to right text"</string>
    <string name="rtl">"والحق أن تترك ونص"</string>
    <string name="rtl">"والحق أن تترك ونص"</string>
    <string name="composing">"\u0644\u0627"</string>
    <string name="composing">"\u0644\u0627"</string>
    <string name="url">www.amazon.co.uk/gp/aw/h.html/275-8912818-8203452</string>
</resources>
</resources>
+45 −0
Original line number Original line Diff line number Diff line
@@ -19,10 +19,14 @@ package com.android.bidi;
import android.app.AlertDialog;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Button;
import android.widget.EditText;


public class BiDiTestBasic extends Fragment {
public class BiDiTestBasic extends Fragment {


@@ -47,6 +51,8 @@ public class BiDiTestBasic extends Fragment {
                showDialog();
                showDialog();
            }
            }
        });
        });

        useSpans();
    }
    }


    private void showDialog() {
    private void showDialog() {
@@ -54,4 +60,43 @@ public class BiDiTestBasic extends Fragment {
        builder.setSingleChoiceItems(items, 0, null);
        builder.setSingleChoiceItems(items, 0, null);
        builder.show();
        builder.show();
    }
    }

    private void useSpans() {
        EditText urlEdit = (EditText) currentView.findViewById(R.id.edittext_url);
        Editable url = urlEdit.getText();
        if (url.length() < 1) {
          return;
        }

        String urlString = url.toString();
        int urlLength = urlString.length();
        String domainAndRegistry = "amazon.co.uk";

        int startSchemeIndex = urlString.startsWith("https") ? 5 : 0;
        int startDomainIndex = urlString.indexOf(domainAndRegistry);
        if (startDomainIndex == -1) {
          assert false;
          return;
        }
        int stopIndex = startDomainIndex + domainAndRegistry.length();

        if (startDomainIndex != 0) {
          url.setSpan(new ForegroundColorSpan(0xfff00fff),
                  startSchemeIndex,
                  startDomainIndex,
                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        url.setSpan(new ForegroundColorSpan(0xff548aff),
                startDomainIndex,
                stopIndex,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (stopIndex < urlString.length()) {
          url.setSpan(new ForegroundColorSpan(0xfff00fff),
                  stopIndex,
                  urlLength,
                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
}