adb.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const gfin = require('get-files-in')
  2. const logger = require('perfect-logger');
  3. const shellExec = require('shell-exec')
  4. const Language = require("@shypes/language-translator");
  5. const inquirer = require('../lib/inquirer');
  6. const common = require('./common')
  7. const chalk = require('chalk');
  8. const globalVariables = require('../lib/globalVars');
  9. const { dualLog } = require('./common');
  10. if (process.platform === 'win32' || process.platform === 'win64') {
  11. adbRun = 'adb'
  12. } else {
  13. adbRun = './adb'
  14. }
  15. module.exports = {
  16. installApk: async (element) => {
  17. Language.setActiveLang(globalVariables.language)
  18. result = await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
  19. if (result.stderr != '') {
  20. common.dualLog('device-not-authorised' + ' ' + result.stderr, 'red')
  21. }
  22. common.dualLog(element + ' - ' + result.stdout)
  23. if (element === "data\\apps\\simpleweather_base.apk") {
  24. 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')
  25. await common.downloadFile('http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/Others/simpleweather_split_config.xhdpi.apk', './data/apps/simpleweather_split_config.xhdpi.apk')
  26. 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(async function (result) {
  27. common.log(result)
  28. common.dualLog('simple-weather-activated-on-watch')
  29. })
  30. }
  31. if (element === "data\\apps\\MoreLocale.apk") {
  32. await shellExec(adbRun + ' shell pm grant jp.co.c_lis.ccl.morelocale android.permission.CHANGE_CONFIGURATION').then(async function (result) {
  33. common.log(result)
  34. common.dualLog('morelocale-activated-on-watch')
  35. })
  36. }
  37. if (element === "data\\apps\\AlbertoLocale.apk") {
  38. 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(async function (result) {
  39. common.log(result)
  40. common.dualLog('alberto-locale-activated-on-watch')
  41. });
  42. }
  43. });
  44. },
  45. removeApk: async (package) => {
  46. Language.setActiveLang(globalVariables.language)
  47. result = await shellExec(adbRun + ' uninstall ' + package)
  48. if (result.stderr != '') {
  49. common.dualLog('device-not-authorised' + ' ' + result.stderr, 'red')
  50. } else {
  51. common.dualLog(await Language.get('removing') + ' ' + package + ' - ' + result.stdout)
  52. }
  53. },
  54. removeXiaomiApk: async (package) => {
  55. Language.setActiveLang(globalVariables.language)
  56. result = await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + package)
  57. if (result.stderr != '') {
  58. common.dualLog('device-not-authorised' + ' ' + result.stderr, 'red')
  59. } else {
  60. common.dualLog(await Language.get('removing') + ' ' + package + ' - ' + result.stdout)
  61. }
  62. },
  63. restoreXiaomiApk: async (package) => {
  64. Language.setActiveLang(globalVariables.language)
  65. result = await shellExec(adbRun + ' shell cmd package install-existing ' + package)
  66. if (result.stderr != '') {
  67. common.dualLog('device-not-authorised' + ' ' + result.stderr, 'red')
  68. } else {
  69. common.dualLog(await Language.get('removing') + ' ' + package + ' - ' + result.stdout)
  70. }
  71. },
  72. restoreAnyApk: async (package) => {
  73. Language.setActiveLang(globalVariables.language)
  74. result = await shellExec(adbRun + ' shell cmd package install-existing ' + package.restoreAnyApp)
  75. if (result.stderr != '') {
  76. common.dualLog('device-not-authorised' + ' ' + result.stderr, 'red')
  77. } else if (result.stdout.includes('doesn\'t exist')) {
  78. logger.info(result.stdout);
  79. console.log(chalk.redBright(result.stdout));
  80. } else {
  81. common.dualLog(await Language.get('restoring') + ' ' + package + ' - ' + result.stdout)
  82. }
  83. },
  84. removeAnyApk: async (package) => {
  85. Language.setActiveLang(globalVariables.language)
  86. result = await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + package.removeAnyApp)
  87. if (result.stderr != '') {
  88. common.dualLog('device-not-authorised' + ' ' + result.stderr, 'red')
  89. } else if (result.stdout.includes('doesn\'t exist')) {
  90. logger.info(result.stdout);
  91. console.log(chalk.redBright(result.stdout));
  92. } else {
  93. common.dualLog(await Language.get('removing') + ' ' + package + ' - ' + result.stdout)
  94. }
  95. },
  96. getInstalledPacakges: async () => {
  97. Language.setActiveLang(globalVariables.language)
  98. result = await shellExec(adbRun + ' shell pm list packages -3')
  99. logger.info(await Language.get('packages-recieved-from-watch'))
  100. if (result.stderr.includes('error')) {
  101. logger.info(result.stderr)
  102. console.log(chalk.red(await Language.get('device-not-authorised')))
  103. common.pause(3000)
  104. await shellExec(adbRun + ' kill-server').then(async function (result) {
  105. logger.info(await Language.get('restarting-adb'))
  106. logger.info(result.stdout)
  107. console.log(await Language.get('please-reconnect-to-watch'))
  108. common.pause(3000)
  109. logger.info(await Language.get('remove-installed-apps-failed'))
  110. module.exports.mainMenu()
  111. })
  112. } else {
  113. if (process.platform === 'win32' || process.platform === 'win64') {
  114. installedAppList = result.stdout.split('\r\n'); // split string on comma space
  115. installedAppList.splice(-1, 1)
  116. } else {
  117. installedAppList = result.stdout.split('\n'); // split string on comma space
  118. installedAppList.splice(-1, 1)
  119. }
  120. const value = await inquirer.installedApps(installedAppList);
  121. return value
  122. }
  123. },
  124. killAdbServer: async () => {
  125. Language.setActiveLang(globalVariables.language)
  126. let result = await shellExec(adbRun + ' kill-server')
  127. // common.log('')
  128. // common.dualLog('restarting-adb' + ' ' + result.stderr, 'red')
  129. },
  130. watchConnection: async (value) => {
  131. Language.setActiveLang(globalVariables.language)
  132. if (value.connection === "usb") {
  133. await module.exports.killAdbServer()
  134. result = await shellExec(adbRun + ' devices')
  135. console.log(result.stdout)
  136. if (result.stdout.includes('device', 15)) {
  137. common.dualLog(await Language.get('connected-via-usb', 'green'))
  138. await common.pause(3000)
  139. globalVariables.localUSB = "X"
  140. return true
  141. } else {
  142. common.dualLog(await Language.get('not-found', 'red'))
  143. await common.pause(2000)
  144. common.dualLog(await Language.get('try-again', 'white'))
  145. await common.pause(1000)
  146. return false
  147. }
  148. }
  149. if (value.connection === "wifi") {
  150. const value = await inquirer.connectWifi();
  151. await module.exports.killAdbServer()
  152. result = await shellExec(adbRun + ' connect ' + value.connectWifi)
  153. logger.info("Connect Wifi Result " + result.stdout)
  154. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  155. common.dualLog(await Language.get('connected', 'green'))
  156. globalVariables.localUSB = ""
  157. globalVariables.miWatchIpaddress = value.connectWifi
  158. await common.pause(3000)
  159. common.dualLog(await Language.get('connect-wifi-complete', 'green'))
  160. return true
  161. } else {
  162. if (result.stdout.includes('failed to authenticate')) {
  163. common.dualLog(await Language.get('not-authenticated', 'red'))
  164. return false
  165. } else {
  166. common.dualLog(result.stdout, 'red')
  167. }
  168. await common.pause(2000)
  169. common.dualLog('try-again', '')
  170. await common.pause(1000)
  171. return false
  172. }
  173. }
  174. }
  175. };