| 1 |
{ |
| 2 |
Copyright 2012-2024 Michalis Kamburelis and Lazarus developers. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
Parts of this file are based on Lazarus LCL code, which has |
| 9 |
exactly the same license as our "Castle Game Engine": |
| 10 |
LGPL with static linking exception, see COPYING.txt for details. |
| 11 |
|
| 12 |
"Castle Game Engine" 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. |
| 15 |
|
| 16 |
---------------------------------------------------------------------------- |
| 17 |
} |
| 18 |
|
| 19 |
{ Opening files and URLs. } |
| 20 |
unit CastleOpenDocument; |
| 21 |
|
| 22 |
{$include castleconf.inc} |
| 23 |
|
| 24 |
interface |
| 25 |
|
| 26 |
resourcestring |
| 27 |
SCannotOpenUrl = 'Browser not found on your system.'; |
| 28 |
|
| 29 |
{ Open URL with the suitable application. |
| 30 |
|
| 31 |
This detects and handles also local files (as filenames, or URLs with "file:" |
| 32 |
protocol). |
| 33 |
|
| 34 |
On Android and iOS, it uses the OS functions to open the URL, |
| 35 |
supporting all URL types that are handled by the installed applications. |
| 36 |
For example, it will support the market:// URLs on Android. |
| 37 |
|
| 38 |
To use this on Android, declare your Android project type as "integrated", |
| 39 |
see https://castle-engine.io/android-Project-Services-Integrated-with-Castle-Game-Engine . } |
| 40 |
function OpenUrl(AUrl: String): Boolean; |
| 41 |
|
| 42 |
{ Open a local file or directory. |
| 43 |
@deprecated You should instead use OpenUrl, |
| 44 |
that automatically detects local filenames and URLs leading to local filenames. } |
| 45 |
function OpenDocument(APath: String): Boolean; |
| 46 |
|
| 47 |
{ Share a text/link through user-choosen application. |
| 48 |
|
| 49 |
This works only on Android and iOS right now. |
| 50 |
For Android, you need to declare the project type as "integrated": |
| 51 |
See https://castle-engine.io/android-Project-Services-Integrated-with-Castle-Game-Engine . |
| 52 |
|
| 53 |
@param(Title The short title of the share.) |
| 54 |
@param(Subject Used as an email subject, and any other app on Android |
| 55 |
that interprets EXTRA_SUBJECT parameter.) |
| 56 |
@param(Content Multi-line share text content, possibly with URL inside.) |
| 57 |
} |
| 58 |
procedure ShareText(const Title, Subject, Content: string); |
| 59 |
|
| 60 |
{ Show the application in the application store (Google Play on Android, |
| 61 |
AppStore on iOS). Ignored on other platforms now. |
| 62 |
|
| 63 |
@unorderedList( |
| 64 |
@itemSpacing Compact |
| 65 |
@item(On Android, ApplicationId should be the qualitied name of the application |
| 66 |
(same thing you use as qualified_name in CastleEngineManifest.xml). |
| 67 |
|
| 68 |
To include the necessary integration code in your Android project, |
| 69 |
you must declare your Android project type as "integrated". |
| 70 |
See https://castle-engine.io/android-Project-Services-Integrated-with-Castle-Game-Engine . |
| 71 |
) |
| 72 |
@item(On iOS, ApplicationId has to be the "Apple ID" number of your application |
| 73 |
(you can see it e.g. in https://itunesconnect.apple.com/ page of your application). |
| 74 |
) |
| 75 |
) } |
| 76 |
procedure OpenApplicationStore(const ApplicationId: string); |
| 77 |
|
| 78 |
{ Vibrate the device. |
| 79 |
|
| 80 |
Available on Android, iOS and Nintendo Switch now. Ignored on other platforms. |
| 81 |
|
| 82 |
To include the necessary integration code in your Android project, |
| 83 |
declare your Android project type as "integrated" with |
| 84 |
the "vibrate" service inside CastleEngineManifest.xml. |
| 85 |
See https://castle-engine.io/android-Project-Services-Integrated-with-Castle-Game-Engine . } |
| 86 |
procedure Vibrate(const Miliseconds: Cardinal); |
| 87 |
|
| 88 |
{ Simple on-screen notification using Android "toast" call. |
| 89 |
|
| 90 |
This is available only on Android right now, ignored elsewhere. |
| 91 |
To include the necessary integration code in your Android project, |
| 92 |
you must declare your Android project type as "integrated". |
| 93 |
See https://castle-engine.io/android-Project-Services-Integrated-with-Castle-Game-Engine . } |
| 94 |
procedure OnScreenNotification(const Message: string); |
| 95 |
deprecated 'This is Android-specific and probably will not be ever supported on other platforms. Better use CGE UI to make cros-platform UI notifications, like TCastleNotifications or just TCastleLabel with animated color/background.'; |
| 96 |
|
| 97 |
implementation |
| 98 |
|
| 99 |
uses |
| 100 |
SysUtils, Classes, |
| 101 |
{$define read_uses} |
| 102 |
{$I castleopendocument_open.inc} |
| 103 |
{$undef read_uses} |
| 104 |
CastleUriUtils, CastleUtils, CastleFilesUtils, CastleLog, CastleMessaging; |
| 105 |
|
| 106 |
{ Has URL any anchor at the end, like "index.html#chapter1". |
| 107 |
For such URLs, converting them to local filename may be possible |
| 108 |
but is lossy: anchor would be lost. |
| 109 |
|
| 110 |
Testcase: API docs to |
| 111 |
file:///..../doc/reference/CastleSoundEngine.TCastleSound.html#DefaultReferenceDistance |
| 112 |
would effectively open |
| 113 |
/..../doc/reference/CastleSoundEngine.TCastleSound.html |
| 114 |
(anchor lost). } |
| 115 |
function UrlHasAnchor(const Url: String): Boolean; |
| 116 |
var |
| 117 |
U, Anchor: String; |
| 118 |
begin |
| 119 |
U := Url; |
| 120 |
URIExtractAnchor(U, Anchor); |
| 121 |
Result := Anchor <> ''; |
| 122 |
end; |
| 123 |
|
| 124 |
{$define read_implementation} |
| 125 |
{$I castleopendocument_open.inc} |
| 126 |
{$undef read_implementation} |
| 127 |
|
| 128 |
procedure ShareText(const Title, Subject, Content: string); |
| 129 |
begin |
| 130 |
Messaging.Send(['share-text', Title, Subject, Content]); |
| 131 |
end; |
| 132 |
|
| 133 |
procedure OpenApplicationStore(const ApplicationId: string); |
| 134 |
begin |
| 135 |
{$ifdef ANDROID} OpenUrl('market://details?id=' + ApplicationId); {$endif} |
| 136 |
|
| 137 |
{$ifdef CASTLE_IOS} Messaging.Send(['open-application-store', ApplicationId]); {$endif} |
| 138 |
end; |
| 139 |
|
| 140 |
procedure Vibrate(const Miliseconds: Cardinal); |
| 141 |
begin |
| 142 |
Messaging.Send(['vibrate', IntToStr(Miliseconds)]); |
| 143 |
end; |
| 144 |
|
| 145 |
procedure OnScreenNotification(const Message: string); |
| 146 |
begin |
| 147 |
Messaging.Send(['on-screen-notification', Message]); |
| 148 |
end; |
| 149 |
|
| 150 |
end. |