cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject git commit: CB-5357 Multistage exit app fires events pause->exit->onunload before exiting completely. WP8 only
Date Tue, 14 Jan 2014 00:16:37 GMT
Updated Branches:
  refs/heads/master 3b7e4acf5 -> af726e211


CB-5357 Multistage exit app fires events pause->exit->onunload before exiting completely.
WP8 only


Project: http://git-wip-us.apache.org/repos/asf/cordova-wp8/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-wp8/commit/af726e21
Tree: http://git-wip-us.apache.org/repos/asf/cordova-wp8/tree/af726e21
Diff: http://git-wip-us.apache.org/repos/asf/cordova-wp8/diff/af726e21

Branch: refs/heads/master
Commit: af726e211f7c797ef6152b8a921d34af2f764119
Parents: 3b7e4ac
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Mon Jan 13 16:14:23 2014 -0800
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Mon Jan 13 16:14:23 2014 -0800

----------------------------------------------------------------------
 wp8/template/cordovalib/CordovaView.xaml.cs | 98 ++++++++----------------
 1 file changed, 34 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/af726e21/wp8/template/cordovalib/CordovaView.xaml.cs
----------------------------------------------------------------------
diff --git a/wp8/template/cordovalib/CordovaView.xaml.cs b/wp8/template/cordovalib/CordovaView.xaml.cs
index b5d880e..5220e3c 100644
--- a/wp8/template/cordovalib/CordovaView.xaml.cs
+++ b/wp8/template/cordovalib/CordovaView.xaml.cs
@@ -77,6 +77,8 @@ namespace WPCordovaClassLib
 
         private ConfigHandler configHandler;
 
+        protected bool IsExiting = false;
+
         private Dictionary<string, IBrowserDecorator> browserDecorators;
 
         public System.Windows.Controls.Grid _LayoutRoot
@@ -214,8 +216,7 @@ namespace WPCordovaClassLib
 
         void AppDeactivated(object sender, DeactivatedEventArgs e)
         {
-            Debug.WriteLine("INFO: AppDeactivated");
-
+            Debug.WriteLine("INFO: AppDeactivated because " + e.Reason);
             try
             {
                 CordovaBrowser.InvokeScript("eval", new string[] { "cordova.fireDocumentEvent('pause');"
});
@@ -246,6 +247,7 @@ namespace WPCordovaClassLib
 
         void CordovaBrowser_Loaded(object sender, RoutedEventArgs e)
         {
+
             this.bmHelper.ScrollDisabled = this.DisableBouncyScrolling;
 
             if (DesignerProperties.IsInDesignTool)
@@ -287,67 +289,6 @@ namespace WPCordovaClassLib
                     }
                 }
 
-                /*
-                 * 11/08/12 Ruslan Kokorev
-                 * Copying files to isolated storage is no more required in WP8. WebBrowser
control now works with files located in XAP.
-                */
-
-                //StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("CordovaSourceDictionary.xml",
UriKind.Relative));
-
-                //if (streamInfo != null)
-                //{
-                //    StreamReader sr = new StreamReader(streamInfo.Stream);
-                //    //This will Read Keys Collection for the xml file
-
-                //    XDocument document = XDocument.Parse(sr.ReadToEnd());
-
-                //    var files = from results in document.Descendants("FilePath")
-                //                select new
-                //                {
-                //                    path = (string)results.Attribute("Value")
-                //                };
-                //    StreamResourceInfo fileResourceStreamInfo;
-
-                //    using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
-                //    {
-
-                //        foreach (var file in files)
-                //        {
-                //            fileResourceStreamInfo = Application.GetResourceStream(new
Uri(file.path, UriKind.Relative));
-
-                //            if (fileResourceStreamInfo != null)
-                //            {
-                //                using (BinaryReader br = new BinaryReader(fileResourceStreamInfo.Stream))
-                //                {
-                //                    byte[] data = br.ReadBytes((int)fileResourceStreamInfo.Stream.Length);
-
-                //                    string strBaseDir = AppRoot + file.path.Substring(0,
file.path.LastIndexOf(System.IO.Path.DirectorySeparatorChar));
-
-                //                    if (!appStorage.DirectoryExists(strBaseDir))
-                //                    {
-                //                        Debug.WriteLine("INFO: Creating Directory :: "
+ strBaseDir);
-                //                        appStorage.CreateDirectory(strBaseDir);
-                //                    }
-
-                //                    // This will truncate/overwrite an existing file, or
-                //                    using (IsolatedStorageFileStream outFile = appStorage.OpenFile(AppRoot
+ file.path, FileMode.Create))
-                //                    {
-                //                        Debug.WriteLine("INFO: Writing data for " + AppRoot
+ file.path + " and length = " + data.Length);
-                //                        using (var writer = new BinaryWriter(outFile))
-                //                        {
-                //                            writer.Write(data);
-                //                        }
-                //                    }
-                //                }
-                //            }
-                //            else
-                //            {
-                //                Debug.WriteLine("ERROR: Failed to write file :: " + file.path
+ " did you forget to add it to the project?");
-                //            }
-                //        }
-                //    }
-                //}
-
                 CordovaBrowser.Navigate(StartPageUri);
                 IsBrowserInitialized = true;
                 AttachHardwareButtonHandlers();
