CustomEditText.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.ui.components;
  21. import android.content.Context;
  22. import android.graphics.Canvas;
  23. import android.graphics.Rect;
  24. import android.text.TextUtils;
  25. import android.util.AttributeSet;
  26. import com.owncloud.android.R;
  27. import com.owncloud.android.authentication.AuthenticatorActivity;
  28. /**
  29. * Custom edit text to support fixed suffix or prefix
  30. */
  31. public class CustomEditText extends android.support.v7.widget.AppCompatEditText {
  32. private Rect fixedRect = new Rect();
  33. private String fixedText = "";
  34. private boolean isPrefixFixed;
  35. public CustomEditText(Context context, AttributeSet attrs) {
  36. super(context, attrs);
  37. String serverInputType = getResources().getString(R.string.server_input_type);
  38. if (AuthenticatorActivity.DIRECTORY_SERVER_INPUT_TYPE.equals(serverInputType)) {
  39. isPrefixFixed = true;
  40. fixedText = getResources().getString(R.string.server_url) + "/";
  41. } else if (AuthenticatorActivity.SUBDOMAIN_SERVER_INPUT_TYPE.equals(serverInputType)) {
  42. isPrefixFixed = false;
  43. fixedText = "." + getResources().getString(R.string.server_url);
  44. }
  45. if (TextUtils.isEmpty(fixedText)) {
  46. setHint(R.string.auth_host_url);
  47. }
  48. }
  49. public String getFullServerUrl() {
  50. if (TextUtils.isEmpty(fixedText)
  51. || getText().toString().startsWith(AuthenticatorActivity.HTTP_PROTOCOL)
  52. || getText().toString().startsWith(AuthenticatorActivity.HTTPS_PROTOCOL)) {
  53. return getText().toString();
  54. } else if (isPrefixFixed) {
  55. return (getResources().getString(R.string.server_url) + "/" + getText().toString());
  56. } else {
  57. return (getText().toString() + "." + getResources().getString(R.string.server_url));
  58. }
  59. }
  60. @Override
  61. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  62. if (!TextUtils.isEmpty(fixedText)) {
  63. getPaint().getTextBounds(fixedText, 0, fixedText.length(), fixedRect);
  64. }
  65. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  66. }
  67. @Override
  68. protected void onDraw(Canvas canvas) {
  69. super.onDraw(canvas);
  70. if (!getText().toString().startsWith(AuthenticatorActivity.HTTP_PROTOCOL)
  71. && !getText().toString().startsWith(AuthenticatorActivity.HTTPS_PROTOCOL)
  72. && !TextUtils.isEmpty(fixedText)) {
  73. if (isPrefixFixed) {
  74. canvas.drawText(fixedText,
  75. super.getCompoundPaddingLeft(),
  76. getBaseline(),
  77. getPaint());
  78. } else {
  79. canvas.drawText(fixedText, super.getCompoundPaddingLeft()
  80. + getPaint().measureText(getText().toString()), getBaseline(), getPaint());
  81. }
  82. }
  83. }
  84. @Override
  85. public int getCompoundPaddingLeft() {
  86. if (!TextUtils.isEmpty(fixedText) && isPrefixFixed) {
  87. return super.getCompoundPaddingLeft() + fixedRect.width();
  88. } else {
  89. return super.getCompoundPaddingLeft();
  90. }
  91. }
  92. }