or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application.mddata-binding.mdfile-system.mdhttp-client.mdimage-handling.mdindex.mdplatform-utils.mdui-components.md

platform-utils.mddocs/

0

# Platform Utilities

1

2

Device information, platform detection, and native utility functions for iOS and Android integration. The platform utilities provide essential information about the runtime environment and cross-platform helper functions.

3

4

## Capabilities

5

6

### Platform Detection

7

8

Functions for detecting the current mobile platform and runtime environment.

9

10

```typescript { .api }

11

/**

12

* Platform detection functions

13

*/

14

function isAndroid(): boolean;

15

function isIOS(): boolean;

16

17

/**

18

* Platform constants

19

*/

20

namespace platformModule {

21

const platformNames: {

22

android: "Android";

23

ios: "iOS";

24

};

25

}

26

```

27

28

**Usage Examples:**

29

30

```typescript

31

import { isAndroid, isIOS } from "tns-core-modules";

32

33

// Platform-specific logic

34

if (isAndroid()) {

35

console.log("Running on Android");

36

// Android-specific code

37

} else if (isIOS()) {

38

console.log("Running on iOS");

39

// iOS-specific code

40

}

41

42

// Platform-specific styling or behavior

43

function getPlatformSpecificConfig() {

44

return {

45

statusBarStyle: isIOS() ? "light" : "dark",

46

navigationStyle: isAndroid() ? "material" : "cupertino",

47

hapticFeedback: isIOS()

48

};

49

}

50

```

51

52

### Device Information

53

54

Comprehensive device information including hardware specs, OS version, and device capabilities.

55

56

```typescript { .api }

57

/**

58

* Device information interface

59

*/

60

interface Device {

61

// Hardware information

62

readonly model: string;

63

readonly deviceType: "Phone" | "Tablet" | "Unknown";

64

readonly manufacturer: string;

65

readonly uuid: string;

66

67

// Operating system

68

readonly os: string; // "Android" or "iOS"

69

readonly osVersion: string; // e.g., "14.5" or "11.0"

70

readonly sdkVersion: string;

71

72

// Localization

73

readonly language: string; // e.g., "en"

74

readonly region: string; // e.g., "US"

75

76

// Capabilities

77

readonly isTablet: boolean;

78

readonly isEmulator: boolean;

79

}

80

81

/**

82

* Device information access

83

*/

84

const device: Device;

85

```

86

87

**Device Information Examples:**

88

89

```typescript

90

import { device } from "tns-core-modules";

91

92

// Log device information

93

console.log("Device Model:", device.model);

94

console.log("OS Version:", device.osVersion);

95

console.log("Language:", device.language);

96

console.log("Region:", device.region);

97

console.log("Is Tablet:", device.isTablet);

98

console.log("Is Emulator:", device.isEmulator);

99

100

// Device-specific logic

101

function configureAppForDevice() {

102

const config = {

103

// Adjust UI for tablet vs phone

104

useTabletLayout: device.isTablet,

105

106

// Enable debug features in emulator

107

debugMode: device.isEmulator,

108

109

// Localization

110

defaultLanguage: device.language,

111

defaultRegion: device.region,

112

113

// Performance optimizations

114

enableAnimations: !device.isEmulator,

115

cacheSize: device.isTablet ? "large" : "medium"

116

};

117

118

return config;

119

}

120

121

// OS version checking

122

function checkOSCompatibility() {

123

const osVersion = parseFloat(device.osVersion);

124

125

if (device.os === "iOS") {

126

if (osVersion < 12.0) {

127

console.warn("iOS version too old, some features may not work");

128

}

129

} else if (device.os === "Android") {

130

if (osVersion < 6.0) {

131

console.warn("Android version too old, some features may not work");

132

}

133

}

134

}

135

```

136

137

### Screen Information

138

139

Screen metrics and display information for responsive design and layout calculations.

140

141

