adb.js 9.1 KB

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