Skip to content

Commit

Permalink
native classes improvements, keep screen on when in installer
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Aug 29, 2024
1 parent df3df9a commit d9c4c70
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 52 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/dergoogler/core/NativeShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Object v2(String jsonArr) {
try {
cmds = Json.getStringArray(new JSONArray(jsonArr));
} catch (JSONException e) {
Log.e(TAG, e.toString());
Log.e(TAG + ":v2", e.toString());
return null;
}
Shell.Job shell = Shell.cmd(cmds);
Expand Down
82 changes: 34 additions & 48 deletions app/src/main/java/com/dergoogler/core/NativeSuFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Base64OutputStream;
import android.util.Log;
import android.webkit.JavascriptInterface;

import androidx.annotation.NonNull;
Expand All @@ -26,6 +27,7 @@

public class NativeSuFile {
private final MainActivity ctx;
private static final String TAG = "NativeSuFile";

public NativeSuFile(MainActivity ctx) {
this.ctx = ctx;
Expand All @@ -42,7 +44,7 @@ public void write(String data) {
outputStream.write(data.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG + ":write", e.toString());
}
}

Expand All @@ -59,7 +61,7 @@ public String read(String def) {
return sb.toString();
}
} catch (IOException e) {
e.printStackTrace();
Log.e( TAG + ":read", e.toString());
return def;
}
}
Expand All @@ -78,14 +80,14 @@ public String readAsBase64() {
}
return baos.toString();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG + ":readAsBase64", e.toString());
return "";
} finally {
closeQuietly(is);
closeQuietly(b64os); // This also closes baos
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG + ":readAsBase64", e.toString());
return "";
}
}
Expand Down Expand Up @@ -114,15 +116,12 @@ public long lastModified() {

@JavascriptInterface
public boolean create(int type) {
switch (type) {
case 0:
return file.createNewFile();
case 1:
return file.mkdirs();
case 2:
return file.mkdir();
}
return false;
return switch (type) {
case 0 -> file.createNewFile();
case 1 -> file.mkdirs();
case 2 -> file.mkdir();
default -> false;
};
}

@JavascriptInterface
Expand All @@ -143,49 +142,36 @@ public boolean exists() {

@JavascriptInterface
public boolean canTypeMethod(int type) {
switch (type) {
case 0:
return file.canRead();
case 1:
return file.canWrite();
case 2:
return file.canExecute();
}
return false;
return switch (type) {
case 0 -> file.canRead();
case 1 -> file.canWrite();
case 2 -> file.canExecute();
default -> false;
};
}

@JavascriptInterface
public boolean _is_TypeMethod(int type) {
switch (type) {
case 0:
return file.isFile();
case 1:
return file.isSymlink();
case 2:
return file.isDirectory();
case 3:
return file.isBlock();
case 4:
return file.isCharacter();
case 5:
return file.isNamedPipe();
case 6:
return file.isSocket();
case 7:
return file.isHidden();
}
return false;
return switch (type) {
case 0 -> file.isFile();
case 1 -> file.isSymlink();
case 2 -> file.isDirectory();
case 3 -> file.isBlock();
case 4 -> file.isCharacter();
case 5 -> file.isNamedPipe();
case 6 -> file.isSocket();
case 7 -> file.isHidden();
default -> false;
};
}

@JavascriptInterface
public boolean createNewSym_link(int type, String existing) {
switch (type) {
case 0:
return file.createNewLink(existing);
case 1:
return file.createNewSymlink(existing);
}
return false;
return switch (type) {
case 0 -> file.createNewLink(existing);
case 1 -> file.createNewSymlink(existing);
default -> false;
};
}

@JavascriptInterface
Expand Down
36 changes: 35 additions & 1 deletion app/src/main/java/com/dergoogler/core/NativeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import android.graphics.Color;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.DisplayCutout;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

Expand All @@ -21,11 +23,16 @@
import androidx.core.view.WindowInsetsControllerCompat;

import com.dergoogler.mmrl.MainActivity;
import com.dergoogler.util.Json;
import com.topjohnwu.superuser.Shell;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

public class NativeView {

private static final String TAG = "NativeView";
private final Activity ctx;
private Insets insets;
private final WindowInsetsControllerCompat windowInsetsController;
Expand Down Expand Up @@ -95,6 +102,32 @@ private void showSystemBars(int type) {
windowInsetsController.show(type);
}

@JavascriptInterface
public void addFlag(int flag) {


try {
ctx.getWindow().addFlags(flag);
} catch (Exception e) {
Log.e(TAG + ":addFlag", e.toString());
}



.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

@JavascriptInterface
public void clearFlag(int flag) {
try {
ctx.getWindow().clearFlags(flag);
} catch (Exception e) {
Log.e(TAG + ":clearFlag", e.toString());
}
}


@Deprecated
@JavascriptInterface
public void setStatusBarColor(String color, boolean white) {
if (white) {
Expand All @@ -111,6 +144,7 @@ public void setStatusBarColor(String color, boolean white) {
}
}

@Deprecated
@JavascriptInterface
public void setNavigationBarColor(String color) {
try {
Expand Down
4 changes: 3 additions & 1 deletion src/activitys/InstallTerminalV2Activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useTheme } from "@Hooks/useTheme";
import { useSettings } from "@Hooks/useSettings";
import { useActivity } from "@Hooks/useActivity";
import { Toolbar } from "@Components/onsenui/Toolbar";
import { view } from "@Native/View";
import { view, WindowManager } from "@Native/View";
import { Page } from "@Components/onsenui/Page";
import { useStrings } from "@Hooks/useStrings";
import { useConfirm } from "material-ui-confirm";
Expand Down Expand Up @@ -271,9 +271,11 @@ export const InstallTerminalV2Activity = () => {
React.useEffect(() => {
document.addEventListener("volumeupbutton", nativeVolumeEventPrevent, false);
document.addEventListener("volumedownbutton", nativeVolumeEventPrevent, false);
view.addFlags([WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON]);
return () => {
document.removeEventListener("volumeupbutton", nativeVolumeEventPrevent, false);
document.removeEventListener("volumedownbutton", nativeVolumeEventPrevent, false);
view.clearFlags([WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON]);
};
}, []);

Expand Down
26 changes: 25 additions & 1 deletion src/native/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface NativeView {
setAppearanceLightStatusBars(isLight: boolean): void;
showSystemBars(type: number): void;
hideSystemBars(type: number): void;
addFlag(flag: int): void;
clearFlag(flag: int): void;
}

class ViewInsetsCompat {
Expand All @@ -28,6 +30,12 @@ class ViewInsetsCompat {
};
}

class WindowManager {
public static LayoutParams = class {
public static readonly FLAG_KEEP_SCREEN_ON = 128;
};
}

class View extends Native<NativeView> {
public constructor() {
super(window.__view__);
Expand Down Expand Up @@ -153,7 +161,23 @@ class View extends Native<NativeView> {
this.interface.setNavigationBarColor(color);
}
}

public addFlags(flags: list<int>) {
if (this.isAndroid) {
for (const flag of flags) {
this.interface.addFlag(flag);
}
}
}

public clearFlags(flags: list<int>) {
if (this.isAndroid) {
for (const flag of flags) {
this.interface.clearFlag(flag);
}
}
}
}

const view: View = new View();
export { view, View, ViewInsetsCompat };
export { view, View, ViewInsetsCompat, WindowManager };
1 change: 1 addition & 0 deletions src/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare module "*.d.ts" {

declare global {
type arr<T> = Array<T>;
type list<T> = Array<T>;
type str = string;
type Str = String;
type int = number;
Expand Down

0 comments on commit d9c4c70

Please sign in to comment.