pages.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 adbRun
  14. module.exports = {
  15. removeCompatibleApps: async () => {
  16. let installedAppList
  17. common.header('Remove Installed Apps')
  18. await shellExec(adbRun + ' shell pm list packages -3').then(async function (result) {
  19. installedAppList = result.stdout.split('\r\n'); // split string on comma space
  20. installedAppList.splice(-1, 1)
  21. });
  22. const value = await inquirer.installedApps(installedAppList);
  23. for (let element of value.removeAppsList) {
  24. console.log('Removing ' + element)
  25. const package = element.substring(8)
  26. await shellExec(adbRun + ' uninstall ' + package).then(async function (result) {
  27. console.log(element + ' - ' + result.stdout);
  28. });
  29. }
  30. console.log(chalk.green('Removed Selected User Apps'))
  31. await common.pause(2000)
  32. module.exports.mainMenu()
  33. },
  34. compatibleApps: async () => {
  35. common.header('Install Compatible Apps')
  36. let compatibleApps
  37. let url = "http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json";
  38. http.get(url, (res) => {
  39. let body = "";
  40. res.on("data", (chunk) => {
  41. body += chunk;
  42. });
  43. res.on("end", () => {
  44. try {
  45. compatibleApps = JSON.parse(body);
  46. // do something with JSON
  47. } catch (error) {
  48. console.error(error.message);
  49. };
  50. });
  51. }).on("error", (error) => {
  52. console.error(error.message);
  53. });
  54. const value = await inquirer.compatibleApps();
  55. await shell.rm('-rf', './data/apps/*.apk');
  56. for (let element of value.removeAppsList) {
  57. for (let element2 of compatibleApps) {
  58. if (element === element2.name) {
  59. const options = {
  60. override: true,
  61. }
  62. const dl = new DownloaderHelper(element2.url, './data/apps/', options);
  63. dl.on('end', () => console.log('Downloading Latest ' + element2.name + ' Complete'))
  64. await dl.start();
  65. }
  66. }
  67. }
  68. const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
  69. for (let element of apkList) {
  70. console.log('Installing ' + element)
  71. await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
  72. console.log(element + ' - ' + result.stdout);
  73. if (element === "data\\apps\\MoreLocale.apk") {
  74. await shellExec(adbRun + ' shell pm grant jp.co.c_lis.ccl.morelocale android.permission.CHANGE_CONFIGURATION').then(function (result) {
  75. console.log('moreLocale Activated On Watch');
  76. });
  77. }
  78. });
  79. }
  80. console.log(chalk.green('Compatible Apps Installed'))
  81. await common.pause(2000)
  82. module.exports.mainMenu()
  83. },
  84. removeApps: async () => {
  85. common.header('Remove Apps')
  86. const value = await inquirer.removeAppsList();
  87. for (let element of value.removeAppsList) {
  88. await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
  89. console.log('Removing ' + element + ' - ' + result.stdout);
  90. });
  91. }
  92. console.log(chalk.green('Removal Complete'))
  93. await common.pause(2000)
  94. module.exports.mainMenu()
  95. },
  96. restoreApps: async () => {
  97. common.header('Restore Apps')
  98. const value = await inquirer.removeAppsList();
  99. for (let element of value.removeAppsList) {
  100. await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
  101. console.log('Restoring ' + element + ' - ' + result.stdout);
  102. });
  103. }
  104. console.log(chalk.green('Restore Complete'))
  105. await common.pause(2000)
  106. module.exports.mainMenu()
  107. },
  108. connectWifi: async () => {
  109. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  110. common.header('Connect Wifi')
  111. if (miwatchData.ipAddress !== "") {
  112. console.log('Trying to connect with stored ipAddress')
  113. shellExec(adbRun + ' connect ' + miwatchData.ipAddress).then(async function (result) {
  114. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  115. console.log(chalk.green('MiWatch Connected'))
  116. await common.pause(3000)
  117. module.exports.mainMenu()
  118. } else {
  119. console.log(chalk.red('MiWatch not found'))
  120. await common.pause(2000)
  121. console.log(chalk.white('Try Again'))
  122. await common.pause(1000)
  123. module.exports.connectWifi()
  124. }
  125. }).catch()
  126. } else {
  127. const value = await inquirer.connectWifi();
  128. const miWatchIpaddress = value.connectWifi
  129. shellExec(adbRun + ' connect ' + miWatchIpaddress).then(async function (result) {
  130. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  131. console.log(chalk.green('MiWatch Connected'))
  132. files.writeIpAddress(miWatchIpaddress)
  133. await common.pause(3000)
  134. module.exports.mainMenu()
  135. } else {
  136. console.log(chalk.red('MiWatch not found'))
  137. await common.pause(2000)
  138. console.log(chalk.white('Try Again'))
  139. await common.pause(1000)
  140. module.exports.connectWifi()
  141. }
  142. }).catch()
  143. }
  144. },
  145. mainMenu: async () => {
  146. common.header('Main Menu')
  147. if (process.platform === 'win32' || process.platform === 'win64') {
  148. adbRun = 'adb'
  149. } else {
  150. adbRun = './adb'
  151. }
  152. const mainMenuSelection = await inquirer.mainMenu();
  153. switch (mainMenuSelection.mainMenu) {
  154. case 'connect to miwatch via wifi':
  155. module.exports.connectWifi()
  156. break;
  157. case 'remove xiaomi apps':
  158. module.exports.removeApps()
  159. break;
  160. case 'restore xiaomi apps':
  161. module.exports.restoreApps()
  162. break;
  163. case 'install compatible apps':
  164. module.exports.compatibleApps()
  165. break;
  166. case 'remove installed apps':
  167. module.exports.removeCompatibleApps()
  168. break;
  169. case 'quit':
  170. break;
  171. default:
  172. // code block
  173. }
  174. }
  175. };