Skip to content

Commit e2c3c8c

Browse files
authored
feat: expose application orientation (NativeScript#7602)
1 parent a14bc9f commit e2c3c8c

15 files changed

+158
-152
lines changed

tests/app/application/application-tests-common.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
// >> application-require
2-
import * as app from "tns-core-modules/application";
1+
import * as app from "tns-core-modules/application";
32
import * as platform from "tns-core-modules/platform";
4-
// << application-require
53

6-
// >> application-app-check
4+
import * as TKUnit from "../tk-unit";
5+
76
if (app.android) {
8-
console.log("We are running on Android device!");
7+
console.log("We are running on an Android device!");
98
} else if (app.ios) {
10-
console.log("We are running on iOS device");
9+
console.log("We are running on an iOS device!");
1110
}
12-
// << application-app-check
13-
14-
import * as TKUnit from "../tk-unit";
1511

1612
export function testInitialized() {
1713
if (platform.device.os === platform.platformNames.android) {
18-
// we have the android defined
1914
TKUnit.assert(app.android, "Application module not properly intialized");
2015
} else if (platform.device.os === platform.platformNames.ios) {
2116
TKUnit.assert(app.ios, "Application module not properly intialized");

tests/app/application/application-tests.android.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ if (app.android) {
3737
}
3838
// << application-app-android-broadcast
3939

40-
export var testAndroidApplicationInitialized = function () {
40+
export function testAndroidApplicationInitialized() {
4141
TKUnit.assert(app.android, "Android application not initialized.");
4242
TKUnit.assert(app.android.context, "Android context not initialized.");
4343
TKUnit.assert(app.android.foregroundActivity, "Android foregroundActivity not initialized.");
44-
TKUnit.assert(app.android.foregroundActivity.isNativeScriptActivity, "Andorid foregroundActivity.isNativeScriptActivity is true");
44+
TKUnit.assert(app.android.foregroundActivity.isNativeScriptActivity, "Andorid foregroundActivity.isNativeScriptActivity is false.");
4545
TKUnit.assert(app.android.startActivity, "Android startActivity not initialized.");
4646
TKUnit.assert(app.android.nativeApp, "Android nativeApp not initialized.");
47+
TKUnit.assert(app.android.orientation, "Android orientation not initialized.");
4748
TKUnit.assert(app.android.packageName, "Android packageName not initialized.");
48-
};
49+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
/* tslint:disable */
2-
//@private
31
import * as android from "./application-tests.android";
4-
import * as iOS from "./application-tests.ios";
2+
import * as iOS from "./application-tests.ios";

tests/app/application/application-tests.ios.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* tslint:disable:no-unused-variable */
22
import * as app from "tns-core-modules/application";
3+
import * as TKUnit from "../tk-unit";
34

45
export * from "./application-tests-common";
56

@@ -39,3 +40,12 @@ if (app.ios) {
3940
}
4041

4142
// << application-ios-delegate
43+
44+
export function testIOSApplicationInitialized() {
45+
TKUnit.assert(app.ios, "iOS application not initialized.");
46+
TKUnit.assert(app.ios.delegate, "iOS delegate not initialized.");
47+
TKUnit.assert(app.ios.nativeApp, "iOS nativeApp not initialized.");
48+
TKUnit.assert(app.ios.orientation, "iOS orientation not initialized.");
49+
TKUnit.assert(app.ios.window, "iOS window not initialized.");
50+
TKUnit.assert(app.ios.rootController, "iOS root controller not initialized.");
51+
}

tests/app/application/application.md

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/app/platform/platform-tests.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
11
import * as TKUnit from "../tk-unit";
22
import * as app from "tns-core-modules/application";
3-
import { isIOS, isAndroid } from "tns-core-modules/platform";
4-
5-
// >> platform-require
63
import * as platformModule from "tns-core-modules/platform";
7-
// << platform-require
84

9-
export function test_setTimeout_isDefined() {
10-
var expected;
5+
export function test_platform() {
6+
let expectedPlatform;
117
if (app.android) {
12-
expected = "Android";
13-
}
14-
else {
15-
expected = "iOS";
8+
expectedPlatform = "Android";
9+
} else {
10+
expectedPlatform = "iOS";
1611
}
17-
TKUnit.assertEqual(platformModule.device.os, expected, "device.os");
12+
TKUnit.assertEqual(platformModule.device.os, expectedPlatform);
1813
}
1914

20-
export function snippet_print_all() {
21-
// >> platform-current
22-
console.log("Device model: " + platformModule.device.model);
23-
console.log("Device type: " + platformModule.device.deviceType);
24-
console.log("Device manufacturer: " + platformModule.device.manufacturer);
25-
console.log("Preferred language: " + platformModule.device.language);
26-
console.log("Preferred region: " + platformModule.device.region);
27-
console.log("OS: " + platformModule.device.os);
28-
console.log("OS version: " + platformModule.device.osVersion);
29-
console.log("SDK version: " + platformModule.device.sdkVersion);
30-
console.log("Device UUID: " + platformModule.device.uuid);
15+
export function test_device_screen() {
16+
TKUnit.assert(platformModule.device.model, "Device model not initialized.");
17+
TKUnit.assert(platformModule.device.manufacturer, "Device manufacturer not initialized.");
18+
TKUnit.assert(platformModule.device.deviceType, "Device type not initialized.");
19+
TKUnit.assert(platformModule.device.uuid, "Device UUID not initialized.");
20+
21+
TKUnit.assert(platformModule.device.language, "Preferred language not initialized.");
22+
TKUnit.assert(platformModule.device.region, "Preferred region not initialized.");
23+
24+
TKUnit.assert(platformModule.device.os, "OS not initialized.");
25+
TKUnit.assert(platformModule.device.osVersion, "OS version not initialized.");
26+
TKUnit.assert(platformModule.device.sdkVersion, "SDK version not initialized.");
3127

32-
console.log("Screen width (px): " + platformModule.screen.mainScreen.widthPixels);
33-
console.log("Screen height (px): " + platformModule.screen.mainScreen.heightPixels);
34-
console.log("Screen width (DIPs): " + platformModule.screen.mainScreen.widthDIPs);
35-
console.log("Screen height (DIPs): " + platformModule.screen.mainScreen.heightDIPs);
36-
console.log("Screen scale: " + platformModule.screen.mainScreen.scale);
37-
// << platform-current
28+
TKUnit.assert(platformModule.screen.mainScreen.widthPixels, "Screen width (px) not initialized.");
29+
TKUnit.assert(platformModule.screen.mainScreen.heightPixels, "Screen height (px) not initialized.");
30+
TKUnit.assert(platformModule.screen.mainScreen.widthDIPs, "Screen width (DIPs) not initialized.");
31+
TKUnit.assert(platformModule.screen.mainScreen.heightDIPs, "Screen height (DIPs) not initialized.");
32+
TKUnit.assert(platformModule.screen.mainScreen.scale, "Screen scale not initialized.");
3833
}
3934

40-
export function testIsIOSandIsAndroid() {
41-
if (isIOS) {
35+
export function test_IsAndroid_IsIOS() {
36+
if (platformModule.isIOS) {
4237
TKUnit.assertTrue(!!NSObject, "isIOS is true-ish but common iOS APIs are not available.");
43-
} else if (isAndroid) {
44-
TKUnit.assertTrue(!!android, "isAndroid is true but common 'android' package is not available.");
38+
} else if (platformModule.isAndroid) {
39+
TKUnit.assertTrue(!!android, "isAndroid is true-ish but common 'android' package is not available.");
4540
}
4641
}

tns-core-modules/application/application-common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export { Observable };
3535
import {
3636
AndroidApplication,
3737
CssChangedEventData,
38+
DiscardedErrorEventData,
3839
iOSApplication,
3940
LoadAppCSSEventData,
40-
UnhandledErrorEventData,
41-
DiscardedErrorEventData,
41+
UnhandledErrorEventData
4242
} from "./application";
4343

4444
export { UnhandledErrorEventData, DiscardedErrorEventData, CssChangedEventData, LoadAppCSSEventData };

tns-core-modules/application/application.android.ts

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
4242
public static activityNewIntentEvent = ActivityNewIntent;
4343
public static activityRequestPermissionsEvent = ActivityRequestPermissions;
4444

45+
private _orientation: "portrait" | "landscape" | "unknown";
4546
public paused: boolean;
4647
public nativeApp: android.app.Application;
4748
public context: android.content.Context;
@@ -80,6 +81,22 @@ export class AndroidApplication extends Observable implements AndroidApplication
8081
this._pendingReceiverRegistrations.length = 0;
8182
}
8283

84+
get orientation(): "portrait" | "landscape" | "unknown" {
85+
if (!this._orientation) {
86+
const resources = this.context.getResources();
87+
const configuration = <android.content.res.Configuration>resources.getConfiguration();
88+
const orientation = configuration.orientation;
89+
90+
this._orientation = getOrientationValue(orientation);
91+
}
92+
93+
return this._orientation;
94+
}
95+
96+
set orientation(value: "portrait" | "landscape" | "unknown") {
97+
this._orientation = value;
98+
}
99+
83100
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
84101
ensureBroadCastReceiverClass();
85102
const that = this;
@@ -232,6 +249,17 @@ global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
232249
livesync(rootView, context);
233250
};
234251

252+
function getOrientationValue(orientation: number): "portrait" | "landscape" | "unknown" {
253+
switch (orientation) {
254+
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
255+
return "landscape";
256+
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
257+
return "portrait";
258+
default:
259+
return "unknown";
260+
}
261+
}
262+
235263
function initLifecycleCallbacks() {
236264
const setThemeOnLaunch = profile("setThemeOnLaunch", (activity: androidx.appcompat.app.AppCompatActivity) => {
237265
// Set app theme after launch screen was used during startup
@@ -321,7 +349,6 @@ function initLifecycleCallbacks() {
321349
return lifecycleCallbacks;
322350
}
323351

324-
let currentOrientation: number;
325352
function initComponentCallbacks() {
326353
let componentCallbacks = new android.content.ComponentCallbacks2({
327354
onLowMemory: profile("onLowMemory", function () {
@@ -335,32 +362,19 @@ function initComponentCallbacks() {
335362
}),
336363

337364
onConfigurationChanged: profile("onConfigurationChanged", function (newConfig: android.content.res.Configuration) {
338-
const newOrientation = newConfig.orientation;
339-
if (newOrientation === currentOrientation) {
340-
return;
365+
const newConfigOrientation = newConfig.orientation;
366+
const newOrientation = getOrientationValue(newConfigOrientation);
367+
368+
if (androidApp.orientation !== newOrientation) {
369+
androidApp.orientation = newOrientation;
370+
371+
notify(<OrientationChangedEventData>{
372+
eventName: orientationChangedEvent,
373+
android: androidApp.nativeApp,
374+
newValue: androidApp.orientation,
375+
object: androidApp
376+
});
341377
}
342-
343-
currentOrientation = newOrientation;
344-
let newValue;
345-
346-
switch (newOrientation) {
347-
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
348-
newValue = "landscape";
349-
break;
350-
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
351-
newValue = "portrait";
352-
break;
353-
default:
354-
newValue = "unknown";
355-
break;
356-
}
357-
358-
notify(<OrientationChangedEventData>{
359-
eventName: orientationChangedEvent,
360-
android: androidApp.nativeApp,
361-
newValue: newValue,
362-
object: androidApp
363-
});
364378
})
365379
});
366380

@@ -399,4 +413,4 @@ declare namespace com {
399413
static getInstance(): NativeScriptApplication;
400414
}
401415
}
402-
}
416+
}

tns-core-modules/application/application.d.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ export class AndroidApplication extends Observable {
413413
*/
414414
startActivity: any /* androidx.appcompat.app.AppCompatActivity */;
415415

416+
/**
417+
* Gets the orientation of the application.
418+
* Available values: "portrait", "landscape", "unknown".
419+
*/
420+
orientation: "portrait" | "landscape" | "unknown";
421+
416422
/**
417423
* The name of the application package.
418424
*/
@@ -582,14 +588,20 @@ export interface iOSApplication {
582588
window: any /* UIWindow */;
583589

584590
/**
585-
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
591+
* The [UIApplicationDelegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html) class.
586592
*/
587-
nativeApp: any /* UIApplication */;
593+
delegate: any /* typeof UIApplicationDelegate */;
588594

589595
/**
590-
* The [UIApplicationDelegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html) class.
596+
* Gets or sets the orientation of the application.
597+
* Available values: "portrait", "landscape", "unknown".
591598
*/
592-
delegate: any /* typeof UIApplicationDelegate */;
599+
orientation: "portrait" | "landscape" | "unknown";
600+
601+
/**
602+
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
603+
*/
604+
nativeApp: any /* UIApplication */;
593605

594606
/**
595607
* Adds an observer to the default notification center for the specified notification.

0 commit comments

Comments
 (0)