digao-dalpiaz / DzHTMLText

Delphi and Lazarus HTML Label component
MIT License
194 stars 53 forks source link

Link - unsupported platform #36

Closed Chandlr closed 3 years ago

Chandlr commented 3 years ago

Minor mod to your code and this works for android with link clicking:

with Units: Androidapi.JNI.Net, Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers

procedure TDzHTMLText.Click;
var Handled: Boolean;
  aTarget: string;

  {$IFDEF ANDROID}
  Intent : JIntent;
  {$ENDIF}

begin
  if FIsLinkHover then
  begin
    Handled := False;
    if Assigned(FOnLinkClick) then
      FOnLinkClick(Self, FSelectedLink, Handled);

    if not Handled then
    begin
      if FSelectedLink is TDHLinkRef then
      begin
        if FAutoOpenLink then
        begin
          aTarget := TDHLinkRef(FSelectedLink).FTarget;
          if not aTarget.IsEmpty then
          begin
            {$IFDEF MSWINDOWS}
            ShellExecute(0, 'open', PChar(aTarget), '', '', SW_SHOWNORMAL);
            {$ELSE}
              {$IFDEF FPC}
              if aTarget.StartsWith('http://', True)
                or aTarget.StartsWith('https://', True)
                or aTarget.StartsWith('www.', True)
              then
                OpenURL(aTarget)
              else
                OpenDocument(aTarget);
              {$ELSE}
                try
                Intent := TJIntent.Create;
                Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
                Intent.setData(StrToJURI(aTarget));

                SharedActivity.startActivity(Intent);
                except
                raise Exception.Create('Unsupported platform');
                end;
              {$ENDIF}
            {$ENDIF}
          end;
        end;
      end else
      if FSelectedLink is TDHSpoiler then
      begin
        TDHSpoiler(FSelectedLink).FExpanded :=
          not TDHSpoiler(FSelectedLink).FExpanded;

        BuildAndPaint;
      end else
        raise EInternalExcept.Create('Invalid link object');
    end;
  end;

  inherited;
end;
digao-dalpiaz commented 3 years ago

Hello @Chandlr ,

Thanks for your time.

I reviewed your code and rewrote it without using the variable:

TAndroidHelper.Activity.startActivity(
              TJIntent.Create
                .setAction(TJIntent.JavaClass.ACTION_VIEW)
                .setData(StrToJURI(aTarget))
            );

Actually, the SharedActivity is marked as deprecated, so I changed to TAndroidHelper.Activity.

Thanks again.

digao-dalpiaz commented 3 years ago

Enhancement included and tested.