Add WiFi activate/deactivate/scan 49/282449/1
authorchaehee-hong <chaehee.hong@samsung.com>
Tue, 4 Oct 2022 03:01:45 +0000 (12:01 +0900)
committerchaehee-hong <chaehee.hong@samsung.com>
Tue, 4 Oct 2022 03:01:48 +0000 (12:01 +0900)
Change-Id: I8db907f173f227cc32274a23e306c89773b7af01

SettingWiFi/SettingWiFi/AP.cs [new file with mode: 0755]
SettingWiFi/SettingWiFi/Device.cs [deleted file]
SettingWiFi/SettingWiFi/WiFi.cs [new file with mode: 0755]
SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs
packaging/org.tizen.cssetting-wifi-1.0.0.tpk

diff --git a/SettingWiFi/SettingWiFi/AP.cs b/SettingWiFi/SettingWiFi/AP.cs
new file mode 100755 (executable)
index 0000000..dd45e47
--- /dev/null
@@ -0,0 +1,145 @@
+using System;\r
+using System.ComponentModel;\r
+using System.Collections.Generic;\r
+using System.Collections.ObjectModel;\r
+using Tizen;\r
+using Tizen.NUI.BaseComponents;\r
+using Tizen.NUI.Components;\r
+using Tizen.NUI.Binding;\r
+using Tizen.Network.WiFi;\r
+\r
+\r
+namespace SettingWiFi\r
+{\r
+    public class AP : INotifyPropertyChanged\r
+    {\r
+        internal static readonly string LogTag = "SettingWiFi";\r
+        string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png";\r
+        private string essid;\r
+        private string state;\r
+        public event PropertyChangedEventHandler PropertyChanged;\r
+\r
+        private void OnPropertyChanged(string propertyName)\r
+        {\r
+            Log.Debug(LogTag, "OnPropertyChanged");\r
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));\r
+        }\r
+\r
+        public AP(string apEssid, string apState)\r
+        {\r
+            essid = apEssid;\r
+            state = apState;\r
+        }\r
+\r
+        public string Essid\r
+        {\r
+            get\r
+            {\r
+                return essid;\r
+            }\r
+            set\r
+            {\r
+                essid = value;\r
+                OnPropertyChanged("Essid");\r
+            }\r
+        }\r
+\r
+        public string ImageUrl\r
+        {\r
+            get\r
+            {\r
+                return iconDir;\r
+            }\r
+        }\r
+\r
+        public string State\r
+        {\r
+            get\r
+            {\r
+                return state;\r
+            }\r
+            set\r
+            {\r
+                state = value;\r
+                OnPropertyChanged("State");\r
+            }\r
+        }\r
+    };\r
+\r
+\r
+    public class DeviceCollection : ObservableCollection<AP>\r
+    {\r
+        internal static readonly string LogTag = "SettingWiFi";\r
+        private string title;\r
+\r
+        public DeviceCollection(string groupTitle)\r
+        {\r
+            title = groupTitle;\r
+            this.UpdateDevices(null);\r
+        }\r
+\r
+        public string Title\r
+        {\r
+            get\r
+            {\r
+                return title;\r
+            }\r
+            set\r
+            {\r
+                title = value;\r
+                OnPropertyChanged(new PropertyChangedEventArgs("Title"));\r
+            }\r
+        }\r
+\r
+        public void UpdateDevices(List<AP> apList)\r
+        {\r
+            Log.Debug(LogTag, "UpdateDevices");\r
+            if (apList == null)\r
+            {\r
+                this.Add(new AP("", ""));\r
+                return;\r
+            }\r
+            // Clear method have some issue about asynchronous actions,\r
+            // so call Remove for all item is recommanded.\r
+            while (this.Count > 0)\r
+            {\r
+                this.RemoveAt(this.Count - 1);\r
+            }\r
+\r
+            foreach (var item in apList)\r
+            {\r
+                this.Add(new AP(item.Essid, item.State));\r
+            }\r
+        }\r
+\r
+        public void RemoveDevices()\r
+        {\r
+            while (this.Count > 0)\r
+            {\r
+                this.RemoveAt(this.Count - 1);\r
+            }\r
+        }\r
+    }\r
+\r
+    public class APSource : ObservableCollection<DeviceCollection>\r
+    {\r
+        internal static readonly string LogTag = "SettingWiFi";\r
+        private DeviceCollection available;\r
+        public APSource()\r
+        {\r
+            Log.Debug(LogTag, "new APSource");\r
+            available = new DeviceCollection("Available networks");\r
+            this.Add(available);\r
+            Log.Debug(LogTag, "Add DeviceCollection");\r
+        }\r
+        public void UpdateDevices(List<AP> apList)\r
+        {\r
+            available.UpdateDevices(apList);\r
+        }\r
+\r
+        public void RemoveDevices()\r
+        {\r
+            available.RemoveDevices();\r
+        }\r
+    }\r
+}
\ No newline at end of file
diff --git a/SettingWiFi/SettingWiFi/Device.cs b/SettingWiFi/SettingWiFi/Device.cs
deleted file mode 100755 (executable)
index 7862484..0000000
+++ /dev/null
@@ -1,150 +0,0 @@
-using System;\r
-using System.Collections.Generic;\r
-using System.Collections.ObjectModel;\r
-using System.ComponentModel;\r
-using Tizen.NUI.BaseComponents;\r
-using Tizen.NUI.Binding;\r
-using Tizen.NUI.Components;\r
-\r
-namespace SettingWiFi\r
-{\r
-    public class Device : INotifyPropertyChanged\r
-    {\r
-        string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png";\r
-        private string name;\r
-        private bool connected;\r
-        private bool registered;\r
-        public event PropertyChangedEventHandler PropertyChanged;\r
-\r
-        private void OnPropertyChanged(string propertyName)\r
-        {\r
-\r
-            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));\r
-        }\r
-\r
-        public Device(string deviceName, bool con, bool reg)\r
-        {\r
-            name = deviceName;\r
-            connected = con;\r
-            registered = reg;\r
-        }\r
-\r
-        public string Name\r
-        {\r
-            get\r
-            {\r
-                return name;\r
-            }\r
-            set\r
-            {\r
-                name = value;\r
-                OnPropertyChanged("Name");\r
-            }\r
-        }\r
-\r
-        public string ImageUrl\r
-        {\r
-            get\r
-            {\r
-                return iconDir;\r
-            }\r
-        }\r
-\r
-        public bool Connected\r
-        {\r
-            get\r
-            {\r
-                return connected;\r
-            }\r
-            set\r
-            {\r
-                connected = value;\r
-                OnPropertyChanged("Connected");\r
-            }\r
-        }\r
-        public bool Registered\r
-        {\r
-            get\r
-            {\r
-                return registered;\r
-            }\r
-            set\r
-            {\r
-                registered = value;\r
-                OnPropertyChanged("Registered");\r
-            }\r
-        }\r
-    };\r
-\r
-    public class DeviceCollection : ObservableCollection<Device>\r
-    {\r
-        string[] devicePool = {\r
-           "Galaxy Buds2 Pro",\r
-           "Galaxy Fold 4",\r
-           "Galaxy Tab S8+",\r
-           "Galaxy Buds2",\r
-           "Galaxy Watch 5",\r
-           "[TV] Samsung 65",\r
-           "Galaxy Fit2",\r
-           "Sony WH-100XM5",\r
-           "Logitech MX Vertical",\r
-           "Ioniq 5",\r
-           "Galaxy Home Mini",\r
-           "Samsung Family Hub",\r
-           "Galaxy Book Pro2",\r
-        };\r
-\r
-        private string title;\r
-\r
-        public DeviceCollection(string groupTitle)\r
-        {\r
-            title = groupTitle;\r
-            UpdateDevices();\r
-        }\r
-\r
-        public string Title\r
-        {\r
-            get\r
-            {\r
-                return title;\r
-            }\r
-            set\r
-            {\r
-                title = value;\r
-                OnPropertyChanged(new PropertyChangedEventArgs("Title"));\r
-            }\r
-        }\r
-\r
-        public void UpdateDevices()\r
-        {\r
-            // Clear method have some issue about asynchronous actions,\r
-            // so call Remove for all item is recommanded.\r
-            while (this.Count > 0)\r
-            {\r
-                this.RemoveAt(this.Count - 1);\r
-            }\r
-            Random rand = new Random();\r
-            int count = rand.Next(13);\r
-\r
-            for (int i = 0; i < count; i++)\r
-            {\r
-                this.Add(new Device(devicePool[rand.Next(13)], false, false));\r
-            }\r
-        }\r
-    }\r
-\r
-    public class DeviceSource : ObservableCollection<DeviceCollection>\r
-    {\r
-        private DeviceCollection available;\r
-        public DeviceSource()\r
-        {\r
-            available = new DeviceCollection("Available networks");\r
-            this.Add(available);\r
-        }\r
-\r
-        public void UpdateDevices()\r
-        {\r
-            available.UpdateDevices();\r
-        }\r
-    }\r
-}
\ No newline at end of file
diff --git a/SettingWiFi/SettingWiFi/WiFi.cs b/SettingWiFi/SettingWiFi/WiFi.cs
new file mode 100755 (executable)
index 0000000..3d47b40
--- /dev/null
@@ -0,0 +1,109 @@
+using System;\r
+using System.Collections.Generic;\r
+using System.Text;\r
+using System.Threading.Tasks;\r
+using Tizen;\r
+using Tizen.Network.WiFi;\r
+\r
+namespace SettingWiFi\r
+{\r
+    public class WiFi\r
+    {\r
+        internal static readonly string LogTag = "SettingWiFi";\r
+        IEnumerable<WiFiAP> apList = null;\r
+\r
+        public async Task Activate()\r
+        {\r
+            try\r
+            {\r
+                await WiFiManager.ActivateAsync();\r
+                if (WiFiManager.IsActive)\r
+                {\r
+                    Log.Debug(LogTag, "WiFi is activated");\r
+                    apList = new List<WiFiAP>();\r
+                }\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                Log.Debug(LogTag, "Exception occurred" + e.ToString());\r
+            }\r
+        }\r
+\r
+        public Task Deactivate()\r
+        {\r
+            return WiFiManager.DeactivateAsync();\r
+        }\r
+\r
+        public async Task Scan()\r
+        {\r
+            try\r
+            {\r
+                await WiFiManager.ScanAsync();\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                Log.Debug(LogTag, "Exception occurred" + e.ToString());\r
+            }\r
+                \r
+        }\r
+\r
+        public List<AP> GetScanResult()\r
+        {\r
+            try\r
+            {\r
+                apList = WiFiManager.GetFoundAPs();\r
+\r
+                List<AP> apInfoList = new List<AP>();\r
+                foreach (var item in apList)\r
+                {\r
+                    Log.Debug(LogTag, "AP name: " + item.NetworkInformation.Essid);\r
+                    //Log.Debug(LogTag, "Connection state: " + item.NetworkInformation.ConnectionState);\r
+                    apInfoList.Add(new AP(item.NetworkInformation.Essid, item.NetworkInformation.ConnectionState.ToString()));\r
+                }\r
+\r
+                return apInfoList;\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                Log.Debug(LogTag, "Exception occurred" + e.ToString());\r
+            }\r
+\r
+            return null;\r
+        }\r
+\r
+        public async Task Connect(string essid)\r
+        {\r
+            WiFiAP ap = null;\r
+            apList = WiFiManager.GetFoundAPs();\r
+\r
+            foreach (var item in apList)\r
+            {\r
+                if (item.NetworkInformation.Essid.Equals(essid))\r
+                {\r
+                    ap = item;\r
+                    break;\r
+                }\r
+            }\r
+\r
+            try\r
+            {\r
+                await ap.ConnectAsync();\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                Log.Debug(LogTag, "Fail to connect" + e.ToString());\r
+            }\r
+        }\r
+\r
+\r
+        public bool IsActive()\r
+        {\r
+            if (WiFiManager.IsActive)\r
+            {\r
+                return true;\r
+            }\r
+\r
+            return false;\r
+        }\r
+    }\r
+}\r
index 2e3a29494e2c32cb29a983cc7fbd0d46cb974ba9..ca3dfbbffc49bfcba3f1826a418f261c5710b5cd 100755 (executable)
@@ -1,24 +1,21 @@
-using System;
-using Tizen;
-using Tizen.Applications;
-using Tizen.NUI;
-using Tizen.NUI.Accessibility;
-using Tizen.NUI.BaseComponents;
-using Tizen.NUI.Binding;
-using Tizen.NUI.Components;
-using Tizen.Network.WiFi;
-
-
-namespace SettingWiFi
-{
-    internal class WidgetSettingWiFi : Widget
-    {
-        internal static readonly string LogTag = "SettingWiFi";
-        Navigator navigator;
-        ContentPage page;
-        View rootView;
-        CollectionView deviceView;
-        DeviceSource deviceSource;\r
+using System;\r
+using System.Collections.Generic;\r
+using Tizen;\r
+using Tizen.Applications;\r
+using Tizen.NUI;\r
+using Tizen.NUI.Accessibility;\r
+using Tizen.NUI.BaseComponents;\r
+using Tizen.NUI.Binding;\r
+using Tizen.NUI.Components;\r
+\r
+\r
+namespace SettingWiFi\r
+{\r
+    internal class WidgetSettingWiFi : Widget\r
+    {\r
+        internal static readonly string LogTag = "SettingWiFi";\r
+        WiFi wifi;\r
+        APSource apSource;\r
 \r
         private RecyclerViewItem GetHeader()\r
         {\r
@@ -32,51 +29,35 @@ namespace SettingWiFi
                 HeightSpecification = LayoutParamPolicies.WrapContent,\r
             };\r
 \r
-            var wifiOnOff = new DefaultLinearItem()\r
+            var toggle = new DefaultLinearItem()\r
             {\r
                 WidthSpecification = LayoutParamPolicies.MatchParent,\r
                 Text = "Wi-Fi",\r
+                IsSelectable = false,\r
             };\r
-            wifiOnOff.Label.HorizontalAlignment = HorizontalAlignment.Begin;\r
-            var wifiOnOffSwith = new Switch();\r
-            wifiOnOffSwith.SelectedChanged += (object obj, SelectedChangedEventArgs ev) =>\r
-            {\r
-                Tizen.Log.Debug("NUI", "Wi-Fi On&Off Switch changed");\r
-                if (obj != null && obj is Switch switchButton)\r
-                {\r
-                    var parent = switchButton.GetParent();\r
-                    if (parent is DefaultLinearItem item)\r
-                    {\r
-                        item.IsSelected = switchButton.IsSelected;\r
-                    }\r
-                }\r
-            };\r
-            wifiOnOff.Extra = wifiOnOffSwith;\r
-            /*\r
-            var myDeviceName = new DefaultLinearItem()\r
-            {\r
-                WidthSpecification = LayoutParamPolicies.MatchParent,\r
-                Text = "Tizen",\r
-            };\r
-            myDeviceName.Label.HorizontalAlignment = HorizontalAlignment.Begin;\r
-            */\r
-            header.Add(wifiOnOff);\r
-            //header.Add(myDeviceName);\r
+            toggle.Label.HorizontalAlignment = HorizontalAlignment.Begin;\r
+\r
+            var onOffSwitch = new Switch();\r
+            toggle.Extra = onOffSwitch;\r
+            onOffSwitch.SelectedChanged += OnSelectedChanged;\r
+            header.Add(toggle);\r
 \r
             return header;\r
-        }
-
-        protected override void OnCreate(string contentInfo, Window window)
-        {
-            Bundle bundle = Bundle.Decode(contentInfo);
-            navigator = window.GetDefaultNavigator();
-
+        }\r
+\r
+        protected override void OnCreate(string contentInfo, Window window)\r
+        {\r
+            Bundle bundle = Bundle.Decode(contentInfo);\r
+            Navigator navigator = window.GetDefaultNavigator();\r
+            wifi = new WiFi();\r
+            apSource = new APSource();\r
+\r
             var appBar = new AppBar()\r
             {\r
                 Title = "Wi-Fi",\r
-            };
-
-            rootView = new View()\r
+            };\r
+\r
+            View rootView = new View()\r
             {\r
                 Layout = new LinearLayout()\r
                 {\r
@@ -85,21 +66,18 @@ namespace SettingWiFi
                 },\r
                 WidthSpecification = LayoutParamPolicies.MatchParent,\r
                 HeightSpecification = LayoutParamPolicies.MatchParent,\r
-            };
-
-            deviceSource = new DeviceSource();\r
+            };\r
 \r
-            deviceView = new CollectionView()\r
+            CollectionView deviceView = new CollectionView()\r
             {\r
-                ItemsSource = deviceSource,\r
+                ItemsSource = apSource,\r
                 ItemsLayouter = new LinearLayouter(),\r
                 ItemTemplate = new DataTemplate(() =>\r
                 {\r
                     DefaultLinearItem item = new DefaultLinearItem();\r
-                    //Set Width Specification as MatchParent to fit the Item width with parent View.\r
                     item.WidthSpecification = LayoutParamPolicies.MatchParent;\r
                     //Decorate Label\r
-                    item.Label.SetBinding(TextLabel.TextProperty, "Name");\r
+                    item.Label.SetBinding(TextLabel.TextProperty, "Essid");\r
                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;\r
                     //Decorate Icon\r
                     item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");\r
@@ -111,7 +89,6 @@ namespace SettingWiFi
                 GroupHeaderTemplate = new DataTemplate(() =>\r
                 {\r
                     DefaultTitleItem group = new DefaultTitleItem();\r
-                    //Set Width Specification as MatchParent to fit the Item width with parent View.\r
                     group.WidthSpecification = LayoutParamPolicies.MatchParent;\r
 \r
                     group.Label.SetBinding(TextLabel.TextProperty, "Title");\r
@@ -119,48 +96,100 @@ namespace SettingWiFi
 \r
                     return group;\r
                 }),\r
-                Header = GetHeader(),\r
                 IsGrouped = true,\r
                 ScrollingDirection = ScrollableBase.Direction.Vertical,\r
                 WidthSpecification = LayoutParamPolicies.MatchParent,\r
                 HeightSpecification = LayoutParamPolicies.MatchParent,\r
                 SelectionMode = ItemSelectionMode.Single,\r
             };\r
-            deviceView.SelectionChanged += DeviceConnectedEvent;
-
-            deviceSource.UpdateDevices();
-            rootView.Add(deviceView);
-
-            page = new ContentPage()\r
+            deviceView.SelectionChanged += DeviceConnectedEvent;\r
+\r
+            var header = GetHeader();\r
+            var scanButton = new Button()\r
+            {\r
+                Text = "Scan",\r
+                WidthSpecification = 300,\r
+                HeightSpecification = 80,\r
+            };\r
+            scanButton.Clicked += OnScanButtonClicked;\r
+\r
+            rootView.Add(header);\r
+            rootView.Add(deviceView);\r
+            rootView.Add(scanButton);\r
+\r
+            ContentPage page = new ContentPage()\r
             {\r
                 AppBar = appBar,\r
                 Content = rootView,\r
             };\r
 \r
-            navigator.Push(page);
+            navigator.Push(page);\r
+        }\r
+\r
+\r
+        /* Call WiFi */\r
+        private async void OnSelectedChanged(object sender, SelectedChangedEventArgs e)\r
+        {\r
+            if (e.IsSelected)\r
+            {\r
+                if (!IsWiFiActive())\r
+                {\r
+                    await wifi.Activate();\r
+                }\r
+                ScanAP();\r
+            }\r
+            else\r
+            {\r
+                apSource.RemoveDevices();\r
+\r
+                if (IsWiFiActive())\r
+                {\r
+                    await wifi.Deactivate();\r
+                }\r
+            }\r
         }\r
-        public void DeviceConnectedEvent(object sender, SelectionChangedEventArgs ev)\r
+\r
+        private void OnScanButtonClicked(object sender, ClickedEventArgs e)\r
         {\r
-            Tizen.Log.Debug("NUI", "DeviceConnectedEvent called");\r
+            Log.Debug(LogTag, "OnScanButtonClicked");\r
+            ScanAP();\r
+        }\r
 \r
+        private bool IsWiFiActive()\r
+        {\r
+            return wifi.IsActive();\r
+        }\r
+        private async void ScanAP()\r
+        {\r
+            if (IsWiFiActive())\r
+            {\r
+                await wifi.Scan();\r
+                List<AP> apList = wifi.GetScanResult();\r
+                apSource.UpdateDevices(apList);\r
+            }\r
+        }\r
+\r
+        public async void DeviceConnectedEvent(object sender, SelectionChangedEventArgs ev)\r
+        {\r
             //SingleSelection Only have 1 or nil object in the list.\r
             foreach (object item in ev.PreviousSelection)\r
             {\r
                 if (item == null) break;\r
-                if (item is Device device)\r
+                if (item is AP device)\r
                 {\r
-                    device.Connected = false;\r
+                    // device.Connected = false;\r
                 }\r
             }\r
             foreach (object item in ev.CurrentSelection)\r
             {\r
                 if (item == null) break;\r
-                if (item is Device device)\r
+                if (item is AP device)\r
                 {\r
-                    device.Connected = true;\r
-                    device.Registered = true;\r
+                    await wifi.Connect(device.Essid);\r
+                    //  device.Connected = true;\r
+                    //  device.Registered = true;\r
                 }\r
             }\r
-        }
-    }
-}
+        }\r
+    }\r
+}\r
index 83470a8af314d0970f6a99f11020b9fdd0ebfd64..4696b3254130a7f34690bdb6db2f29d1488916e5 100755 (executable)
Binary files a/packaging/org.tizen.cssetting-wifi-1.0.0.tpk and b/packaging/org.tizen.cssetting-wifi-1.0.0.tpk differ