Exemplo n.º 1
0
        public async Task NotifyChange()
        {
            if (!_hasWearCompanion)
            {
                return;
            }

            ICollection <INode> nodes;

            try
            {
                nodes = (await WearableClass.GetCapabilityClient(_context)
                         .GetCapabilityAsync(RefreshCapability, CapabilityClient.FilterReachable)).Nodes;
            }
            catch (ApiException e)
            {
                Logger.Error(e);
                return;
            }

            var client = WearableClass.GetMessageClient(_context);

            foreach (var node in nodes)
            {
                try
                {
                    await client.SendMessageAsync(node.Id, RefreshCapability, Array.Empty <byte>());
                }
                catch (ApiException e)
                {
                    Logger.Error(e);
                }
            }
        }
Exemplo n.º 2
0
 public async Task StartListening()
 {
     if (_hasWearAPIs)
     {
         await WearableClass.GetCapabilityClient(_context).AddListenerAsync(this, RefreshCapability);
     }
 }
        protected override async void OnResume()
        {
            base.OnResume();
            await _onCreateLock.WaitAsync();

            try
            {
                await WearableClass.GetMessageClient(this).AddListenerAsync(this);
                await FindServerNode();
            }
            catch (ApiException)
            {
                RunOnUiThread(CheckOfflineState);
                return;
            }

            await Refresh();

            RunOnUiThread(delegate
            {
                CheckOfflineState();
                CheckEmptyState();
            });

            _onResumeLock.Release();
        }
Exemplo n.º 4
0
        private async Task Refresh()
        {
            if (_serverNode == null)
            {
                return;
            }

            Interlocked.Exchange(ref _responsesReceived, 0);
            Interlocked.Exchange(ref _responsesRequired, 4);

            var client = WearableClass.GetMessageClient(this);

            async Task MakeRequest(string capability)
            {
                await client.SendMessageAsync(_serverNode.Id, capability, new byte[] { });
            }

            await _requestLock.WaitAsync();

            await MakeRequest(ListAuthenticatorsCapability);
            await MakeRequest(ListCategoriesCapability);
            await MakeRequest(ListCustomIconsCapability);
            await MakeRequest(GetPreferencesCapability);

            _requestLock.Release();

            await _responseLock.WaitAsync();
        }
Exemplo n.º 5
0
        private async Task FindServerNode()
        {
            var capabilityInfo = await WearableClass.GetCapabilityClient(this)
                                 .GetCapabilityAsync(ProtocolVersion, CapabilityClient.FilterReachable);

            var capableNode = capabilityInfo.Nodes.FirstOrDefault(n => n.IsNearby);

            if (capableNode == null)
            {
                _serverNode = null;
                return;
            }

            // Immediately after disconnecting from the phone, the device may still show up in the list of reachable nodes.
            // But since it's disconnected, any attempt to send a message will fail.
            // So, make sure that the phone *really* is connected before continuing.
            try
            {
                await WearableClass.GetMessageClient(this).SendMessageAsync(capableNode.Id, ProtocolVersion, new byte[] { });

                _serverNode = capableNode;
            }
            catch (ApiException)
            {
                _serverNode = null;
            }
        }
Exemplo n.º 6
0
        private async Task FindServerNode()
        {
            var capabilityInfo = await WearableClass.GetCapabilityClient(this)
                                 .GetCapabilityAsync(QueryCapability, CapabilityClient.FilterReachable);

            _serverNode =
                capabilityInfo.Nodes.FirstOrDefault(n => n.IsNearby);
        }
Exemplo n.º 7
0
        protected override async void OnStop()
        {
            base.OnStop();
            _timer.Stop();
            await WearableClass.GetMessageClient(this).RemoveListenerAsync(this);

            Finish();
        }
Exemplo n.º 8
0
        protected override async void OnPause()
        {
            base.OnPause();

            try
            {
                await WearableClass.GetMessageClient(this).RemoveListenerAsync(this);
            }
            catch (ApiException) { }
        }
