Allow to set static IP infomation 95/282995/2
authorcheoleun moon <chleun.moon@samsung.com>
Fri, 14 Oct 2022 09:03:07 +0000 (18:03 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Mon, 17 Oct 2022 01:55:28 +0000 (10:55 +0900)
Change-Id: I33a7d3fd51dccb76a950860d264464727a530656

SettingWiFi/SettingWiFi/controller/WiFi.cs
SettingWiFi/SettingWiFi/model/AP.cs
SettingWiFi/SettingWiFi/view/ApInfoSource.cs
SettingWiFi/SettingWiFi/view/InfoPage.cs

index c9b46996a151ecc121f5cccce527ca54bed265f7..70df92068f0c135975d01a5cb058b63845bd2cb1 100755 (executable)
@@ -4,8 +4,10 @@ using System.Text;
 using System.Threading.Tasks;\r
 using Tizen;\r
 using Tizen.Network.WiFi;\r
+using Tizen.Network.Connection;\r
 using SettingWiFi.res.locale;\r
 using static SettingWiFi.Logger;\r
+using System.Net;\r
 \r
 namespace SettingWiFi\r
 {\r
@@ -200,42 +202,54 @@ namespace SettingWiFi
 \r
                 //bool isPassphraseRequired = item.SecurityInformation.IsPassphraseRequired;\r
 \r
-                var securityType = item.SecurityInformation.SecurityType;\r
-                bool isWpsSupported = item.SecurityInformation.IsWpsSupported;\r
+                var ap = CreateAp(item);\r
 \r
+                Debug("AP Info. IPv4[" + ap.IPv4 + "] IPv6[" + ap.IPv6 + "] Mac[" + ap.Bssid + "] "\r
+                    + "ProxyAddress[" + ap.ProxyAddress + "] ProxyPort[" + ap.ProxyPort + "]");\r
 \r
-                WiFiState state = convertWiFiState(item.NetworkInformation.ConnectionState);\r
+                apInfoList.Add(ap);\r
+            }\r
 \r
-                var networkInfo = item.NetworkInformation;\r
-                var ap = new AP(item, networkInfo.Essid,\r
-                    state, securityType, isWpsSupported,\r
-                    networkInfo.Bssid, networkInfo.RssiLevel);\r
+            return apInfoList;\r
+        }\r
 \r
-                ap.IPv4 = networkInfo.IPv4Setting.IP.ToString();\r
-                ap.IPv6 = networkInfo.IPv6Setting.IP.ToString();\r
-                var proxy = networkInfo.ProxyAddress;\r
-                if (proxy.Contains(':'))\r
+        private AP CreateAp(WiFiAP wifiAp)\r
+        {\r
+            var securityType = wifiAp.SecurityInformation.SecurityType;\r
+            bool isWpsSupported = wifiAp.SecurityInformation.IsWpsSupported;\r
+            WiFiState state = convertWiFiState(wifiAp.NetworkInformation.ConnectionState);\r
+            var networkInfo = wifiAp.NetworkInformation;\r
+\r
+            var ap = new AP(wifiAp, networkInfo.Essid,\r
+                state, securityType, isWpsSupported,\r
+                networkInfo.Bssid, networkInfo.RssiLevel);\r
+\r
+            ap.IPv4 = networkInfo.IPv4Setting.IP.ToString();\r
+            ap.IPv6 = networkInfo.IPv6Setting.IP.ToString();\r
+            var proxy = networkInfo.ProxyAddress;\r
+            if (proxy.Contains(':'))\r
+            {\r
+                var result = proxy.Split(":");\r
+                ap.ProxyAddress = result[0];\r
+                int port = 0;\r
+                if (Int32.TryParse(result[1], out port))\r
                 {\r
-                    var result = proxy.Split(":");\r
-                    ap.ProxyAddress = result[0];\r
-                    int port = 0;\r
-                    if (Int32.TryParse(result[1], out port))\r
-                    {\r
-                        ap.ProxyPort = port;\r
-                    }\r
-                    else\r
-                    {\r
-                        ap.ProxyPort = -1;\r
-                    }\r
+                    ap.ProxyPort = port;\r
                 }\r
+                else\r
+                {\r
+                    ap.ProxyPort = -1;\r
+                }\r
+            }\r
 \r
-                Debug("AP Info. IPv4[" + ap.IPv4 + "] IPv6[" + ap.IPv6 + "] Mac[" + ap.Bssid + "] "\r
-                    + "ProxyAddress[" + ap.ProxyAddress + "] ProxyPort[" + ap.ProxyPort + "]");\r
+            ap.StaticIPConfig = (networkInfo.IPv4Setting.IPConfigType == IPConfigType.Static);\r
 \r
-                apInfoList.Add(ap);\r
-            }\r
+            return ap;\r
+        }\r
 \r
