From: chaehee-hong Date: Wed, 5 Oct 2022 04:33:03 +0000 (+0900) Subject: Add a connect method X-Git-Tag: accepted/tizen/unified/20221216.024031~37 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=71998b61143376cd48c227efc92363233de71406;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsetting-wifi.git Add a connect method Change-Id: I7c1e248deb045aaa414531163aa7f7ce57df87ae --- diff --git a/SettingWiFi/SettingWiFi/AP.cs b/SettingWiFi/SettingWiFi/AP.cs index dd45e47..3a445c4 100755 --- a/SettingWiFi/SettingWiFi/AP.cs +++ b/SettingWiFi/SettingWiFi/AP.cs @@ -13,22 +13,23 @@ namespace SettingWiFi { public class AP : INotifyPropertyChanged { - internal static readonly string LogTag = "SettingWiFi"; string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png"; + string infoIconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png"; private string essid; private string state; + private string stateText; public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { - Log.Debug(LogTag, "OnPropertyChanged"); + Log.Debug(Program.LogTag, "OnPropertyChanged"); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public AP(string apEssid, string apState) { essid = apEssid; - state = apState; + stateText = apState; } public string Essid @@ -64,18 +65,38 @@ namespace SettingWiFi OnPropertyChanged("State"); } } + + public string StateText + { + get + { + return stateText; + } + set + { + stateText = value; + OnPropertyChanged("StateText"); + } + } + + public string Info + { + get + { + return infoIconDir; + } + } }; public class DeviceCollection : ObservableCollection { - internal static readonly string LogTag = "SettingWiFi"; private string title; public DeviceCollection(string groupTitle) { title = groupTitle; - this.UpdateDevices(null); + this.UpdateScanList(null); } public string Title @@ -91,11 +112,12 @@ namespace SettingWiFi } } - public void UpdateDevices(List apList) + public void UpdateScanList(List apList) { - Log.Debug(LogTag, "UpdateDevices"); + Log.Debug(Program.LogTag, "UpdateDevices"); if (apList == null) { + Log.Debug(Program.LogTag, "apList is null"); this.Add(new AP("", "")); return; } @@ -108,7 +130,7 @@ namespace SettingWiFi foreach (var item in apList) { - this.Add(new AP(item.Essid, item.State)); + this.Add(new AP(item.Essid, item.StateText)); } } @@ -123,18 +145,17 @@ namespace SettingWiFi public class APSource : ObservableCollection { - internal static readonly string LogTag = "SettingWiFi"; private DeviceCollection available; public APSource() { - Log.Debug(LogTag, "new APSource"); + Log.Debug(Program.LogTag, "new APSource"); available = new DeviceCollection("Available networks"); this.Add(available); - Log.Debug(LogTag, "Add DeviceCollection"); + Log.Debug(Program.LogTag, "Add DeviceCollection"); } public void UpdateDevices(List apList) { - available.UpdateDevices(apList); + available.UpdateScanList(apList); } public void RemoveDevices() diff --git a/SettingWiFi/SettingWiFi/SettingWiFi.cs b/SettingWiFi/SettingWiFi/SettingWiFi.cs index 5bf4785..f2aafc5 100755 --- a/SettingWiFi/SettingWiFi/SettingWiFi.cs +++ b/SettingWiFi/SettingWiFi/SettingWiFi.cs @@ -6,6 +6,7 @@ namespace SettingWiFi { public class Program : NUIWidgetApplication { + internal static readonly string LogTag = "SettingWiFi.App"; public Program(Dictionary widgetSet) : base(widgetSet) { } diff --git a/SettingWiFi/SettingWiFi/WiFi.cs b/SettingWiFi/SettingWiFi/WiFi.cs index 3d47b40..aa63a1b 100755 --- a/SettingWiFi/SettingWiFi/WiFi.cs +++ b/SettingWiFi/SettingWiFi/WiFi.cs @@ -9,23 +9,17 @@ namespace SettingWiFi { public class WiFi { - internal static readonly string LogTag = "SettingWiFi"; - IEnumerable apList = null; + IEnumerable apList = new List(); public async Task Activate() { try { await WiFiManager.ActivateAsync(); - if (WiFiManager.IsActive) - { - Log.Debug(LogTag, "WiFi is activated"); - apList = new List(); - } } catch (Exception e) { - Log.Debug(LogTag, "Exception occurred" + e.ToString()); + Log.Debug(Program.LogTag, "Fail to activate WiFi " + e.ToString()); } } @@ -42,9 +36,8 @@ namespace SettingWiFi } catch (Exception e) { - Log.Debug(LogTag, "Exception occurred" + e.ToString()); + Log.Debug(Program.LogTag, "Fail to scan " + e.ToString()); } - } public List GetScanResult() @@ -52,37 +45,28 @@ namespace SettingWiFi try { apList = WiFiManager.GetFoundAPs(); - - List apInfoList = new List(); - foreach (var item in apList) - { - Log.Debug(LogTag, "AP name: " + item.NetworkInformation.Essid); - //Log.Debug(LogTag, "Connection state: " + item.NetworkInformation.ConnectionState); - apInfoList.Add(new AP(item.NetworkInformation.Essid, item.NetworkInformation.ConnectionState.ToString())); - } - - return apInfoList; + return GetAPList(); } catch (Exception e) { - Log.Debug(LogTag, "Exception occurred" + e.ToString()); + Log.Debug(Program.LogTag, "Fail to get scan result " + e.ToString()); } return null; } - public async Task Connect(string essid) + public async Task Connect(string essid, string password) { - WiFiAP ap = null; - apList = WiFiManager.GetFoundAPs(); + WiFiAP ap = FindAP(essid); + if (ap == null) + { + Log.Debug(Program.LogTag, "Cannet find " + essid); + return; + } - foreach (var item in apList) + if (password.Length > 0) { - if (item.NetworkInformation.Essid.Equals(essid)) - { - ap = item; - break; - } + ap.SecurityInformation.SetPassphrase(password); } try @@ -91,10 +75,46 @@ namespace SettingWiFi } catch (Exception e) { - Log.Debug(LogTag, "Fail to connect" + e.ToString()); + Log.Debug(Program.LogTag, "Fail to connect" + e.ToString()); } + } + public Task Disconnect(string essid) + { + WiFiAP ap = FindAP(essid); + if (ap == null) + { + return Task.FromException(new ArgumentException("Cannot find " + essid)); + } + return ap.DisconnectAsync(); + } + + public void Forget(string essid) + { + WiFiAP ap = FindAP(essid); + if (ap == null) + { + Log.Debug(Program.LogTag, "Cannot find " + essid); + return; + } + ap.ForgetAP(); + } + + private WiFiAP FindAP(string essid) + { + GetScanResult(); + foreach (var item in apList) + { + Log.Debug(Program.LogTag, "Found AP\t" + item.NetworkInformation.Essid); + if (item.NetworkInformation.Essid.Equals(essid)) + { + return item; + } + } + + return null; + } public bool IsActive() { @@ -105,5 +125,23 @@ namespace SettingWiFi return false; } + + public string GetConnectedAP() + { + return WiFiManager.GetConnectedAP().NetworkInformation.Essid; + } + + public List GetAPList() + { + List apInfoList = new List(); + foreach (var item in apList) + { + Log.Debug(Program.LogTag, "AP name: " + item.NetworkInformation.Essid); + //Log.Debug(LogTag, "Connection state: " + item.NetworkInformation.ConnectionState); + apInfoList.Add(new AP(item.NetworkInformation.Essid, item.NetworkInformation.ConnectionState.ToString())); + } + + return apInfoList; + } } } diff --git a/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs b/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs index ca3dfbb..37a0416 100755 --- a/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs +++ b/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs @@ -13,10 +13,19 @@ namespace SettingWiFi { internal class WidgetSettingWiFi : Widget { - internal static readonly string LogTag = "SettingWiFi"; + CollectionView scanList; + Navigator navigator; + + ContentPage mainPage; + ContentPage infoPage; + ContentPage morePage; + WiFi wifi; APSource apSource; + private static readonly int itemCount = 1; + private MenuItem[] menuItems = new MenuItem[itemCount]; + private RecyclerViewItem GetHeader() { var header = new RecyclerViewItem() @@ -29,6 +38,14 @@ namespace SettingWiFi HeightSpecification = LayoutParamPolicies.WrapContent, }; + bool isWiFiOn = false; + if (IsWiFiActive()) + { + isWiFiOn = true; + Log.Debug(Program.LogTag, "ScanAP"); + ScanAP(); + } + var toggle = new DefaultLinearItem() { WidthSpecification = LayoutParamPolicies.MatchParent, @@ -37,27 +54,62 @@ namespace SettingWiFi }; toggle.Label.HorizontalAlignment = HorizontalAlignment.Begin; - var onOffSwitch = new Switch(); + var onOffSwitch = new Switch() + { + IsSelected = isWiFiOn, + }; + toggle.Extra = onOffSwitch; - onOffSwitch.SelectedChanged += OnSelectedChanged; + onOffSwitch.SelectedChanged += OnWiFiSelected; header.Add(toggle); return header; } - protected override void OnCreate(string contentInfo, Window window) + private ContentPage CreateMainPage() { - Bundle bundle = Bundle.Decode(contentInfo); - Navigator navigator = window.GetDefaultNavigator(); - wifi = new WiFi(); - apSource = new APSource(); + var moreButton = new Button() + { + Text = "More", + }; + + MenuItem[] menuItems = new MenuItem[itemCount]; + menuItems[0] = new MenuItem() { Text = "Add WiFi network" }; + menuItems[0].SelectedChanged += (object sender, SelectedChangedEventArgs args) => + { + var menuItem = sender as MenuItem; + navigator.Push(morePage); + }; + + moreButton.Clicked += (object sender, ClickedEventArgs args) => + { + var menu = new Menu() + { + Anchor = moreButton, + HorizontalPositionToAnchor = Menu.RelativePosition.Center, + VerticalPositionToAnchor = Menu.RelativePosition.End, + Items = menuItems, + }; + menu.Post(); + }; var appBar = new AppBar() { Title = "Wi-Fi", + Actions = new View[] { moreButton, }, }; - View rootView = new View() + AppBarStyle appBarStyle = (AppBarStyle)ThemeManager.GetStyle("Tizen.NUI.Components.AppBar"); + Button backButton = new Button(((AppBarStyle)appBarStyle).BackButton); + + backButton.Clicked += (object source, ClickedEventArgs args) => + { + navigator.Pop(); + }; + + appBar.NavigationContent = backButton; + + View mainView = new View() { Layout = new LinearLayout() { @@ -68,7 +120,7 @@ namespace SettingWiFi HeightSpecification = LayoutParamPolicies.MatchParent, }; - CollectionView deviceView = new CollectionView() + scanList = new CollectionView() { ItemsSource = apSource, ItemsLayouter = new LinearLayouter(), @@ -76,10 +128,12 @@ namespace SettingWiFi { DefaultLinearItem item = new DefaultLinearItem(); item.WidthSpecification = LayoutParamPolicies.MatchParent; - //Decorate Label item.Label.SetBinding(TextLabel.TextProperty, "Essid"); + item.Label.HorizontalAlignment = HorizontalAlignment.Begin; - //Decorate Icon + item.SubLabel.SetBinding(TextLabel.TextProperty, "StateText"); + + item.SubLabel.HorizontalAlignment = HorizontalAlignment.Begin; item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl"); item.Icon.WidthSpecification = 40; item.Icon.HeightSpecification = 40; @@ -102,7 +156,7 @@ namespace SettingWiFi HeightSpecification = LayoutParamPolicies.MatchParent, SelectionMode = ItemSelectionMode.Single, }; - deviceView.SelectionChanged += DeviceConnectedEvent; + scanList.SelectionChanged += OnScanListSelected; var header = GetHeader(); var scanButton = new Button() @@ -113,22 +167,112 @@ namespace SettingWiFi }; scanButton.Clicked += OnScanButtonClicked; - rootView.Add(header); - rootView.Add(deviceView); - rootView.Add(scanButton); + mainView.Add(header); + mainView.Add(scanList); + mainView.Add(scanButton); + ContentPage page = new ContentPage() + { + AppBar = appBar, + Content = mainView, + }; + + return page; + } + + private ContentPage CreateInfoPage() + { + var appBar = new AppBar() + { + Title = "Wi-Fi network info", + }; + + AppBarStyle appBarStyle = (AppBarStyle)ThemeManager.GetStyle("Tizen.NUI.Components.AppBar"); + Button backButton = new Button(((AppBarStyle)appBarStyle).BackButton); + + backButton.Clicked += (object source, ClickedEventArgs args) => + { + navigator.Pop(); + }; + + View infoView = new View() + { + Layout = new LinearLayout() + { + LinearOrientation = LinearLayout.Orientation.Vertical, + HorizontalAlignment = HorizontalAlignment.Center, + }, + WidthSpecification = LayoutParamPolicies.MatchParent, + HeightSpecification = LayoutParamPolicies.MatchParent, + }; + + var forgetButton = new Button() + { + Text = "Forget", + WidthSpecification = 300, + HeightSpecification = 80, + }; + forgetButton.Clicked += OnforgetButtonClicked; + infoView.Add(forgetButton); ContentPage page = new ContentPage() { AppBar = appBar, - Content = rootView, + Content = infoView, }; - navigator.Push(page); + return page; } + private ContentPage CreatemorePage() + { + var appBar = new AppBar() + { + Title = "Add Wi-Fi network", + }; + + AppBarStyle appBarStyle = (AppBarStyle)ThemeManager.GetStyle("Tizen.NUI.Components.AppBar"); + Button backButton = new Button(((AppBarStyle)appBarStyle).BackButton); + + backButton.Clicked += (object source, ClickedEventArgs args) => + { + navigator.Pop(); + }; + + View moreView = new View() + { + Layout = new LinearLayout() + { + LinearOrientation = LinearLayout.Orientation.Vertical, + HorizontalAlignment = HorizontalAlignment.Center, + }, + WidthSpecification = LayoutParamPolicies.MatchParent, + HeightSpecification = LayoutParamPolicies.MatchParent, + }; + + ContentPage page = new ContentPage() + { + AppBar = appBar, + Content = moreView, + }; + + return page; + } + + protected override void OnCreate(string contentInfo, Window window) + { + navigator = window.GetDefaultNavigator(); + wifi = new WiFi(); + apSource = new APSource(); + + mainPage = CreateMainPage(); + infoPage = CreateInfoPage(); + morePage = CreatemorePage(); + + navigator.Push(mainPage); + } /* Call WiFi */ - private async void OnSelectedChanged(object sender, SelectedChangedEventArgs e) + private async void OnWiFiSelected(object sender, SelectedChangedEventArgs e) { if (e.IsSelected) { @@ -136,29 +280,36 @@ namespace SettingWiFi { await wifi.Activate(); } + /* TODO: auto connect */ ScanAP(); } else { - apSource.RemoveDevices(); - if (IsWiFiActive()) { await wifi.Deactivate(); } + apSource.RemoveDevices(); } } private void OnScanButtonClicked(object sender, ClickedEventArgs e) { - Log.Debug(LogTag, "OnScanButtonClicked"); + Log.Debug(Program.LogTag, "OnScanButtonClicked"); ScanAP(); } + private void OnforgetButtonClicked(object sender, ClickedEventArgs e) + { + Log.Debug(Program.LogTag, "OnforgetButtonClicked"); + //wifi.Forget(essid); + } + private bool IsWiFiActive() { return wifi.IsActive(); } + private async void ScanAP() { if (IsWiFiActive()) @@ -169,14 +320,22 @@ namespace SettingWiFi } } - public async void DeviceConnectedEvent(object sender, SelectionChangedEventArgs ev) + public async void OnScanListSelected(object sender, SelectionChangedEventArgs ev) { + /* TODO: disable to click the connected ap */ + + + //SingleSelection Only have 1 or nil object in the list. foreach (object item in ev.PreviousSelection) { if (item == null) break; if (item is AP device) { + //Log.Debug(LogTag, "Disconnect "); + //await wifi.Disconnect(device.Essid); + //device.State = "Disonnected"; + // device.Connected = false; } } @@ -185,7 +344,20 @@ namespace SettingWiFi if (item == null) break; if (item is AP device) { - await wifi.Connect(device.Essid); + if (device.StateText.Equals("Connected")) + { + Log.Debug(Program.LogTag, "already connected"); + return; + } + + Log.Debug(Program.LogTag, "new connect"); /* TODO: pop-up(password, cancel, connect) */ + device.StateText = "Connecting"; + await wifi.Connect(device.Essid, "datanetwork"); + if(wifi.GetConnectedAP().Equals(device.Essid)) + { + device.StateText = "Connected"; + + } // device.Connected = true; // device.Registered = true; } diff --git a/SettingWiFi/SettingWiFi/tizen-manifest.xml b/SettingWiFi/SettingWiFi/tizen-manifest.xml index c1b1256..9864604 100755 --- a/SettingWiFi/SettingWiFi/tizen-manifest.xml +++ b/SettingWiFi/SettingWiFi/tizen-manifest.xml @@ -9,4 +9,9 @@ + + http://tizen.org/privilege/network.get + http://tizen.org/privilege/network.set + http://tizen.org/privilege/network.profile +