pages.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if (result.stderr.includes('error')) {
  20. console.log(chalk.red('Device not authorised'))
  21. common.pause(3000)
  22. await shellExec(adbRun + ' kill-server').then(async function (result) {
  23. console.log('Please reconnect to watch')
  24. common.pause(3000)
  25. module.exports.mainMenu()
  26. })
  27. } else {
  28. if (process.platform === 'win32' || process.platform === 'win64') {
  29. installedAppList = result.stdout.split('\r\n'); // split string on comma space
  30. installedAppList.splice(-1, 1)
  31. } else {
  32. installedAppList = result.stdout.split('\n'); // split string on comma space
  33. installedAppList.splice(-1, 1)
  34. }
  35. const value = await inquirer.installedApps(installedAppList);
  36. for (let element of value.removeAppsList) {
  37. console.log('Removing ' + element)
  38. const package = element.substring(8)
  39. await shellExec(adbRun + ' uninstall ' + package).then(async function (result) {
  40. console.log(element + ' - ' + result.stdout);
  41. });
  42. }
  43. // });
  44. console.log(chalk.green('Removed Selected User Apps'))
  45. await common.pause(2000)
  46. module.exports.mainMenu()
  47. }
  48. })
  49. },
  50. compatibleApps: async () => {
  51. common.header('Install Compatible Apps')
  52. let compatibleApps
  53. let url = "http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json";
  54. http.get(url, (res) => {
  55. let body = "";
  56. res.on("data", (chunk) => {
  57. body += chunk;
  58. });
  59. res.on("end", () => {
  60. try {
  61. compatibleApps = JSON.parse(body);
  62. // do something with JSON
  63. } catch (error) {
  64. console.error(error.message);
  65. };
  66. });
  67. }).on("error", (error) => {
  68. console.error(error.message);
  69. });
  70. const value = await inquirer.compatibleApps();
  71. await shell.rm('-rf', './data/apps/*.apk');
  72. for (let element of value.removeAppsList) {
  73. for (let element2 of compatibleApps) {
  74. if (element === element2.name) {
  75. const options = {
  76. override: true,
  77. }
  78. const dl = new DownloaderHelper(element2.url, './data/apps/', options);
  79. dl.on('end', () => console.log('Downloading Latest ' + element2.name + ' Complete'))
  80. await dl.start();
  81. }
  82. }
  83. }
  84. const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
  85. for (let element of apkList) {
  86. console.log('Installing ' + element)
  87. await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
  88. console.log(element + ' - ' + result.stdout);
  89. if (element === "data\\apps\\MoreLocale.apk") {
  90. await shellExec(adbRun + ' shell pm grant jp.co.c_lis.ccl.morelocale android.permission.CHANGE_CONFIGURATION').then(function (result) {
  91. console.log('moreLocale Activated On Watch');
  92. });
  93. }
  94. });
  95. }
  96. console.log(chalk.green('Compatible Apps Installed'))
  97. await common.pause(2000)
  98. module.exports.mainMenu()
  99. },
  100. removeApps: async () => {
  101. common.header('Remove Apps')
  102. const value = await inquirer.removeAppsList();
  103. for (let element of value.removeAppsList) {
  104. await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
  105. console.log('Removing ' + element + ' - ' + result.stdout);
  106. });
  107. }
  108. console.log(chalk.green('Removal Complete'))
  109. await common.pause(2000)
  110. module.exports.mainMenu()
  111. },
  112. restoreApps: async () => {
  113. common.header('Restore Apps')
  114. const value = await inquirer.removeAppsList();
  115. for (let element of value.removeAppsList) {
  116. await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
  117. console.log('Restoring ' + element + ' - ' + result.stdout);
  118. });
  119. }
  120. console.log(chalk.green('Restore Complete'))
  121. await common.pause(2000)
  122. module.exports.mainMenu()
  123. },
  124. connectWifi: async () => {
  125. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  126. common.header('Connect Wifi')
  127. if (miwatchData.ipAddress !== "") {
  128. console.log('Trying to connect with stored ipAddress')
  129. shellExec(adbRun + ' connect ' + miwatchData.ipAddress).then(async function (result) {
  130. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  131. console.log(chalk.green('MiWatch Connected'))
  132. await common.pause(3000)
  133. module.exports.mainMenu()
  134. } else {
  135. console.log(chalk.red('MiWatch not found'))
  136. await common.pause(2000)
  137. files.writeIpAddress('')
  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 installed apps':
  184. module.exports.removeCompatibleApps()
  185. break;
  186. case 'quit':
  187. break;
  188. default:
  189. // code block
  190. }
  191. }
  192. };