-            return apInfoList;\r
+        public void UpdateApInfo(AP ap)\r
+        {\r
+            ap.ApHandle.Update();\r
         }\r
 \r
         public List<AP> GetSpecificAPList()\r
@@ -332,6 +346,91 @@ namespace SettingWiFi
 \r
             return wpsPin;\r
         }\r
+\r
+        public void UpateIpConfigMethod(AP ap, bool isStaticIpConfig)\r
+        {\r
+            if (isStaticIpConfig)\r
+            {\r
+                ap.ApHandle.NetworkInformation.IPv4Setting.IPConfigType = IPConfigType.Static;\r
+                ap.ApHandle.NetworkInformation.IPv6Setting.IPConfigType = IPConfigType.Static;\r
+            }\r
+            else\r
+            {\r
+                ap.ApHandle.NetworkInformation.IPv4Setting.IPConfigType = IPConfigType.Dynamic;\r
+                ap.ApHandle.NetworkInformation.IPv6Setting.IPConfigType = IPConfigType.Dynamic;\r
+            }\r
+        }\r
+\r
+        public bool UpdateIPAddress(AP ap, string ip)\r
+        {\r
+            IPAddress ipAddress;\r
+            if (IPAddress.TryParse(ip, out ipAddress) == false)\r
+            {\r
+                Error("IP address parsing error");\r
+                return false;\r
+            }\r
+\r
+            ap.ApHandle.NetworkInformation.IPv4Setting.IP = ipAddress;\r
+            return true;\r
+        }\r
+\r
+        public bool UpdateSubnetMask(AP ap, string mask)\r
+        {\r
+            IPAddress ipAddress;\r
+            if (IPAddress.TryParse(mask, out ipAddress) == false)\r
+            {\r
+                Error("IP address parsing error");\r
+                return false;\r
+            }\r
+\r
+            ap.ApHandle.NetworkInformation.IPv4Setting.SubnetMask = ipAddress;\r
+            return true;\r
+        }\r
+\r
+        public bool UpdateGatewayAddress(AP ap, string address)\r
+        {\r
+            IPAddress ipAddress;\r
+            if (IPAddress.TryParse(address, out ipAddress) == false)\r
+            {\r
+                Error("IP address parsing error");\r
+                return false;\r
+            }\r
+\r
+            ap.ApHandle.NetworkInformation.IPv4Setting.Gateway = ipAddress;\r
+            return true;\r
+        }\r
+\r
+        public bool UpdateDns1(AP ap, string address)\r
+        {\r
+            IPAddress ipAddress;\r
+            if (IPAddress.TryParse(address, out ipAddress) == false)\r
+            {\r
+                Error("IP address parsing error");\r
+                return false;\r
+            }\r
+\r
+            ap.ApHandle.NetworkInformation.IPv4Setting.Dns1 = ipAddress;\r
+            return true;\r
+        }\r
+\r
+        public bool UpdateDns2(AP ap, string address)\r
+        {\r
+            IPAddress ipAddress;\r
+            if (IPAddress.TryParse(address, out ipAddress) == false)\r
+            {\r
+                Error("IP address parsing error");\r
+                return false;\r
+            }\r
+\r
+            ap.ApHandle.NetworkInformation.IPv4Setting.Dns2 = ipAddress;\r
+            return true;\r
+        }\r
+\r
+        public bool UpdateProxy(AP ap, string proxy)\r
+        {\r
+            ap.ApHandle.NetworkInformation.ProxyAddress = proxy;\r
+            return true;\r
+        }\r
     }\r
 \r
     internal class WiFiStateChangedEventArgs\r
