Skip to content

Commit

Permalink
feat: expose application orientation (#7602)
Browse files Browse the repository at this point in the history
  • Loading branch information
vchimev authored Aug 14, 2019
1 parent a14bc9f commit e2c3c8c
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 152 deletions.
15 changes: 5 additions & 10 deletions tests/app/application/application-tests-common.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
// >> application-require
import * as app from "tns-core-modules/application";
import * as app from "tns-core-modules/application";
import * as platform from "tns-core-modules/platform";
// << application-require

// >> application-app-check
import * as TKUnit from "../tk-unit";

if (app.android) {
console.log("We are running on Android device!");
console.log("We are running on an Android device!");
} else if (app.ios) {
console.log("We are running on iOS device");
console.log("We are running on an iOS device!");
}
// << application-app-check

import * as TKUnit from "../tk-unit";

export function testInitialized() {
if (platform.device.os === platform.platformNames.android) {
// we have the android defined
TKUnit.assert(app.android, "Application module not properly intialized");
} else if (platform.device.os === platform.platformNames.ios) {
TKUnit.assert(app.ios, "Application module not properly intialized");
Expand Down
7 changes: 4 additions & 3 deletions tests/app/application/application-tests.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ if (app.android) {
}
// << application-app-android-broadcast

export var testAndroidApplicationInitialized = function () {
export function testAndroidApplicationInitialized() {
TKUnit.assert(app.android, "Android application not initialized.");
TKUnit.assert(app.android.context, "Android context not initialized.");
TKUnit.assert(app.android.foregroundActivity, "Android foregroundActivity not initialized.");
TKUnit.assert(app.android.foregroundActivity.isNativeScriptActivity, "Andorid foregroundActivity.isNativeScriptActivity is true");
TKUnit.assert(app.android.foregroundActivity.isNativeScriptActivity, "Andorid foregroundActivity.isNativeScriptActivity is false.");
TKUnit.assert(app.android.startActivity, "Android startActivity not initialized.");
TKUnit.assert(app.android.nativeApp, "Android nativeApp not initialized.");
TKUnit.assert(app.android.orientation, "Android orientation not initialized.");
TKUnit.assert(app.android.packageName, "Android packageName not initialized.");
};
}
4 changes: 1 addition & 3 deletions tests/app/application/application-tests.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
/* tslint:disable */
//@private
import * as android from "./application-tests.android";
import * as iOS from "./application-tests.ios";
import * as iOS from "./application-tests.ios";
10 changes: 10 additions & 0 deletions tests/app/application/application-tests.ios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* tslint:disable:no-unused-variable */
import * as app from "tns-core-modules/application";
import * as TKUnit from "../tk-unit";

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

Expand Down Expand Up @@ -39,3 +40,12 @@ if (app.ios) {
}

// << application-ios-delegate

export function testIOSApplicationInitialized() {
TKUnit.assert(app.ios, "iOS application not initialized.");
TKUnit.assert(app.ios.delegate, "iOS delegate not initialized.");
TKUnit.assert(app.ios.nativeApp, "iOS nativeApp not initialized.");
TKUnit.assert(app.ios.orientation, "iOS orientation not initialized.");
TKUnit.assert(app.ios.window, "iOS window not initialized.");
TKUnit.assert(app.ios.rootController, "iOS root controller not initialized.");
}
34 changes: 0 additions & 34 deletions tests/app/application/application.md

This file was deleted.

59 changes: 27 additions & 32 deletions tests/app/platform/platform-tests.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
import * as TKUnit from "../tk-unit";
import * as app from "tns-core-modules/application";
import { isIOS, isAndroid } from "tns-core-modules/platform";

// >> platform-require
import * as platformModule from "tns-core-modules/platform";
// << platform-require

export function test_setTimeout_isDefined() {
var expected;
export function test_platform() {
let expectedPlatform;
if (app.android) {
expected = "Android";
}
else {
expected = "iOS";
expectedPlatform = "Android";
} else {
expectedPlatform = "iOS";
}
TKUnit.assertEqual(platformModule.device.os, expected, "device.os");
TKUnit.assertEqual(platformModule.device.os, expectedPlatform);
}

export function snippet_print_all() {
// >> platform-current
console.log("Device model: " + platformModule.device.model);
console.log("Device type: " + platformModule.device.deviceType);
console.log("Device manufacturer: " + platformModule.device.manufacturer);
console.log("Preferred language: " + platformModule.device.language);
console.log("Preferred region: " + platformModule.device.region);
console.log("OS: " + platformModule.device.os);
console.log("OS version: " + platformModule.device.osVersion);
console.log("SDK version: " + platformModule.device.sdkVersion);
console.log("Device UUID: " + platformModule.device.uuid);
export function test_device_screen() {
TKUnit.assert(platformModule.device.model, "Device model not initialized.");
TKUnit.assert(platformModule.device.manufacturer, "Device manufacturer not initialized.");
TKUnit.assert(platformModule.device.deviceType, "Device type not initialized.");
TKUnit.assert(platformModule.device.uuid, "Device UUID not initialized.");

TKUnit.assert(platformModule.device.language, "Preferred language not initialized.");
TKUnit.assert(platformModule.device.region, "Preferred region not initialized.");

TKUnit.assert(platformModule.device.os, "OS not initialized.");
TKUnit.assert(platformModule.device.osVersion, "OS version not initialized.");
TKUnit.assert(platformModule.device.sdkVersion, "SDK version not initialized.");

console.log("Screen width (px): " + platformModule.screen.mainScreen.widthPixels);
console.log("Screen height (px): " + platformModule.screen.mainScreen.heightPixels);
console.log("Screen width (DIPs): " + platformModule.screen.mainScreen.widthDIPs);
console.log("Screen height (DIPs): " + platformModule.screen.mainScreen.heightDIPs);
console.log("Screen scale: " + platformModule.screen.mainScreen.scale);
// << platform-current
TKUnit.assert(platformModule.screen.mainScreen.widthPixels, "Screen width (px) not initialized.");
TKUnit.assert(platformModule.screen.mainScreen.heightPixels, "Screen height (px) not initialized.");
TKUnit.assert(platformModule.screen.mainScreen.widthDIPs, "Screen width (DIPs) not initialized.");
TKUnit.assert(platformModule.screen.mainScreen.heightDIPs, "Screen height (DIPs) not initialized.");
TKUnit.assert(platformModule.screen.mainScreen.scale, "Screen scale not initialized.");
}

export function testIsIOSandIsAndroid() {
if (isIOS) {
export function test_IsAndroid_IsIOS() {
if (platformModule.isIOS) {
TKUnit.assertTrue(!!NSObject, "isIOS is true-ish but common iOS APIs are not available.");
} else if (isAndroid) {
TKUnit.assertTrue(!!android, "isAndroid is true but common 'android' package is not available.");
} else if (platformModule.isAndroid) {
TKUnit.assertTrue(!!android, "isAndroid is true-ish but common 'android' package is not available.");
}
}
4 changes: 2 additions & 2 deletions tns-core-modules/application/application-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export { Observable };
import {
AndroidApplication,
CssChangedEventData,
DiscardedErrorEventData,
iOSApplication,
LoadAppCSSEventData,
UnhandledErrorEventData,
DiscardedErrorEventData,
UnhandledErrorEventData
} from "./application";

export { UnhandledErrorEventData, DiscardedErrorEventData, CssChangedEventData, LoadAppCSSEventData };
Expand Down
68 changes: 41 additions & 27 deletions tns-core-modules/application/application.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
public static activityNewIntentEvent = ActivityNewIntent;
public static activityRequestPermissionsEvent = ActivityRequestPermissions;

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

get orientation(): "portrait" | "landscape" | "unknown" {
if (!this._orientation) {
const resources = this.context.getResources();
const configuration = <android.content.res.Configuration>resources.getConfiguration();
const orientation = configuration.orientation;

this._orientation = getOrientationValue(orientation);
}

return this._orientation;
}

set orientation(value: "portrait" | "landscape" | "unknown") {
this._orientation = value;
}

public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
ensureBroadCastReceiverClass();
const that = this;
Expand Down Expand Up @@ -232,6 +249,17 @@ global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
livesync(rootView, context);
};

function getOrientationValue(orientation: number): "portrait" | "landscape" | "unknown" {
switch (orientation) {
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
return "landscape";
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
return "portrait";
default:
return "unknown";
}
}

function initLifecycleCallbacks() {
const setThemeOnLaunch = profile("setThemeOnLaunch", (activity: androidx.appcompat.app.AppCompatActivity) => {
// Set app theme after launch screen was used during startup
Expand Down Expand Up @@ -321,7 +349,6 @@ function initLifecycleCallbacks() {
return lifecycleCallbacks;
}

let currentOrientation: number;
function initComponentCallbacks() {
let componentCallbacks = new android.content.ComponentCallbacks2({
onLowMemory: profile("onLowMemory", function () {
Expand All @@ -335,32 +362,19 @@ function initComponentCallbacks() {
}),

onConfigurationChanged: profile("onConfigurationChanged", function (newConfig: android.content.res.Configuration) {
const newOrientation = newConfig.orientation;
if (newOrientation === currentOrientation) {
return;
const newConfigOrientation = newConfig.orientation;
const newOrientation = getOrientationValue(newConfigOrientation);

if (androidApp.orientation !== newOrientation) {
androidApp.orientation = newOrientation;

notify(<OrientationChangedEventData>{
eventName: orientationChangedEvent,
android: androidApp.nativeApp,
newValue: androidApp.orientation,
object: androidApp
});
}

currentOrientation = newOrientation;
let newValue;

switch (newOrientation) {
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
newValue = "landscape";
break;
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
newValue = "portrait";
break;
default:
newValue = "unknown";
break;
}

notify(<OrientationChangedEventData>{
eventName: orientationChangedEvent,
android: androidApp.nativeApp,
newValue: newValue,
object: androidApp
});
})
});

Expand Down Expand Up @@ -399,4 +413,4 @@ declare namespace com {
static getInstance(): NativeScriptApplication;
}
}
}
}
20 changes: 16 additions & 4 deletions tns-core-modules/application/application.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ export class AndroidApplication extends Observable {
*/
startActivity: any /* androidx.appcompat.app.AppCompatActivity */;

/**
* Gets the orientation of the application.
* Available values: "portrait", "landscape", "unknown".
*/
orientation: "portrait" | "landscape" | "unknown";

/**
* The name of the application package.
*/
Expand Down Expand Up @@ -582,14 +588,20 @@ export interface iOSApplication {
window: any /* UIWindow */;

/**
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
* The [UIApplicationDelegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html) class.
*/
nativeApp: any /* UIApplication */;
delegate: any /* typeof UIApplicationDelegate */;

/**
* The [UIApplicationDelegate](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html) class.
* Gets or sets the orientation of the application.
* Available values: "portrait", "landscape", "unknown".
*/
delegate: any /* typeof UIApplicationDelegate */;
orientation: "portrait" | "landscape" | "unknown";

/**
* The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html).
*/
nativeApp: any /* UIApplication */;

/**
* Adds an observer to the default notification center for the specified notification.
Expand Down
Loading

0 comments on commit e2c3c8c

Please sign in to comment.