pages.js 15 KB

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