TextDrawable.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.authentication.AccountUtils;
  33. import com.owncloud.android.utils.BitmapUtils;
  34. import com.owncloud.android.utils.NextcloudServer;
  35. import java.io.UnsupportedEncodingException;
  36. import java.security.NoSuchAlgorithmException;
  37. import java.util.Locale;
  38. import androidx.annotation.NonNull;
  39. /**
  40. * A Drawable object that draws text (1 character) on top of a circular/filled background.
  41. */
  42. public class TextDrawable extends Drawable {
  43. /**
  44. * the text to be rendered.
  45. */
  46. private String mText;
  47. /**
  48. * the text paint to be rendered.
  49. */
  50. private Paint mTextPaint;
  51. /**
  52. * the background to be rendered.
  53. */
  54. private Paint mBackground;
  55. /**
  56. * the radius of the circular background to be rendered.
  57. */
  58. private float mRadius;
  59. /**
  60. * Create a TextDrawable with the given radius.
  61. *
  62. * @param text the text to be rendered
  63. * @param r rgb red value
  64. * @param g rgb green value
  65. * @param b rgb blue value
  66. * @param radius circle radius
  67. */
  68. public TextDrawable(String text, int r, int g, int b, float radius) {
  69. mRadius = radius;
  70. mText = text;
  71. mBackground = new Paint();
  72. mBackground.setStyle(Paint.Style.FILL);
  73. mBackground.setAntiAlias(true);
  74. mBackground.setColor(Color.rgb(r, g, b));
  75. mTextPaint = new Paint();
  76. mTextPaint.setColor(Color.WHITE);
  77. mTextPaint.setTextSize(radius);
  78. mTextPaint.setAntiAlias(true);
  79. mTextPaint.setTextAlign(Paint.Align.CENTER);
  80. }
  81. /**
  82. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
  83. * given radius.
  84. *
  85. * @param account user account
  86. * @param radiusInDp the circle's radius
  87. * @return the avatar as a TextDrawable
  88. * @throws UnsupportedEncodingException if the charset is not supported when calculating the color values
  89. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  90. */
  91. @NonNull
  92. @NextcloudServer(max = 12)
  93. public static TextDrawable createAvatar(Account account, float radiusInDp) throws
  94. NoSuchAlgorithmException {
  95. String username = UserAccountManager.getUsername(account);
  96. return createNamedAvatar(username, radiusInDp);
  97. }
  98. /**
  99. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
  100. * given radius.
  101. *
  102. * @param userId userId to use
  103. * @param radiusInDp the circle's radius
  104. * @return the avatar as a TextDrawable
  105. * @throws UnsupportedEncodingException if the charset is not supported when calculating the color values
  106. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  107. */
  108. @NonNull
  109. @NextcloudServer(max = 12)
  110. public static TextDrawable createAvatarByUserId(String userId, float radiusInDp) throws
  111. NoSuchAlgorithmException {
  112. return createNamedAvatar(userId, radiusInDp);
  113. }
  114. /**
  115. * creates an avatar in form of a TextDrawable with the first letter of a name in a circle with the
  116. * given radius.
  117. *
  118. * @param name the name
  119. * @param radiusInDp the circle's radius
  120. * @return the avatar as a TextDrawable
  121. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  122. */
  123. @NonNull
  124. public static TextDrawable createNamedAvatar(String name, float radiusInDp) throws NoSuchAlgorithmException {
  125. int[] hsl = BitmapUtils.calculateHSL(name);
  126. int[] rgb = BitmapUtils.HSLtoRGB(hsl[0], hsl[1], hsl[2], 1);
  127. return new TextDrawable(name.substring(0, 1).toUpperCase(Locale.getDefault()), rgb[0], rgb[1], rgb[2],
  128. radiusInDp);
  129. }
  130. /**
  131. * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
  132. * filter (set via setColorFilter) a circular background with a user's first character.
  133. *
  134. * @param canvas The canvas to draw into
  135. */
  136. @Override
  137. public void draw(@NonNull Canvas canvas) {
  138. canvas.drawCircle(mRadius, mRadius, mRadius, mBackground);
  139. canvas.drawText(mText, mRadius, mRadius - ((mTextPaint.descent() + mTextPaint.ascent()) / 2), mTextPaint);
  140. }
  141. @Override
  142. public void setAlpha(int alpha) {
  143. mTextPaint.setAlpha(alpha);
  144. }
  145. @Override
  146. public void setColorFilter(ColorFilter cf) {
  147. mTextPaint.setColorFilter(cf);
  148. }
  149. @Override
  150. public int getOpacity() {
  151. return PixelFormat.TRANSLUCENT;
  152. }
  153. }