From 54a8f32f60a3a6c00d985be17eb98c8ffd101078 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Fri, 11 Nov 2016 16:46:43 -0800 Subject: [PATCH] Add System.openFile / System.openURL --- lime/system/System.hx | 76 +++++++++++++++++++ project/include/system/System.h | 2 + project/src/ExternalInterface.cpp | 20 +++++ project/src/system/System.mm | 44 +++++++++++ .../main/java/org/haxe/lime/GameActivity.java | 54 +++++++++++++ 5 files changed, 196 insertions(+) diff --git a/lime/system/System.hx b/lime/system/System.hx index 73eafa0df..1ab456945 100644 --- a/lime/system/System.hx +++ b/lime/system/System.hx @@ -5,6 +5,7 @@ import lime.app.Application; import lime.math.Rectangle; #if flash +import flash.net.URLRequest; import flash.system.Capabilities; import flash.Lib; #end @@ -163,6 +164,7 @@ class System { } + public static inline function load (library:String, method:String, args:Int = 0, lazy:Bool = false):Dynamic { #if !macro @@ -174,6 +176,78 @@ class System { } + public static function openFile (path:String):Void { + + if (path != null) { + + #if windows + + Sys.command ("start", [ path ]); + + #elseif mac + + Sys.command ("/usr/bin/open", [ path ]); + + #elseif linux + + Sys.command ("/usr/bin/xdg-open", [ path, "&" ]); + + #elseif (js && html5) + + Browser.window.open (path, "_blank"); + + #elseif flash + + Lib.getURL (new URLRequest (path), "_blank"); + + #elseif android + + var openFile = JNI.createStaticMethod ("org/haxe/lime/GameActivity", "openFile", "(Ljava/lang/String;)V"); + openFile (path); + + #elseif (lime_cffi && !macro) + + lime_system_open_file (path); + + #end + + } + + } + + + public static function openURL (url:String, target:String = "_blank"):Void { + + if (url != null) { + + #if desktop + + openFile (url); + + #elseif (js && html5) + + Browser.window.open (url, target); + + #elseif flash + + Lib.getURL (new URLRequest (url), target); + + #elseif android + + var openURL = JNI.createStaticMethod ("org/haxe/lime/GameActivity", "openURL", "(Ljava/lang/String;Ljava/lang/String;)V"); + openURL (url, target); + + #elseif (lime_cffi && !macro) + + lime_system_open_url (url, target); + + #end + + } + + } + + @:noCompletion private static function __getDirectory (type:SystemDirectory):String { #if (lime_cffi && !macro) @@ -366,6 +440,8 @@ class System { @:cffi private static function lime_system_get_display (index:Int):Dynamic; @:cffi private static function lime_system_get_num_displays ():Int; @:cffi private static function lime_system_get_timer ():Float; + @:cffi private static function lime_system_open_file (path:String):Void; + @:cffi private static function lime_system_open_url (url:String, target:String):Void; #end diff --git a/project/include/system/System.h b/project/include/system/System.h index 0027a5fac..0275b0e07 100644 --- a/project/include/system/System.h +++ b/project/include/system/System.h @@ -34,6 +34,8 @@ namespace lime { static value GetDisplay (int id); static int GetNumDisplays (); static double GetTimer (); + static void OpenFile (const char* path); + static void OpenURL (const char* url, const char* target); static bool SetAllowScreenTimeout (bool allow); diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 48154665e..b8f1bf73a 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -1328,6 +1328,24 @@ namespace lime { } + void lime_system_open_file (HxString path) { + + #ifdef IPHONE + System::OpenFile (path.__s); + #endif + + } + + + void lime_system_open_url (HxString url, HxString target) { + + #ifdef IPHONE + System::OpenURL (url.__s, target.__s); + #endif + + } + + bool lime_system_set_allow_screen_timeout (bool allow) { return System::SetAllowScreenTimeout (allow); @@ -1734,6 +1752,8 @@ namespace lime { DEFINE_PRIME1 (lime_system_get_display); DEFINE_PRIME0 (lime_system_get_num_displays); DEFINE_PRIME0 (lime_system_get_timer); + DEFINE_PRIME1v (lime_system_open_file); + DEFINE_PRIME2v (lime_system_open_url); DEFINE_PRIME1 (lime_system_set_allow_screen_timeout); DEFINE_PRIME2v (lime_text_event_manager_register); DEFINE_PRIME3 (lime_text_layout_create); diff --git a/project/src/system/System.mm b/project/src/system/System.mm index 7609089ff..52b0bfd9f 100644 --- a/project/src/system/System.mm +++ b/project/src/system/System.mm @@ -54,4 +54,48 @@ namespace lime { } + void System::OpenFile (const char* path) { + + OpenURL (path, NULL); + + } + + + void System::OpenURL (const char* url, const char* target) { + + #ifndef OBJC_ARC + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + #endif + + UIApplication *application = [UIApplication sharedApplication]; + NSString *str = [[NSString alloc] initWithUTF8String: url]; + NSURL *_url = [NSURL URLWithString: str]; + + if ([[UIApplication sharedApplication] canOpenURL: _url]) { + + if ([application respondsToSelector: @selector (openURL:options:completionHandler:)]) { + + [application openURL: _url options: @{} + completionHandler:^(BOOL success) { + //NSLog(@"Open %@: %d", _url, success); + } + ]; + + } else { + + BOOL success = [application openURL: _url]; + //NSLog(@"Open %@: %d",scheme,success); + + } + + } + + #ifndef OBJC_ARC + [str release]; + [pool drain]; + #endif + + } + + } \ No newline at end of file diff --git a/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java b/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java index 2714e2ddd..cc57dc515 100644 --- a/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java +++ b/templates/android/template/app/src/main/java/org/haxe/lime/GameActivity.java @@ -4,13 +4,17 @@ package org.haxe.lime; import android.content.res.AssetManager; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Vibrator; +import android.util.Log; import android.view.KeyEvent; import android.view.KeyCharacterMap; import android.view.View; +import android.webkit.MimeTypeMap; +import java.io.File; import java.util.ArrayList; import java.util.List; @@ -271,6 +275,56 @@ public class GameActivity extends SDLActivity { ::end:: + public static void openFile (String path) { + + try { + + String extension = path; + int index = path.lastIndexOf ('.'); + + if (index > 0) { + + extension = path.substring (index + 1); + + } + + String mimeType = MimeTypeMap.getSingleton ().getMimeTypeFromExtension (extension); + File file = new File (path); + + Intent intent = new Intent (); + intent.setAction (Intent.ACTION_VIEW); + intent.setDataAndType (Uri.fromFile (file), mimeType); + + Extension.mainActivity.startActivity (intent); + + } catch (Exception e) { + + Log.e ("GameActivity", e.toString ()); + return; + + } + + } + + + public static void openURL (String url, String target) { + + Intent browserIntent = new Intent (Intent.ACTION_VIEW).setData (Uri.parse (url)); + + try { + + Extension.mainActivity.startActivity (browserIntent); + + } catch (Exception e) { + + Log.e ("GameActivity", e.toString ()); + return; + + } + + } + + public static void postUICallback (final long handle) { Extension.callbackHandler.post (new Runnable () {