pages.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\\simpleweather_base.apk") {
  114. const dl = new DownloaderHelper('http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/Others/simpleweather_split_config.armeabi_v7a.apk', './data/apps/', {
  115. override: true,
  116. });
  117. dl.on('end', () => console.log('Downloading Latest Complete'),
  118. logger.info('Downloading Latest Complete')
  119. )
  120. const dl2 = new DownloaderHelper('http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/Others/simpleweather_split_config.xhdpi.apk', './data/apps/', {
  121. override: true,
  122. });
  123. dl2.on('end', () => console.log('Downloading Latest Complete'),
  124. logger.info('Downloading Latest Complete')
  125. )
  126. await dl.start();
  127. await dl2.start();
  128. await shellExec(adbRun + ' install-multiple "data\\apps\\simpleweather_base.apk" "data\\apps\\simpleweather_split_config.armeabi_v7a.apk" "data\\apps\\simpleweather_split_config.xhdpi.apk"').then(function (result) {
  129. console.log(result)
  130. console.log('moreLocale Activated On Watch');
  131. logger.info('moreLocale Activated On Watch');
  132. })
  133. }
  134. if (element === "data\\apps\\MoreLocale.apk") {
  135. await shellExec(adbRun + ' shell pm grant jp.co.c_lis.ccl.morelocale android.permission.CHANGE_CONFIGURATION').then(function (result) {
  136. console.log('moreLocale Activated On Watch');
  137. logger.info('moreLocale Activated On Watch');
  138. })
  139. }
  140. if (element === "data\\apps\\com.alberto.locale.apk") {
  141. await shellExec(adbRun + ' shell pm grant com.alberto.locale android.permission.CHANGE_CONFIGURATION && ' + adbRun + ' shell am start -n com.alberto.locale/com.alberto.locale.MainActivity && ' + adbRun + ' shell pm grant com.alberto.locale android.permission.CHANGE_CONFIGURATION').then(function (result) {
  142. console.log(result)
  143. console.log('Alberto Locale Activated On Watch');
  144. logger.info('Alberto Locale Activated On Watch');
  145. });
  146. }
  147. });
  148. }
  149. console.log(chalk.green('Compatible Apps Installed'))
  150. logger.info('Compatible Apps Installed')
  151. await common.pause(2000)
  152. module.exports.mainMenu()
  153. },
  154. removeApps: async () => {
  155. logger.info("Remove Apps")
  156. common.header('Remove Apps')
  157. const value = await inquirer.removeAppsList();
  158. for (let element of value.removeAppsList) {
  159. // await shellExec(adbRun + ' shell pm disable-user --0 ' + element).then(function (result) {
  160. await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
  161. if (result.stderr != '') {
  162. logger.info('Error ' + result.stderr);
  163. console.log(chalk.redBright('Error - Device not authorised'));
  164. } else {
  165. logger.info('Removing ' + element + ' - ' + result.stdout);
  166. console.log('Removing ' + element + ' - ' + result.stdout);
  167. }
  168. });
  169. }
  170. console.log(chalk.green('Removal Complete'))
  171. await common.pause(2000)
  172. logger.info("Remove Complete")
  173. module.exports.mainMenu()
  174. },
  175. restoreApps: async () => {
  176. logger.info("Restore Apps")
  177. common.header('Restore Apps')
  178. const value = await inquirer.removeAppsList();
  179. for (let element of value.removeAppsList) {
  180. await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
  181. if (result.stderr != '') {
  182. logger.info('Error ' + result.stderr);
  183. console.log(chalk.redBright('Error - Device not authorised'));
  184. } else {
  185. logger.info('Restoring ' + element + ' - ' + result.stdout);
  186. console.log('Restoring ' + element + ' - ' + result.stdout);
  187. }
  188. });
  189. }
  190. console.log(chalk.green('Restore Complete'))
  191. await common.pause(2000)
  192. logger.info("Restore Apps Complete")
  193. module.exports.mainMenu()
  194. },
  195. connectWifi: async () => {
  196. logger.info("Connect Wifi")
  197. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  198. common.header('Connect Wifi')
  199. if (miwatchData.ipAddress !== "") {
  200. await shellExec(adbRun + ' kill-server')
  201. console.log('Trying to connect with stored ipAddress')
  202. shellExec(adbRun + ' connect ' + miwatchData.ipAddress).then(async function (result) {
  203. logger.info("Connect Wifi Result " + result.stdout)
  204. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  205. console.log(chalk.green('MiWatch Connected'))
  206. await common.pause(3000)
  207. logger.info("Connect Wifi Complete")
  208. module.exports.mainMenu()
  209. } else {
  210. console.log(chalk.red('MiWatch not found'))
  211. await common.pause(2000)
  212. files.writeIpAddress('')
  213. console.log(chalk.white('Try Again'))
  214. await common.pause(1000)
  215. module.exports.connectWifi()
  216. }
  217. }).catch()
  218. } else {
  219. await shellExec(adbRun + ' kill-server')
  220. const value = await inquirer.connectWifi();
  221. const miWatchIpaddress = value.connectWifi
  222. shellExec(adbRun + ' connect ' + miWatchIpaddress).then(async function (result) {
  223. logger.info("Connect Wifi Result " + result.stdout)
  224. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  225. console.log(chalk.green('MiWatch Connected'))
  226. files.writeIpAddress(miWatchIpaddress)
  227. await common.pause(3000)
  228. logger.info("Connect Wifi Complete")
  229. module.exports.mainMenu()
  230. } else {
  231. if (result.stdout.includes('failed to authenticate')) {
  232. console.log(chalk.redBright('MiWatch not authenticated'))
  233. } else {
  234. console.log(chalk.red('MiWatch not found'))
  235. }
  236. await common.pause(2000)
  237. console.log(chalk.white('Try Again'))
  238. await common.pause(1000)
  239. module.exports.connectWifi()
  240. }
  241. }).catch()
  242. }
  243. },
  244. mainMenu: async () => {
  245. common.header('Main Menu')
  246. if (process.platform === 'win32' || process.platform === 'win64') {
  247. adbRun = 'adb'
  248. } else {
  249. adbRun = './adb'
  250. }
  251. logger.info(process.platform + " detected")
  252. const mainMenuSelection = await inquirer.mainMenu();
  253. switch (mainMenuSelection.mainMenu) {
  254. case 'connect to miwatch via wifi':
  255. module.exports.connectWifi()
  256. break;
  257. case 'remove xiaomi apps':
  258. module.exports.removeApps()
  259. break;
  260. case 'restore xiaomi apps':
  261. module.exports.restoreApps()
  262. break;
  263. case 'install compatible apps':
  264. module.exports.compatibleApps()
  265. break;
  266. case 'remove installed apps':
  267. module.exports.removeCompatibleApps()
  268. break;
  269. case 'quit':
  270. break;
  271. default:
  272. // code block
  273. }
  274. }
  275. };