Exemplo n.º 9
0
        protected override async void OnResume()
        {
            base.OnResume();

            _loadingLayout.Visibility = ViewStates.Visible;
            await WearableClass.GetMessageClient(this).AddListenerAsync(this);

            await FindServerNode();
            await Refresh();
        }
Exemplo n.º 10
0
        private async Task GetPreferences(string nodeId)
        {
            var preferences = new PreferenceWrapper(this);
            var settings    = new WearPreferences(preferences.DefaultCategory);
            var json        = JsonConvert.SerializeObject(settings);
            var data        = Encoding.UTF8.GetBytes(json);

            await WearableClass.GetMessageClient(this)
            .SendMessageAsync(nodeId, GetPreferencesCapability, data);
        }
Exemplo n.º 11
0
        private async Task ListAuthenticators(string nodeId)
        {
            var response = _source.Authenticators.Select(item =>
                                                         new WearAuthenticatorResponse(item.Type, item.Icon, item.Issuer, item.Username, item.Period, item.Digits)).ToList();

            var json = JsonConvert.SerializeObject(response);
            var data = Encoding.UTF8.GetBytes(json);

            await WearableClass.GetMessageClient(this).SendMessageAsync(nodeId,
                                                                        ListCapability, data);
        }
Exemplo n.º 12
0
        protected override async void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.activityMain);

            try
            {
                await WearableClass.GetMessageClient(this).AddListenerAsync(this);
                await FindServerNode();
            }
            catch (ApiException) { }

            _loadingLayout = FindViewById <RelativeLayout>(Resource.Id.layoutLoading);
            _emptyLayout   = FindViewById <RelativeLayout>(Resource.Id.layoutEmpty);
            _offlineLayout = FindViewById <LinearLayout>(Resource.Id.layoutOffline);

            _authList = FindViewById <WearableRecyclerView>(Resource.Id.list);
            _authList.EdgeItemsCenteringEnabled = true;
            _authList.HasFixedSize = true;
            _authList.SetItemViewCacheSize(12);
            _authList.SetItemAnimator(null);

            var layoutCallback = new ScrollingListLayoutCallback(Resources.Configuration.IsScreenRound);

            _authList.SetLayoutManager(new WearableLinearLayoutManager(this, layoutCallback));

            _authCache       = new ListCache <WearAuthenticator>("authenticators", this);
            _categoryCache   = new ListCache <WearCategory>("categories", this);
            _customIconCache = new CustomIconCache(this);

            await _authCache.Init();

            _authSource = new AuthenticatorSource(_authCache);

            _authListAdapter              = new AuthenticatorListAdapter(_authSource, _customIconCache);
            _authListAdapter.ItemClick   += OnItemClick;
            _authListAdapter.HasStableIds = true;
            _authList.SetAdapter(_authListAdapter);

            if (_authCache.GetItems().Count > 0 || _serverNode == null)
            {
                UpdateViewState();
            }

            await _categoryCache.Init();

            var categoriesDrawer = FindViewById <WearableNavigationDrawerView>(Resource.Id.drawerCategories);

            _categoryListAdapter = new CategoryListAdapter(this, _categoryCache);
            categoriesDrawer.SetAdapter(_categoryListAdapter);
            categoriesDrawer.ItemSelected += OnCategorySelected;

            await Refresh();
        }
Exemplo n.º 13
0
        protected override async void OnPause()
        {
            base.OnPause();

            _timer?.Stop();
            _pauseTime = DateTime.Now;

            if (_hasWearAPIs)
            {
                await WearableClass.GetCapabilityClient(this).RemoveListenerAsync(this, WearRefreshCapability);
            }
        }
