VulpesOS/assets/webapps/bluetooth.gaiamobile.org/js/settings.js
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

1 line
No EOL
10 KiB
JavaScript

;navigator.mozL10n.once(function(){function e(e){l.checked=e}function t(){if(c.enabled){var e=c.getDefaultAdapter();e.onsuccess=function(){return d=e.result,null==d?void o.createLock().set({"bluetooth.enabled":!1}):(d.ondevicefound=v.onDeviceFound,u.initWithAdapter(),void v.initWithAdapter())},e.onerror=function(){o.createLock().set({"bluetooth.enabled":!1})}}}var n={HFP:4382,A2DP:4365},i=navigator.mozL10n.get,o=(navigator.mozL10n,navigator.mozSettings),c=navigator.mozBluetooth,d=null;const r="bluetooth.device.connected";var a=20;if(o&&c){var s=document.querySelector("gaia-header");s.addEventListener("action",function(){window.close()});var l=document.querySelector(".bluetooth-status gaia-switch");l.addEventListener("change",function(){console.log("GOT CHANGE EVENT!, setting disabled");var e=o.createLock().set({"bluetooth.enabled":this.checked});l.setAttribute("disabled",!0),e.onerror=function(){l.removeAttribute("disabled")}});var u=function(){function e(e){var t=d.setName(e);t.onsuccess=function(){E=l.textContent=d.name}}function t(e){if(v.hidden=!e,e){s.hidden=!1;var t=o.createLock().get("bluetooth.visible");t.onsuccess=function(){var e=t.result["bluetooth.visible"];"undefined"==typeof e&&(e=!0),r(e)},t.onerror=function(){u.checked=!0}}else s.hidden=!0,f.disabled=!0,g&&(clearTimeout(g),g=null)}function n(){u.checked=d.discoverable,r(u.checked),setTimeout(function(){E=l.textContent=d.name,f.disabled=!1},1e3)}function r(e){c.enabled&&d&&(o.createLock().set({"bluetooth.visible":e}),d.setDiscoverable(e),e?g||(g=setTimeout(function(){r(!1)},y)):g&&(clearTimeout(g),g=null),u.checked=e)}var s=document.querySelector(".device-visible"),l=document.querySelector(".bluetooth-device-name"),u=document.querySelector(".device-visible gaia-switch"),v=document.getElementById("bluetooth-rename"),f=document.getElementById("rename-device"),h=document.getElementById("update-device-name"),m=document.getElementById("update-device-name-input"),p=document.getElementById("update-device-name-cancel"),b=document.getElementById("update-device-name-confirm"),g=null,y=12e4,E="";return u.addEventListener("change",function(){r(u.checked)}),f.onclick=function(){""===E&&(E=l.textContent=d.name),m.value=E,h.hidden=!1,m.focus();var e=m.value.length;m.setSelectionRange(0,e)},p.onclick=function(e){e&&e.preventDefault(),h.hidden=!0},b.onclick=function(t){t&&t.preventDefault();var n=m.value;if(n=n.replace(/^\s+|\s+$/g,""),n.length>a){var r=window.confirm(i("bluetooth-name-maxlength-alert",{length:a}));return void(r||(h.hidden=!0))}if(n===E||!c.enabled||!d)return void(h.hidden=!0);if(""!==n)e(n);else{var s=o.createLock().get("deviceinfo.product_model");s.onsuccess=function(){var t=s.result["deviceinfo.product_model"];e(t)}}h.hidden=!0},{update:t,initWithAdapter:n}}(),v=function(){function e(e,t){var n=document.createElement("span");""!==e.name?n.textContent=e.name:n.setAttribute("data-l10n-id","unnamed-device");var i=document.createElement("small");t&&i.setAttribute("data-l10n-id",t);var o=document.createElement("a");o.appendChild(n),o.appendChild(i);var c=document.createElement("li");return c.classList.add("bluetooth-device"),c.classList.add("bluetooth-type-"+e.icon),c.appendChild(o),c}function t(e){x.hidden=!e,e?(C.hidden=!0,O.show(!0),k.hidden=!1,document.addEventListener("visibilitychange",y)):(O.show(!1),D.show(!1),C.hidden=!1,k.hidden=!0,S.close(),w=null,L=null,P=null,clearTimeout(I),I=null,document.removeEventListener("visibilitychange",y))}function a(){d.onpairedstatuschanged=function(e){dispatchEvent(new CustomEvent("bluetooth-pairedstatuschanged")),f(e.status,"Authentication Failed")},d.ondiscoverystatechanged=function(e){e.discovering?(A.disabled=!0,k.hidden=!1):(A.disabled=!1,k.hidden=!0,clearTimeout(I),I=null)},d.onhfpstatuschanged=function(e){b(e.address,e.status,n.HFP)},d.ona2dpstatuschanged=function(e){b(e.address,e.status,n.A2DP)},l(function(){for(var e in D.index){var t=D.index[e];if(Object.keys(t.connectedProfiles).length>0)return}s()}),g()}function s(){var e=o.createLock().get(r);e.onsuccess=function(){var t=e.result[r];if(t&&D.index[t]){var n=D.index[t].device;p(n)}}}function l(t){if(c.enabled&&d){var n=d.getPairedDevices();n.onsuccess=function(){var i=n.result.slice(),o=i.length;if(0==o)return void D.show(!1);D.clear(),i.sort(function(e,t){return e.name>t.name});for(var c=0;o>c;c++)!function(t){var n=e(t,"");if(n.onclick=function(){S.show(t)},D.list.appendChild(n),D.index[t.address]={device:t,item:n,connectedProfiles:{}},t.address===L&&"audio-card"===t.icon){var i=n.querySelector("small");i.setAttribute("data-l10n-id","device-status-connecting"),setTimeout(function(){p(t)},5e3)}}(i[c]);u(function(e){e.length>0&&e.forEach(function(e){var t=e.device,n=e.connectedProfiles;for(var i in n)b(t.address,!0,i)}),D.show(!0),t&&t()})}}}function u(e){if(e){if(!d)return void e([]);var t=function(e,t){if(t){var n=d.getConnectedDevices(e);n.onsuccess=function(){t(n.result||[])},n.onerror=function(){t(null)}}},i={},o=function(e,t){t&&t.forEach(function(t){var n=i[t.address];n?n.connectedProfiles[e]=!0:(n={device:t,connectedProfiles:{}},n.connectedProfiles[e]=!0),i[t.address]=n})};t(n.HFP,function(c){o(n.HFP,c),t(n.A2DP,function(t){o(n.A2DP,t);var c=[];for(var d in i){var r=i[d];c.push(r)}e(c)})})}}function v(t){var n=t.device,i=O.index[n.address]||D.index[n.address];if(i){var o=i.item;if(n.name&&o){var c=o.querySelector("a > span");c&&(c.removeAttribute("data-l10n-id"),c.textContent=n.name)}}else{var r=e(n,"device-status-tap-connect");r.onclick=function(){if(!w){var e=r.querySelector("small");e.setAttribute("data-l10n-id","device-status-pairing"),this.setAttribute("aria-disabled",!0),E();var t=d.pair(n.address);w=n.address,t.onerror=function(e){f(!1,t.error.name)}}},O.list.appendChild(r),O.index[n.address]={device:n,item:r,connectedProfiles:{}}}}function f(e,t){if(!w)return void l();var n=w;if(w=null,e){if(O.index[n]){var o=(O.index[n].device,O.index[n].item);O.list.removeChild(o),delete O.index[n],L=n}}else{var c=i("error-pair-title");if("Repeated Attempts"===t?c=c+"\n"+i("error-pair-toofast"):"Authentication Failed"===t&&(c=c+"\n"+i("error-pair-pincode")),window.alert(c),O.index[n]){var o=O.index[n].item,d=o.querySelector("small");o.removeAttribute("aria-disabled"),d.setAttribute("data-l10n-id","device-status-tap-connect")}}l()}function h(e){if(e.address===P){var t=i("unpair-title")+"\n"+i("unpair-msg");if(!window.confirm(t))return;P=null}var n=d.unpair(e.address);n.onerror=function(){f(!0,null)}}function m(e,t){if(!c.enabled||!d||e.address!==P)return void(t&&t());var n=d.disconnect(e);n.onsuccess=n.onerror=function(){t&&t()}}function p(e){if(!c.enabled||!d||"audio-card"!==e.icon||e.address===P)return void(L=null);var t=function(){var t=function(){L&&(L=null)},n=function(){if(L){var e=D.index[L].item.querySelector("small");e.removeAttribute("data-l10n-id"),e.textContent="",L=null,window.alert(i("error-connect-msg"))}};E();var o=d.connect(e);if(o.onsuccess=t,o.onerror=n,L=e.address,D.index[L]){var c=D.index[L].item.querySelector("small");c.setAttribute("data-l10n-id","device-status-connecting")}};P&&D.index[P]?m(D.index[P].device,t):t()}function b(e,t,i){var c=D.index[e];if(c){c.connectedProfiles[i]=t;var d=!1;if(t)d=!0;else for(var i in c.connectedProfiles)d=d||c.connectedProfiles[i];d?(P=e,o.createLock().set({"bluetooth.device.connected":P})):P===e&&(P=null,o.createLock().set({"bluetooth.device.connected":null}));var r="",a=c.connectedProfiles[n.HFP],s=c.connectedProfiles[n.A2DP];r=a&&s?"device-status-connected-phone-media":a?"device-status-connected-phone":s?"device-status-connected-media":null;var l=D.index[e].item.querySelector("small");r?l.setAttribute("data-l10n-id",r):(l.removeAttribute("data-l10n-id"),l.textContent="")}}function g(){if(c.enabled&&d&&!I&&!document.hidden){var e=d.startDiscovery();e.onsuccess=function(){I||(I=setTimeout(E,B))},e.onerror=function(){console.error("Can not discover nearby device")}}}function y(){document.hidden&&E()}function E(){if(c.enabled&&d&&I){var e=d.stopDiscovery();e.onerror=function(){console.error("Can not stop discover nearby device")},clearTimeout(I),I=null}}var x=document.getElementById("bluetooth-search"),A=document.getElementById("search-device"),k=document.getElementById("bluetooth-searching"),C=document.getElementById("bluetooth-enable-msg"),w=null,L=null,P=null,B=6e4,I=null,D={title:document.getElementById("bluetooth-paired-title"),list:document.getElementById("bluetooth-paired-devices"),index:[],clear:function(){for(;this.list.hasChildNodes();)this.list.removeChild(this.list.lastChild);this.index=[]},show:function(e){e||this.clear(),this.title.hidden=!e,this.list.hidden=!e}},O={title:document.getElementById("bluetooth-found-title"),list:document.getElementById("bluetooth-devices"),index:[],clear:function(){for(;this.list.hasChildNodes();)this.list.removeChild(this.list.lastChild);this.index=[]},show:function(e){e||this.clear(),this.title.hidden=!e,this.list.hidden=!e}},S={menu:document.getElementById("paired-device-option"),connectOpt:document.getElementById("connect-option"),disconnectOpt:document.getElementById("disconnect-option"),unpairOpt:document.getElementById("unpair-option"),confirmDlg:document.getElementById("unpair-device"),unpairCancel:document.getElementById("unpair-device-cancel"),confirmOpt:document.getElementById("confirm-option"),showActions:function(){var e=this;P&&this.device.address===P?(this.connectOpt.style.display="none",this.disconnectOpt.style.display="block",this.disconnectOpt.onclick=function(){m(e.device)}):(this.connectOpt.style.display="block",this.disconnectOpt.style.display="none",this.connectOpt.onclick=function(){p(e.device)}),this.unpairOpt.onclick=function(){h(e.device)},this.menu.addEventListener("gaiamenu-cancel",function(){return e.close()}),this.menu.show()},showConfirm:function(){var e=this;this.unpairCancel.onclick=function(){return e.close()},this.confirmOpt.onclick=function(){return h(e.device),e.close()},this.confirmDlg.hidden=!1},show:function(e){this.device=e,this["audio-card"===this.device.icon?"showActions":"showConfirm"]()},close:function(){return this.menu.hide(),this.confirmDlg.hidden=!0,!1}};return A.onclick=function(){t(!0),O.clear(),g()},{update:t,initWithAdapter:a,startDiscovery:g,onDeviceFound:v}}(),f=!1;o.addObserver("bluetooth.enabled",function(t){var n=t.settingValue;f!=n&&(l.setAttribute("disabled",!0),f=n,e(n),v.update(n),u.update(n),n||(d=null))});var h=o.createLock().get("bluetooth.enabled");h.onsuccess=function(){f=h.result["bluetooth.enabled"],f&&t(),e(f),v.update(f),u.update(f)},c.addEventListener("adapteradded",function(){l.removeAttribute("disabled"),t()}),c.addEventListener("disabled",function(){l.removeAttribute("disabled"),d=null})}});