pages.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. const chalk = require('chalk');
  2. const common = require('../lib/common');
  3. const inquirer = require('../lib/inquirer');
  4. const shellExec = require('shell-exec')
  5. const files = require('../lib/files')
  6. const fs = require('fs')
  7. const {
  8. DownloaderHelper
  9. } = require('node-downloader-helper');
  10. const getFilesIn = require('get-files-in')
  11. const http = require('http')
  12. var shell = require('shelljs');
  13. let logger = require('perfect-logger');
  14. let adbRun
  15. logger.initialize('RunTIme', {
  16. logLevelFile: 0, // Log level for file
  17. logLevelConsole: -1, // Log level for STDOUT/STDERR
  18. logDirectory: 'data/', // Log directory
  19. });
  20. module.exports = {
  21. removeCompatibleApps: async () => {
  22. let installedAppList
  23. common.header('Remove Installed Apps')
  24. logger.info('Remove Installed Apps')
  25. await shellExec(adbRun + ' shell pm list packages -3').then(async function (result) {
  26. logger.info('Packages recieved from watch')
  27. if (result.stderr.includes('error')) {
  28. logger.info(result.stderr)
  29. console.log(chalk.red('Device not authorised'))
  30. common.pause(3000)
  31. await shellExec(adbRun + ' kill-server').then(async function (result) {
  32. logger.info('Restarting ADB')
  33. logger.info(result.stdout)
  34. console.log('Please reconnect to watch')
  35. common.pause(3000)
  36. logger.info('Remove Installed Apps Failed')
  37. module.exports.mainMenu()
  38. })
  39. } else {
  40. if (process.platform === 'win32' || process.platform === 'win64') {
  41. installedAppList = result.stdout.split('\r\n'); // split string on comma space
  42. installedAppList.splice(-1, 1)
  43. } else {
  44. installedAppList = result.stdout.split('\n'); // split string on comma space
  45. installedAppList.splice(-1, 1)
  46. }
  47. const value = await inquirer.installedApps(installedAppList);
  48. for (let element of value.removeAppsList) {
  49. console.log('Removing ' + element)
  50. logger.info('Removing ' + element)
  51. const package = element.substring(8)
  52. await shellExec(adbRun + ' uninstall ' + package).then(async function (result) {
  53. console.log(element + ' - ' + result.stdout);
  54. logger.info(element + ' - ' + result.stdout);
  55. });
  56. }
  57. console.log(chalk.green('Removed Selected User Apps'))
  58. logger.info('Removed Selected User Apps')
  59. await common.pause(2000)
  60. module.exports.mainMenu()
  61. }
  62. })
  63. },
  64. compatibleApps: async () => {
  65. logger.info("Compatible Apps")
  66. common.header('Install Compatible Apps')
  67. let compatibleApps
  68. let url = "http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json";
  69. http.get(url, (res) => {
  70. let body = "";
  71. res.on("data", (chunk) => {
  72. body += chunk;
  73. });
  74. res.on("end", () => {
  75. try {
  76. compatibleApps = JSON.parse(body);
  77. logger.info("Compatible Apps found online")
  78. // do something with JSON
  79. } catch (error) {
  80. console.error(error.message);
  81. };
  82. });
  83. }).on("error", (error) => {
  84. console.error(error.message);
  85. });
  86. const value = await inquirer.compatibleApps();
  87. await shell.rm('-rf', './data/apps/*.apk');
  88. for (let element of value.removeAppsList) {
  89. for (let element2 of compatibleApps) {
  90. if (element === element2.name) {
  91. const options = {
  92. override: true,
  93. }
  94. const dl = new DownloaderHelper(element2.url, './data/apps/', options);
  95. dl.on('end', () => console.log('Downloading Latest ' + element2.name + ' Complete'),
  96. logger.info('Downloading Latest ' + element2.name + ' Complete')
  97. )
  98. await dl.start();
  99. }
  100. }
  101. }
  102. const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
  103. for (let element of apkList) {
  104. console.log('Installing ' + element)
  105. logger.info('Installing ' + element)
  106. await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
  107. if (result.stderr != '') {
  108. logger.info('Error ' + result.stderr);
  109. console.log(chalk.redBright('Error - Device not authorised'));
  110. }
  111. console.log(element + ' - ' + result.stdout);
  112. logger.info(element + ' - ' + result.stdout);
  113. if (element === "data\\apps\\com.alberto.locale.apk") {
  114. await shellExec(adbRun + ' shell pm grant com.alberto.locale android.permission.CHANGE_CONFIGURATION && ' + adbRun + ' shell am start -n com.alberto.locale/com.alberto.locale.MainActivity && ' + adbRun + ' shell pm grant com.alberto.locale android.permission.CHANGE_CONFIGURATION').then(function (result) {
  115. console.log(result)
  116. console.log('Alberto Locale Activated On Watch');
  117. logger.info('Alberto Locale Activated On Watch');
  118. });
  119. }
  120. });
  121. }
  122. console.log(chalk.green('Compatible Apps Installed'))
  123. logger.info('Compatible Apps Installed')
  124. await common.pause(2000)
  125. module.exports.mainMenu()
  126. },
  127. removeApps: async () => {
  128. logger.info("Remove Apps")
  129. common.header('Remove Apps')
  130. const value = await inquirer.removeAppsList();
  131. for (let element of value.removeAppsList) {
  132. await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
  133. if (result.stderr != '') {
  134. logger.info('Error ' + result.stderr);
  135. console.log(chalk.redBright('Error - Device not authorised'));
  136. } else {
  137. logger.info('Removing ' + element + ' - ' + result.stdout);
  138. console.log('Removing ' + element + ' - ' + result.stdout);
  139. }
  140. });
  141. }
  142. console.log(chalk.green('Removal Complete'))
  143. await common.pause(2000)
  144. logger.info("Remove Complete")
  145. module.exports.mainMenu()
  146. },
  147. restoreApps: async () => {
  148. logger.info("Restore Apps")
  149. common.header('Restore Apps')
  150. const value = await inquirer.removeAppsList();
  151. for (let element of value.removeAppsList) {
  152. await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
  153. if (result.stderr != '') {
  154. logger.info('Error ' + result.stderr);
  155. console.log(chalk.redBright('Error - Device not authorised'));
  156. } else {
  157. logger.info('Restoring ' + element + ' - ' + result.stdout);
  158. console.log('Restoring ' + element + ' - ' + result.stdout);
  159. }
  160. });
  161. }
  162. console.log(chalk.green('Restore Complete'))
  163. await common.pause(2000)
  164. logger.info("Restore Apps Complete")
  165. module.exports.mainMenu()
  166. },
  167. connectWifi: async () => {
  168. logger.info("Connect Wifi")
  169. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  170. common.header('Connect Wifi')
  171. if (miwatchData.ipAddress !== "") {
  172. await shellExec(adbRun + ' kill-server')
  173. console.log('Trying to connect with stored ipAddress')
  174. shellExec(adbRun + ' connect ' + miwatchData.ipAddress).then(async function (result) {
  175. logger.info("Connect Wifi Result " + result.stdout)
  176. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  177. console.log(chalk.green('MiWatch Connected'))
  178. await common.pause(3000)
  179. logger.info("Connect Wifi Complete")
  180. module.exports.mainMenu()
  181. } else {
  182. console.log(chalk.red('MiWatch not found'))
  183. await common.pause(2000)
  184. files.writeIpAddress('')
  185. console.log(chalk.white('Try Again'))
  186. await common.pause(1000)
  187. module.exports.connectWifi()
  188. }
  189. }).catch()
  190. } else {
  191. await shellExec(adbRun + ' kill-server')
  192. const value = await inquirer.connectWifi();
  193. const miWatchIpaddress = value.connectWifi
  194. shellExec(adbRun + ' connect ' + miWatchIpaddress).then(async function (result) {
  195. logger.info("Connect Wifi Result " + result.stdout)
  196. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  197. console.log(chalk.green('MiWatch Connected'))
  198. files.writeIpAddress(miWatchIpaddress)
  199. await common.pause(3000)
  200. logger.info("Connect Wifi Complete")
  201. module.exports.mainMenu()
  202. } else {
  203. if (result.stdout.includes('failed to authenticate')) {
  204. console.log(chalk.redBright('MiWatch not authenticated'))
  205. } else {
  206. console.log(chalk.red('MiWatch not found'))
  207. }
  208. await common.pause(2000)
  209. console.log(chalk.white('Try Again'))
  210. await common.pause(1000)
  211. module.exports.connectWifi()
  212. }
  213. }).catch()
  214. }
  215. },
  216. mainMenu: async () => {
  217. common.header('Main Menu')
  218. if (process.platform === 'win32' || process.platform === 'win64') {
  219. adbRun = 'adb'
  220. } else {
  221. adbRun = './adb'
  222. }
  223. logger.info(process.platform + " detected")
  224. const mainMenuSelection = await inquirer.mainMenu();
  225. switch (mainMenuSelection.mainMenu) {
  226. case 'connect to miwatch via wifi':
  227. module.exports.connectWifi()
  228. break;
  229. case 'remove xiaomi apps':
  230. module.exports.removeApps()
  231. break;
  232. case 'restore xiaomi apps':
  233. module.exports.restoreApps()
  234. break;
  235. case 'install compatible apps':
  236. module.exports.compatibleApps()
  237. break;
  238. case 'remove installed apps':
  239. module.exports.removeCompatibleApps()
  240. break;
  241. case 'quit':
  242. break;
  243. default:
  244. // code block
  245. }
  246. }
  247. };