pages.js 7.9 KB

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