Skip to content

Commit

Permalink
feat: add os version major/minor int constants
Browse files Browse the repository at this point in the history
- Added new integer constants:
  * Ti.Platform.versionMajor
  * Ti.Platform.versionMinor
  * OS_VERSION_MAJOR
  * OS_VERSION_MINOR

Fixes TIMOB-28061
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Aug 5, 2020
1 parent 1cf07b2 commit 3fd8535
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public class PlatformModule extends KrollModule
protected DisplayCapsProxy displayCaps;

private List<Processor> processors;

private int versionMajor;
private int versionMinor;
protected int batteryState;
protected double batteryLevel;
protected boolean batteryStateReady;
Expand All @@ -84,6 +85,17 @@ public PlatformModule()

batteryState = BATTERY_STATE_UNKNOWN;
batteryLevel = -1;

// Extract "<major>.<minor>" integers from OS version string.
String[] versionComponents = Build.VERSION.RELEASE.split("\\.");
try {
this.versionMajor = Integer.parseInt(versionComponents[0]);
if (versionComponents.length >= 2) {
this.versionMinor = Integer.parseInt(versionComponents[1]);
}
} catch (Exception ex) {
Log.e(TAG, "Failed to parse OS version string.", ex);
}
}

@Kroll.method
Expand Down Expand Up @@ -139,6 +151,18 @@ public String getVersion()
return APSAnalyticsMeta.getOsVersion();
}

@Kroll.getProperty
public int getVersionMajor()
{
return this.versionMajor;
}

@Kroll.getProperty
public int getVersionMinor()
{
return this.versionMinor;
}

@Kroll.method
@Kroll.getProperty
public double getAvailableMemory()
Expand Down
19 changes: 19 additions & 0 deletions apidoc/Global/Global.yml
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,25 @@ properties:
permission: read-only
since: "9.0.0"

- name: OS_VERSION_MAJOR
summary: The operation system's major version number.
description: |
``` js
if (OS_IOS && (OS_VERSION_MAJOR >= 13)) {
// Do something on iOS 13 or higher only.
}
```
type: Number
permission: read-only
since: "9.2.0"

- name: OS_VERSION_MINOR
summary: The operating system's minor version number.
description: Will return zero if OS does not have a minor version.
type: Number
permission: read-only
since: "9.2.0"

- name: ENV_DEV
summary: Alias for <ENV_DEVELOPMENT>
type: Boolean
Expand Down
15 changes: 14 additions & 1 deletion apidoc/Titanium/Platform/Platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,23 @@ properties:
platforms: [android, iphone, ipad]

- name: version
summary: System's OS version.
summary: The operating system's version string.
type: String
permission: read-only

- name: versionMajor
summary: The operating system's major version number.
type: Number
permission: read-only
since: "9.2.0"

- name: versionMinor
summary: The operating system's minor version number.
description: Will return zero if OS does not have a minor version.
type: Number
permission: read-only
since: "9.2.0"

examples:
- title: Battery Event Example
example: |
Expand Down
12 changes: 12 additions & 0 deletions common/Resources/ti.internal/extensions/globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2020 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

// Add global constants.
Object.defineProperties(global, {
OS_VERSION_MAJOR: { value: Ti.Platform.versionMajor, writable: false },
OS_VERSION_MINOR: { value: Ti.Platform.versionMinor, writable: false }
});
3 changes: 3 additions & 0 deletions common/Resources/ti.internal/extensions/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// First, load our globals. Our other extensions depend on these.
import './globals';

// Load all JavaScript extensions/polyfills
import './js';
// Load extensions to polyfill our own APIs
Expand Down
7 changes: 4 additions & 3 deletions common/Resources/ti.internal/extensions/ti/ti.blob.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2019 by Axway, Inc. All Rights Reserved.
* Copyright (c) 2019-2020 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* globals OS_IOS, OS_VERSION_MAJOR */