```typescript { .api }

142

/**

143

* Screen metrics interface

144

*/

145

interface ScreenMetrics {

146

readonly widthPixels: number;

147

readonly heightPixels: number;

148

readonly widthDIPs: number;

149

readonly heightDIPs: number;

150

readonly scale: number;

151

}

152

153

/**

154

* Screen information interface

155

*/

156

interface Screen {

157

readonly mainScreen: ScreenMetrics;

158

readonly widthDIPs: number;

159

readonly heightDIPs: number;

160

readonly scale: number;

161

}

162

163

/**

164

* Screen information access

165

*/

166

const screen: Screen;

167

```

168

169

**Screen Information Examples:**

170

171

```typescript

172

import { screen } from "tns-core-modules";

173

174

// Get screen dimensions

175

console.log("Screen Width (DIPs):", screen.widthDIPs);

176

console.log("Screen Height (DIPs):", screen.heightDIPs);

177

console.log("Screen Scale:", screen.scale);

178

179

// Calculate pixel dimensions

180

const widthPixels = screen.widthDIPs * screen.scale;

181

const heightPixels = screen.heightDIPs * screen.scale;

182

183

console.log(`Physical resolution: ${widthPixels}x${heightPixels}`);

184

185

// Responsive design helper

186

function getScreenCategory(): "small" | "medium" | "large" | "xlarge" {

187

const width = screen.widthDIPs;

188

189

if (width < 360) return "small";

190

if (width < 480) return "medium";

191

if (width < 720) return "large";

192

return "xlarge";

193

}

194

195

// Layout calculations

196

function calculateOptimalImageSize() {

197

const screenWidth = screen.widthDIPs;

198

const margin = 20;

199

const columns = screenWidth > 480 ? 2 : 1;

200

201

const imageWidth = (screenWidth - (margin * (columns + 1))) / columns;

202

203

return {

204

width: imageWidth,

205

height: imageWidth * 0.75, // 4:3 aspect ratio

206

columns: columns

207

};

208

}

209

```

210

211

### Utility Functions

212

213

Cross-platform utility functions for common mobile development tasks.

214

215

```typescript { .api }

216

/**

217

* Utility functions namespace

218

*/

219

namespace Utils {

220

// Threading

221

function executeOnMainThread(func: () => void): void;

222

function mainThreadify<T extends Function>(func: T): T;

223

function isMainThread(): boolean;

224

function dispatchToMainThread(func: () => void): void;

225

226

// Memory management

227

function GC(): void;

228

function releaseNativeObject(object: any): void;

229

230

// URI and path utilities

231

function isFontIconURI(uri: string): boolean;

232

function isDataURI(uri: string): boolean;

233

function isFileOrResourcePath(path: string): boolean;

234

235

// System integration

236

function openFile(filePath: string): boolean;

237

function openUrl(url: string): boolean;

238

function isRealDevice(): boolean;

239

240

// Module utilities

241

function getModuleName(path: string): string;

242

243

// Layout utilities

244

const layout: LayoutUtils;

245

246

// Platform-specific utilities

247

const android: AndroidUtils;

248

const ios: IOSUtils;

249

}

250

```

251

252

**Utility Functions Examples:**

253

254

```typescript

255

import { Utils } from "tns-core-modules";

256

257

// Thread management

258

function updateUIFromBackground(data: any) {

259

// Ensure UI updates happen on main thread

260

Utils.executeOnMainThread(() => {

261

// Update UI elements

262

console.log("Updating UI with data:", data);

263

});

264

}

265

266

// Create main-thread wrapper function

267

const mainThreadCallback = Utils.mainThreadify((result: string) => {

268

console.log("Result:", result);

269

});

270

271

// Check current thread

272

if (!Utils.isMainThread()) {

273

console.log("Running on background thread");

274

}

275

276

// Memory management

277

function cleanupResources() {

278

// Force garbage collection

279

Utils.GC();

280

281

// Release native objects

282

Utils.releaseNativeObject(someNativeObject);

283

}

284

285

// System integration

286

function handleFileOpen(filePath: string) {

287

const success = Utils.openFile(filePath);

288

if (!success) {

289

console.log("Failed to open file with system app");

290

}

291

}

292

293

function handleUrlOpen(url: string) {

294

const success = Utils.openUrl(url);

295

if (!success) {

296

console.log("Failed to open URL in browser");

297

}

298

}

299

300

// URI validation

301

function validateImageSource(source: string): boolean {

302

return Utils.isDataURI(source) ||

303

Utils.isFileOrResourcePath(source) ||

304

source.startsWith("http");

305

}

306

307

// Development helpers

308

function setupDevelopmentFeatures() {

309

if (!Utils.isRealDevice()) {

310

console.log("Running in emulator - enabling debug features");

311

// Enable debugging features for emulator

312

}

313

}

314

```