Exemplo n.º 14
0
        private async Task ListCategories(string nodeId)
        {
            await _categorySource.Update();

            var categories =
                _categorySource.GetView().Select(c => new WearCategory(c.Id, c.Name)).ToList();

            var json = JsonConvert.SerializeObject(categories);
            var data = Encoding.UTF8.GetBytes(json);

            await WearableClass.GetMessageClient(this)
            .SendMessageAsync(nodeId, ListCategoriesCapability, data);
        }
Exemplo n.º 15
0
        private async Task ListCustomIcons(string nodeId)
        {
            await _customIconSource.Update();

            var ids = new List <string>();

            _customIconSource.GetView().ForEach(i => ids.Add(i.Id));

            var json = JsonConvert.SerializeObject(ids);
            var data = Encoding.UTF8.GetBytes(json);

            await WearableClass.GetMessageClient(this)
            .SendMessageAsync(nodeId, ListCustomIconsCapability, data);
        }
Exemplo n.º 16
0
        protected override async void OnResume()
        {
            base.OnResume();

            await _onCreateLock.WaitAsync();

            _onCreateLock.Release();

            try
            {
                await WearableClass.GetMessageClient(this).AddListenerAsync(this);
                await FindServerNode();
            }
            catch (ApiException)
            {
                RunOnUiThread(CheckOfflineState);
                return;
            }

            RunOnUiThread(CheckOfflineState);
            await Refresh();

            await _refreshLock.WaitAsync();

            _refreshLock.Release();

            var defaultCategory = _preferences.DefaultCategory;

            if (defaultCategory == null)
            {
                RunOnUiThread(CheckEmptyState);
                return;
            }

            _authSource.SetCategory(defaultCategory);
            var position = _categoryCache.FindIndex(c => c.Id == defaultCategory) + 1;

            if (position < 0)
            {
                return;
            }

            RunOnUiThread(delegate
            {
                _categoryList.SetCurrentItem(position, false);
                _authListAdapter.NotifyDataSetChanged();
                CheckEmptyState();
            });
        }
Exemplo n.º 17
0
        private async Task GetCustomIcon(string customIconId, string nodeId)
        {
            var icon = await _customIconRepository.GetAsync(customIconId);

            var data = Array.Empty <byte>();

            if (icon != null)
            {
                var response = new WearCustomIcon(icon.Id, icon.Data);
                var json     = JsonConvert.SerializeObject(response);
                data = Encoding.UTF8.GetBytes(json);
            }

            await WearableClass.GetMessageClient(this).SendMessageAsync(nodeId, GetCustomIconCapability, data);
        }
Exemplo n.º 18
0
        private async Task Refresh()
        {
            // Send the position as a string instead of an int, because dealing with endianess sucks.
            var data = Encoding.UTF8.GetBytes(_position.ToString());

            try
            {
                await WearableClass.GetMessageClient(this)
                .SendMessageAsync(_nodeId, WearGetCodeCapability, data);
            }
            // If the connection has dropped, just go back
            catch (ApiException)
            {
                Finish();
            }
        }
Exemplo n.º 19
0
        private async Task Refresh()
        {
            if (_serverNode == null)
            {
                AnimUtil.FadeInView(_disconnectedLayout, 200, true);
                _loadingLayout.Visibility = ViewStates.Invisible;
                return;
            }

            AnimUtil.FadeOutView(_disconnectedLayout, 200);
            AnimUtil.FadeOutView(_emptyLayout, 200);
            AnimUtil.FadeOutView(_authList, 200);

            await WearableClass.GetMessageClient(this)
            .SendMessageAsync(_serverNode.Id, ListCapability, new byte[] { });
        }
Exemplo n.º 20
0
        public async Task StopListening()
        {
            if (!_hasWearApis)
            {
                return;
            }

            try
            {
                await WearableClass.GetCapabilityClient(_context).RemoveListenerAsync(this, RefreshCapability);
            }
            catch (ApiException e)
            {
                Logger.Error(e);
            }
        }
