pages.js 15 KB

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