Esempio n. 1
0
    public void build(MessageWireframe wireframe, int desiredWidth, StelsApplication application) {

      Logger.d(TAG, "Build layout start");

      checkResources(application);

      senderPaint = initTextPaint();
      senderPaint.setTypeface(FontController.loadTypeface(application, "regular"));
      senderPaint.setTextSize(bodyPaint.getTextSize());
      senderPaint.setColor(0xff000000);

      forwardingPaint = initTextPaint();
      forwardingPaint.setTypeface(FontController.loadTypeface(application, "light"));
      forwardingPaint.setTextSize(bodyPaint.getTextSize());
      forwardingPaint.setColor(0xff000000);

      this.layoutDesiredWidth = desiredWidth;
      this.isOut = wireframe.message.isOut();
      this.showState = isOut;
      this.isGroup = wireframe.message.getPeerType() == PeerType.PEER_CHAT && !isOut;
      if (isGroup) {
        User user = wireframe.senderUser;
        this.senderName = user.getDisplayName();
        if (!wireframe.message.isForwarded()) {
          senderPaint.setColor(
              Placeholders.USER_PLACEHOLDERS_COLOR[
                  wireframe.message.getSenderId() % Placeholders.USER_PLACEHOLDERS_COLOR.length]);
          forwardingPaint.setColor(
              Placeholders.USER_PLACEHOLDERS_COLOR[
                  wireframe.message.getSenderId() % Placeholders.USER_PLACEHOLDERS_COLOR.length]);
        }
      }

      if (wireframe.message.isForwarded()) {
        isForwarded = true;
        this.forwarderName = wireframe.forwardUser.getDisplayName();
        if (isOut) {
          forwardingPaint.setColor(0xff739f53);
          senderPaint.setColor(0xff739f53);
        } else {
          forwardingPaint.setColor(0xff4884cf);
          senderPaint.setColor(0xff4884cf);
        }
      } else {
        isForwarded = false;
      }

      if (isGroup) {
        User user = application.getEngine().getUser(wireframe.message.getSenderId());
        this.senderName = user.getDisplayName();
      }
      if (wireframe.message.isForwarded()) {
        isForwarded = true;
        this.forwarderName = wireframe.forwardUser.getDisplayName();
      } else {
        isForwarded = false;
      }

      layoutDesiredWidth = desiredWidth;
      long start = SystemClock.uptimeMillis();

      this.spannable =
          application
              .getEmojiProcessor()
              .processEmojiCompatMutable(
                  wireframe.message.getMessage(), EmojiProcessor.CONFIGURATION_BUBBLES);

      // spannable = new SpannableString(wireframe.message.getMessage());
      Logger.d(TAG, "Emoji processed in " + (SystemClock.uptimeMillis() - start) + " ms");
      start = SystemClock.uptimeMillis();
      Linkify.addLinks(
          this.spannable, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES);
      fixLinks(spannable);
      Logger.d(TAG, "Added links in " + (SystemClock.uptimeMillis() - start) + " ms");
      start = SystemClock.uptimeMillis();
      layout =
          new StaticLayout(
              spannable, bodyPaint, desiredWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
      Logger.d(TAG, "Built base layout in " + (SystemClock.uptimeMillis() - start) + " ms");

      if (layout.getLineCount() < 20) {
        int layoutTextWidth = 0;

        for (int i = 0; i < layout.getLineCount(); i++) {
          layoutTextWidth = (int) Math.max(layout.getLineWidth(i), layoutTextWidth);
        }

        if (layoutTextWidth < layout.getWidth() - px(10)) {
          layout =
              new StaticLayout(
                  spannable,
                  bodyPaint,
                  layoutTextWidth + px(2),
                  Layout.Alignment.ALIGN_NORMAL,
                  1.0f,
                  0.0f,
                  true);
        }
      }

      layoutRealWidth = layout.getWidth();

      timeWidth = (int) clockOutPaint.measureText(wireframe.date) + px((showState ? 23 : 0) + 6);

      if (layout.getLineCount() == 1) {
        boolean isLastRtl = layout.getParagraphDirection(0) == Layout.DIR_RIGHT_TO_LEFT;
        if (!isLastRtl && desiredWidth - layoutRealWidth > timeWidth) {
          layoutRealWidth += timeWidth;
          layoutHeight = layout.getHeight() + px(3);
        } else if (isLastRtl && desiredWidth - layout.getWidth() > timeWidth) {
          layoutRealWidth = layout.getWidth() + timeWidth;
          layoutHeight = layout.getHeight() + px(3);
        } else {
          if (isLastRtl) {
            layoutRealWidth = layout.getWidth();
          }

          layoutHeight = layout.getHeight() + px(17);
        }
      } else {
        boolean isLastRtl =
            layout.getParagraphDirection(layout.getLineCount() - 1) == Layout.DIR_RIGHT_TO_LEFT;
        if (!isLastRtl
            && (desiredWidth - layout.getLineWidth(layout.getLineCount() - 1) > timeWidth)) {
          layoutRealWidth =
              (int)
                  Math.max(
                      layoutRealWidth, layout.getLineWidth(layout.getLineCount() - 1) + timeWidth);
          layoutHeight = layout.getHeight() + px(3);
        } else if (isLastRtl && (desiredWidth - layout.getWidth() > timeWidth)) {
          layoutRealWidth = (int) Math.max(layoutRealWidth, layout.getWidth() + timeWidth);
          layoutHeight = layout.getHeight() + px(3);
        } else {
          layoutHeight = layout.getHeight() + px(17);
        }
      }

      if (layoutRealWidth < timeWidth) {
        layoutRealWidth = timeWidth;
      }

      if (isForwarded) {
        layoutHeight += px(19) * 2;
        forwardOffset = (int) forwardingPaintBase.measureText("From ");
        layoutRealWidth =
            (int) Math.max(layoutRealWidth, forwardingPaintBase.measureText("Forwarded message"));
        forwarderNameMeasured =
            TextUtils.ellipsize(
                    forwarderName,
                    senderPaintBase,
                    desiredWidth - forwardOffset,
                    TextUtils.TruncateAt.END)
                .toString();
        layoutRealWidth =
            (int)
                Math.max(
                    layoutRealWidth,
                    forwardOffset + senderPaintBase.measureText(forwarderNameMeasured));
      }

      if (isGroup && !isOut && !isForwarded) {
        layoutHeight += px(19);
        senderNameMeasured =
            TextUtils.ellipsize(senderName, senderPaintBase, desiredWidth, TextUtils.TruncateAt.END)
                .toString();
        int width = (int) senderPaintBase.measureText(senderNameMeasured);
        layoutRealWidth = Math.max(layoutRealWidth, width);
      }

      Logger.d(TAG, "Build layout end");
    }
Esempio n. 2
0
  private static void checkResources(Context context) {
    if (isLoaded) {
      return;
    }

    StelsApplication application = (StelsApplication) context.getApplicationContext();

    int fontSize;
    int fontSizeClock;
    switch (application.getUserSettings().getBubbleFontSizeId()) {
      default:
      case UserSettings.BUBBLE_FONT_NORMAL:
        fontSize = UserSettings.BUBBLE_FONT_NORMAL_VALUE;
        fontSizeClock = UserSettings.BUBBLE_FONT_NORMAL_VALUE_CLOCK;
        break;
      case UserSettings.BUBBLE_FONT_HUGE:
        fontSize = UserSettings.BUBBLE_FONT_HUGE_VALUE;
        fontSizeClock = UserSettings.BUBBLE_FONT_HUGE_VALUE_CLOCK;
        break;
      case UserSettings.BUBBLE_FONT_SMALL:
        fontSize = UserSettings.BUBBLE_FONT_SMALL_VALUE;
        fontSizeClock = UserSettings.BUBBLE_FONT_SMALL_VALUE_CLOCK;
        break;
      case UserSettings.BUBBLE_FONT_TINY:
        fontSize = UserSettings.BUBBLE_FONT_TINY_VALUE;
        fontSizeClock = UserSettings.BUBBLE_FONT_TINY_VALUE_CLOCK;
        break;
      case UserSettings.BUBBLE_FONT_LARGE:
        fontSize = UserSettings.BUBBLE_FONT_LARGE_VALUE;
        fontSizeClock = UserSettings.BUBBLE_FONT_LARGE_VALUE_CLOCK;
        break;
    }

    bodyPaint = initTextPaint();
    bodyPaint.setTypeface(FontController.loadTypeface(context, "regular"));
    bodyPaint.setTextSize(sp(fontSize));
    bodyPaint.setColor(0xff000000);

    clockOutPaint = initTextPaint();
    clockOutPaint.setTypeface(FontController.loadTypeface(context, "regular"));
    clockOutPaint.setTextSize(sp(fontSizeClock));
    clockOutPaint.setColor(0xff70B15C);

    senderPaintBase = initTextPaint();
    senderPaintBase.setTypeface(FontController.loadTypeface(context, "regular"));
    senderPaintBase.setTextSize(sp(fontSize));
    senderPaintBase.setColor(0xff000000);

    forwardingPaintBase = initTextPaint();
    forwardingPaintBase.setTypeface(FontController.loadTypeface(context, "regular"));
    forwardingPaintBase.setTextSize(sp(fontSize));
    forwardingPaintBase.setColor(0xff000000);

    Drawable inBubble = context.getResources().getDrawable(R.drawable.st_bubble_in);
    inBubblePadding = new Rect();
    inBubble.getPadding(inBubblePadding);

    Drawable outBubble = context.getResources().getDrawable(R.drawable.st_bubble_out);
    outBubblePadding = new Rect();
    outBubble.getPadding(outBubblePadding);

    isLoaded = true;
  }
Esempio n. 3
0
  private static void checkResources(Context context) {
    if (!isLoaded) {
      TelegramApplication application = (TelegramApplication) context.getApplicationContext();
      IS_LARGE =
          application.getUserSettings().getDialogItemSize() == UserSettings.DIALOG_SIZE_LARGE;

      avatarPaint = new Paint();

      if (FontController.USE_SUBPIXEL) {
        titlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        titlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }

      titlePaint.setColor(0xff222222);
      if (IS_LARGE) {
        titlePaint.setTextSize(sp(20f));
        titlePaint.setTypeface(FontController.loadTypeface(context, "medium"));
      } else {
        titlePaint.setTextSize(sp(18f));
        titlePaint.setTypeface(FontController.loadTypeface(context, "medium"));
      }

      if (FontController.USE_SUBPIXEL) {
        titleHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        titleHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      titleHighlightPaint.setColor(0xff006FC8);
      if (IS_LARGE) {
        titleHighlightPaint.setTextSize(sp(20f));
        titleHighlightPaint.setTypeface(FontController.loadTypeface(context, "medium"));
      } else {
        titleHighlightPaint.setTextSize(sp(18f));
        titleHighlightPaint.setTypeface(FontController.loadTypeface(context, "medium"));
      }

      if (FontController.USE_SUBPIXEL) {
        titleEncryptedPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        titleEncryptedPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      titleEncryptedPaint.setColor(0xff368c3e);
      if (IS_LARGE) {
        titleEncryptedPaint.setTextSize(sp(20f));
        titleEncryptedPaint.setTypeface(FontController.loadTypeface(context, "medium"));
      } else {
        titleEncryptedPaint.setTextSize(sp(18f));
        titleEncryptedPaint.setTypeface(FontController.loadTypeface(context, "medium"));
      }

      if (FontController.USE_SUBPIXEL) {
        unreadClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        unreadClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      unreadClockPaint.setColor(UNREAD_TIME_COLOR);
      unreadClockPaint.setTextSize(sp(14));
      unreadClockPaint.setTypeface(FontController.loadTypeface(context, "regular"));

      if (FontController.USE_SUBPIXEL) {
        readClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        readClockPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      readClockPaint.setColor(READ_TIME_COLOR);
      readClockPaint.setTextSize(sp(14));
      readClockPaint.setTypeface(FontController.loadTypeface(context, "regular"));

      if (FontController.USE_SUBPIXEL) {
        bodyPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        bodyPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      bodyPaint.setColor(READ_COLOR);
      bodyPaint.setTextSize(sp(17f));
      bodyPaint.setTypeface(FontController.loadTypeface(context, "regular"));

      if (FontController.USE_SUBPIXEL) {
        bodyUnreadPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        bodyUnreadPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      bodyUnreadPaint.setColor(UNREAD_COLOR);
      bodyUnreadPaint.setTextSize(sp(17f));
      bodyUnreadPaint.setTypeface(FontController.loadTypeface(context, "regular"));

      if (FontController.USE_SUBPIXEL) {
        bodyHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        bodyHighlightPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      bodyHighlightPaint.setColor(HIGHLIGHT_COLOR);
      bodyHighlightPaint.setTextSize(sp(17f));
      bodyHighlightPaint.setTypeface(FontController.loadTypeface(context, "regular"));

      if (FontController.USE_SUBPIXEL) {
        typingPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        typingPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      typingPaint.setColor(0xff006FC8);
      typingPaint.setTextSize(sp(16f));
      typingPaint.setTypeface(FontController.loadTypeface(context, "regular"));

      if (FontController.USE_SUBPIXEL) {
        counterTitlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        counterTitlePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      counterTitlePaint.setColor(0xffffffff);
      if (IS_LARGE) {
        counterTitlePaint.setTextSize(sp(15.5f));
      } else {
        counterTitlePaint.setTextSize(sp(14f));
      }
      counterTitlePaint.setTypeface(FontController.loadTypeface(context, "regular"));

      counterPaint = new Paint();
      counterPaint.setColor(0xff8fc478);
      counterPaint.setAntiAlias(true);

      placeholderPaint = new Paint();
      placeholderPaint.setAntiAlias(true);

      if (FontController.USE_SUBPIXEL) {
        placeholderTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
      } else {
        placeholderTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      }
      placeholderTextPaint.setColor(0xffffffff);
      placeholderTextPaint.setTextSize(px(28f));
      placeholderTextPaint.setTypeface(FontController.loadTypeface(context, "regular"));
      placeholderTextPaint.setTextAlign(Paint.Align.CENTER);

      isLoaded = true;
    }
  }