Exemplo n.º 21
0
        protected override async void OnResume()
        {
            base.OnResume();
            _loadingLayout.Visibility = ViewStates.Visible;

            try
            {
                await WearableClass.GetMessageClient(this).AddListenerAsync(this);
                await FindServerNode();
                await Refresh();
            }
            catch (ApiException)
            {
                Toast.MakeText(this, Resource.String.connectionError, ToastLength.Long).Show();
                _disconnectedLayout.Visibility = ViewStates.Visible;
            }
        }
Exemplo n.º 22
0
        private async Task GetCustomIcon(string customIconId, string nodeId)
        {
            await _customIconSource.Update();

            var icon = _customIconSource.Get(customIconId);

            var data = new byte[] { };

            if (icon != null)
            {
                var response = new WearCustomIcon(icon.Id, icon.Data);
                var json     = JsonConvert.SerializeObject(response);
                data = Encoding.UTF8.GetBytes(json);
            }

            await WearableClass.GetMessageClient(this).SendMessageAsync(nodeId, GetCustomIconCapability, data);
        }
Exemplo n.º 23
0
        private async Task NotifyWearAppOfChange()
        {
            if (!_hasWearCompanion)
            {
                return;
            }

            var nodes = (await WearableClass.GetCapabilityClient(this)
                         .GetCapabilityAsync(WearRefreshCapability, CapabilityClient.FilterReachable)).Nodes;

            var client = WearableClass.GetMessageClient(this);

            foreach (var node in nodes)
            {
                await client.SendMessageAsync(node.Id, WearRefreshCapability, new byte[] { });
            }
        }
Exemplo n.º 24
0
        private async Task GetCode(int position, string nodeId)
        {
            var auth = _source.Authenticators.ElementAtOrDefault(position);
            var data = new byte[] {};

            if (auth != null)
            {
                var code     = auth.GetCode();
                var response = new WearAuthenticatorCodeResponse(code, auth.TimeRenew);

                var json = JsonConvert.SerializeObject(response);
                data = Encoding.UTF8.GetBytes(json);
            }

            await WearableClass.GetMessageClient(this).SendMessageAsync(nodeId,
                                                                        GetCodeCapability, data);
        }
Exemplo n.º 25
0
        private async Task Refresh()
        {
            if (_serverNode == null)
            {
                return;
            }

            Interlocked.Exchange(ref _responsesReceived, 0);
            Interlocked.Exchange(ref _responsesRequired, 3);

            await WearableClass.GetMessageClient(this)
            .SendMessageAsync(_serverNode.Id, ListAuthenticatorsCapability, new byte[] { });

            await WearableClass.GetMessageClient(this)
            .SendMessageAsync(_serverNode.Id, ListCategoriesCapability, new byte[] { });

            await WearableClass.GetMessageClient(this)
            .SendMessageAsync(_serverNode.Id, ListCustomIconsCapability, new byte[] { });
        }
Exemplo n.º 26
0
        protected override async void OnResume()
        {
            base.OnResume();

            await _onCreateLock.WaitAsync();

            _onCreateLock.Release();

            try
            {
                await WearableClass.GetMessageClient(this).AddListenerAsync(this);
                await FindServerNode();
            }
            catch (ApiException)
            {
                RunOnUiThread(CheckOfflineState);
                return;
            }

            if (_justLaunched)
            {
                _justLaunched = false;
                var defaultAuth = _preferences.DefaultAuth;

                if (defaultAuth != null)
                {
                    var authPosition = _authSource.FindIndex(a => a.Secret.GetHashCode() == defaultAuth);
                    OnItemClick(null, authPosition);
                }
            }

            await Refresh();

            RunOnUiThread(delegate
            {
                AnimUtil.FadeOutView(_loadingLayout, AnimUtil.LengthShort, false, delegate
                {
                    CheckOfflineState();
                    CheckEmptyState();
                });
            });
        }
