pages.js 9.3 KB

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