Add System.openFile / System.openURL

This commit is contained in:
Joshua Granick
2016-11-11 16:46:43 -08:00
parent c850459fa5
commit 54a8f32f60
5 changed files with 196 additions and 0 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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);

View File

@@ -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
}
}

View File

@@ -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 () {