Exemplo n.º 27
0
        protected override async void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.activityCode);

            _progressBar  = FindViewById <ProgressBar>(Resource.Id.progressBar);
            _codeTextView = FindViewById <TextView>(Resource.Id.textCode);

            var usernameText = FindViewById <TextView>(Resource.Id.textUsername);

            usernameText.Text = Intent.Extras.GetString("username");

            var iconView = FindViewById <ImageView>(Resource.Id.imageIcon);

            iconView.SetImageResource(Icon.GetService(Intent.Extras.GetString("icon"), true));

            _nodeId   = Intent.Extras.GetString("nodeId");
            _position = Intent.Extras.GetInt("position");

            _period = Intent.Extras.GetInt("period");
            _digits = Intent.Extras.GetInt("digits");
            _type   = (AuthenticatorType)Intent.Extras.GetInt("type");

            _timer = new Timer {
                Interval  = 1000,
                AutoReset = true
            };

            switch (_type)
            {
            case AuthenticatorType.Totp:
                _timer.Enabled  = true;
                _timer.Elapsed += Tick;
                break;

            case AuthenticatorType.Hotp:
                _progressBar.Visibility = ViewStates.Invisible;
                break;
            }

            await WearableClass.GetMessageClient(this).AddListenerAsync(this);
        }
Exemplo n.º 28
0
        protected override async void OnResume()
        {
            base.OnResume();

            await _onCreateLock.WaitAsync();

            _onCreateLock.Release();

            try
            {
                await WearableClass.GetMessageClient(this).AddListenerAsync(this);
                await FindServerNode();
            }
            catch (ApiException)
            {
                RunOnUiThread(UpdateViewState);
                return;
            }

            RunOnUiThread(UpdateViewState);
            await Refresh();
        }
Exemplo n.º 29
0
        private async Task DetectWearOSCapability()
        {
            if (!_areGoogleAPIsAvailable)
            {
                _hasWearAPIs      = false;
                _hasWearCompanion = false;
                return;
            }

            try
            {
                var capabiltyInfo = await WearableClass.GetCapabilityClient(this)
                                    .GetCapabilityAsync(WearRefreshCapability, CapabilityClient.FilterReachable);

                _hasWearAPIs      = true;
                _hasWearCompanion = capabiltyInfo.Nodes.Count > 0;
            }
            catch (ApiException)
            {
                _hasWearAPIs      = false;
                _hasWearCompanion = false;
            }
        }
Exemplo n.º 30
0
        private async Task GetSyncBundle(string nodeId)
        {
            await _authenticatorView.LoadFromPersistenceAsync();

            var auths = new List <WearAuthenticator>();

            var authCategories = await _authenticatorCategoryRepository.GetAllAsync();

            foreach (var auth in _authenticatorView)
            {
                var bindings = authCategories
                               .Where(c => c.AuthenticatorSecret == auth.Secret)
                               .Select(c => new WearAuthenticatorCategory(c.CategoryId, c.Ranking))
                               .ToList();

                var item = new WearAuthenticator(
                    auth.Type, auth.Secret, auth.Icon, auth.Issuer, auth.Username, auth.Period, auth.Digits,
                    auth.Algorithm, auth.Ranking, bindings);

                auths.Add(item);
            }

            var categories = (await _categoryRepository.GetAllAsync()).Select(c => new WearCategory(c.Id, c.Name))
                             .ToList();
            var customIconIds = (await _customIconRepository.GetAllAsync()).Select(i => i.Id).ToList();

            var preferenceWrapper = new PreferenceWrapper(this);
            var preferences       = new WearPreferences(
                preferenceWrapper.DefaultCategory, preferenceWrapper.SortMode, preferenceWrapper.CodeGroupSize);

            var bundle = new WearSyncBundle(auths, categories, customIconIds, preferences);

            var json = JsonConvert.SerializeObject(bundle);
            var data = Encoding.UTF8.GetBytes(json);

            await WearableClass.GetMessageClient(this).SendMessageAsync(nodeId, GetSyncBundleCapability, data);
        }