Bläddra i källkod

added getDeviceName()

ncannasse 12 år sedan
förälder
incheckning
7f205b0ce4
1 ändrade filer med 40 tillägg och 4 borttagningar
  1. 40 4
      h3d/System.hx

+ 40 - 4
h3d/System.hx

@@ -52,13 +52,20 @@ class System {
 	public static function setLoop( f : Void -> Void ) {
 		if( loop != null )
 			flash.Lib.current.removeEventListener(flash.events.Event.ENTER_FRAME, loop);
-		loop = function(_) f();
-		flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME, loop);
+		if( f == null )
+			loop = null;
+		else {
+			loop = function(_) f();
+			flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME, loop);
+		}
 	}
 
+	static function isAir() {
+		return flash.system.Capabilities.playerType == "Desktop";
+	}
+	
 	public static function exit() {
-		var isAir = flash.system.Capabilities.playerType == "Desktop";
-		if( isAir ) {
+		if( isAir() ) {
 			var d : Dynamic = flash.Lib.current.loaderInfo.applicationDomain.getDefinition("flash.desktop.NativeApplication");
 			Reflect.field(Reflect.field(d,"nativeApplication"),"exit")();
 		} else
@@ -73,6 +80,35 @@ class System {
 		case TextInput: "ibeam";
 		}
 	}
+		
+
+	/**
+		Returns the device name:
+			"PC" for a desktop computer
+			Or the android device name
+			(will add iPad/iPhone/iPod soon)
+	**/
+	static var CACHED_NAME = null;
+	public static function getDeviceName() {
+		if( CACHED_NAME != null )
+			return CACHED_NAME;
+		var name;
+		if( isAndroid && isAir() ) {
+			try {
+				var f : Dynamic = Type.createInstance(flash.Lib.current.loaderInfo.applicationDomain.getDefinition("flash.filesystem.File"), ["/system/build.prop"]);
+				var fs : flash.utils.IDataInput = Type.createInstance(flash.Lib.current.loaderInfo.applicationDomain.getDefinition("flash.filesystem.FileStream"), []);
+				Reflect.callMethod(fs, Reflect.field(fs, "open"), [f, "read"]);
+				var content = fs.readUTFBytes(fs.bytesAvailable);
+				name = StringTools.trim(content.split("ro.product.model=")[1].split("\n")[0]);
+			} catch( e : Dynamic ) {
+				name = "Android";
+			}
+		} else
+			name = "PC";
+		CACHED_NAME = name;
+		return name;
+	}
+	
 
 	#end