Skip to content

Commit

Permalink
Add device selector
Browse files Browse the repository at this point in the history
  • Loading branch information
reisxd committed Aug 25, 2022
1 parent 5e46455 commit 2398638
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 8 deletions.
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const {
CheckFileAlreadyExists,
SelectAppVersion,
PatchApp,
CheckForUpdates
CheckForUpdates,
GetDevices,
SetDevice
} = require('./wsEvents/index.js');
const morgan = require('morgan');
const { platform } = require('os');
Expand Down Expand Up @@ -118,6 +120,16 @@ wsServer.on('connection', (ws) => {
break;
}

case 'getDevices': {
await GetDevices(message, ws);
break;
}

case 'setDevice': {
await SetDevice(message, ws);
break;
}

case 'selectApp': {
await SelectApp(message, ws);
break;
Expand Down
81 changes: 81 additions & 0 deletions public/devices/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Version Selector</title>
<link href="../styles/fontawesome.css" rel="stylesheet" type="text/css" />
<link href="../styles/core.css" rel="stylesheet" type="text/css" />
<link href="../styles/apps.css" rel="stylesheet" type="text/css" />
<link href="../styles/dependencies.css" rel="stylesheet" type="text/css" />
<script src="../index.js"></script>
<script src="../colors.js" defer></script>
</head>

<body>
<div id="container">
<main>
<header>
<h1>Select the device you want to install ReVanced to</h1>
<span>ReVanced Builder has detected that you have more than one device plugged in.<br>Please select the device ID that you want to install ReVanced.</span>
</header>
<div id="content--wrapper">
<div id="content">
<ul id="devices"></ul>
</div>
</div>
</main>
<footer>
<i
class="social-icon fa-brands fa-xl fa-github"
onclick="openGitHub();"
></i>
<div style="margin-left: 10px"></div>
<i
class="social-icon fa-xl fa-solid fa-question-circle"
onclick="openAbout();"
></i>
<div style="margin-left: 10px"></div>
<i
class="social-icon fa-xl fa-solid fa-palette"
onclick="openColorPicker();"
></i>
<div class="theming">
<ul id="themecolor-picker">
<li>
<span style="background: #4873b3 none repeat scroll 0% 0%"></span>
</li>
<li>
<span
style="background: #df0000bf none repeat scroll 0% 0%"
></span>
</li>
<li>
<span
style="background: #605dbbb3 none repeat scroll 0% 0%"
></span>
</li>
<li>
<span
style="background: #00e60069 none repeat scroll 0% 0%"
></span>
</li>
<li>
<span
style="background: #0067fdc7 none repeat scroll 0% 0%"
></span>
</li>
</ul>
</div>
<span class="right"></span>
<button id="back" onclick="history.back()">Back</button>
<button class="highlighted" id="continue" onclick="setDevice()">
Continue
</button>
</footer>
</div>
</body>
<script>
ws.onopen = () => getDevices();
</script>
</html>
36 changes: 36 additions & 0 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ window.addEventListener('keypress', (e) => {
}
});


function setDevice () {
if (!document.querySelector('input[name="device"]:checked')) {
return alert("You didn't select an device!");
}

sendCommand({
event: 'setDevice',
selectedApp: document.querySelector('input[name="device"]:checked').value
});

location.href = '/patches';
}

function getDevices() {
sendCommand({ event: 'getDevices' });
}

ws.onmessage = (msg) => {
const message = JSON.parse(msg.data);
switch (message.event) {
Expand Down Expand Up @@ -356,6 +374,24 @@ ws.onmessage = (msg) => {
<dialog>
<span>Your current version of Builder is not up to date.<br>Do you want to update to ${message.builderVersion}?</span>
<div class="buttonContainer"><button class="highlighted" onclick="window.open('https://github.com/reisxd/revanced-builder/releases/latest', '_blank'); document.getElementById('container').removeChild(document.getElementsByTagName('dialog')[0]);">Yes</button> <button onclick="document.getElementById('container').removeChild(document.getElementsByTagName('dialog')[0]);">No</button></div></dialog>`;
break;
}

case 'multipleDevices': {
location.href = '/devices';
break;
}

case 'devices': {
let i = 0;
for (const deviceId of message.deviceIds) {
document.getElementById('devices').innerHTML += `
<li>
<input type="radio" name="device" id="app-${i}" value="${deviceId}"/>
<label for="app-${i}">${deviceId}</label></li>`;
i++;
}
break;
}
}
};
10 changes: 9 additions & 1 deletion utils/checkJDKAndADB.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ module.exports = async function (ws) {
})
);
}
await getDeviceID();
const deviceIds = await getDeviceID();

if (deviceIds[1]) {
return ws.send(JSON.stringify({
event: 'multipleDevices'
}));
} else {
global.jarNames.deviceID = deviceIds[0];
}
} catch (e) {
if (e.stderr.includes('java')) {
return ws.send(
Expand Down
11 changes: 6 additions & 5 deletions utils/getDeviceID.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ const actualExec = promisify(exec);
module.exports = async function () {
try {
const { stdout } = await actualExec('adb devices');
const adbDeviceIdRegex = new RegExp(`${os.EOL}(.*?)\t`);
const adbDeviceIdRegex = new RegExp(`${os.EOL}(.*?)\t`, 'g');
const match = stdout.match(adbDeviceIdRegex);
if (match === null) {
return null;
}

const deviceIdN = match[1];
global.jarNames.deviceID = deviceIdN;
return deviceIdN;
let deviceIds = [];
for (let deviceId of match) {
deviceIds.push(deviceId.replace(os.EOL, '').replace('\t', ''));
}
return deviceIds;
} catch (e) {
return null;
}
Expand Down
11 changes: 11 additions & 0 deletions wsEvents/GetDevices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const getDeviceID = require('../utils/getDeviceID.js');

module.exports = async function (message, ws) {
const deviceIds = await getDeviceID();

return ws.send(JSON.stringify({
event: 'devices',
deviceIds
}));
};

4 changes: 4 additions & 0 deletions wsEvents/SetDevice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = async function (message, ws) {
global.jarNames.deviceID = message.deviceId;
};

6 changes: 5 additions & 1 deletion wsEvents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const GetPatches = require('./GetPatches.js');
const GetAppVersion = require('./GetAppVersion.js');
const CheckFileAlreadyExists = require('./CheckFileAlreadyExists.js');
const CheckForUpdates = require('./CheckForUpdates.js');
const GetDevices = require('./GetDevices.js');
const SetDevice = require('./SetDevice.js');

module.exports = {
PatchApp,
Expand All @@ -17,5 +19,7 @@ module.exports = {
GetPatches,
GetAppVersion,
CheckFileAlreadyExists,
CheckForUpdates
CheckForUpdates,
GetDevices,
SetDevice
};

0 comments on commit 2398638

Please sign in to comment.