Files
NativeScript/utils/utils.android.ts
2015-03-03 10:34:40 +02:00

98 lines
3.0 KiB
TypeScript

import application = require("application");
import common = require("utils/utils-common");
// merge the exports of the common file with the exports of this file
declare var exports;
require("utils/module-merge").merge(common, exports);
export module layout {
var density = -1;
var metrics: android.util.DisplayMetrics;
// cache the MeasureSpec constants here, to prevent extensive marshaling calls to and from Java
// TODO: While this boosts the performance it is error-prone in case Google changes these constants
var MODE_SHIFT = 30;
var MODE_MASK = 0x3 << MODE_SHIFT;
var sdkVersion = -1;
var useOldMeasureSpec = false;
export function makeMeasureSpec(size: number, mode: number): number {
if (sdkVersion === -1 && application.android && application.android.context) {
// check whether the old layout is needed
sdkVersion = application.android.context.getApplicationInfo().targetSdkVersion;
useOldMeasureSpec = sdkVersion <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
}
if (useOldMeasureSpec) {
return size + mode;
}
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
export function getDisplayDensity(): number {
if (density === -1) {
density = getDisplayMetrics().density;
}
return density;
}
function getDisplayMetrics(): android.util.DisplayMetrics {
if (!metrics) {
metrics = application.android.context.getResources().getDisplayMetrics();
}
return metrics;
}
}
// We are using "ad" here to avoid namespace collision with the global android object
export module ad {
export module collections {
export function stringArrayToStringSet(str: string[]): any {
var hashSet = new java.util.HashSet();
if ("undefined" !== typeof str) {
for (var element in str) {
hashSet.add('' + str[element]);
}
}
return hashSet;
}
export function stringSetToStringArray(stringSet: any): string[] {
var arr = [];
if ("undefined" !== typeof stringSet) {
var it = stringSet.iterator();
while (it.hasNext()) {
var element = '' + it.next();
arr.push(element);
}
}
return arr;
}
}
export module resources {
export function getDrawableId(name) {
return getId(":drawable/" + name);
}
export function getStringId(name) {
return getId(":string/" + name);
}
export function getId(name: string): number {
var context = application.android.context;
var resources = context.getResources();
var packageName = context.getPackageName();
var uri = packageName + name;
return resources.getIdentifier(uri, null, null);
}
}
}
export function GC() {
gc();
}