Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
appt2 committed Apr 26, 2024
1 parent 541633c commit 65f5b10
Show file tree
Hide file tree
Showing 30 changed files with 7,164 additions and 6,687 deletions.
13 changes: 1 addition & 12 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,6 @@
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden"

/>
<activity
android:name=".activities.ImgShowActivity"
android:configChanges="orientation|screenSize|keyboardHidden|smallestScreenSize|screenLayout"
android:hardwareAccelerated="true"
android:supportsPictureInPicture="true"

/>
<activity
android:name=".activities.IconModActivity"
Expand Down Expand Up @@ -709,11 +702,7 @@
android:supportsPictureInPicture="true"

/>
<service
android:name="Ninja.coder.Ghostemane.code.PHPProcess"
android:process=":PHPWebServerService"

/>

<receiver android:name="Ninja.coder.Ghostemane.code.PluginManager.FilePostBroadcastReceiver">
<intent-filter>
<action android:name="Ninja.coder.code.Ghostemane.SEND_FILE_PATH" />
Expand Down
187 changes: 48 additions & 139 deletions app/src/main/java/Ninja/coder/Ghostemane/code/PHPProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,178 +4,87 @@
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import android.provider.Telephony;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import kotlin.jvm.internal.Intrinsics;
import kotlin.text.StringsKt;

public final class PHPProcess extends Service {
private boolean isStarted;
public class PHPProcess extends Service {
private boolean isStarted = false;

@Override
public IBinder onBind(Intent intent) {
return null;
}

public final boolean isStarted() {
return this.isStarted;
public boolean isStarted() {
return isStarted;
}

public final void setStarted(boolean started) {
this.isStarted = started;
public void setStarted(boolean z) {
isStarted = z;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intrinsics.checkNotNullParameter(intent, "intent");
if (this.isStarted) {
return super.onStartCommand(intent, flags, startId);
} else {
this.isStarted = true;
int port = intent.getIntExtra("port", -1);
String projectPath = intent.getStringExtra("projectPath");
File phpLibrary = new File(getApplicationInfo().nativeLibraryDir, "libphp-8.2.8.so");
File libraryDir = new File(getFilesDir().getPath() + "/lib");
File errorDir = new File(getFilesDir(), "php_error");
File pidFile = new File(getFilesDir(), "php_pid");
File iniFile = new File(getFilesDir(), "php.ini");

errorDir.delete();
try {
pidFile.createNewFile();
} catch (IOException err) {
err.printStackTrace();
}

try {
write(String.valueOf(Process.myPid()), pidFile);
} catch (IOException err) {
err.printStackTrace();
}

new Thread(
() -> {
startPHPProcess(libraryDir, phpLibrary, port, projectPath, iniFile, errorDir, this);
})
.start();

if (isStarted) {
return super.onStartCommand(intent, flags, startId);
}
isStarted = true;
int intExtra = intent.getIntExtra(Telephony.Carriers.PORT, -1);
String stringExtra = intent.getStringExtra("projectPath");
File file = new File(getApplicationInfo().nativeLibraryDir, "libphp-8.2.8.so");
File filesDir = getFilesDir();
File file2 = new File(filesDir, "lib");
File file3 = new File(filesDir, "php_error");
File file4 = new File(filesDir, "php_pid");
File file5 = new File(filesDir, "php.ini");
file3.delete();
try {
file4.createNewFile();
Utils.write(String.valueOf(Process.myPid()), file4);

Thread thread =
new Thread(
new Runnable() {
@Override
public void run() {
onStartCommandLambda(
file, file2, intExtra, stringExtra, file5, file3, PHPProcess.this);
}
});
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}

private static void startPHPProcess(
File libraryDir,
File phpLibrary,
int port,
String projectPath,
File iniFile,
File errorDir,
PHPProcess phpProcess) {

private void onStartCommandLambda(
File lib, File php, int i, String path, File phpIni, File errorFile, PHPProcess process) {
try {
String processOutput =
runCommand(
Utils.runCommand(
new String[] {
"/system/bin/sh",
"-c",
"export LD_LIBRARY_PATH=\""
+ libraryDir.getPath()
+ lib.getPath()
+ "\" && \""
+ phpLibrary.getPath()
+ php.getPath()
+ "\" -S 0.0.0.0:"
+ port
+ i
+ " -t \""
+ projectPath
+ path
+ "\" -c \""
+ iniFile.getPath()
+ phpIni.getPath()
+ "\""
});

if (StringsKt.startsWith(processOutput, "Error : ", false)) {
errorDir.createNewFile();
write(processOutput, errorDir);
phpProcess.stopSelf();
if (processOutput != null && processOutput.startsWith("Error: ")) {
errorFile.createNewFile();
Utils.write(processOutput, errorFile);
process.stopSelf();
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void write(String str, File file) throws IOException {
BufferedOutputStream bufferedOutputStream =
new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(str.getBytes(), 0, str.getBytes().length);
bufferedOutputStream.flush();
bufferedOutputStream.close();
}

public static void write(InputStream inputStream, File file) throws IOException {
BufferedOutputStream bufferedOutputStream =
new BufferedOutputStream(new FileOutputStream(file));
byte[] readStream = readStream(inputStream);
bufferedOutputStream.write(readStream, 0, readStream.length);
bufferedOutputStream.flush();
bufferedOutputStream.close();
}

public static void write(byte[] bArr, File file) throws IOException {
BufferedOutputStream bufferedOutputStream =
new BufferedOutputStream(new FileOutputStream(file));
bufferedOutputStream.write(bArr, 0, bArr.length);
bufferedOutputStream.flush();
bufferedOutputStream.close();
}

public static byte[] readStream(InputStream inputStream) throws IOException {
byte[] bArr = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while (true) {
int read = inputStream.read(bArr);
if (read != -1) {
byteArrayOutputStream.write(bArr, 0, read);
} else {
inputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}
}
}

public static String runCommand(String[] strArr) throws Exception {
java.lang.Process runCommandAndGetProcess = runCommandAndGetProcess(strArr);
runCommandAndGetProcess.waitFor();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(runCommandAndGetProcess.getInputStream()));
String str = "";
while (true) {
String readLine = bufferedReader.readLine();
if (readLine == null) {
break;
}
str = str + readLine + "\n";
}
if (!str.equals("")) {
return str;
}
BufferedReader bufferedReader2 =
new BufferedReader(new InputStreamReader(runCommandAndGetProcess.getErrorStream()));
while (true) {
String readLine2 = bufferedReader2.readLine();
if (readLine2 == null) {
break;
}
str = str + readLine2 + "\n";
}
return !str.equals("") ? "Error: " + str : str;
}

public static java.lang.Process runCommandAndGetProcess(String[] strArr) throws Exception {
return Runtime.getRuntime().exec(strArr);
}
}
Loading

0 comments on commit 65f5b10

Please sign in to comment.