pages.js 12 KB

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