@@ -417,6 +358,14 @@ namespace WPCordovaClassLib
 
         void CordovaBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs
e)
         {
+            if (IsExiting)
+            {
+                // Special case, we navigate to about:blank when we are about to exit.
+                IsolatedStorageSettings.ApplicationSettings.Save();
+                Application.Current.Terminate();
+                return;
+            }
+
             Debug.WriteLine("CordovaBrowser_LoadCompleted");
             string[] autoloadPlugs = this.configHandler.AutoloadPlugins;
             foreach (string plugName in autoloadPlugs)
@@ -424,8 +373,8 @@ namespace WPCordovaClassLib
                 //nativeExecution.ProcessCommand(commandCallParams);
             }
 
+            // send js code to fire ready event
             string nativeReady = "(function(){ cordova.require('cordova/channel').onNativeReady.fire()})();";
-
             try
             {
                 CordovaBrowser.InvokeScript("execScript", new string[] { nativeReady });
@@ -434,6 +383,16 @@ namespace WPCordovaClassLib
             {
                 Debug.WriteLine("Error calling js to fire nativeReady event. Did you include
cordova.js in your html script tag?");
             }
+            // attach js code to dispatch exitApp 
+            string appExitHandler = "(function(){navigator.app = navigator.app || {}; navigator.app.exitApp=
function(){cordova.exec(null,null,'CoreEvents','__exitApp',[]); }})();";
+            try
+            {
+                CordovaBrowser.InvokeScript("execScript", new string[] { appExitHandler });
+            }
+            catch (Exception /*ex*/)
+            {
+                Debug.WriteLine("Error calling js to add appExit funtion.");
+            }
 
             if (this.CordovaBrowser.Opacity < 1)
             {
@@ -491,6 +450,17 @@ namespace WPCordovaClassLib
                         string arg0 = JsonHelper.Deserialize<string[]>(commandCallParams.Args)[0];
                         this.OverrideBackButton = (arg0 != null && arg0.Length >
0 && arg0.ToLower() == "true");
                         break;
+                    case "__exitapp":
+                        Debug.WriteLine("Received exitApp command from javascript, app will
now exit.");
+                        CordovaBrowser.InvokeScript("eval", new string[] { "cordova.fireDocumentEvent('pause');"
});
+                        CordovaBrowser.InvokeScript("eval", new string[] { "setTimeout(function(){
cordova.fireDocumentEvent('exit'); cordova.exec(null,null,'CoreEvents','__finalexit',[]);
},0);" });
+                        break;
+                    case "__finalexit":
+                        IsExiting = true;
+                        // hide the browser to prevent white flashes, since about:blank seems
to always be white
+                        CordovaBrowser.Opacity = 0d; 
+                        CordovaBrowser.Navigate(new Uri("about:blank", UriKind.Absolute));
+                        break;
                 }
             }
             else


Mime
View raw message