1+ /*
2+ * Copyright (c) 2026 Fora
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16+ *
17+ * Contact: Fora <fora060823@gmail.com>
18+ * Created: 27-01-2026
19+ */
120package com .aexon ;
221
322import android .content .Context ;
423import android .os .Build ;
524import android .os .Handler ;
625import android .os .Looper ;
726import android .os .StrictMode ;
27+
28+ import androidx .annotation .NonNull ;
29+ import androidx .annotation .Nullable ;
30+ import androidx .annotation .RequiresApi ;
31+
832import java .io .BufferedReader ;
933import java .io .InputStreamReader ;
1034import java .io .OutputStream ;
1135import java .net .Socket ;
36+ import java .nio .charset .StandardCharsets ;
1237import java .util .List ;
1338import java .util .concurrent .CopyOnWriteArrayList ;
1439
15- import com .aexon .annotation .NonNull ;
16- import com .aexon .annotation .Nullable ;
17-
40+ @ RequiresApi (api = Build .VERSION_CODES .O )
1841public class Aexon {
1942
2043 private static final String HOST = "127.0.0.1" ;
@@ -25,9 +48,9 @@ public class Aexon {
2548 private static final byte [] STARTER_PID_FILE_ENC = {0x75 , 0x3E , 0x3B , 0x2E , 0x3B , 0x75 , 0x36 , 0x35 , 0x39 , 0x3B , 0x36 , 0x75 , 0x2E , 0x37 , 0x2A , 0x75 , 0x74 , 0x3B , 0x22 , 0x05 , 0x29 , 0x2E , 0x3B , 0x28 , 0x2E , 0x3F , 0x28 };
2649 private static final Handler mainHandler = new Handler (Looper .getMainLooper ());
2750
28- private @ NonNull String [] command ;
29- private @ Nullable String [] env ;
30- private @ Nullable String dir ;
51+ private final @ NonNull String [] command ;
52+ private final @ Nullable String [] env ;
53+ private final @ Nullable String dir ;
3154
3255 private static final List <OnBinderReceivedListener > binderReceivedListeners = new CopyOnWriteArrayList <>();
3356 private static final List <OnBinderDeadListener > binderDeadListeners = new CopyOnWriteArrayList <>();
@@ -59,11 +82,7 @@ private Aexon(@NonNull String[] command, @Nullable String[] env, @Nullable Strin
5982 for (int i = 0 ; i < enc .length ; i ++) {
6083 out [i ] = (byte ) (enc [i ] ^ AX_KEY );
6184 }
62- try {
63- return new String (out , "UTF-8" );
64- } catch (Exception e ) {
65- return "" ;
66- }
85+ return new String (out , StandardCharsets .UTF_8 );
6786 }
6887
6988 private static void allowNetwork () {
@@ -77,8 +96,12 @@ private static void allowNetwork() {
7796 public void exec () {
7897 new Thread (() -> {
7998 StringBuilder cmd = new StringBuilder ();
80- if (env != null ) for (String e : env ) cmd .append (e ).append (" " );
81- if (dir != null ) cmd .append ("cd " ).append (dir ).append (" && " );
99+ if (env != null ) {
100+ for (String e : env ) cmd .append (e ).append (" " );
101+ }
102+ if (dir != null ) {
103+ cmd .append ("cd " ).append (dir ).append (" && " );
104+ }
82105 for (int i = 0 ; i < command .length ; i ++) {
83106 cmd .append (command [i ]);
84107 if (i < command .length - 1 ) cmd .append (" " );
@@ -89,8 +112,12 @@ public void exec() {
89112
90113 public @ NonNull AexonProcess execResult () {
91114 StringBuilder cmd = new StringBuilder ();
92- if (env != null ) for (String e : env ) cmd .append (e ).append (" " );
93- if (dir != null ) cmd .append ("cd " ).append (dir ).append (" && " );
115+ if (env != null ) {
116+ for (String e : env ) cmd .append (e ).append (" " );
117+ }
118+ if (dir != null ) {
119+ cmd .append ("cd " ).append (dir ).append (" && " );
120+ }
94121 for (int i = 0 ; i < command .length ; i ++) {
95122 cmd .append (command [i ]);
96123 if (i < command .length - 1 ) cmd .append (" " );
@@ -100,21 +127,19 @@ public void exec() {
100127
101128 private static @ NonNull String sendCommand (@ NonNull String command ) {
102129 allowNetwork ();
103- try {
104- Socket socket = new Socket (HOST , PORT );
130+ try (Socket socket = new Socket (HOST , PORT )) {
105131 socket .setSoTimeout (5000 );
106132 OutputStream out = socket .getOutputStream ();
107- out .write ((axDecode (AUTH_TOKEN_ENC ) + "\n " ).getBytes ("UTF-8" ));
133+ out .write ((axDecode (AUTH_TOKEN_ENC ) + "\n " ).getBytes (StandardCharsets . UTF_8 ));
108134 out .flush ();
109- out .write ((command + "\n " ).getBytes ("UTF-8" ));
135+ out .write ((command + "\n " ).getBytes (StandardCharsets . UTF_8 ));
110136 out .flush ();
111- BufferedReader reader = new BufferedReader (new InputStreamReader (socket .getInputStream (), "UTF-8" ));
137+ BufferedReader reader = new BufferedReader (new InputStreamReader (socket .getInputStream (), StandardCharsets . UTF_8 ));
112138 StringBuilder result = new StringBuilder ();
113139 String line ;
114140 while ((line = reader .readLine ()) != null ) {
115141 result .append (line ).append ("\n " );
116142 }
117- socket .close ();
118143 return result .toString ().trim ();
119144 } catch (Exception e ) {
120145 return "Error: " + e .getMessage ();
@@ -123,15 +148,13 @@ public void exec() {
123148
124149 public static boolean isBinder () {
125150 allowNetwork ();
126- try {
127- Socket socket = new Socket (HOST , PORT );
151+ try (Socket socket = new Socket (HOST , PORT )) {
128152 socket .setSoTimeout (500 );
129153 OutputStream out = socket .getOutputStream ();
130- out .write ((axDecode (AUTH_TOKEN_ENC ) + "\n " ).getBytes ("UTF-8" ));
154+ out .write ((axDecode (AUTH_TOKEN_ENC ) + "\n " ).getBytes (StandardCharsets . UTF_8 ));
131155 out .flush ();
132- out .write (("echo ok\n " ).getBytes ("UTF-8" ));
156+ out .write (("echo ok\n " ).getBytes (StandardCharsets . UTF_8 ));
133157 out .flush ();
134- socket .close ();
135158 return true ;
136159 } catch (Exception e ) {
137160 return false ;
@@ -149,24 +172,22 @@ public static void stopDaemonPermanently() {
149172 public static void execStream (@ NonNull String command , @ NonNull OnProcessOutputListener callback ) {
150173 new Thread (() -> {
151174 allowNetwork ();
152- try {
153- Socket socket = new Socket (HOST , PORT );
175+ try (Socket socket = new Socket (HOST , PORT )) {
154176 socket .setSoTimeout (0 );
155177 OutputStream out = socket .getOutputStream ();
156- out .write ((axDecode (AUTH_TOKEN_ENC ) + "\n " ).getBytes ("UTF-8" ));
178+ out .write ((axDecode (AUTH_TOKEN_ENC ) + "\n " ).getBytes (StandardCharsets . UTF_8 ));
157179 out .flush ();
158- out .write (("@@EXEC:" + command + "\n " ).getBytes ("UTF-8" ));
180+ out .write (("@@EXEC:" + command + "\n " ).getBytes (StandardCharsets . UTF_8 ));
159181 out .flush ();
160182
161- BufferedReader reader = new BufferedReader (new InputStreamReader (socket .getInputStream (), "UTF-8" ));
183+ BufferedReader reader = new BufferedReader (new InputStreamReader (socket .getInputStream (), StandardCharsets . UTF_8 ));
162184
163185 String pidLine = reader .readLine ();
164186 long pid = -1 ;
165187 if (pidLine != null && pidLine .startsWith ("@@PID:" )) {
166188 try {
167189 pid = Long .parseLong (pidLine .substring (6 ).trim ());
168190 } catch (Exception ignored ) {
169-
170191 }
171192 }
172193 final long finalPid = pid ;
@@ -179,14 +200,12 @@ public static void execStream(@NonNull String command, @NonNull OnProcessOutputL
179200 try {
180201 exitCode = Integer .parseInt (line .substring (7 ).trim ());
181202 } catch (Exception ignored ) {
182-
183203 }
184204 break ;
185205 }
186206 final String outLine = line ;
187207 mainHandler .post (() -> callback .onOutput (outLine ));
188208 }
189- socket .close ();
190209 final int finalExit = exitCode ;
191210 mainHandler .post (() -> callback .onExit (finalExit ));
192211 } catch (Exception e ) {
@@ -229,7 +248,6 @@ public static void addBinderReceivedListener(@NonNull OnBinderReceivedListener l
229248 try {
230249 Thread .sleep (500 );
231250 } catch (Exception ignored ) {
232-
233251 }
234252 }
235253 }).start ();
@@ -254,10 +272,9 @@ private static void startMonitoring() {
254272 new Thread (() -> {
255273 boolean wasRunning = isBinder ();
256274 while (true ) {
257- try {
275+ try {
258276 Thread .sleep (500 );
259277 } catch (Exception ignored ) {
260-
261278 }
262279 boolean isRunning = isBinder ();
263280 if (wasRunning && !isRunning ) {
0 commit comments