diff --git a/lcl/interfaces/customdrawn/customdrawnwslazdeviceapis.pas b/lcl/interfaces/customdrawn/customdrawnwslazdeviceapis.pas index b958711d87..385b83c86f 100644 --- a/lcl/interfaces/customdrawn/customdrawnwslazdeviceapis.pas +++ b/lcl/interfaces/customdrawn/customdrawnwslazdeviceapis.pas @@ -55,6 +55,7 @@ type // TLazDevice class function GetDeviceManufacturer: string; override; class function GetDeviceModel: string; override; + class function GetScreenRotation(AScreenIndex: Integer): TScreenRotation; override; class procedure Vibrate(ADurationMS: Cardinal); override; end; @@ -179,6 +180,55 @@ begin javaEnvRef^^.ReleaseStringUTFChars(javaEnvRef, lJavaString, lNativeString); end; +class function TCDWSLazDeviceAPIs.GetScreenRotation(AScreenIndex: Integer + ): TScreenRotation; +var + windowManagerClass, displayClass : jclass; + javaMethodId_WindowService, javaMethodId_getDefaultDisplay, javaMethodId_getRotation : JMethodID; + windowManagerObject, displayObject: JObject; + javaString_WINDOW_SERVICE, javaString_getDefaultDisplay : JString; + lRotation: Jint; +const + ROTATION_0 = 0; + ROTATION_180 = 2; + ROTATION_270 = 3; + ROTATION_90 = 1; +begin + windowManagerClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/view/WindowManager'); + displayClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/view/Display'); + + // get the string Context.WINDOW_SERVICE remember that NewStringUTF does not require ReleaseStringUTFChars + javaString_WINDOW_SERVICE := javaEnvRef^^.NewStringUTF(javaEnvRef, pchar('getWindowManager')); + + // get method id for WindowManager object + javaMethodId_WindowService := javaEnvRef^^.GetMethodID(javaEnvRef, javaAndroidAppActivityClass, 'getWindowManager', '()Landroid/view/WindowManager;'); + + // Get the WindowManager object + // Window w = (Window) getSystemService(Context.WINDOW_SERVICE); + windowManagerObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, javaActivityObject, javaMethodId_WindowService); + + javaString_getDefaultDisplay := javaEnvRef^^.NewStringUTF(javaEnvRef, pchar('getDefaultDisplay')); + + // Get method id for Display object + javaMethodId_getDefaultDisplay := javaEnvRef^^.GetMethodID(javaEnvRef, windowManagerClass, 'getDefaultDisplay', '()Landroid/view/Display;'); + + // Get the display object + displayObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, windowManagerObject, javaMethodId_getDefaultDisplay); + + // Get method id for method: getRotation + javaMethodId_getRotation := javaEnvRef^^.GetMethodID(javaEnvRef, displayClass, 'getRotation', '()I'); + + // Now call getRotation method in the display object + lRotation := javaEnvRef^^.CallIntMethod(javaEnvRef, displayObject, javaMethodId_getRotation); + case lRotation of + ROTATION_180: Result := srRotation_180; + ROTATION_270: Result := srRotation_270; + ROTATION_90: Result := srRotation_90; + else + Result := srRotation_0; + end; +end; + class procedure TCDWSLazDeviceAPIs.Vibrate(ADurationMS: Cardinal); var lVibratorObject: JObject; diff --git a/lcl/lazdeviceapis.pas b/lcl/lazdeviceapis.pas index 6acbfd08a0..0f6ad59497 100644 --- a/lcl/lazdeviceapis.pas +++ b/lcl/lazdeviceapis.pas @@ -126,12 +126,15 @@ type // TLazDevice + TScreenRotation = (srRotation_0, srRotation_90, srRotation_180, srRotation_270); + TLazDevice = class private function GetDeviceManufacturer: string; function GetDeviceModel: string; public procedure Vibrate(ADurationMS: Cardinal); + function GetScreenRotation(AScreenIndex: Integer): TScreenRotation; property Manufacturer: string read GetDeviceManufacturer; property Model: string read GetDeviceModel; end; @@ -172,6 +175,14 @@ begin WidgetsetClass.Vibrate(ADurationMS); end; +function TLazDevice.GetScreenRotation(AScreenIndex: Integer): TScreenRotation; +var + WidgetsetClass: TWSLazDeviceAPIsClass; +begin + WidgetsetClass := TWSLazDeviceAPIsClass(GetWSLazDeviceAPIs()); + Result := WidgetsetClass.GetScreenRotation(AScreenIndex); +end; + { TLazAccelerometer } procedure TLazAccelerometer.StartReadingAccelerometerData; diff --git a/lcl/widgetset/wslazdeviceapis.pas b/lcl/widgetset/wslazdeviceapis.pas index 7ae5374cc9..fbca70aa40 100644 --- a/lcl/widgetset/wslazdeviceapis.pas +++ b/lcl/widgetset/wslazdeviceapis.pas @@ -63,6 +63,7 @@ type // TLazDevice class function GetDeviceManufacturer: string; virtual; class function GetDeviceModel: string; virtual; + class function GetScreenRotation(AScreenIndex: Integer): TScreenRotation; virtual; class procedure Vibrate(ADurationMS: Cardinal); virtual; end; @@ -118,6 +119,12 @@ begin Result := ''; end; +class function TWSLazDeviceAPIs.GetScreenRotation(AScreenIndex: Integer + ): TScreenRotation; +begin + Result := srRotation_0; +end; + class procedure TWSLazDeviceAPIs.Vibrate(ADurationMS: Cardinal); begin