TextDrawable.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Tobias Kaminsiky
  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.UserAccountManager;
  32. import com.owncloud.android.utils.BitmapUtils;
  33. import com.owncloud.android.utils.NextcloudServer;
  34. import java.security.NoSuchAlgorithmException;
  35. import java.util.Locale;
  36. import androidx.annotation.NonNull;
  37. import androidx.annotation.VisibleForTesting;
  38. /**
  39. * A Drawable object that draws text (1 character) on top of a circular/filled background.
  40. */
  41. public class TextDrawable extends Drawable {
  42. /**
  43. * the text to be rendered.
  44. */
  45. private String mText;
  46. /**
  47. * the text paint to be rendered.
  48. */
  49. private Paint mTextPaint;
  50. /**
  51. * the background to be rendered.
  52. */
  53. private Paint mBackground;
  54. /**
  55. * the radius of the circular background to be rendered.
  56. */
  57. private float mRadius;
  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.rgb(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. }
  78. /**
  79. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
  80. * given radius.
  81. *
  82. * @param account user account
  83. * @param radiusInDp the circle's radius
  84. * @return the avatar as a TextDrawable
  85. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  86. */
  87. @NonNull
  88. @NextcloudServer(max = 12)
  89. public static TextDrawable createAvatar(Account account, float radiusInDp) throws
  90. NoSuchAlgorithmException {
  91. String username = UserAccountManager.getDisplayName(account);
  92. return createNamedAvatar(username, radiusInDp);
  93. }
  94. /**
  95. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
  96. * given radius.
  97. *
  98. * @param userId userId to use
  99. * @param radiusInDp the circle's radius
  100. * @return the avatar as a TextDrawable
  101. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  102. */
  103. @NonNull
  104. @NextcloudServer(max = 12)
  105. public static TextDrawable createAvatarByUserId(String userId, float radiusInDp) throws NoSuchAlgorithmException {
  106. return createNamedAvatar(userId, radiusInDp);
  107. }
  108. /**
  109. * creates an avatar in form of a TextDrawable with the first letter of a name in a circle with the
  110. * given radius.
  111. *
  112. * @param name the name
  113. * @param radiusInDp the circle's radius
  114. * @return the avatar as a TextDrawable
  115. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  116. */
  117. @NonNull
  118. public static TextDrawable createNamedAvatar(String name, float radiusInDp) throws NoSuchAlgorithmException {
  119. BitmapUtils.Color color = BitmapUtils.usernameToColor(name);
  120. return new TextDrawable(extractCharsFromDisplayName(name), color, radiusInDp);
  121. }
  122. @VisibleForTesting
  123. public static String extractCharsFromDisplayName(@NonNull String displayName) {
  124. if (displayName.isEmpty()) {
  125. return "";
  126. }
  127. String[] nameParts = displayName.split("\\s+");
  128. StringBuilder firstTwoLetters = new StringBuilder();
  129. for (int i = 0; i < Math.min(2, nameParts.length); i++) {
  130. firstTwoLetters.append(nameParts[i].substring(0, 1).toUpperCase(Locale.getDefault()));
  131. }
  132. return firstTwoLetters.toString();
  133. }
  134. /**
  135. * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
  136. * filter (set via setColorFilter) a circular background with a user's first character.
  137. *
  138. * @param canvas The canvas to draw into
  139. */
  140. @Override
  141. public void draw(@NonNull Canvas canvas) {
  142. canvas.drawCircle(mRadius, mRadius, mRadius, mBackground);
  143. canvas.drawText(mText, mRadius, mRadius - ((mTextPaint.descent() + mTextPaint.ascent()) / 2), mTextPaint);
  144. }
  145. @Override
  146. public void setAlpha(int alpha) {
  147. mTextPaint.setAlpha(alpha);
  148. }
  149. @Override
  150. public void setColorFilter(ColorFilter cf) {
  151. mTextPaint.setColorFilter(cf);
  152. }
  153. @Override
  154. public int getOpacity() {
  155. return PixelFormat.TRANSLUCENT;
  156. }
  157. }