/usr/src/castle-game-engine-6.4/services/castleads.pas is in castle-game-engine-src 6.4+dfsg1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | {
Copyright 2015-2017 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Ads (advertisements) in game (TAds). }
unit CastleAds;
{$I castleconf.inc}
interface
uses Classes, CastleRectangles, CastleStringUtils;
const
{ Test banner ad "unit id". You can use it with @link(TAds.InitializeAdMob) for testing purposes
(but eventually you want to create your own, to show non-testing ads!).
From https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start }
TestAdMobBannerUnitId = 'ca-app-pub-3940256099942544/6300978111';
{ Test interstitial ad "unit id". You can use it with @link(TAds.InitializeAdMob) for testing purposes
(but eventually you want to create your own, to show non-testing ads!).
From http://stackoverflow.com/questions/12553929/is-there-any-admob-dummy-id and
https://github.com/googleads/googleads-mobile-android-examples/blob/master/admob/InterstitialExample/app/src/main/res/values/strings.xml }
TestAdMobInterstitialUnitId = 'ca-app-pub-3940256099942544/1033173712';
type
TAdNetwork = (anAdMob, anChartboost, anStartApp, anHeyzap);
TFullScreenAdType = (atInterstitialStatic, atInterstitialVideo, atReward);
TAdClosedEvent = procedure (const Sender: TObject; const Watched: boolean) of object;
{ Advertisements in game.
Right now only on Android (does nothing on other platforms,
as CastleMessaging does nothing on non-Android platforms).
Usage:
@orderedList(
@item(Create an instance of this class (only a single instance allowed).)
@item(Initialize at least one ad network using one of the
@code(InitializeXxx) methods. Usually you want to call the initialization
from @link(TCastleApplication.OnInitialize).)
@item(Use remaining methods of this class to show / hide ads, like
@link(ShowFullScreenAd), @link(ShowBanner), @link(HideBanner).)
@item(To include the necessary integration code in your Android project,
declare your Android project type as "integrated" with
the appropriate services (admob, chartboost, startapp, heyzap...)
inside CastleEngineManifest.xml .
See https://github.com/castle-engine/castle-engine/wiki/Android-Project-Services-Integrated-with-Castle-Game-Engine .)
)
}
TAds = class(TComponent)
private
type
TAdNetworkHandler = class abstract
strict private
FParent: TAds;
FBannerShowing: boolean;
FBannerGravity: Integer;
function MessageReceived(const Received: TCastleStringList): boolean;
procedure FullScreenAdClosed(const Watched: boolean);
strict protected
FFullScreenAdVisible: boolean;
procedure ReinitializeJavaActivity(Sender: TObject); virtual;
public
constructor Create(const AParent: TAds);
destructor Destroy; override;
property Parent: TAds read FParent;
class function Name: string; virtual; abstract;
procedure ShowBanner(const Gravity: Integer); virtual;
procedure HideBanner; virtual;
procedure ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean); virtual;
procedure StartTestActivity; virtual;
end;
TAdMobHandler = class(TAdNetworkHandler)
strict private
FBannerUnitId, FInterstitialUnitId, FTestDeviceIdsGlued: string;
strict protected
procedure ReinitializeJavaActivity(Sender: TObject); override;
public
constructor Create(const AParent: TAds;
const ABannerUnitId, AInterstitialUnitId: string;
const TestDeviceIds: array of string);
class function Name: string; override;
procedure ShowBanner(const Gravity: Integer); override;
procedure HideBanner; override;
procedure ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean); override;
end;
TChartboostHandler = class(TAdNetworkHandler)
strict private
FAppId, FAppSignature: string;
strict protected
procedure ReinitializeJavaActivity(Sender: TObject); override;
public
constructor Create(const AParent: TAds;
const AnAppId, AnAppSignature: string);
class function Name: string; override;
procedure ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean); override;
end;
TStartappHandler = class(TAdNetworkHandler)
strict private
FAppId: string;
strict protected
procedure ReinitializeJavaActivity(Sender: TObject); override;
public
constructor Create(const AParent: TAds;
const AnAppId: string);
class function Name: string; override;
procedure ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean); override;
end;
THeyzapHandler = class(TAdNetworkHandler)
strict private
FPublisherId: string;
strict protected
procedure ReinitializeJavaActivity(Sender: TObject); override;
public
constructor Create(const AParent: TAds;
const APublisherId: string);
class function Name: string; override;
procedure ShowBanner(const Gravity: Integer); override;
procedure HideBanner; override;
procedure ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean); override;
procedure StartTestActivity; override;
end;
var
FBannerSize: TRectangle;
FNetworks: array [TAdNetwork] of TAdNetworkHandler;
FOnFullScreenAdClosed: TAdClosedEvent;
protected
procedure FullScreenAdClosed(const Watched: boolean); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Initialize AdMob ads. You need to create the unit ids on AdMob website
(or use TestAdMobBannerUnitId, TestAdMobInterstitialUnitId for testing).
TestDeviceIds may be empty, or it may contain a list of devices
where you always want to see test ads --- even with real (non-test) ad units.
This is useful when you test ads in your application (with real ad units,
on a real phone). Paste here the list of your devices
(see https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start ,
you can see your device ids in "adb logcat" output) in order to avoid
getting banned for clicking on your own ads.
Usually called from @link(TCastleApplication.OnInitialize). }
procedure InitializeAdMob(const BannerUnitId, InterstitialUnitId: string;
const TestDeviceIds: array of string);
{ Initialize StartApp ads.
You need to register your game on http://startapp.com/ to get app id.
Usually called from @link(TCastleApplication.OnInitialize). }
procedure InitializeStartapp(const AppId: string);
{ Initialize Chartboost ads.
You need to register your game on http://chartboost.com/ to get app id and signature.
Usually called from @link(TCastleApplication.OnInitialize). }
procedure InitializeChartboost(const AppId, AppSignature: string);
{ Initialize Heyzap ads.
You need to register your game on https://www.heyzap.com/ to get publisher id.
Usually called from @link(TCastleApplication.OnInitialize). }
procedure InitializeHeyzap(const PublisherId: string);
{ Show full-screen ad, interstitial (in-between) or reward ad.
Not all ad networks support (or differentiate between) all full-screen
ad types possible in TFullScreenAdType enumeration.
So we eventually show other sensible ad type instead.
If WaitUntilLoaded, then we wait until the ad contents are fetched
(be sure to show some "loading" state to user, as it may take a while
in case of a slow network). If not WaitUntilLoaded, then we immediately
resign if ad is not loaded yet. Not all ad implementations support this
setting reliably, so this is only a suggestion.
In any case, after this method is called, we make sure to call callback
OnFullScreenAdClosed sometime. }
procedure ShowFullScreenAd(const AdNetwork: TAdNetwork;
const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean);
{ Show banner ad.
Banners are not supported by all ad networks (only AdMob and Heyzap now),
in case they are unsupported this call is silently ignored. }
procedure ShowBanner(const AdNetwork: TAdNetwork;
const HorizontalGravity: THorizontalPosition;
const VerticalGravity: TVerticalPosition);
{ Hide banner ad.
Banners are not supported by all ad networks (only AdMob and Heyzap now),
in case they are unsupported this call is silently ignored. }
procedure HideBanner(const AdNetwork: TAdNetwork);
{ Show Heyzap activity to test various ad networks integrations.
This is for now supported only by anHeyzap. }
procedure StartTestActivity(const AdNetwork: TAdNetwork);
property OnFullScreenAdClosed: TAdClosedEvent
read FOnFullScreenAdClosed write FOnFullScreenAdClosed;
property BannerSize: TRectangle read FBannerSize;
end;
implementation
uses SysUtils,
CastleUtils, CastleMessaging, CastleLog, CastleApplicationProperties;
{ TAdNetworkHandler ---------------------------------------------------------- }
constructor TAds.TAdNetworkHandler.Create(const AParent: TAds);
begin
inherited Create;
FParent := AParent;
Messaging.OnReceive.Add(@MessageReceived);
ApplicationProperties.OnInitializeJavaActivity.Add(@ReinitializeJavaActivity);
end;
destructor TAds.TAdNetworkHandler.Destroy;
begin
if Messaging <> nil then
Messaging.OnReceive.Remove(@MessageReceived);
if ApplicationProperties(false) <> nil then
ApplicationProperties(false).OnInitializeJavaActivity.Remove(@ReinitializeJavaActivity);
inherited;
end;
function TAds.TAdNetworkHandler.MessageReceived(const Received: TCastleStringList): boolean;
begin
Result := false;
if (Received.Count = 2) and
(Received[0] = 'ads-' + Name + '-full-screen-ad-closed') then
begin
FullScreenAdClosed(StrToBool(Received[1]));
Result := true;
end else
if (Received.Count = 5) and
(Received[0] = 'ads-' + Name + '-banner-size') then
begin
Result := true;
try
Parent.FBannerSize := Rectangle(
StrToInt(Received[1]),
StrToInt(Received[2]),
StrToInt(Received[3]),
StrToInt(Received[4])
);
except
on EConvertError do
WritelnWarning('Ads', 'Cannot process banner size from ' + GlueStrings(Received, NL));
end;
end;
end;
procedure TAds.TAdNetworkHandler.ShowBanner(const Gravity: Integer);
begin
FBannerShowing := true;
FBannerGravity := Gravity;
end;
procedure TAds.TAdNetworkHandler.HideBanner;
begin
FBannerShowing := false;
end;
procedure TAds.TAdNetworkHandler.ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean);
begin
{ if the network doesn't support showing full-screen ads, pretend it's shown,
in case user code waits for OnFullScreenAdClosed. }
FullScreenAdClosed(false);
end;
procedure TAds.TAdNetworkHandler.StartTestActivity;
begin
end;
procedure TAds.TAdNetworkHandler.ReinitializeJavaActivity(Sender: TObject);
begin
{ this is important if the Java application was killed, but native code survived,
while waiting for ad to finish. Reproduce: turn on "kill app when sending to bg"
in Android debug options, then use "watch ad" and return to game. }
if FFullScreenAdVisible then
FullScreenAdClosed(false);
Parent.FBannerSize := TRectangle.Empty;
{ reshow banner, if necessary }
if FBannerShowing then
ShowBanner(FBannerGravity);
end;
procedure TAds.TAdNetworkHandler.FullScreenAdClosed(const Watched: boolean);
begin
FFullScreenAdVisible := false;
Parent.FullScreenAdClosed(Watched);
end;
{ TAdMobHandler -------------------------------------------------------------- }
constructor TAds.TAdMobHandler.Create(const AParent: TAds;
const ABannerUnitId, AInterstitialUnitId: string;
const TestDeviceIds: array of string);
begin
inherited Create(AParent);
FTestDeviceIdsGlued := GlueStrings(TestDeviceIds, ',');
FBannerUnitId := ABannerUnitId;
FInterstitialUnitId := AInterstitialUnitId;
ReinitializeJavaActivity(nil);
end;
procedure TAds.TAdMobHandler.ReinitializeJavaActivity(Sender: TObject);
begin
Messaging.Send(['ads-' + Name + '-initialize', FBannerUnitId, FInterstitialUnitId, FTestDeviceIdsGlued]);
inherited;
end;
class function TAds.TAdMobHandler.Name: string;
begin
Result := 'admob';
end;
procedure TAds.TAdMobHandler.ShowBanner(const Gravity: Integer);
begin
inherited;
Messaging.Send(['ads-' + Name + '-banner-show', IntToStr(Gravity)]);
end;
procedure TAds.TAdMobHandler.HideBanner;
begin
Messaging.Send(['ads-' + Name + '-banner-hide']);
inherited;
end;
procedure TAds.TAdMobHandler.ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean);
begin
FFullScreenAdVisible := true;
if WaitUntilLoaded then
Messaging.Send(['ads-' + Name + '-show-interstitial', 'wait-until-loaded']) else
Messaging.Send(['ads-' + Name + '-show-interstitial', 'no-wait']);
end;
{ TChartboostHandler --------------------------------------------------------- }
constructor TAds.TChartboostHandler.Create(const AParent: TAds;
const AnAppId, AnAppSignature: string);
begin
inherited Create(AParent);
FAppId := AnAppId;
FAppSignature := AnAppSignature;
ReinitializeJavaActivity(nil);
end;
procedure TAds.TChartboostHandler.ReinitializeJavaActivity(Sender: TObject);
begin
Messaging.Send(['ads-' + Name + '-initialize', FAppId, FAppSignature]);
inherited;
end;
class function TAds.TChartboostHandler.Name: string;
begin
Result := 'chartboost';
end;
procedure TAds.TChartboostHandler.ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean);
begin
FFullScreenAdVisible := true;
Messaging.Send(['ads-' + Name + '-show-interstitial']);
end;
{ TStartappHandler --------------------------------------------------------- }
constructor TAds.TStartappHandler.Create(const AParent: TAds;
const AnAppId: string);
begin
inherited Create(AParent);
FAppId := AnAppId;
ReinitializeJavaActivity(nil);
end;
procedure TAds.TStartappHandler.ReinitializeJavaActivity(Sender: TObject);
begin
Messaging.Send(['ads-' + Name + '-initialize', FAppId]);
inherited;
end;
class function TAds.TStartappHandler.Name: string;
begin
Result := 'startapp';
end;
procedure TAds.TStartappHandler.ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean);
begin
FFullScreenAdVisible := true;
Messaging.Send(['ads-' + Name + '-show-interstitial']);
end;
{ THeyzapHandler --------------------------------------------------------- }
constructor TAds.THeyzapHandler.Create(const AParent: TAds;
const APublisherId: string);
begin
inherited Create(AParent);
FPublisherId := APublisherId;
ReinitializeJavaActivity(nil);
end;
procedure TAds.THeyzapHandler.ReinitializeJavaActivity(Sender: TObject);
begin
Messaging.Send(['ads-' + Name + '-initialize', FPublisherId]);
inherited;
end;
class function TAds.THeyzapHandler.Name: string;
begin
Result := 'heyzap';
end;
procedure TAds.THeyzapHandler.ShowBanner(const Gravity: Integer);
begin
inherited;
Messaging.Send(['ads-' + Name + '-banner-show', IntToStr(Gravity)]);
end;
procedure TAds.THeyzapHandler.HideBanner;
begin
Messaging.Send(['ads-' + Name + '-banner-hide']);
inherited;
end;
procedure TAds.THeyzapHandler.ShowFullScreenAd(const AdType: TFullScreenAdType;
const WaitUntilLoaded: boolean);
begin
FFullScreenAdVisible := true;
case AdType of
atInterstitialStatic: Messaging.Send(['ads-' + Name + '-show-full-screen', 'interstitial-static']);
atInterstitialVideo : Messaging.Send(['ads-' + Name + '-show-full-screen', 'interstitial-video']);
else { atReward :}Messaging.Send(['ads-' + Name + '-show-full-screen', 'reward']);
end;
end;
procedure TAds.THeyzapHandler.StartTestActivity;
begin
inherited;
Messaging.Send(['ads-' + Name + '-start-test-activity']);
end;
{ TAds ----------------------------------------------------------------------- }
constructor TAds.Create(AOwner: TComponent);
begin
inherited;
FBannerSize := TRectangle.Empty;
end;
destructor TAds.Destroy;
var
AdNetwork: TAdNetwork;
begin
for AdNetwork := Low(AdNetwork) to High(AdNetwork) do
FreeAndNil(FNetworks[AdNetwork]);
inherited;
end;
procedure TAds.InitializeAdMob(const BannerUnitId, InterstitialUnitId: string;
const TestDeviceIds: array of string);
begin
if FNetworks[anAdMob] <> nil then
FreeAndNil(FNetworks[anAdMob]);
FNetworks[anAdMob] := TAdMobHandler.Create(Self,
BannerUnitId, InterstitialUnitId, TestDeviceIds);
end;
procedure TAds.InitializeStartapp(const AppId: string);
begin
if FNetworks[anStartApp] <> nil then
FreeAndNil(FNetworks[anStartApp]);
FNetworks[anStartApp] := TStartAppHandler.Create(Self, AppId);
end;
procedure TAds.InitializeChartboost(const AppId, AppSignature: string);
begin
if FNetworks[anChartboost] <> nil then
FreeAndNil(FNetworks[anChartboost]);
FNetworks[anChartboost] := TChartboostHandler.Create(Self, AppId, AppSignature);
end;
procedure TAds.InitializeHeyzap(const PublisherId: string);
begin
if FNetworks[anHeyzap] <> nil then
FreeAndNil(FNetworks[anHeyzap]);
FNetworks[anHeyzap] := THeyzapHandler.Create(Self, PublisherId);
end;
procedure TAds.FullScreenAdClosed(const Watched: boolean);
begin
if Assigned(OnFullScreenAdClosed) then
OnFullScreenAdClosed(Self, Watched);
end;
procedure TAds.ShowFullScreenAd(const AdNetwork: TAdNetwork;
const AdType: TFullScreenAdType; const WaitUntilLoaded: boolean);
begin
{$ifdef ANDROID}
if FNetworks[AdNetwork] <> nil then
FNetworks[AdNetwork].ShowFullScreenAd(AdType, WaitUntilLoaded) else
{ if the network is not initialized, pretend it's shown,
in case user code waits for OnFullScreenAdClosed. }
FullScreenAdClosed(false);
{$else}
{ since this is not supported on non-Android now, just make
FullScreenAdClosed(false) immediately, to avoid the app waiting
for OnFullScreenAdClosed forever. }
FullScreenAdClosed(false);
{$endif}
end;
procedure TAds.ShowBanner(const AdNetwork: TAdNetwork;
const HorizontalGravity: THorizontalPosition;
const VerticalGravity: TVerticalPosition);
function GravityToInt(
const HorizontalGravity: THorizontalPosition;
const VerticalGravity: TVerticalPosition): Integer;
const
{ Gravity constants for some messages, for example to indicate ad placement.
Equal to constants on
http://developer.android.com/reference/android/view/Gravity.html .
@groupBegin }
GravityLeft = $00000003; //< Push object to the left of its container, not changing its size.
GravityRight = $00000005; //< Push object to the right of its container, not changing its size.
GravityTop = $00000030; //< Push object to the top of its container, not changing its size.
GravityBottom = $00000050; //< Push object to the bottom of its container, not changing its size.
GravityCenterHorizontal = $00000001; //< Place object in the horizontal center of its container, not changing its size.
GravityCenterVertical = $00000010; //< Place object in the vertical center of its container, not changing its size.
//GravityNo = 0; //< Constant indicating that no gravity has been set.
{ @groupEnd }
begin
Result := 0;
case HorizontalGravity of
hpLeft: Result := Result or GravityLeft;
hpRight: Result := Result or GravityRight;
hpMiddle: Result := Result or GravityCenterHorizontal;
else raise EInternalError.Create('ShowBanner:HorizontalGravity?');
end;
case VerticalGravity of
vpTop: Result := Result or GravityTop;
vpBottom: Result := Result or GravityBottom;
vpMiddle: Result := Result or GravityCenterVertical;
else raise EInternalError.Create('ShowBanner:VerticalGravity?');
end;
end;
begin
if FNetworks[AdNetwork] <> nil then
FNetworks[AdNetwork].ShowBanner(GravityToInt(HorizontalGravity, VerticalGravity));
end;
procedure TAds.HideBanner(const AdNetwork: TAdNetwork);
begin
if FNetworks[AdNetwork] <> nil then
FNetworks[AdNetwork].HideBanner;
end;
procedure TAds.StartTestActivity(const AdNetwork: TAdNetwork);
begin
if FNetworks[AdNetwork] <> nil then
FNetworks[AdNetwork].StartTestActivity;
end;
end.
|