index 8d5ae07b5c489c4eb316cefc6235479ad9f03563..fdf42ffeba16242e178a58f4632485ffd5309385 100755 (executable)
@@ -257,5 +257,35 @@ namespace SettingWiFi
             get;\r
             set;\r
         }\r
+\r
+        public bool StaticIPConfig\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public string SubnetMask\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public string GatewayAddress\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public string Dns1\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public string Dns2\r
+        {\r
+            get;\r
+            set;\r
+        }\r
     };\r
 }\r
index ed88f789dee0f3da870590eac3c8e79e0a1d6bf5..8ce95affae34806d132fa6cb0fdd8d102f6fa855 100644 (file)
+using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
+using SettingWiFi.res.locale;
 using static SettingWiFi.Logger;
 
 namespace SettingWiFi
 {
     public class ApInfoSource : ObservableCollection<ApInfoText>
     {
-        public ApInfoSource()
+        private WiFi mWifi;
+        private AP mAp;
+
+        private string mProxyAddress = "";
+
+        public ApInfoSource(WiFi wifi, AP ap)
         {
+            mWifi = wifi;
+            mAp = ap;
         }
 
-        internal void initialize(string ipv4, string ipv6, string bssid, string proxy, int proxyPort)
+        internal void ShowInfo(bool isStaticIpConfig, bool isConfigChanged)
         {
-            this.Add(new ApInfoText("IPv4", ipv4));
-            if (ipv6?.Length > 0)
+            if (isStaticIpConfig)
+            {
+                ShowStaticInfo(isConfigChanged);
+            }
+            else
+            {
+                ShowDynamicInfo(isConfigChanged);
+            }
+        }
+
+        private void ShowDynamicInfo(bool isConfigChanged)
+        {
+            Add(new ApInfoText(Resources.IDS_ST_BODY_IPV4, mAp.IPv4, null));
+            if (mAp.IPv6?.Length > 0)
+            {
+                Add(new ApInfoText(Resources.IDS_ST_BODY_IPV6, mAp.IPv6, null));
+            }
+            Add(new ApInfoText(Resources.IDS_WIFI_BODY_MAC_ADDRESS, mAp.Bssid, null));
+            if (mAp.ProxyAddress?.Length > 0)
             {
-                this.Add(new ApInfoText("IPv6", ipv6));
+                Add(new ApInfoText(Resources.IDS_ST_SBODY_PROXY_ADDRESS, mAp.ProxyAddress, null));
             }
-            this.Add(new ApInfoText("MAC address", bssid));
-            if (proxy?.Length > 0)
+            if (mAp.ProxyPort >= 0)
             {
-                this.Add(new ApInfoText("Proxy address", proxy));
+                Add(new ApInfoText(Resources.IDS_ST_SBODY_PROXY_PORT, "" + mAp.ProxyPort, null));
             }
-            if (proxyPort >= 0)
+        }
+
+        private void ShowStaticInfo(bool isConfigChanged)
+        {
+            if (isConfigChanged)
+            {
+                mAp.IPv4 = "";
+                mAp.SubnetMask = "";
+                mAp.GatewayAddress = "";
+                mAp.Dns1 = "";
+                mAp.Dns2 = "";
+                mAp.ProxyAddress = "";
+                mAp.ProxyPort = 0;
+            }
+
+            Add(new ApInfoText(Resources.IDS_WIFI_BODY_IP_ADDRESS, mAp.IPv4, UpdateIPAddress));
+            Add(new ApInfoText(Resources.IDS_WIFI_BODY_SUBNET_MASK, mAp.SubnetMask, UpdateSubnetMask));
+            Add(new ApInfoText(Resources.IDS_WIFI_BODY_GATEWAY_ADDRESS, mAp.GatewayAddress, UpdateGatewayAddress));
+            Add(new ApInfoText(Resources.IDS_WIFI_BODY_DNS_1, mAp.Dns1, UpdateDns1));
+            Add(new ApInfoText(Resources.IDS_WIFI_BODY_DNS_2, mAp.Dns2, UpdateDns2));
+            Add(new ApInfoText(Resources.IDS_WIFI_BODY_MAC_ADDRESS, mAp.Bssid, null));
+            Add(new ApInfoText(Resources.IDS_ST_SBODY_PROXY_ADDRESS, mAp.ProxyAddress, UpdateProxyAddress));
+            Add(new ApInfoText(Resources.IDS_ST_SBODY_PROXY_PORT, "" + mAp.ProxyPort, UpdateProxyPort));
+        }
+
+        internal IList<ApInfoText> GetItemList()
+        {
+            return this.Items;
+        }
+
+        public bool UpdateIPAddress(string ip)
+        {
+            return mWifi.UpdateIPAddress(mAp, ip);
+        }
+
+        public bool UpdateSubnetMask(string mask)
+        {
+            return mWifi.UpdateSubnetMask(mAp, mask);
+        }
+
+        public bool UpdateGatewayAddress(string address)
+        {
+            return mWifi.UpdateGatewayAddress(mAp, address);
+        }
+
+        public bool UpdateDns1(string address)
+        {
+            return mWifi.UpdateDns1(mAp, address);
+        }
+
+        public bool UpdateDns2(string address)
+        {
+            return mWifi.UpdateDns2(mAp, address);
+        }
+
+        public bool UpdateProxyAddress(string address)
+        {
+            mProxyAddress = address;
+            return true;
+        }
+
+        public bool UpdateProxyPort(string port)
+        {
+            int portNum;
+            if (Int32.TryParse(port, out portNum) == false)
             {
-                this.Add(new ApInfoText("Proxy port", "" + proxyPort));
+                Error("port parsing error");
+                return false;
             }
+            mProxyAddress += ":" + port;
+            return UpdateProxyAddress(mProxyAddress);
         }
     }
 
     public class ApInfoText : INotifyPropertyChanged
     {
-        private string infoTitle;
-        private string infoValue;
+        private string mInfoTitle;
+        private string mInfoValue;
+        public Predicate<string> mUpdater;
 
         public event PropertyChangedEventHandler PropertyChanged;
 
-        public ApInfoText(string title, string value)
+
+        public ApInfoText(string title, string value, Predicate<string> updater)
         {
             InfoTitle = title;
             InfoValue = value;
+            mUpdater = updater;
         }
 
         private void OnPropertyChanged(string propertyName)
@@ -51,11 +147,11 @@ namespace SettingWiFi
         {
             get
             {
-                return infoTitle;
+                return mInfoTitle;
             }
             set
             {
-                infoTitle = value;
+                mInfoTitle = value;
                 OnPropertyChanged("InfoTitle");
             }
         }
@@ -64,11 +160,11 @@ namespace SettingWiFi
         {
             get
             {
-                return infoValue;
+                return mInfoValue;
             }
             set
             {
-                infoValue = value;
+                mInfoValue = value;
                 OnPropertyChanged("InfoValue");
             }
         }
index 49d6e49a7ac1a3ad7738045ab5aef494a709b0ff..be1b1bf5cecdbe6e36a117fa3a07e1b46c493a0d 100644 (file)
@@ -13,11 +13,16 @@ namespace SettingWiFi
 {
     internal class InfoPage : ContentPage
     {
+        Switch mOnOffSwitch;
+        CollectionView mApInfoListView;
+
         private AP mAp;
         private WiFi mWifi;
 
         ApInfoSource mApInfoSource;
 
+        bool mIsOriginOnOffSwitchSelected;
+
         internal InfoPage(WiFi wifi)
         {
             mWifi = wifi;
@@ -49,10 +54,9 @@ namespace SettingWiFi
                 HeightSpecification = LayoutParamPolicies.MatchParent,
             };
 
-            mApInfoSource = new ApInfoSource();
-            mApInfoSource.initialize(ap.IPv4, ap.IPv6, ap.Bssid, ap.ProxyAddress, ap.ProxyPort);
+            mApInfoSource = new ApInfoSource(mWifi, mAp);
 
-            CollectionView apInfoListView = new CollectionView()
+            mApInfoListView = new CollectionView()
             {
                 ItemsSource = mApInfoSource,
                 ItemsLayouter = new LinearLayouter(),
@@ -61,9 +65,12 @@ namespace SettingWiFi
                 ScrollingDirection = ScrollableBase.Direction.Vertical,
                 WidthSpecification = LayoutParamPolicies.MatchParent,
                 HeightSpecification = LayoutParamPolicies.MatchParent,
-                SelectionMode = ItemSelectionMode.Single,
+                SelectionMode = mAp.StaticIPConfig ? ItemSelectionMode.Single : ItemSelectionMode.None,
             };
-            apInfoListView.SelectionChanged += OnInfoListViewSelected;
+            if (mAp.StaticIPConfig)
+            {
+                mApInfoListView.SelectionChanged += OnInfoListViewSelected;
+            }
 
             var header = GetHeader();
 
@@ -77,11 +84,13 @@ namespace SettingWiFi
             forgetButton.Clicked += OnForgetClicked;
 
             infoView.Add(header);
-            infoView.Add(apInfoListView);
+            infoView.Add(mApInfoListView);
             infoView.Add(forgetButton);
 
             AppBar = appBar;
             Content = infoView;
+
+            mApInfoSource.ShowInfo(mOnOffSwitch.IsSelected, false);
         }
 
         private void OnInfoListViewSelected(object sender, SelectionChangedEventArgs e)
@@ -139,17 +148,38 @@ namespace SettingWiFi
             };
             toggle.Label.HorizontalAlignment = HorizontalAlignment.Begin;
 
-            var onOffSwitch = new Switch()
+            mIsOriginOnOffSwitchSelected = mAp.StaticIPConfig;
+            mOnOffSwitch = new Switch()
             {
-                IsSelected = false,
+                IsSelected = mAp.StaticIPConfig,
             };
+            mOnOffSwitch.SelectedChanged += OnSwitchClicked;
 
-            toggle.Extra = onOffSwitch;
+            toggle.Extra = mOnOffSwitch;
             header.Add(toggle);
 
             return header;
         }
 
+        private void OnSwitchClicked(object sender, SelectedChangedEventArgs e)
+        {
+            if (e.IsSelected)
+            {
+                mApInfoListView.SelectionMode = ItemSelectionMode.Single;
+                mApInfoListView.SelectionChanged += OnInfoListViewSelected;
+            }
+            else
+            {
+                mApInfoListView.SelectionMode = ItemSelectionMode.None;
+                mApInfoListView.SelectionChanged -= OnInfoListViewSelected;
+            }
+
+            mApInfoSource.Clear();
+            mApInfoSource.ShowInfo(e.IsSelected, true);
+
+
+        }
+
         private ContentPage CreateForgetPage()
         {
             var forgetView = new View()
@@ -196,9 +226,34 @@ namespace SettingWiFi
 
         private void OnBackClicked(object source, ClickedEventArgs args)
         {
+            if (mIsOriginOnOffSwitchSelected != mOnOffSwitch.IsSelected)
+            {
+                UpdateApInfo(mOnOffSwitch.IsSelected);
+            }
             Navigator.Pop();
         }
 
+        private void UpdateApInfo(bool isStaticIpConfig)
+        {
+            mWifi.UpateIpConfigMethod(mAp, isStaticIpConfig);
+            if (isStaticIpConfig)
+            {
+                GetStaticIpInfo();
+            }
+            mWifi.UpdateApInfo(mAp);
+        }
+
+        private void GetStaticIpInfo()
+        {
+            Debug("GetStaticIpInfo from mApInfoSource");
+            var list = mApInfoSource.GetItemList();
+            foreach (var info in list)
+            {
+                if (info.mUpdater != null)
+                    info.mUpdater(info.InfoValue);
+            }
+        }
+
         private void OnForgetClicked(object source, ClickedEventArgs args)
         {
             Debug("Forget " + mAp.Essid);