Einzelnen Beitrag anzeigen

philipp.hofmann

Registriert seit: 21. Mär 2012
Ort: Hannover
862 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: $IFDEF für Firemonkey

  Alt 25. Jun 2020, 12:26
Hier mal meine Hilfsklasse um zu prüfen, ob ich auf einem Phone oder einem Tablet unterwegs bin.
Als $IFDEF kann dies aber eigentlich nicht funktionieren, du musst deine App ja zur Laufzeit dynamisch auf die Gegebenheiten anpassen.


Delphi-Quellcode:
unit MyDeviceInfo;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Devices,
  FMX.BehaviorManager, System.Math, FMX.Platform, FMX.MultiView;

type
  TMyDeviceInfo = class(TCustomMultiView)
  private
    FDeviceService: IFMXDeviceService;
    function DefineContext: TFmxObject;
    function GetDevice: TDeviceInfo;
  public
    class function isPhoneMode(form:TForm; myFormatSettings:TFormatSettings):boolean;
  end;

implementation

uses MyLog;

function TMyDeviceInfo.DefineContext: TFmxObject;
begin
  if Owner is TFmxObject then
    Result := TFmxObject(Owner)
  else
    Result := Parent;
end;

function TMyDeviceInfo.GetDevice: TDeviceInfo;
var
  DeviceService: IDeviceBehavior;
  Context: TFmxObject;
begin
  if csDesigning in ComponentState then
  begin
    Context := DefineContext;
    if TBehaviorServices.Current.SupportsBehaviorService(IDeviceBehavior,DeviceService, Context)
    then
      Exit(DeviceService.GetDevice(Context));
  end;
  Result := TDeviceInfo.ThisDevice;
end;

class function TMyDeviceInfo.isPhoneMode(form:TForm; myFormatSettings:TFormatSettings):boolean;
var FMyCustomMV : TMyDeviceInfo;
    ThisDevice: TDeviceInfo;
    logStr:String;
begin
  try
    FMyCustomMV := TMyDeviceInfo.Create(form);
    TPlatformServices.Current.SupportsPlatformService(IFMXDeviceService,FMyCustomMV.FDeviceService);
    ThisDevice := FMyCustomMV.GetDevice;
    case ThisDevice.DeviceClass of
      TDeviceInfo.TDeviceClass.Phone:
        begin
          logStr:='Phone ';
          Result:=true;
        end;
      TDeviceInfo.TDeviceClass.Tablet:
        begin
          logStr:='Tablet ';
          Result:=false;
        end;
      TDeviceInfo.TDeviceClass.Desktop:
        begin
          logStr:='Desktop ';
          Result:=false;
        end
      else begin
        logStr:='Unknown ';
        Result:=true;
      end;
    end;
    if (FMyCustomMV.GetOrientation in [TScreenOrientation.Landscape, TScreenOrientation.InvertedLandscape]) then
      logStr:=logStr+'(landscape) '
    else
      logStr:=logStr+'(portrait) ';
    {logStr:=logStr+CurrToStrF(ThisDevice.MinPhysicalScreenSize.Width,ffNumber,1,myFormatSettings)+' ';
    logStr:=logStr+CurrToStrF(ThisDevice.MinPhysicalScreenSize.Height,ffNumber,1,myFormatSettings)+' ';
    logStr:=logStr+CurrToStrF(ThisDevice.MaxPhysicalScreenSize.Width,ffNumber,1,myFormatSettings)+' ';
    logStr:=logStr+CurrToStrF(ThisDevice.MaxPhysicalScreenSize.Height,ffNumber,1,myFormatSettings)+' ';
    logStr:=logStr+IntToStr(ThisDevice.PixelsPerInch)+' ';
    logStr:=logStr+CurrToStrF(ThisDevice.MaxDiagonal*2.54,ffNumber,1,myFormatSettings)+'cm';}

    //mlog.info('Display: '+logStr);
  except on E: Exception do
    begin
      Result:=false;
      //mlog.error('Can´t determine phone mode: '+e.message);
    end;
  end;
end;

end.
Speziell für Android nutze ich noch eine weitere Funktion, weil es gerade bei Tablets relativ viele Amazon-Fire-Tablets gibt und diese ja ein wenig anders ticken (z.B. kein Google-Playstore):

Delphi-Quellcode:
class function TStringUtils.isFireOS():boolean;
begin
  {$IFDEF ANDROID}
    if (pos(JStringToString(TJBuild.JavaClass.MODEL),'KFMAWI,KFMUWI,KFOT,KFTT,KFJWI,KFJWA,KFSOWI,KFTHWA,KFTHWI,KFAPWA,KFAPWI,KFARWI,KFASWI,KFSAWA,KFSAWI,KFTBWI,KFMEWI,KFFOWI,KFGIWI,KFAUWI,KFSUWI,KFKAWI,KFDOWI,SD4930UR')>0) then
      Result:=true
    else
      Result:=false;
  {$ELSE}
    Result:=false;
  {$ENDIF}
end;
Dies Liste muss man nur von Zeit zu Zeit erweitern.
  Mit Zitat antworten Zitat