315

316

### Layout Utilities

317

318

Utilities for layout calculations and responsive design.

319

320

```typescript { .api }

321

/**

322

* Layout utilities interface

323

*/

324

interface LayoutUtils {

325

// Measurement conversion

326

toDevicePixels(value: number): number;

327

toDeviceIndependentPixels(value: number): number;

328

329

// Layout measurement

330

measureWidth(view: any): number;

331

measureHeight(view: any): number;

332

333

// Responsive helpers

334

getDisplayMetrics(): {

335

widthPixels: number;

336

heightPixels: number;

337

density: number;

338

densityDpi: number;

339

};

340

}

341

```

342

343

### Platform-Specific Utilities

344

345

Platform-specific utility functions for Android and iOS integration.

346

347

```typescript { .api }

348

/**

349

* Android-specific utilities

350

*/

351

interface AndroidUtils {

352

// Context access

353

getApplicationContext(): any;

354

getCurrentActivity(): any;

355

356

// System services

357

getSystemService(name: string): any;

358

359

// Intent helpers

360

createIntent(action: string): any;

361

startActivity(intent: any): void;

362

363

// Permissions

364

requestPermissions(permissions: string[]): Promise<boolean>;

365

hasPermission(permission: string): boolean;

366

}

367

368

/**

369

* iOS-specific utilities

370

*/

371

interface IOSUtils {

372

// View controller access

373

getRootViewController(): any;

374

getCurrentViewController(): any;

375

376

// System integration

377

openSettings(): void;

378

canOpenUrl(url: string): boolean;

379

380

// Notifications

381

registerForPushNotifications(): void;

382

}

383

```

384

385

**Platform-Specific Examples:**

386

387

```typescript

388

import { Utils, isAndroid, isIOS } from "tns-core-modules";

389

390

// Android-specific functionality

391

if (isAndroid()) {

392

const context = Utils.android.getApplicationContext();

393

const activity = Utils.android.getCurrentActivity();

394

395

// Request camera permission

396

Utils.android.requestPermissions(["android.permission.CAMERA"])

397

.then(granted => {

398

if (granted) {

399

console.log("Camera permission granted");

400

}

401

});

402

403

// Check if permission is already granted

404

const hasCamera = Utils.android.hasPermission("android.permission.CAMERA");

405

}

406

407

// iOS-specific functionality

408

if (isIOS()) {

409

const rootVC = Utils.ios.getRootViewController();

410

411

// Check if can open settings

412

if (Utils.ios.canOpenUrl("app-settings:")) {

413

Utils.ios.openSettings();

414

}

415

416

// Register for push notifications

417

Utils.ios.registerForPushNotifications();

418

}

419

420

// Cross-platform utility class

421

class PlatformHelper {

422

static getDeviceInfo() {

423

return {

424

platform: isAndroid() ? "Android" : "iOS",

425

isRealDevice: Utils.isRealDevice(),

426

screenScale: screen.scale,

427

screenSize: {

428

width: screen.widthDIPs,

429

height: screen.heightDIPs

430

}

431

};

432

}

433

434

static async requestCameraPermission(): Promise<boolean> {

435

if (isAndroid()) {

436

return Utils.android.requestPermissions(["android.permission.CAMERA"]);

437

} else if (isIOS()) {

438

// iOS handles camera permission automatically when accessing camera

439

return Promise.resolve(true);

440

}

441

return Promise.resolve(false);

442

}

443

444

static openAppSettings(): void {

445

if (isAndroid()) {

446

const intent = Utils.android.createIntent("android.settings.APPLICATION_DETAILS_SETTINGS");

447

Utils.android.startActivity(intent);

448

} else if (isIOS()) {

449

Utils.ios.openSettings();

450

}

451

}

452

}

453

```