pages.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 logger = require('perfect-logger');
  14. let adbRun
  15. logger.initialize('RunTIme', {
  16. logLevelFile: 0, // Log level for file
  17. logLevelConsole: -1, // Log level for STDOUT/STDERR
  18. logDirectory: 'data/', // Log directory
  19. });
  20. module.exports = {
  21. removeCompatibleApps: async () => {
  22. let installedAppList
  23. common.header('Remove Installed Apps')
  24. logger.info('Remove Installed Apps')
  25. await shellExec(adbRun + ' shell pm list packages -3').then(async function (result) {
  26. logger.info('Packages recieved from watch')
  27. if (result.stderr.includes('error')) {
  28. logger.info(result.stderr)
  29. console.log(chalk.red('Device not authorised'))
  30. common.pause(3000)
  31. await shellExec(adbRun + ' kill-server').then(async function (result) {
  32. logger.info('Restarting ADB')
  33. logger.info(result.stdout)
  34. console.log('Please reconnect to watch')
  35. common.pause(3000)
  36. logger.info('Remove Installed Apps Failed')
  37. module.exports.mainMenu()
  38. })
  39. } else {
  40. if (process.platform === 'win32' || process.platform === 'win64') {
  41. installedAppList = result.stdout.split('\r\n'); // split string on comma space
  42. installedAppList.splice(-1, 1)
  43. } else {
  44. installedAppList = result.stdout.split('\n'); // split string on comma space
  45. installedAppList.splice(-1, 1)
  46. }
  47. const value = await inquirer.installedApps(installedAppList);
  48. for (let element of value.removeAppsList) {
  49. console.log('Removing ' + element)
  50. logger.info('Removing ' + element)
  51. const package = element.substring(8)
  52. await shellExec(adbRun + ' uninstall ' + package).then(async function (result) {
  53. console.log(element + ' - ' + result.stdout);
  54. logger.info(element + ' - ' + result.stdout);
  55. });
  56. }
  57. console.log(chalk.green('Removed Selected User Apps'))
  58. logger.info('Removed Selected User Apps')
  59. await common.pause(2000)
  60. module.exports.mainMenu()
  61. }
  62. })
  63. },
  64. compatibleApps: async () => {
  65. logger.info("Compatible Apps")
  66. common.header('Install Compatible Apps')
  67. let compatibleApps
  68. let url = "http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json";
  69. http.get(url, (res) => {
  70. let body = "";
  71. res.on("data", (chunk) => {
  72. body += chunk;
  73. });
  74. res.on("end", () => {
  75. try {
  76. compatibleApps = JSON.parse(body);
  77. logger.info("Compatible Apps found online")
  78. // do something with JSON
  79. } catch (error) {
  80. console.error(error.message);
  81. };
  82. });
  83. }).on("error", (error) => {
  84. console.error(error.message);
  85. });
  86. const value = await inquirer.compatibleApps();
  87. await shell.rm('-rf', './data/apps/*.apk');
  88. for (let element of value.removeAppsList) {
  89. for (let element2 of compatibleApps) {
  90. if (element === element2.name) {
  91. const options = {
  92. override: true,
  93. }
  94. const dl = new DownloaderHelper(element2.url, './data/apps/', options);
  95. dl.on('end', () => console.log('Downloading Latest ' + element2.name + ' Complete'),
  96. logger.info('Downloading Latest ' + element2.name + ' Complete')
  97. )
  98. await dl.start();
  99. }
  100. }
  101. }
  102. const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
  103. for (let element of apkList) {
  104. console.log('Installing ' + element)
  105. logger.info('Installing ' + element)
  106. await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
  107. if (result.stderr != '') {
  108. logger.info('Error ' + result.stderr);
  109. console.log(chalk.redBright('Error - Device not authorised'));
  110. }
  111. console.log(element + ' - ' + result.stdout);
  112. logger.info(element + ' - ' + result.stdout);
  113. if (element === "data\\apps\\MoreLocale.apk") {
  114. await shellExec(adbRun + ' shell pm grant jp.co.c_lis.ccl.morelocale android.permission.CHANGE_CONFIGURATION').then(function (result) {
  115. console.log('moreLocale Activated On Watch');
  116. logger.info('moreLocale Activated On Watch');
  117. });
  118. }
  119. });
  120. }
  121. console.log(chalk.green('Compatible Apps Installed'))
  122. logger.info('Compatible Apps Installed')
  123. await common.pause(2000)
  124. module.exports.mainMenu()
  125. },
  126. removeApps: async () => {
  127. logger.info("Remove Apps")
  128. common.header('Remove Apps')
  129. const value = await inquirer.removeAppsList();
  130. for (let element of value.removeAppsList) {
  131. await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
  132. if (result.stderr != '') {
  133. logger.info('Error ' + result.stderr);
  134. console.log(chalk.redBright('Error - Device not authorised'));
  135. } else {
  136. logger.info('Removing ' + element + ' - ' + result.stdout);
  137. console.log('Removing ' + element + ' - ' + result.stdout);
  138. }
  139. });
  140. }
  141. console.log(chalk.green('Removal Complete'))
  142. await common.pause(2000)
  143. logger.info("Remove Complete")
  144. module.exports.mainMenu()
  145. },
  146. restoreApps: async () => {
  147. logger.info("Restore Apps")
  148. common.header('Restore Apps')
  149. const value = await inquirer.removeAppsList();
  150. for (let element of value.removeAppsList) {
  151. await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
  152. if (result.stderr != '') {
  153. logger.info('Error ' + result.stderr);
  154. console.log(chalk.redBright('Error - Device not authorised'));
  155. } else {
  156. logger.info('Restoring ' + element + ' - ' + result.stdout);
  157. console.log('Restoring ' + element + ' - ' + result.stdout);
  158. }
  159. });
  160. }
  161. console.log(chalk.green('Restore Complete'))
  162. await common.pause(2000)
  163. logger.info("Restore Apps Complete")
  164. module.exports.mainMenu()
  165. },
  166. connectWifi: async () => {
  167. logger.info("Connect Wifi")
  168. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  169. common.header('Connect Wifi')
  170. if (miwatchData.ipAddress !== "") {
  171. await shellExec(adbRun + ' kill-server')
  172. console.log('Trying to connect with stored ipAddress')
  173. shellExec(adbRun + ' connect ' + miwatchData.ipAddress).then(async function (result) {
  174. logger.info("Connect Wifi Result " + result.stdout)
  175. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  176. console.log(chalk.green('MiWatch Connected'))
  177. await common.pause(3000)
  178. logger.info("Connect Wifi Complete")
  179. module.exports.mainMenu()
  180. } else {
  181. console.log(chalk.red('MiWatch not found'))
  182. await common.pause(2000)
  183. files.writeIpAddress('')
  184. console.log(chalk.white('Try Again'))
  185. await common.pause(1000)
  186. module.exports.connectWifi()
  187. }
  188. }).catch()
  189. } else {
  190. await shellExec(adbRun + ' kill-server')
  191. const value = await inquirer.connectWifi();
  192. const miWatchIpaddress = value.connectWifi
  193. shellExec(adbRun + ' connect ' + miWatchIpaddress).then(async function (result) {
  194. logger.info("Connect Wifi Result " + result.stdout)
  195. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  196. console.log(chalk.green('MiWatch Connected'))
  197. files.writeIpAddress(miWatchIpaddress)
  198. await common.pause(3000)
  199. logger.info("Connect Wifi Complete")
  200. module.exports.mainMenu()
  201. } else {
  202. if (result.stdout.includes('failed to authenticate')) {
  203. console.log(chalk.redBright('MiWatch not authenticated'))
  204. } else {
  205. console.log(chalk.red('MiWatch not found'))
  206. }
  207. await common.pause(2000)
  208. console.log(chalk.white('Try Again'))
  209. await common.pause(1000)
  210. module.exports.connectWifi()
  211. }
  212. }).catch()
  213. }
  214. },
  215. oneClick: async () => {
  216. logger.info("1-Click")
  217. common.header('1-Click Remove & Install')
  218. await common.pause(1000)
  219. console.log(chalk.white('Removing Xiaomi Apps'))
  220. await common.pause(1000)
  221. let value = await files.loadPackageList()
  222. for (let element of value.apps) {
  223. await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
  224. if (result.stderr != '') {
  225. logger.info('Error ' + result.stderr);
  226. console.log(chalk.redBright('Error - Device not authorised'));
  227. } else {
  228. logger.info('Removing ' + element + ' - ' + result.stdout);
  229. console.log('Removing ' + element + ' - ' + result.stdout);
  230. }
  231. });
  232. }
  233. console.log(chalk.green('Removal Complete'))
  234. await common.pause(2000)
  235. logger.info("Remove Complete")
  236. await common.pause(1000)
  237. console.log(chalk.white('Installing Google Apps'))
  238. await common.pause(1000)
  239. module.exports.mainMenu()
  240. },
  241. mainMenu: async () => {
  242. common.header('Main Menu')
  243. if (process.platform === 'win32' || process.platform === 'win64') {
  244. adbRun = 'adb'
  245. } else {
  246. adbRun = './adb'
  247. }
  248. logger.info(process.platform + " detected")
  249. const mainMenuSelection = await inquirer.mainMenu();
  250. switch (mainMenuSelection.mainMenu) {
  251. case 'connect to miwatch via wifi':
  252. module.exports.connectWifi()
  253. break;
  254. case 'remove xiaomi apps':
  255. module.exports.removeApps()
  256. break;
  257. case 'restore xiaomi apps':
  258. module.exports.restoreApps()
  259. break;
  260. case 'install compatible apps':
  261. module.exports.compatibleApps()
  262. break;
  263. case 'remove installed apps':
  264. module.exports.removeCompatibleApps()
  265. break;
  266. case '1-click remove & install':
  267. module.exports.oneClick()
  268. break;
  269. case 'quit':
  270. break;
  271. default:
  272. // code block
  273. }
  274. }
  275. };