package Ġcom . mat kos mol jan . apps . auto usb te ther ing . domain . shell Ċ Ċ import Ġandroid . os . Build Ċ import Ġandroid . os . Build . VERSION _ CODES .* Ċ import Ġandroid . preference . Preference Manager Ċ import Ġcom . mat kos mol jan . apps . auto usb te ther ing . Function Result Ċ import Ġcom . mat kos mol jan . apps . auto usb te ther ing . domain . Te ther Switch Ċ import Ġjava . io . Data OutputStream ĊĊ Ċ /** Ċ Ġ* ĠA Ġ[ Te ther Switch ] Ġbased Ġon Ġshell Ġcommand Ġexecution Ċ Ġ*/ Ċ class ĠShell Te ther Switch Ġ: ĠTe ther Switch Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġandroid ApiVersion To Method Numbers Ġ= Ġmap Of ( ĊĠĠĠĠĠĠĠ ĠK IT K AT Ġto Ġ 3 4 , ĊĠĠĠĠĠĠĠ ĠL OL LI POP Ġto Ġ 3 0 , ĊĠĠĠĠĠĠĠ ĠN Ġto Ġ 3 3 , ĊĠĠĠĠĠĠĠ ĠO Ġto Ġ 3 4 , Ġ// 3 4 Ġdoesn 't Ġworks Ġfor ĠSamsung ĠS 7 ĊĠĠĠĠĠĠĠ ĠP Ġto Ġ 3 3 ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġoverride Ġfun Ġturn Te ther ing On (): ĠFunction Result < Unit > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmethod Number Ġ= Ġget Method Number () ĊĠĠĠĠĠĠĠ Ġval Ġte ther ing Shell Command Ġ= ĠTe ther Switch Shell Command Generator . generate Switch On Command ( method Number ) ĊĠĠĠĠĠĠĠ Ġval Ġis Command Executed Successfully Ġ= Ġexecute Command ( te ther ing Shell Command ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( is Command Executed Successfully ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Success ( Unit ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Failure ( IllegalStateException (" The Ġte ther ing Ġshell Ġcall Ġhasn 't Ġreturned Ġsuccessfully ")) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġturn Te ther ing Off (): ĠFunction Result < Unit > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmethod Number Ġ= Ġget Method Number () ĊĠĠĠĠĠĠĠ Ġval Ġte ther ing Shell Command Ġ= ĠTe ther Switch Shell Command Generator . generate Switch Off Command ( method Number ) ĊĠĠĠĠĠĠĠ Ġval Ġis Command Executed Successfully Ġ= Ġexecute Command ( te ther ing Shell Command ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( is Command Executed Successfully ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Success ( Unit ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Failure ( IllegalStateException (" The Ġte ther ing Ġshell Ġcall Ġhasn 't Ġreturned Ġsuccessfully ")) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġturn Te ther ing On ( method : String ): ĠFunction Result < Unit > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġte ther ing Shell Command Ġ= ĠTe ther Switch Shell Command Generator . generate Switch On Command ( method ) ĊĠĠĠĠĠĠĠ Ġval Ġis Command Executed Successfully Ġ= Ġexecute Command ( te ther ing Shell Command ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( is Command Executed Successfully ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Success ( Unit ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Failure ( IllegalStateException (" The Ġte ther ing Ġshell Ġcall Ġhasn 't Ġreturned Ġsuccessfully ")) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġturn Te ther ing Off ( method : String ): ĠFunction Result < Unit > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġte ther ing Shell Command Ġ= ĠTe ther Switch Shell Command Generator . generate Switch Off Command ( method ) ĊĠĠĠĠĠĠĠ Ġval Ġis Command Executed Successfully Ġ= Ġexecute Command ( te ther ing Shell Command ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( is Command Executed Successfully ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Success ( Unit ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFunction Result . Failure ( IllegalStateException (" The Ġte ther ing Ġshell Ġcall Ġhasn 't Ġreturned Ġsuccessfully ")) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġget Method Number (): ĠInt Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġcurrent ApiVersion Ġ= ĠBuild . VERSION . SDK _ INT ĊĊĠĠĠĠĠĠĠ Ġreturn Ġandroid ApiVersion To Method Numbers ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. filter Keys Ġ{ Ġapi Ġ-> Ġapi Ġ<= Ġcurrent ApiVersion Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. max By Ġ{ Ġentry Ġ-> Ġentry . key Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ? . value ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ?: Ġthrow ĠNullPointerException (" A Ġmethod Ġnumber Ġhasn 't Ġbeen Ġfound . ĠThis Ġshould Ġbe Ġimpossible .") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* Ġ@ return Ġtrue Ġif Ġthe Ġcommand Ġexecuted Ġsuccessfully , Ġotherwise Ġfalse ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġ// ĠReference : Ġhttps :// stackoverflow . com / a / 3 0 5 5 7 8 6 ĊĠĠĠ Ġ@ Throws ( Exception :: class ) ĊĠĠĠ Ġprivate Ġfun Ġexecute Command ( command : ĠString ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġprocess Ġ= ĠRuntime . get Runtime (). exec (" su ") ĊĠĠĠĠĠĠĠ Ġval Ġos Ġ= ĠData OutputStream ( process . output Stream ) ĊĊĠĠĠĠĠĠĠ Ġos . write Bytes ( command Ġ+ Ġ"\ n ") ĊĠĠĠĠĠĠĠ Ġos . write Bytes (" exit \ n ") ĊĠĠĠĠĠĠĠ Ġos . flush () ĊĠĠĠĠĠĠĠ Ġos . close () ĊĊĠĠĠĠĠĠĠ Ġreturn Ġprocess . waitFor () Ġ== Ġ 0 ĊĠĠĠ Ġ} Ċ }