From: cheoleun moon Date: Fri, 14 Oct 2022 09:03:07 +0000 (+0900) Subject: Allow to set static IP infomation X-Git-Tag: accepted/tizen/unified/20221216.024031~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5eca6684f5449e516502d09f4876e6221b387fb3;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsetting-wifi.git Allow to set static IP infomation Change-Id: I33a7d3fd51dccb76a950860d264464727a530656 --- diff --git a/SettingWiFi/SettingWiFi/controller/WiFi.cs b/SettingWiFi/SettingWiFi/controller/WiFi.cs index c9b4699..70df920 100755 --- a/SettingWiFi/SettingWiFi/controller/WiFi.cs +++ b/SettingWiFi/SettingWiFi/controller/WiFi.cs @@ -4,8 +4,10 @@ using System.Text; using System.Threading.Tasks; using Tizen; using Tizen.Network.WiFi; +using Tizen.Network.Connection; using SettingWiFi.res.locale; using static SettingWiFi.Logger; +using System.Net; namespace SettingWiFi { @@ -200,42 +202,54 @@ namespace SettingWiFi //bool isPassphraseRequired = item.SecurityInformation.IsPassphraseRequired; - var securityType = item.SecurityInformation.SecurityType; - bool isWpsSupported = item.SecurityInformation.IsWpsSupported; + var ap = CreateAp(item); + Debug("AP Info. IPv4[" + ap.IPv4 + "] IPv6[" + ap.IPv6 + "] Mac[" + ap.Bssid + "] " + + "ProxyAddress[" + ap.ProxyAddress + "] ProxyPort[" + ap.ProxyPort + "]"); - WiFiState state = convertWiFiState(item.NetworkInformation.ConnectionState); + apInfoList.Add(ap); + } - var networkInfo = item.NetworkInformation; - var ap = new AP(item, networkInfo.Essid, - state, securityType, isWpsSupported, - networkInfo.Bssid, networkInfo.RssiLevel); + return apInfoList; + } - ap.IPv4 = networkInfo.IPv4Setting.IP.ToString(); - ap.IPv6 = networkInfo.IPv6Setting.IP.ToString(); - var proxy = networkInfo.ProxyAddress; - if (proxy.Contains(':')) + private AP CreateAp(WiFiAP wifiAp) + { + var securityType = wifiAp.SecurityInformation.SecurityType; + bool isWpsSupported = wifiAp.SecurityInformation.IsWpsSupported; + WiFiState state = convertWiFiState(wifiAp.NetworkInformation.ConnectionState); + var networkInfo = wifiAp.NetworkInformation; + + var ap = new AP(wifiAp, networkInfo.Essid, + state, securityType, isWpsSupported, + networkInfo.Bssid, networkInfo.RssiLevel); + + ap.IPv4 = networkInfo.IPv4Setting.IP.ToString(); + ap.IPv6 = networkInfo.IPv6Setting.IP.ToString(); + var proxy = networkInfo.ProxyAddress; + if (proxy.Contains(':')) + { + var result = proxy.Split(":"); + ap.ProxyAddress = result[0]; + int port = 0; + if (Int32.TryParse(result[1], out port)) { - var result = proxy.Split(":"); - ap.ProxyAddress = result[0]; - int port = 0; - if (Int32.TryParse(result[1], out port)) - { - ap.ProxyPort = port; - } - else - { - ap.ProxyPort = -1; - } + ap.ProxyPort = port; } + else + { + ap.ProxyPort = -1; + } + } - Debug("AP Info. IPv4[" + ap.IPv4 + "] IPv6[" + ap.IPv6 + "] Mac[" + ap.Bssid + "] " - + "ProxyAddress[" + ap.ProxyAddress + "] ProxyPort[" + ap.ProxyPort + "]"); + ap.StaticIPConfig = (networkInfo.IPv4Setting.IPConfigType == IPConfigType.Static); - apInfoList.Add(ap); - } + return ap; + } - return apInfoList; + public void UpdateApInfo(AP ap) + { + ap.ApHandle.Update(); } public List GetSpecificAPList() @@ -332,6 +346,91 @@ namespace SettingWiFi return wpsPin; } + + public void UpateIpConfigMethod(AP ap, bool isStaticIpConfig) + { + if (isStaticIpConfig) + { + ap.ApHandle.NetworkInformation.IPv4Setting.IPConfigType = IPConfigType.Static; + ap.ApHandle.NetworkInformation.IPv6Setting.IPConfigType = IPConfigType.Static; + } + else + { + ap.ApHandle.NetworkInformation.IPv4Setting.IPConfigType = IPConfigType.Dynamic; + ap.ApHandle.NetworkInformation.IPv6Setting.IPConfigType = IPConfigType.Dynamic; + } + } + + public bool UpdateIPAddress(AP ap, string ip) + { + IPAddress ipAddress; + if (IPAddress.TryParse(ip, out ipAddress) == false) + { + Error("IP address parsing error"); + return false; + } + + ap.ApHandle.NetworkInformation.IPv4Setting.IP = ipAddress; + return true; + } + + public bool UpdateSubnetMask(AP ap, string mask) + { + IPAddress ipAddress; + if (IPAddress.TryParse(mask, out ipAddress) == false) + { + Error("IP address parsing error"); + return false; + } + + ap.ApHandle.NetworkInformation.IPv4Setting.SubnetMask = ipAddress; + return true; + } + + public bool UpdateGatewayAddress(AP ap, string address) + { + IPAddress ipAddress; + if (IPAddress.TryParse(address, out ipAddress) == false) + { + Error("IP address parsing error"); + return false; + } + + ap.ApHandle.NetworkInformation.IPv4Setting.Gateway = ipAddress; + return true; + } + + public bool UpdateDns1(AP ap, string address) + { + IPAddress ipAddress; + if (IPAddress.TryParse(address, out ipAddress) == false) + { + Error("IP address parsing error"); + return false; + } + + ap.ApHandle.NetworkInformation.IPv4Setting.Dns1 = ipAddress; + return true; + } + + public bool UpdateDns2(AP ap, string address) + { + IPAddress ipAddress; + if (IPAddress.TryParse(address, out ipAddress) == false) + { + Error("IP address parsing error"); + return false; + } + + ap.ApHandle.NetworkInformation.IPv4Setting.Dns2 = ipAddress; + return true; + } + + public bool UpdateProxy(AP ap, string proxy) + { + ap.ApHandle.NetworkInformation.ProxyAddress = proxy; + return true; + } } internal class WiFiStateChangedEventArgs diff --git a/SettingWiFi/SettingWiFi/model/AP.cs b/SettingWiFi/SettingWiFi/model/AP.cs index 8d5ae07..fdf42ff 100755 --- a/SettingWiFi/SettingWiFi/model/AP.cs +++ b/SettingWiFi/SettingWiFi/model/AP.cs @@ -257,5 +257,35 @@ namespace SettingWiFi get; set; } + + public bool StaticIPConfig + { + get; + set; + } + + public string SubnetMask + { + get; + set; + } + + public string GatewayAddress + { + get; + set; + } + + public string Dns1 + { + get; + set; + } + + public string Dns2 + { + get; + set; + } }; } diff --git a/SettingWiFi/SettingWiFi/view/ApInfoSource.cs b/SettingWiFi/SettingWiFi/view/ApInfoSource.cs index ed88f78..8ce95af 100644 --- a/SettingWiFi/SettingWiFi/view/ApInfoSource.cs +++ b/SettingWiFi/SettingWiFi/view/ApInfoSource.cs @@ -1,45 +1,141 @@ +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 { - 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 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 mUpdater; public event PropertyChangedEventHandler PropertyChanged; - public ApInfoText(string title, string value) + + public ApInfoText(string title, string value, Predicate 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"); } } diff --git a/SettingWiFi/SettingWiFi/view/InfoPage.cs b/SettingWiFi/SettingWiFi/view/InfoPage.cs index 49d6e49..be1b1bf 100644 --- a/SettingWiFi/SettingWiFi/view/InfoPage.cs +++ b/SettingWiFi/SettingWiFi/view/InfoPage.cs @@ -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);