Added logic to launch Browser from Windows

This commit is contained in:
Javier Suárez Ruiz 2017-11-16 08:43:01 +01:00
parent de9102f38a
commit b8e71c9c66
1 changed files with 147 additions and 120 deletions

View File

@ -18,60 +18,67 @@ namespace Ooui
static readonly Type androidActivityType; static readonly Type androidActivityType;
static readonly Type androidWebViewType; static readonly Type androidWebViewType;
static Platform () static Platform()
{ {
var asms = AppDomain.CurrentDomain.GetAssemblies ().ToDictionary ( var asms = AppDomain.CurrentDomain.GetAssemblies().ToDictionary(
x => x.GetName ().Name); x => x.GetName().Name);
asms.TryGetValue ("Xamarin.iOS", out iosAssembly); asms.TryGetValue("Xamarin.iOS", out iosAssembly);
if (iosAssembly != null) { if (iosAssembly != null)
iosUIViewControllerType = iosAssembly.GetType ("UIKit.UIViewController");
iosUIApplicationType = iosAssembly.GetType ("UIKit.UIApplication");
iosUIWebViewType = iosAssembly.GetType ("UIKit.UIWebView");
iosNSUrl = iosAssembly.GetType ("Foundation.NSUrl");
iosNSUrlRequest = iosAssembly.GetType ("Foundation.NSUrlRequest");
}
asms.TryGetValue ("Mono.Android", out androidAssembly);
if (androidAssembly != null) {
androidActivityType = androidAssembly.GetType ("Android.App.Activity");
androidWebViewType = androidAssembly.GetType ("Android.Webkit.WebView");
}
}
public static void OpenBrowser (string url, object presenter)
{ {
if (iosAssembly != null) { iosUIViewControllerType = iosAssembly.GetType("UIKit.UIViewController");
OpenBrowserOniOS (url, presenter); iosUIApplicationType = iosAssembly.GetType("UIKit.UIApplication");
iosUIWebViewType = iosAssembly.GetType("UIKit.UIWebView");
iosNSUrl = iosAssembly.GetType("Foundation.NSUrl");
iosNSUrlRequest = iosAssembly.GetType("Foundation.NSUrlRequest");
} }
else if (androidAssembly != null) {
OpenBrowserOnAndroid (url, presenter); asms.TryGetValue("Mono.Android", out androidAssembly);
} if (androidAssembly != null)
else { {
StartBrowserProcess (url); androidActivityType = androidAssembly.GetType("Android.App.Activity");
androidWebViewType = androidAssembly.GetType("Android.Webkit.WebView");
} }
} }
static void OpenBrowserOnAndroid (string url, object presenter) public static void OpenBrowser(string url, object presenter)
{ {
var presenterType = GetObjectType (presenter); if (iosAssembly != null)
{
OpenBrowserOniOS(url, presenter);
}
else if (androidAssembly != null)
{
OpenBrowserOnAndroid(url, presenter);
}
else
{
StartBrowserProcess(url);
}
}
static void OpenBrowserOnAndroid(string url, object presenter)
{
var presenterType = GetObjectType(presenter);
object presenterWebView = null; object presenterWebView = null;
if (presenter != null && androidWebViewType.IsAssignableFrom (presenterType)) { if (presenter != null && androidWebViewType.IsAssignableFrom(presenterType))
{
presenterWebView = presenter; presenterWebView = presenter;
} }
if (presenterWebView == null) { if (presenterWebView == null)
throw new ArgumentException ("Presenter must be a WebView", nameof(presenter));
}
var m = androidWebViewType.GetMethod ("LoadUrl", BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, new Type[] { typeof(string) }, null);
m.Invoke (presenterWebView, new object[] { url });
}
static void OpenBrowserOniOS (string url, object presenter)
{ {
var presenterType = GetObjectType (presenter); throw new ArgumentException("Presenter must be a WebView", nameof(presenter));
}
var m = androidWebViewType.GetMethod("LoadUrl", BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, new Type[] { typeof(string) }, null);
m.Invoke(presenterWebView, new object[] { url });
}
static void OpenBrowserOniOS(string url, object presenter)
{
var presenterType = GetObjectType(presenter);
// //
// Find a presenter view controller // Find a presenter view controller
@ -80,74 +87,94 @@ namespace Ooui
// 3. Create a window? // 3. Create a window?
// //
object presenterViewController = null; object presenterViewController = null;
if (presenter != null && iosUIViewControllerType.IsAssignableFrom (presenterType)) { if (presenter != null && iosUIViewControllerType.IsAssignableFrom(presenterType))
{
presenterViewController = presenter; presenterViewController = presenter;
} }
if (presenterViewController == null) { if (presenterViewController == null)
var app = iosUIApplicationType.GetProperty ("SharedApplication").GetValue (null, null); {
var window = iosUIApplicationType.GetProperty ("KeyWindow").GetValue (app, null); var app = iosUIApplicationType.GetProperty("SharedApplication").GetValue(null, null);
if (window != null) { var window = iosUIApplicationType.GetProperty("KeyWindow").GetValue(app, null);
var rvc = window.GetType ().GetProperty ("RootViewController").GetValue (window, null); if (window != null)
if (rvc != null) { {
var pvc = rvc.GetType ().GetProperty ("PresentedViewController").GetValue (rvc, null); var rvc = window.GetType().GetProperty("RootViewController").GetValue(window, null);
if (rvc != null)
{
var pvc = rvc.GetType().GetProperty("PresentedViewController").GetValue(rvc, null);
presenterViewController = pvc ?? rvc; presenterViewController = pvc ?? rvc;
} }
} }
} }
if (presenterViewController == null) { if (presenterViewController == null)
throw new InvalidOperationException ("Cannot find a view controller from which to present"); {
throw new InvalidOperationException("Cannot find a view controller from which to present");
} }
// //
// Create the browser // Create the browser
// //
var browserVC = Activator.CreateInstance (iosUIViewControllerType); var browserVC = Activator.CreateInstance(iosUIViewControllerType);
var browserV = Activator.CreateInstance (iosUIWebViewType); var browserV = Activator.CreateInstance(iosUIWebViewType);
var nsUrl = iosNSUrl.GetMethod ("FromString").Invoke (null, new object[] { url }); var nsUrl = iosNSUrl.GetMethod("FromString").Invoke(null, new object[] { url });
var nsUrlRequest = iosNSUrlRequest.GetMethod ("FromUrl").Invoke (null, new object[] { nsUrl }); var nsUrlRequest = iosNSUrlRequest.GetMethod("FromUrl").Invoke(null, new object[] { nsUrl });
iosUIWebViewType.GetMethod ("LoadRequest").Invoke (browserV, new object[] { nsUrlRequest }); iosUIWebViewType.GetMethod("LoadRequest").Invoke(browserV, new object[] { nsUrlRequest });
iosUIViewControllerType.GetProperty ("View").SetValue (browserVC, browserV, null); iosUIViewControllerType.GetProperty("View").SetValue(browserVC, browserV, null);
var m = iosUIViewControllerType.GetMethod ("PresentViewController"); var m = iosUIViewControllerType.GetMethod("PresentViewController");
// Console.WriteLine (presenterViewController); // Console.WriteLine (presenterViewController);
// Console.WriteLine (browserVC); // Console.WriteLine (browserVC);
m.Invoke (presenterViewController, new object[] { browserVC, false, null }); m.Invoke(presenterViewController, new object[] { browserVC, false, null });
} }
static Type GetObjectType (object o) static Type GetObjectType(object o)
{ {
var t = typeof (object); var t = typeof(object);
if (o is IReflectableType rt) { if (o is IReflectableType rt)
t = rt.GetTypeInfo ().AsType (); {
t = rt.GetTypeInfo().AsType();
} }
else if (o != null) { else if (o != null)
t = o.GetType (); {
t = o.GetType();
} }
return t; return t;
} }
static Process StartBrowserProcess (string url) static Process StartBrowserProcess(string url)
{ {
var cmd = url; var cmd = url;
var args = ""; var args = string.Empty;
var osv = Environment.OSVersion; var osv = Environment.OSVersion;
if (osv.Platform == PlatformID.Unix) {
if (osv.Platform == PlatformID.Unix)
{
cmd = "open"; cmd = "open";
args = url; args = url;
} }
var platform = (int)Environment.OSVersion.Platform;
var isWindows = ((platform != 4) && (platform != 6) && (platform != 128));
if (isWindows)
{
cmd = "explorer.exe";
args = url;
}
// var vs = Environment.GetEnvironmentVariables (); // var vs = Environment.GetEnvironmentVariables ();
// foreach (System.Collections.DictionaryEntry kv in vs) { // foreach (System.Collections.DictionaryEntry kv in vs) {
// System.Console.WriteLine($"K={kv.Key}, V={kv.Value}"); // System.Console.WriteLine($"K={kv.Key}, V={kv.Value}");
// } // }
// Console.WriteLine ($"Process.Start {cmd} {args}"); // Console.WriteLine ($"Process.Start {cmd} {args}");
return Process.Start (cmd, args);
return Process.Start(cmd, args);
} }
} }
} }