pages.js 16 KB

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