pages.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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(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. }
  72. console.log(chalk.green('Compatible Apps Installed'))
  73. await common.pause(2000)
  74. module.exports.mainMenu()
  75. },
  76. removeApps: async () => {
  77. common.header('Remove Apps')
  78. const value = await inquirer.removeAppsList();
  79. for (let element of value.removeAppsList) {
  80. if (process.platform === 'win32' || process.platform === 'win64') {
  81. await shellExec('adb shell pm uninstall -k --user 0 ' + element).then(function (result) {
  82. console.log('Removing ' + element + ' - ' + result.stdout);
  83. });
  84. } else {
  85. await shellExec('./adb shell pm uninstall -k --user 0 ' + element).then(function (result) {
  86. console.log('Removing ' + element + ' - ' + result.stdout);
  87. });
  88. }
  89. }
  90. console.log(chalk.green('Removal Complete'))
  91. await common.pause(2000)
  92. module.exports.mainMenu()
  93. },
  94. restoreApps: async () => {
  95. common.header('Restore Apps')
  96. const value = await inquirer.removeAppsList();
  97. for (let element of value.removeAppsList) {
  98. if (process.platform === 'win32' || process.platform === 'win64') {
  99. await shellExec('adb shell cmd package install-existing ' + element).then(function (result) {
  100. console.log('Restoring ' + element + ' - ' + result.stdout);
  101. });
  102. } else {
  103. await shellExec('./adb shell cmd package install-existing ' + element).then(function (result) {
  104. console.log('Restoring ' + element + ' - ' + result.stdout);
  105. });
  106. }
  107. }
  108. console.log(chalk.green('Restore Complete'))
  109. await common.pause(2000)
  110. module.exports.mainMenu()
  111. },
  112. connectWifi: async () => {
  113. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  114. common.header('Connect Wifi')
  115. if (miwatchData.ipAddress !== "") {
  116. console.log('Trying to connect with stored ipAddress')
  117. if (process.platform === 'win32' || process.platform === 'win64') {
  118. shellExec('adb connect ' + miwatchData.ipAddress).then(async function (result) {
  119. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  120. console.log(chalk.green('MiWatch Connected'))
  121. await common.pause(3000)
  122. module.exports.mainMenu()
  123. } else {
  124. console.log(chalk.red('MiWatch not found'))
  125. await common.pause(2000)
  126. console.log(chalk.white('Try Again'))
  127. await common.pause(1000)
  128. module.exports.connectWifi()
  129. }
  130. }).catch()
  131. } else {
  132. shellExec('./adb connect ' + miwatchData.ipAddress).then(async function (result) {
  133. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  134. console.log(chalk.green('MiWatch Connected'))
  135. files.writeIpAddress(miWatchIpaddress)
  136. await common.pause(3000)
  137. module.exports.mainMenu()
  138. } else {
  139. console.log(chalk.red('MiWatch not found'))
  140. await common.pause(2000)
  141. console.log(chalk.white('Try Again'))
  142. await common.pause(1000)
  143. module.exports.connectWifi()
  144. }
  145. }).catch()
  146. }
  147. } else {
  148. const value = await inquirer.connectWifi();
  149. const miWatchIpaddress = value.connectWifi
  150. if (process.platform === 'win32' || process.platform === 'win64') {
  151. shellExec('adb connect ' + miWatchIpaddress).then(async function (result) {
  152. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  153. console.log(chalk.green('MiWatch Connected'))
  154. files.writeIpAddress(miWatchIpaddress)
  155. await common.pause(3000)
  156. module.exports.mainMenu()
  157. } else {
  158. console.log(chalk.red('MiWatch not found'))
  159. await common.pause(2000)
  160. console.log(chalk.white('Try Again'))
  161. await common.pause(1000)
  162. module.exports.connectWifi()
  163. }
  164. }).catch()
  165. } else {
  166. shellExec('./adb connect ' + miWatchIpaddress).then(async function (result) {
  167. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  168. console.log(chalk.green('MiWatch Connected'))
  169. files.writeIpAddress(miWatchIpaddress)
  170. await common.pause(3000)
  171. module.exports.mainMenu()
  172. } else {
  173. console.log(chalk.red('MiWatch not found'))
  174. await common.pause(2000)
  175. console.log(chalk.white('Try Again'))
  176. await common.pause(1000)
  177. module.exports.connectWifi()
  178. }
  179. }).catch()
  180. }
  181. }
  182. },
  183. mainMenu: async () => {
  184. common.header('Main Menu')
  185. const mainMenuSelection = await inquirer.mainMenu();
  186. switch (mainMenuSelection.mainMenu) {
  187. case 'connect to miwatch via wifi':
  188. module.exports.connectWifi()
  189. break;
  190. case 'remove xiaomi apps':
  191. module.exports.removeApps()
  192. break;
  193. case 'restore xiaomi apps':
  194. module.exports.restoreApps()
  195. break;
  196. case 'install compatible apps':
  197. module.exports.compatibleApps()
  198. break;
  199. case 'quit':
  200. break;
  201. default:
  202. // code block
  203. }
  204. }
  205. };