Fix issue where line breaks would be wrong

It's not possible to getLayout() after calling setText since the new
layout will only be calculated on the next onMeasure.

Test: visual
Bug: 78891765
Change-Id: I69ba425407e277156b702050a97dbbd29c8d66a9
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardSliceView.java b/packages/SystemUI/src/com/android/keyguard/KeyguardSliceView.java
index ffe92fd..33cac3b 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardSliceView.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardSliceView.java
@@ -166,18 +166,6 @@
             SliceItem mainTitle = header.getTitleItem();
             CharSequence title = mainTitle != null ? mainTitle.getText() : null;
             mTitle.setText(title);
-
-            // Check if we're already ellipsizing the text.
-            // We're going to figure out the best possible line break if not.
-            Layout layout = mTitle.getLayout();
-            if (layout != null){
-                final int lineCount = layout.getLineCount();
-                if (lineCount > 0) {
-                    if (layout.getEllipsisCount(lineCount - 1) == 0) {
-                        mTitle.setText(findBestLineBreak(title));
-                    }
-                }
-            }
         }
 
         mClickActions.clear();
@@ -381,6 +369,27 @@
         mIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.widget_icon_size);
     }
 
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+
+        // Find best ellipsis strategy for the title.
+        // Done on onMeasure since TextView#getLayout needs a measure pass to calculate its bounds.
+        Layout layout = mTitle.getLayout();
+        if (layout != null) {
+            final int lineCount = layout.getLineCount();
+            if (lineCount > 0) {
+                if (layout.getEllipsisCount(lineCount - 1) == 0) {
+                    CharSequence title = mTitle.getText();
+                    CharSequence bestLineBreak = findBestLineBreak(title);
+                    if (!TextUtils.equals(title, bestLineBreak)) {
+                        mTitle.setText(bestLineBreak);
+                    }
+                }
+            }
+        }
+    }
+
     public static class Row extends LinearLayout {
 
         /**