/MATE-20_EMUI_11.0.0/src/main/java/huawei/com/android/internal/widget/HwNotificationActionListLayout.java

https://github.com/dstmath/HWFramework · Java · 134 lines · 124 code · 9 blank · 1 comment · 29 complexity · b0f0e4401a51eed29296c0689058fdee MD5 · raw file

  1. package huawei.com.android.internal.widget;
  2. import android.content.Context;
  3. import android.graphics.drawable.Drawable;
  4. import android.text.Layout;
  5. import android.text.TextUtils;
  6. import android.util.AttributeSet;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.RemoteViews;
  10. import android.widget.TextView;
  11. import com.android.internal.widget.NotificationActionListLayout;
  12. import com.huawei.android.os.storage.StorageManagerExt;
  13. import java.text.BreakIterator;
  14. import java.util.Locale;
  15. @RemoteViews.RemoteView
  16. public class HwNotificationActionListLayout extends NotificationActionListLayout {
  17. private static final int END_DRAWABLE = 2;
  18. private static final int MULTIPLE_LINE = 2;
  19. private static final int SINGLE_LINE = 1;
  20. private static final String TAG = "HwNotiActionListLayout";
  21. private boolean mHasCompoundDrawable = false;
  22. private int mLeftRightPadding = getResources().getDimensionPixelSize(34472196);
  23. public HwNotificationActionListLayout(Context context, AttributeSet attrs) {
  24. super(context, attrs);
  25. }
  26. /* access modifiers changed from: protected */
  27. public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  28. HwNotificationActionListLayout.super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  29. int childCount = getChildCount();
  30. boolean isMultipleLines = false;
  31. for (int i = 0; i < childCount; i++) {
  32. View child = getChildAt(i);
  33. if (child instanceof TextView) {
  34. TextView button = (TextView) child;
  35. if (button.getText().length() > 0) {
  36. boolean isMultipleLinesTemp = isMultipleLines(button);
  37. if (!isMultipleLines && isMultipleLinesTemp) {
  38. isMultipleLines = true;
  39. }
  40. this.mHasCompoundDrawable = hasCompoundDrawables(button);
  41. }
  42. }
  43. }
  44. if (isMultipleLines) {
  45. updateGravity(childCount);
  46. }
  47. }
  48. private void updateGravity(int childCount) {
  49. for (int i = 0; i < childCount; i++) {
  50. View child = getChildAt(i);
  51. if (child instanceof TextView) {
  52. TextView button = (TextView) child;
  53. if (button.getText().length() > 0) {
  54. int i2 = this.mLeftRightPadding;
  55. button.setPadding(i2, 0, i2, 0);
  56. if (!this.mHasCompoundDrawable) {
  57. button.setGravity(19);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. private boolean hasCompoundDrawables(TextView textView) {
  64. if (textView == null) {
  65. Log.w(TAG, "hasCompoundDrawables, textview is null");
  66. return false;
  67. } else if (this.mHasCompoundDrawable) {
  68. return true;
  69. } else {
  70. for (Drawable drawable : textView.getCompoundDrawablesRelative()) {
  71. if (drawable != null) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. }
  78. private boolean isMultipleLines(TextView textView) {
  79. if (textView == null) {
  80. Log.w(TAG, "isMultipleLines, textview is null");
  81. return false;
  82. }
  83. int availableWidth = ((textView.getMeasuredWidth() - textView.getPaddingLeft()) - textView.getPaddingRight()) - calculateCompoundDrawableWidth(textView);
  84. String textString = textView.getText().toString();
  85. float buttonTextWidth = Layout.getDesiredWidth(textString.toUpperCase(Locale.ROOT), 0, textString.length(), textView.getPaint());
  86. String firstWord = getFirstWord(textString);
  87. float firstWordWidth = Layout.getDesiredWidth(firstWord.toUpperCase(Locale.ROOT), 0, firstWord.length(), textView.getPaint());
  88. float multipleLineTextSize = (float) textView.getContext().getResources().getDimensionPixelSize(34472741);
  89. if (firstWordWidth > ((float) availableWidth) || ((float) availableWidth) >= buttonTextWidth) {
  90. textView.setSingleLine(true);
  91. textView.setAllCaps(true);
  92. return false;
  93. }
  94. textView.setSingleLine(false);
  95. textView.setAllCaps(true);
  96. textView.setMaxLines(2);
  97. textView.setTextSize(0, multipleLineTextSize);
  98. textView.setEllipsize(TextUtils.TruncateAt.END);
  99. return true;
  100. }
  101. private int calculateCompoundDrawableWidth(TextView textView) {
  102. if (textView == null) {
  103. Log.w(TAG, "calculateCompoundDrawableWidth, textview is null");
  104. return 0;
  105. }
  106. int compoundDrawableWidth = 0;
  107. Drawable[] drawables = textView.getCompoundDrawablesRelative();
  108. if (drawables[0] != null) {
  109. compoundDrawableWidth = 0 + drawables[0].getBounds().width() + textView.getCompoundDrawablePadding();
  110. }
  111. if (drawables[2] != null) {
  112. return compoundDrawableWidth + drawables[2].getBounds().width() + textView.getCompoundDrawablePadding();
  113. }
  114. return compoundDrawableWidth;
  115. }
  116. private String getFirstWord(String rawText) {
  117. if (TextUtils.isEmpty(rawText)) {
  118. Log.w(TAG, "getFirstWord, input text is empty");
  119. return StorageManagerExt.INVALID_KEY_DESC;
  120. }
  121. BreakIterator boundary = BreakIterator.getWordInstance();
  122. boundary.setText(rawText);
  123. return rawText.substring(boundary.first(), boundary.next());
  124. }
  125. }