TextDrawable.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Tobias Kaminsky
  6. * @author Chris Narkiewicz
  7. * Copyright (C) 2016 ownCloud Inc.
  8. * Copyright (C) 2018 Andy Scherzinger
  9. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package com.owncloud.android.ui;
  24. import android.accounts.Account;
  25. import android.graphics.Canvas;
  26. import android.graphics.Color;
  27. import android.graphics.ColorFilter;
  28. import android.graphics.Paint;
  29. import android.graphics.PixelFormat;
  30. import android.graphics.drawable.Drawable;
  31. import com.nextcloud.client.account.User;
  32. import com.nextcloud.client.account.UserAccountManager;
  33. import com.owncloud.android.utils.BitmapUtils;
  34. import java.util.Locale;
  35. import androidx.annotation.NonNull;
  36. import androidx.annotation.VisibleForTesting;
  37. /**
  38. * A Drawable object that draws text (1 character) on top of a circular/filled background.
  39. */
  40. public class TextDrawable extends Drawable {
  41. /**
  42. * the text to be rendered.
  43. */
  44. private String mText;
  45. /**
  46. * the text paint to be rendered.
  47. */
  48. private Paint mTextPaint;
  49. /**
  50. * the background to be rendered.
  51. */
  52. private Paint mBackground;
  53. /**
  54. * the radius of the circular background to be rendered.
  55. */
  56. private float mRadius;
  57. private boolean bigText = false;
  58. /**
  59. * Create a TextDrawable with the given radius.
  60. *
  61. * @param text the text to be rendered
  62. * @param color color
  63. * @param radius circle radius
  64. */
  65. public TextDrawable(String text, BitmapUtils.Color color, float radius) {
  66. mRadius = radius;
  67. mText = text;
  68. mBackground = new Paint();
  69. mBackground.setStyle(Paint.Style.FILL);
  70. mBackground.setAntiAlias(true);
  71. mBackground.setColor(Color.argb(color.a, color.r, color.g, color.b));
  72. mTextPaint = new Paint();
  73. mTextPaint.setColor(Color.WHITE);
  74. mTextPaint.setTextSize(radius);
  75. mTextPaint.setAntiAlias(true);
  76. mTextPaint.setTextAlign(Paint.Align.CENTER);
  77. setBounds(0, 0, (int) radius * 2, (int) radius * 2);
  78. }
  79. /**
  80. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the given
  81. * radius.
  82. *
  83. * @param user user account
  84. * @param radiusInDp the circle's radius
  85. * @return the avatar as a TextDrawable
  86. */
  87. @NonNull
  88. public static TextDrawable createAvatar(User user, float radiusInDp) {
  89. String username = UserAccountManager.getDisplayName(user.toPlatformAccount());
  90. return createNamedAvatar(username, radiusInDp);
  91. }
  92. /**
  93. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the given
  94. * radius.
  95. *
  96. * @param userId userId to use
  97. * @param radiusInDp the circle's radius
  98. * @return the avatar as a TextDrawable
  99. */
  100. @NonNull
  101. public static TextDrawable createAvatarByUserId(String userId, float radiusInDp) {
  102. return createNamedAvatar(userId, radiusInDp);
  103. }
  104. /**
  105. * creates an avatar in form of a TextDrawable with the first letter of a name in a circle with the
  106. * given radius.
  107. *
  108. * @param name the name
  109. * @param radiusInDp the circle's radius
  110. * @return the avatar as a TextDrawable
  111. */
  112. @NonNull
  113. public static TextDrawable createNamedAvatar(String name, float radiusInDp) {
  114. BitmapUtils.Color color = BitmapUtils.usernameToColor(name);
  115. return new TextDrawable(extractCharsFromDisplayName(name), color, radiusInDp);
  116. }
  117. @VisibleForTesting
  118. public static String extractCharsFromDisplayName(@NonNull String displayName) {
  119. if (displayName.isEmpty()) {
  120. return "";
  121. }
  122. String[] nameParts = displayName.split("\\s+");
  123. StringBuilder firstTwoLetters = new StringBuilder();
  124. for (int i = 0; i < Math.min(2, nameParts.length); i++) {
  125. firstTwoLetters.append(nameParts[i].substring(0, 1).toUpperCase(Locale.getDefault()));
  126. }
  127. return firstTwoLetters.toString();
  128. }
  129. /**
  130. * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
  131. * filter (set via setColorFilter) a circular background with a user's first character.
  132. *
  133. * @param canvas The canvas to draw into
  134. */
  135. @Override
  136. public void draw(@NonNull Canvas canvas) {
  137. canvas.drawCircle(mRadius, mRadius, mRadius, mBackground);
  138. if (bigText) {
  139. mTextPaint.setTextSize(1.8f * mRadius);
  140. }
  141. canvas.drawText(mText, mRadius, mRadius - ((mTextPaint.descent() + mTextPaint.ascent()) / 2), mTextPaint);
  142. }
  143. @Override
  144. public void setAlpha(int alpha) {
  145. mTextPaint.setAlpha(alpha);
  146. }
  147. @Override
  148. public void setColorFilter(ColorFilter cf) {
  149. mTextPaint.setColorFilter(cf);
  150. }
  151. @Override
  152. public int getOpacity() {
  153. return PixelFormat.TRANSLUCENT;
  154. }
  155. }