From: cheoleun moon Date: Thu, 6 Oct 2022 03:25:24 +0000 (+0900) Subject: Define classes for each ContentPage X-Git-Tag: accepted/tizen/unified/20221216.024031~35 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F63%2F282563%2F2;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsetting-wifi.git Define classes for each ContentPage Change-Id: I4433088182959f983023a19c3e9350b2fb7473ae --- diff --git a/SettingWiFi/SettingWiFi/AP.cs b/SettingWiFi/SettingWiFi/AP.cs deleted file mode 100755 index d394406..0000000 --- a/SettingWiFi/SettingWiFi/AP.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.ComponentModel; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using Tizen; -using Tizen.NUI.BaseComponents; -using Tizen.NUI.Components; -using Tizen.NUI.Binding; -using Tizen.Network.WiFi; - - -namespace SettingWiFi -{ - public class AP : INotifyPropertyChanged - { - string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png"; - private string essid; - private string state; - - public event PropertyChangedEventHandler PropertyChanged; - - private void OnPropertyChanged(string propertyName) - { - Log.Debug(Program.LogTag, "OnPropertyChanged"); - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - public AP(string apEssid, string apState) - { - essid = apEssid; - state = apState; - } - - public string Essid - { - get - { - return essid; - } - set - { - essid = value; - OnPropertyChanged("Essid"); - } - } - - public string ImageUrl - { - get - { - return iconDir; - } - } - - public string State - { - get - { - return state; - } - set - { - state = value; - OnPropertyChanged("State"); - } - } - }; - - public class DeviceCollection : ObservableCollection - { - private string title; - - public DeviceCollection(string groupTitle) - { - title = groupTitle; - this.UpdateScanList(null); - } - - public string Title - { - get - { - return title; - } - set - { - title = value; - OnPropertyChanged(new PropertyChangedEventArgs("Title")); - } - } - - public void UpdateScanList(List apList) - { - if (apList == null) - { - Log.Debug(Program.LogTag, "apList is null"); - this.Add(new AP("", "")); - return; - } - // Clear method have some issue about asynchronous actions, - // so call Remove for all item is recommanded. - while (this.Count > 0) - { - this.RemoveAt(this.Count - 1); - } - - foreach (var item in apList) - { - this.Add(new AP(item.Essid, item.State)); - } - } - - public void RemoveDevices() - { - while (this.Count > 0) - { - this.RemoveAt(this.Count - 1); - } - } - } - - public class APSource : ObservableCollection - { - private DeviceCollection available; - public APSource() - { - Log.Debug(Program.LogTag, "new APSource"); - available = new DeviceCollection("Available networks"); - this.Add(available); - Log.Debug(Program.LogTag, "Add DeviceCollection"); - } - public void UpdateScanList(List apList) - { - available.UpdateScanList(apList); - } - - public void RemoveDevices() - { - available.RemoveDevices(); - } - } -} \ No newline at end of file diff --git a/SettingWiFi/SettingWiFi/SettingWiFi.cs b/SettingWiFi/SettingWiFi/SettingWiFi.cs index f2aafc5..8ec3fec 100755 --- a/SettingWiFi/SettingWiFi/SettingWiFi.cs +++ b/SettingWiFi/SettingWiFi/SettingWiFi.cs @@ -4,10 +4,9 @@ using Tizen.NUI; namespace SettingWiFi { - public class Program : NUIWidgetApplication + public class SettingWiFi : NUIWidgetApplication { - internal static readonly string LogTag = "SettingWiFi.App"; - public Program(Dictionary widgetSet) : base(widgetSet) + public SettingWiFi(Dictionary widgetSet) : base(widgetSet) { } @@ -28,7 +27,7 @@ namespace SettingWiFi { Dictionary widgetSet = new Dictionary(); widgetSet.Add(typeof(WidgetSettingWiFi), "wifi@org.tizen.cssetting-wifi"); - var app = new Program(widgetSet); + var app = new SettingWiFi(widgetSet); app.Run(args); } } diff --git a/SettingWiFi/SettingWiFi/WiFi.cs b/SettingWiFi/SettingWiFi/WiFi.cs deleted file mode 100755 index b6e1a2b..0000000 --- a/SettingWiFi/SettingWiFi/WiFi.cs +++ /dev/null @@ -1,177 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Tizen; -using Tizen.Network.WiFi; - -namespace SettingWiFi -{ - public class WiFi - { - IEnumerable apList = new List(); - - public async Task Activate() - { - try - { - await WiFiManager.ActivateAsync(); - } - catch (Exception e) - { - Log.Debug(Program.LogTag, "Fail to activate WiFi " + e.ToString()); - } - } - - public Task Deactivate() - { - return WiFiManager.DeactivateAsync(); - } - - public async Task Scan() - { - try - { - await WiFiManager.ScanAsync(); - } - catch (Exception e) - { - Log.Debug(Program.LogTag, "Fail to scan " + e.ToString()); - } - } - - public List GetScanResult() - { - try - { - apList = WiFiManager.GetFoundAPs(); - return GetAPList(); - } - catch (Exception e) - { - Log.Debug(Program.LogTag, "Fail to get scan result " + e.ToString()); - } - - return null; - } - - public async Task Connect(string essid, string password) - { - WiFiAP ap = FindAP(essid); - if (ap == null) - { - Log.Debug(Program.LogTag, "Cannet find " + essid); - return; - } - - if (password.Length > 0) - { - ap.SecurityInformation.SetPassphrase(password); - } - - try - { - await ap.ConnectAsync(); - } - catch (Exception e) - { - 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() - { - if (WiFiManager.IsActive) - { - return true; - } - - 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(Program.LogTag, "AP connection state: " + item.NetworkInformation.ConnectionState.ToString()); - - string connectionState = item.NetworkInformation.ConnectionState.ToString(); - string securityType = item.SecurityInformation.SecurityType.ToString(); - //bool isPassphraseRequired = item.SecurityInformation.IsPassphraseRequired; - bool isWpsSupported = item.SecurityInformation.IsWpsSupported; - - if (item.NetworkInformation.ConnectionState.ToString().Equals("Connected")) - { - apInfoList.Add(new AP(item.NetworkInformation.Essid, "Connected")); - continue; - } - string text = ""; - - if (securityType.Equals("None")) - { - text = "Open"; - } - else if (securityType.Equals("Eap")) - { - text = "Secured (Eap)"; - } - else if (isWpsSupported) - { - text = "Secured (WPS available)"; - } - else - { - text = "Secured"; - } - - apInfoList.Add(new AP(item.NetworkInformation.Essid, text)); - } - - return apInfoList; - } - } -} diff --git a/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs b/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs deleted file mode 100755 index b9fedba..0000000 --- a/SettingWiFi/SettingWiFi/WidgetSettingWiFi.cs +++ /dev/null @@ -1,379 +0,0 @@ -using System; -using System.Collections.Generic; -using Tizen; -using Tizen.Applications; -using Tizen.NUI; -using Tizen.NUI.Accessibility; -using Tizen.NUI.BaseComponents; -using Tizen.NUI.Binding; -using Tizen.NUI.Components; - - -namespace SettingWiFi -{ - internal class WidgetSettingWiFi : Widget - { - 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() - { - Layout = new LinearLayout() - { - LinearOrientation = LinearLayout.Orientation.Vertical, - }, - WidthSpecification = LayoutParamPolicies.MatchParent, - HeightSpecification = LayoutParamPolicies.WrapContent, - }; - - bool isWiFiOn = false; - if (IsWiFiActive()) - { - isWiFiOn = true; - Log.Debug(Program.LogTag, "ScanAP"); - ScanAP(); - } - - var toggle = new DefaultLinearItem() - { - WidthSpecification = LayoutParamPolicies.MatchParent, - Text = "Wi-Fi", - IsSelectable = false, - }; - toggle.Label.HorizontalAlignment = HorizontalAlignment.Begin; - - var onOffSwitch = new Switch() - { - IsSelected = isWiFiOn, - }; - - toggle.Extra = onOffSwitch; - onOffSwitch.SelectedChanged += OnWiFiSelected; - header.Add(toggle); - - return header; - } - - private ContentPage CreateMainPage() - { - 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, }, - }; - - 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() - { - LinearOrientation = LinearLayout.Orientation.Vertical, - HorizontalAlignment = HorizontalAlignment.Center, - }, - WidthSpecification = LayoutParamPolicies.MatchParent, - HeightSpecification = LayoutParamPolicies.MatchParent, - }; - - scanList = new CollectionView() - { - ItemsSource = apSource, - ItemsLayouter = new LinearLayouter(), - ItemTemplate = new DataTemplate(() => - { - DefaultLinearItem item = new DefaultLinearItem() //itemÀÌ °¢ AP - { - WidthSpecification = LayoutParamPolicies.MatchParent, - }; - item.Label.SetBinding(TextLabel.TextProperty, "Essid"); - item.Label.HorizontalAlignment = HorizontalAlignment.Begin; - item.SubLabel.SetBinding(TextLabel.TextProperty, "State"); - item.SubLabel.HorizontalAlignment = HorizontalAlignment.Begin; - item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl"); - item.Icon.WidthSpecification = 40; - item.Icon.HeightSpecification = 40; - - var infoButton = new Button() - { - Text = "i", - WidthSpecification = 40, - HeightSpecification = 40, - }; - //infoButton.Icon.SetBinding(ImageView.ResourceUrlProperty, "InfoImageUrl"); - infoButton.Clicked += OnInfoButtonClicked; - item.Extra = infoButton; - - return item; - }), - GroupHeaderTemplate = new DataTemplate(() => - { - DefaultTitleItem group = new DefaultTitleItem(); - group.WidthSpecification = LayoutParamPolicies.MatchParent; - - group.Label.SetBinding(TextLabel.TextProperty, "Title"); - group.Label.HorizontalAlignment = HorizontalAlignment.Begin; - - return group; - }), - IsGrouped = true, - ScrollingDirection = ScrollableBase.Direction.Vertical, - WidthSpecification = LayoutParamPolicies.MatchParent, - HeightSpecification = LayoutParamPolicies.MatchParent, - SelectionMode = ItemSelectionMode.Single, - }; - scanList.SelectionChanged += OnScanListSelected; - - var header = GetHeader(); - var scanButton = new Button() - { - Text = "Scan", - WidthSpecification = 300, - HeightSpecification = 80, - }; - scanButton.Clicked += OnScanButtonClicked; - - mainView.Add(header); - mainView.Add(scanList); - mainView.Add(scanButton); - ContentPage page = new ContentPage() - { - AppBar = appBar, - Content = mainView, - }; - - return page; - } - - private ContentPage CreateInfoPage(AP ap) - { - 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 += (object source, ClickedEventArgs args) => - { - Log.Debug(Program.LogTag, "Forget " + ap.Essid); - wifi.Forget(ap.Essid); - ScanAP(); - }; - - infoView.Add(forgetButton); - ContentPage page = new ContentPage() - { - AppBar = appBar, - Content = infoView, - }; - - 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(); - morePage = CreatemorePage(); - - navigator.Push(mainPage); - } - - /* Call WiFi */ - private async void OnWiFiSelected(object sender, SelectedChangedEventArgs e) - { - if (e.IsSelected) - { - if (!IsWiFiActive()) - { - await wifi.Activate(); - } - /* TODO: auto connect */ - ScanAP(); - } - else - { - if (IsWiFiActive()) - { - await wifi.Deactivate(); - } - apSource.RemoveDevices(); - } - } - - private void OnScanButtonClicked(object sender, ClickedEventArgs e) - { - Log.Debug(Program.LogTag, "OnScanButtonClicked"); - ScanAP(); - } - - private void OnInfoButtonClicked(object sender, ClickedEventArgs e) - { - Log.Debug(Program.LogTag, "OnInfoButtonClicked"); - Button button = (Button)sender; - infoPage = CreateInfoPage(button.BindingContext as AP); - navigator.Push(infoPage); - } - - private bool IsWiFiActive() - { - return wifi.IsActive(); - } - - private async void ScanAP() - { - if (IsWiFiActive()) - { - await wifi.Scan(); - List apList = wifi.GetScanResult(); - apSource.UpdateScanList(apList); - } - } - - public async void OnScanListSelected(object sender, SelectionChangedEventArgs ev) - { - //SingleSelection Only have 1 or nil object in the list. - foreach (object item in ev.PreviousSelection) - { - if (item == null) break; - if (item is AP ap) - { - //Log.Debug(LogTag, "Disconnect "); - //await wifi.Disconnect(device.Essid); - //device.State = "Disonnected"; - - // device.Connected = false; - } - } - foreach (object item in ev.CurrentSelection) - { - if (item == null) break; - if (item is AP ap) - { - if (ap.State.Equals("Connected")) - { - Log.Debug(Program.LogTag, "already connected"); - return; - } - - /* TODO: pop-up (password, cancel, connect) */ - ap.State = "Connecting"; - await wifi.Connect(ap.Essid, "datanetwork"); - if(wifi.GetConnectedAP().Equals(ap.Essid)) - { - ap.State = "Connected"; - } - // device.Connected = true; - // device.Registered = true; - } - } - } - } -} diff --git a/SettingWiFi/SettingWiFi/controller/WiFi.cs b/SettingWiFi/SettingWiFi/controller/WiFi.cs new file mode 100755 index 0000000..2b4068c --- /dev/null +++ b/SettingWiFi/SettingWiFi/controller/WiFi.cs @@ -0,0 +1,189 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tizen; +using Tizen.Network.WiFi; +using static SettingWiFi.Logger; + +namespace SettingWiFi +{ + public class WiFi + { + IEnumerable apList = new List(); + + public async Task Activate() + { + Debug("WiFi.Activate"); + try + { + await WiFiManager.ActivateAsync(); + } + catch (Exception e) + { + Debug("Fail to activate WiFi " + e.ToString()); + } + } + + public Task Deactivate() + { + Debug("WiFi.Deactivate"); + return WiFiManager.DeactivateAsync(); + } + + public async Task Scan() + { + Debug("WiFi.Scan"); + try + { + await WiFiManager.ScanAsync(); + } + catch (Exception e) + { + Debug("Fail to scan " + e.ToString()); + } + } + + public List GetScanResult() + { + Debug("WiFi.GetScanResult"); + try + { + apList = WiFiManager.GetFoundAPs(); + return GetAPList(); + } + catch (Exception e) + { + Debug("Fail to get scan result " + e.ToString()); + } + + return null; + } + + public async Task Connect(string essid, string password) + { + Debug("WiFi.Connect"); + WiFiAP ap = FindAP(essid); + if (ap == null) + { + Debug("Cannet find " + essid); + return; + } + + if (password.Length > 0) + { + ap.SecurityInformation.SetPassphrase(password); + } + + try + { + await ap.ConnectAsync(); + } + catch (Exception e) + { + Debug("Fail to connect" + e.ToString()); + } + + } + + public Task Disconnect(string essid) + { + Debug("WiFi.Disconnect"); + WiFiAP ap = FindAP(essid); + if (ap == null) + { + return Task.FromException(new ArgumentException("Cannot find " + essid)); + } + return ap.DisconnectAsync(); + } + + public void Forget(string essid) + { + Debug("WiFi.Forget"); + WiFiAP ap = FindAP(essid); + if (ap == null) + { + Debug("Cannot find " + essid); + return; + } + ap.ForgetAP(); + } + + private WiFiAP FindAP(string essid) + { + Debug("WiFi.FindAP"); + GetScanResult(); + foreach (var item in apList) + { + Debug("Found AP\t" + item.NetworkInformation.Essid); + if (item.NetworkInformation.Essid.Equals(essid)) + { + return item; + } + } + + return null; + } + + public bool IsActive() + { + Debug("WiFi.IsActive"); + if (WiFiManager.IsActive) + { + return true; + } + + return false; + } + + public string GetConnectedAP() + { + Debug("WiFi.GetConnectedAP"); + return WiFiManager.GetConnectedAP().NetworkInformation.Essid; + } + + public List GetAPList() + { + Debug("WiFi.GetAPList"); + List apInfoList = new List(); + foreach (var item in apList) + { + //Debug("AP name: " + item.NetworkInformation.Essid); + //Debug("AP connection state: " + item.NetworkInformation.ConnectionState.ToString()); + + string connectionState = item.NetworkInformation.ConnectionState.ToString(); + string securityType = item.SecurityInformation.SecurityType.ToString(); + //bool isPassphraseRequired = item.SecurityInformation.IsPassphraseRequired; + bool isWpsSupported = item.SecurityInformation.IsWpsSupported; + + if (item.NetworkInformation.ConnectionState.ToString().Equals("Connected")) + { + apInfoList.Add(new AP(item.NetworkInformation.Essid, "Connected")); + continue; + } + string text = ""; + + if (securityType.Equals("None")) + { + text = "Open"; + } + else if (securityType.Equals("Eap")) + { + text = "Secured (Eap)"; + } + else if (isWpsSupported) + { + text = "Secured (WPS available)"; + } + else + { + text = "Secured"; + } + + apInfoList.Add(new AP(item.NetworkInformation.Essid, text)); + } + + return apInfoList; + } + } +} diff --git a/SettingWiFi/SettingWiFi/view/AP.cs b/SettingWiFi/SettingWiFi/view/AP.cs new file mode 100755 index 0000000..b290194 --- /dev/null +++ b/SettingWiFi/SettingWiFi/view/AP.cs @@ -0,0 +1,143 @@ +using System; +using System.ComponentModel; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Tizen; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Components; +using Tizen.NUI.Binding; +using Tizen.Network.WiFi; +using static SettingWiFi.Logger; + + +namespace SettingWiFi +{ + public class AP : INotifyPropertyChanged + { + string iconDir = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "icon.png"; + private string essid; + private string state; + + public event PropertyChangedEventHandler PropertyChanged; + + private void OnPropertyChanged(string propertyName) + { + Debug("OnPropertyChanged"); + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + public AP(string apEssid, string apState) + { + essid = apEssid; + state = apState; + } + + public string Essid + { + get + { + return essid; + } + set + { + essid = value; + OnPropertyChanged("Essid"); + } + } + + public string ImageUrl + { + get + { + return iconDir; + } + } + + public string State + { + get + { + return state; + } + set + { + state = value; + OnPropertyChanged("State"); + } + } + }; + + public class DeviceCollection : ObservableCollection + { + private string title; + + public DeviceCollection(string groupTitle) + { + title = groupTitle; + this.UpdateScanList(null); + } + + public string Title + { + get + { + return title; + } + set + { + title = value; + OnPropertyChanged(new PropertyChangedEventArgs("Title")); + } + } + + public void UpdateScanList(List apList) + { + if (apList == null) + { + Debug("apList is null"); + this.Add(new AP("", "")); + return; + } + // Clear method have some issue about asynchronous actions, + // so call Remove for all item is recommanded. + while (this.Count > 0) + { + this.RemoveAt(this.Count - 1); + } + + foreach (var item in apList) + { + this.Add(new AP(item.Essid, item.State)); + } + } + + public void RemoveDevices() + { + while (this.Count > 0) + { + this.RemoveAt(this.Count - 1); + } + } + } + + public class APSource : ObservableCollection + { + private DeviceCollection available; + public APSource() + { + Debug("new APSource"); + available = new DeviceCollection("Available networks"); + this.Add(available); + Debug("Add DeviceCollection"); + } + public void UpdateScanList(List apList) + { + available.UpdateScanList(apList); + } + + public void RemoveDevices() + { + available.RemoveDevices(); + } + } +} \ No newline at end of file diff --git a/SettingWiFi/SettingWiFi/view/InfoPage.cs b/SettingWiFi/SettingWiFi/view/InfoPage.cs new file mode 100644 index 0000000..94210af --- /dev/null +++ b/SettingWiFi/SettingWiFi/view/InfoPage.cs @@ -0,0 +1,69 @@ +using Tizen; +using Tizen.NUI; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Components; +using static SettingWiFi.Logger; + +namespace SettingWiFi +{ + internal class InfoPage : ContentPage + { + private AP mAp; + private WiFi mWifi; + + internal InfoPage(WiFi wifi) + { + mWifi = wifi; + } + + internal void CreateComponents(AP ap) + { + mAp = ap; + + 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 += OnBackButtonClicked; + 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); + + AppBar = appBar; + Content = infoView; + } + + private void OnBackButtonClicked(object source, ClickedEventArgs args) + { + Navigator.Pop(); + } + + private void OnForgetButtonClicked(object source, ClickedEventArgs args) + { + Debug("Forget " + mAp.Essid); + mWifi.Forget(mAp.Essid); + } + } +} \ No newline at end of file diff --git a/SettingWiFi/SettingWiFi/view/Logger.cs b/SettingWiFi/SettingWiFi/view/Logger.cs new file mode 100644 index 0000000..9b58722 --- /dev/null +++ b/SettingWiFi/SettingWiFi/view/Logger.cs @@ -0,0 +1,24 @@ +using Tizen; + +namespace SettingWiFi +{ + internal class Logger + { + internal static readonly string LogTag = "SettingWiFi.App"; + + internal static void Debug(string msg) + { + Log.Debug(LogTag, msg); + } + + internal static void Info(string msg) + { + Log.Info(LogTag, msg); + } + + internal static void Error(string msg) + { + Log.Error(LogTag, msg); + } + } +} \ No newline at end of file diff --git a/SettingWiFi/SettingWiFi/view/MainPage.cs b/SettingWiFi/SettingWiFi/view/MainPage.cs new file mode 100644 index 0000000..8471231 --- /dev/null +++ b/SettingWiFi/SettingWiFi/view/MainPage.cs @@ -0,0 +1,302 @@ +using System.Collections.Generic; +using Tizen; +using Tizen.NUI; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Binding; +using Tizen.NUI.Components; +using static SettingWiFi.Logger; + + +namespace SettingWiFi +{ + internal class MainPage : ContentPage + { + CollectionView mScanList; + + ContentPage mInfoPage; + ContentPage mMorePage; + + WiFi mWifi; + APSource mApSource; + + private static readonly int itemCount = 1; + private MenuItem[] menuItems = new MenuItem[itemCount]; + + internal MainPage(WiFi wifi) + { + mWifi = wifi; + } + + internal void CreateComponents() + { + mApSource = 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(mMorePage); + }; + + 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, }, + }; + + 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() + { + LinearOrientation = LinearLayout.Orientation.Vertical, + HorizontalAlignment = HorizontalAlignment.Center, + }, + WidthSpecification = LayoutParamPolicies.MatchParent, + HeightSpecification = LayoutParamPolicies.MatchParent, + }; + + mScanList = new CollectionView() + { + ItemsSource = mApSource, + ItemsLayouter = new LinearLayouter(), + ItemTemplate = CreateItemTemplate(), + GroupHeaderTemplate = new DataTemplate(() => + { + DefaultTitleItem group = new DefaultTitleItem(); + group.WidthSpecification = LayoutParamPolicies.MatchParent; + + group.Label.SetBinding(TextLabel.TextProperty, "Title"); + group.Label.HorizontalAlignment = HorizontalAlignment.Begin; + + return group; + }), + IsGrouped = true, + ScrollingDirection = ScrollableBase.Direction.Vertical, + WidthSpecification = LayoutParamPolicies.MatchParent, + HeightSpecification = LayoutParamPolicies.MatchParent, + SelectionMode = ItemSelectionMode.Single, + }; + mScanList.SelectionChanged += OnScanListSelected; + + var header = GetHeader(); + var scanButton = new Button() + { + Text = "Scan", + WidthSpecification = 300, + HeightSpecification = 80, + }; + scanButton.Clicked += OnScanButtonClicked; + + mainView.Add(header); + mainView.Add(mScanList); + mainView.Add(scanButton); + + AppBar = appBar; + Content = mainView; + } + + private DataTemplate CreateItemTemplate() + { + return new DataTemplate(() => + { + DefaultLinearItem item = new DefaultLinearItem() //itemÀÌ °¢ AP + { + WidthSpecification = LayoutParamPolicies.MatchParent, + }; + item.Label.SetBinding(TextLabel.TextProperty, "Essid"); + item.Label.HorizontalAlignment = HorizontalAlignment.Begin; + item.SubLabel.SetBinding(TextLabel.TextProperty, "State"); + item.SubLabel.HorizontalAlignment = HorizontalAlignment.Begin; + item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl"); + item.Icon.WidthSpecification = 40; + item.Icon.HeightSpecification = 40; + + var infoButton = new Button() + { + Text = "i", + WidthSpecification = 40, + HeightSpecification = 40, + }; + //infoButton.Icon.SetBinding(ImageView.ResourceUrlProperty, "InfoImageUrl"); + infoButton.Clicked += OnInfoButtonClicked; + item.Extra = infoButton; + + return item; + }); + } + + private RecyclerViewItem GetHeader() + { + var header = new RecyclerViewItem() + { + Layout = new LinearLayout() + { + LinearOrientation = LinearLayout.Orientation.Vertical, + }, + WidthSpecification = LayoutParamPolicies.MatchParent, + HeightSpecification = LayoutParamPolicies.WrapContent, + }; + + bool isWiFiOn = false; + if (IsWiFiActive()) + { + isWiFiOn = true; + Debug("ScanAP"); + ScanAP(); + } + + var toggle = new DefaultLinearItem() + { + WidthSpecification = LayoutParamPolicies.MatchParent, + Text = "Wi-Fi", + IsSelectable = false, + }; + toggle.Label.HorizontalAlignment = HorizontalAlignment.Begin; + + var onOffSwitch = new Switch() + { + IsSelected = isWiFiOn, + }; + + toggle.Extra = onOffSwitch; + onOffSwitch.SelectedChanged += OnWiFiSelected; + header.Add(toggle); + + return header; + } + + private ContentPage CreateInfoPage(AP ap) + { + var page = new InfoPage(mWifi); + page.CreateComponents(ap); + return page; + } + + private ContentPage CreateMorePage() + { + var page = new MorePage(mWifi); + page.CreateComponents(); + return page; + } + + /* Call WiFi */ + private async void OnWiFiSelected(object sender, SelectedChangedEventArgs e) + { + Debug("OnWiFiSelected"); + if (e.IsSelected) + { + if (!IsWiFiActive()) + { + await mWifi.Activate(); + } + /* TODO: auto connect */ + ScanAP(); + } + else + { + if (IsWiFiActive()) + { + await mWifi.Deactivate(); + } + mApSource.RemoveDevices(); + } + } + + private void OnScanButtonClicked(object sender, ClickedEventArgs e) + { + Debug("OnScanButtonClicked"); + ScanAP(); + } + + private void OnInfoButtonClicked(object sender, ClickedEventArgs e) + { + Debug("OnInfoButtonClicked"); + Button button = (Button)sender; + mInfoPage = CreateInfoPage(button.BindingContext as AP); + + Navigator.Push(mInfoPage); + } + + private bool IsWiFiActive() + { + return mWifi.IsActive(); + } + + private async void ScanAP() + { + if (IsWiFiActive()) + { + await mWifi.Scan(); + List apList = mWifi.GetScanResult(); + mApSource.UpdateScanList(apList); + } + } + + public async void OnScanListSelected(object sender, SelectionChangedEventArgs ev) + { + //SingleSelection Only have 1 or nil object in the list. + foreach (object item in ev.PreviousSelection) + { + if (item == null) break; + if (item is AP ap) + { + //Log.Debug(LogTag, "Disconnect "); + //await wifi.Disconnect(device.Essid); + //device.State = "Disonnected"; + + // device.Connected = false; + } + } + foreach (object item in ev.CurrentSelection) + { + if (item == null) break; + if (item is AP ap) + { + if (ap.State.Equals("Connected")) + { + Debug("already connected"); + return; + } + + /* TODO: pop-up (password, cancel, connect) */ + ap.State = "Connecting"; + await mWifi.Connect(ap.Essid, "datanetwork"); + if(mWifi.GetConnectedAP().Equals(ap.Essid)) + { + ap.State = "Connected"; + } + // device.Connected = true; + // device.Registered = true; + } + } + } + } +} diff --git a/SettingWiFi/SettingWiFi/view/MorePage.cs b/SettingWiFi/SettingWiFi/view/MorePage.cs new file mode 100644 index 0000000..a0a2c1d --- /dev/null +++ b/SettingWiFi/SettingWiFi/view/MorePage.cs @@ -0,0 +1,49 @@ + +using Tizen.NUI; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Components; + +namespace SettingWiFi +{ + internal class MorePage : ContentPage + { + WiFi mWifi; + + internal MorePage(WiFi wifi) + { + mWifi = wifi; + } + + internal void CreateComponents() + { + 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 += OnBackButtonClicked; + + View moreView = new View() + { + Layout = new LinearLayout() + { + LinearOrientation = LinearLayout.Orientation.Vertical, + HorizontalAlignment = HorizontalAlignment.Center, + }, + WidthSpecification = LayoutParamPolicies.MatchParent, + HeightSpecification = LayoutParamPolicies.MatchParent, + }; + + AppBar = appBar; + Content = moreView; + } + + private void OnBackButtonClicked(object source, ClickedEventArgs args) + { + Navigator.Pop(); + } + } +} \ No newline at end of file diff --git a/SettingWiFi/SettingWiFi/view/WidgetSettingWiFi.cs b/SettingWiFi/SettingWiFi/view/WidgetSettingWiFi.cs new file mode 100755 index 0000000..acc4a5f --- /dev/null +++ b/SettingWiFi/SettingWiFi/view/WidgetSettingWiFi.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using Tizen; +using Tizen.Applications; +using Tizen.NUI; +using Tizen.NUI.Accessibility; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Binding; +using Tizen.NUI.Components; + + +namespace SettingWiFi +{ + internal class WidgetSettingWiFi : Widget + { + Navigator navigator; + + ContentPage mMainPage; + + WiFi mWifi; + + private ContentPage CreateMainPage() + { + var page = new MainPage(mWifi); + page.CreateComponents(); + return page; + } + + protected override void OnCreate(string contentInfo, Window window) + { + navigator = window.GetDefaultNavigator(); + mWifi = new WiFi(); + + mMainPage = CreateMainPage(); + navigator.Push(mMainPage); + } + } +}