if (Ti.Platform.osname === 'iphone' || Ti.Platform.osname === 'ipad') {
if (OS_IOS) {
const buffer = Ti.createBuffer({ value: '' });
const blob = buffer.toBlob();
blob.constructor.prototype.toString = function () {
const value = this.text;
return (value === undefined) ? '[object TiBlob]' : value;
};

if ((parseInt(Ti.Platform.version.split('.')[0]) < 11)) {
if (OS_VERSION_MAJOR < 11) {
// This is hack to fix TIMOB-27707. Remove it after minimum target set iOS 11+
setTimeout(function () {}, Infinity);
}
Expand Down
4 changes: 2 additions & 2 deletions common/Resources/ti.internal/extensions/ti/ti.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* globals OS_ANDROID,OS_IOS */
/* globals OS_ANDROID,OS_IOS, OS_VERSION_MAJOR */
import Color from '../../../../lib/color';
const isIOS13Plus = OS_IOS && parseInt(Ti.Platform.version.split('.')[0]) >= 13;
const isIOS13Plus = OS_IOS && (OS_VERSION_MAJOR >= 13);

// As Android passes a new instance of Ti.UI to every JS file we can't just
// Ti.UI within this file, we must call kroll.binding to get the Titanium
Expand Down
2 changes: 2 additions & 0 deletions iphone/Classes/PlatformModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ READONLY_PROPERTY(NSNumber *, totalMemory, TotalMemory);
READONLY_PROPERTY(NSNumber *, uptime, Uptime);
READONLY_PROPERTY(NSString *, username, Username);
READONLY_PROPERTY(NSString *, version, Version);
READONLY_PROPERTY(NSNumber *, versionMajor, VersionMajor);
READONLY_PROPERTY(NSNumber *, versionMinor, VersionMinor);

// Methods
- (BOOL)canOpenURL:(NSString *)url;
Expand Down
15 changes: 14 additions & 1 deletion iphone/Classes/PlatformModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@implementation PlatformModule

@synthesize architecture, availableMemory, model, name, osname, ostype, processorCount, totalMemory, uptime, username, version;
@synthesize architecture, availableMemory, model, name, osname, ostype, processorCount, totalMemory, uptime, username, version, versionMajor, versionMinor;

#pragma mark Internal

Expand All @@ -41,6 +41,15 @@ - (id)init
name = [[theDevice systemName] retain];
version = [[theDevice systemVersion] retain];

// Extract "<major>.<minor>" integers from OS version string.
NSArray *versionComponents = [version componentsSeparatedByString:@"."];
versionMajor = [NSNumber numberWithInt:[versionComponents[0] intValue]];
if ([versionComponents count] >= 2) {
versionMinor = [NSNumber numberWithInt:[versionComponents[1] intValue]];
} else {
versionMinor = @0;
}

// grab logical CPUs
int cores = 1;
size_t sizeof_cores = sizeof(cores);
Expand Down Expand Up @@ -90,6 +99,8 @@ - (void)dealloc
RELEASE_TO_NIL(name);
RELEASE_TO_NIL(model);
RELEASE_TO_NIL(version);
RELEASE_TO_NIL(versionMajor);
RELEASE_TO_NIL(versionMinor);
RELEASE_TO_NIL(architecture);
RELEASE_TO_NIL(processorCount);
RELEASE_TO_NIL(username);
Expand Down Expand Up @@ -460,6 +471,8 @@ - (NSString *)netmask
GETTER_IMPL(NSNumber *, uptime, Uptime);
GETTER_IMPL(NSString *, username, Username);
GETTER_IMPL(NSString *, version, Version);
GETTER_IMPL(NSNumber *, versionMajor, VersionMajor);
GETTER_IMPL(NSNumber *, versionMinor, VersionMinor);

MAKE_SYSTEM_PROP(BATTERY_STATE_UNKNOWN, UIDeviceBatteryStateUnknown);
MAKE_SYSTEM_PROP(BATTERY_STATE_UNPLUGGED, UIDeviceBatteryStateUnplugged);
Expand Down
3 changes: 3 additions & 0 deletions iphone/Resources/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ btn.addEventListener('click', function() {

win.add(btn);
win.open();

console.log("@@@ OS_VERSION_MAJOR: " + OS_VERSION_MAJOR);
console.log("@@@ OS_VERSION_MINOR: " + OS_VERSION_MINOR);
4 changes: 2 additions & 2 deletions tests/Resources/intl.numberformat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* global OS_IOS */
/* global OS_IOS, OS_VERSION_MAJOR */
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('Intl.NumberFormat', () => {
});

describe('#formatToParts()', () => {
if (OS_IOS && (parseInt(Ti.Platform.version.split('.')[0]) < 13)) {
if (OS_IOS && (OS_VERSION_MAJOR < 13)) {
return;
}

Expand Down
7 changes: 3 additions & 4 deletions tests/Resources/ti.app.ios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* global OS_VERSION_MAJOR */
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
Expand Down Expand Up @@ -191,8 +192,7 @@ describe.ios('Titanium.App.iOS', function () {
should(Ti.App.iOS.EVENT_ACCESSIBILITY_LAYOUT_CHANGED).be.a.String();
should(Ti.App.iOS.EVENT_ACCESSIBILITY_SCREEN_CHANGED).be.a.String();

const isiOS13 = parseInt(Ti.Platform.version.split('.')[0]) >= 13;
if (isiOS13) {
if (OS_VERSION_MAJOR >= 13) {
should(Ti.App.iOS.USER_INTERFACE_STYLE_UNSPECIFIED).be.a.Number();
should(Ti.App.iOS.USER_INTERFACE_STYLE_LIGHT).be.a.Number();
should(Ti.App.iOS.USER_INTERFACE_STYLE_DARK).be.a.Number();
Expand Down Expand Up @@ -261,8 +261,7 @@ describe.ios('Titanium.App.iOS', function () {
});

it.ios('.userInterfaceStyle', () => {
const isiOS13 = parseInt(Ti.Platform.version.split('.')[0]) >= 13;
if (isiOS13) {
if (OS_VERSION_MAJOR >= 13) {
// We only check for the type, since the value (light, dark, unspecified)
// can vary between device configs
should(Ti.App.iOS.userInterfaceStyle).be.a.Number();
Expand Down
2 changes: 1 addition & 1 deletion tests/Resources/ti.network.httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ describe('Titanium.Network.HTTPClient', function () {

it.android('TLSv3 support', function (finish) {
// Only supported on Android 10+
if (parseInt(Ti.Platform.version.split('.')[0]) < 10) {
if (Ti.Platform.Android.API_LEVEL < 29) {
return finish();
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Resources/ti.platform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* global OS_VERSION_MAJOR, OS_VERSION_MINOR */
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
Expand Down Expand Up @@ -279,6 +280,16 @@ describe('Titanium.Platform', function () {
should(Ti.Platform).have.readOnlyProperty('version').which.is.a.String();
});

it('.versionMajor', () => {
should(Ti.Platform).have.readOnlyProperty('versionMajor').which.is.a.Number();
should(Ti.Platform.versionMajor).be.eql(OS_VERSION_MAJOR);
});

it('.versionMinor', () => {
should(Ti.Platform).have.readOnlyProperty('versionMinor').which.is.a.Number();
should(Ti.Platform.versionMinor).be.eql(OS_VERSION_MINOR);
});

it.ios('.identifierForVendor', () => {
should(Ti.Platform.identifierForVendor).be.a.String();
should(Ti.Platform.getIdentifierForVendor).be.a.Function();
Expand Down
4 changes: 2 additions & 2 deletions tests/Resources/ti.ui.ios.tableviewstyle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* global OS_VERSION_MAJOR */
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
Expand All @@ -14,8 +15,7 @@ describe.ios('Titanium.UI.iOS.TableViewStyle', function () {
it('#constants', function () {
should(Titanium.UI.iOS.TableViewStyle.PLAIN).be.a.Number();
should(Titanium.UI.iOS.TableViewStyle.GROUPED).be.a.Number();
const isiOS13 = parseInt(Ti.Platform.version.split('.')[0]) >= 13;
if (isiOS13) {
if (OS_VERSION_MAJOR >= 13) {
should(Titanium.UI.iOS.TableViewStyle.INSET_GROUPED).be.a.Number();
}
});
Expand Down
7 changes: 3 additions & 4 deletions tests/Resources/ti.ui.ios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* global OS_VERSION_MAJOR */
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');

describe.ios('Titanium.UI.iOS', function () {
const isiOS13 = (parseInt(Ti.Platform.version.split('.')[0]) >= 13);

// --- properties ---
it.iosBroken('.appBadge', function () {
should(Ti.UI.iOS.appBadge).be.undefined(); // FIXME: Defaults to 0!
Expand Down Expand Up @@ -147,7 +146,7 @@ describe.ios('Titanium.UI.iOS', function () {
});

it('#systemImage()', function () {
if (isiOS13) {
if (OS_VERSION_MAJOR >= 13) {
should(Ti.UI.iOS.systemImage).not.be.undefined();
should(Ti.UI.iOS.systemImage).be.a.Function();
const systemImage = Ti.UI.iOS.systemImage('drop.triangle.fill');
Expand All @@ -157,7 +156,7 @@ describe.ios('Titanium.UI.iOS', function () {

it('.BLUR_EFFECT_STYLE_SYSTEM_* constants', function () {
// Used in BlurView.effect. Need to copy under #constatnt test case
if (isiOS13) {
if (OS_VERSION_MAJOR >= 13) {
should(Ti.UI.iOS.BLUR_EFFECT_STYLE_SYSTEM_ULTRA_THIN_MATERIAL).be.a.Number();
should(Ti.UI.iOS.BLUR_EFFECT_STYLE_SYSTEM_THIN_MATERIAL).be.a.Number();
should(Ti.UI.iOS.BLUR_EFFECT_STYLE_SYSTEM_MATERIAL).be.a.Number();
Expand Down
4 changes: 2 additions & 2 deletions tests/Resources/ti.ui.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* globals OS_ANDROID,OS_IOS */
/* globals OS_ANDROID, OS_IOS, OS_VERSION_MAJOR */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');
Expand Down Expand Up @@ -218,7 +218,7 @@ describe('Titanium.UI', function () {
});

describe('Semantic Colors', () => {
const isIOS13Plus = OS_IOS && parseInt(Ti.Platform.version.split('.')[0]) >= 13;
const isIOS13Plus = OS_IOS && (OS_VERSION_MAJOR >= 13);

it('#fetchSemanticColor() with user colors', () => {
const semanticColors = require('./semantic.colors.json');
Expand Down

0 comments on commit 3fd8535

Please sign in to comment.