Skip to content

Commit

Permalink
allow install module when clicking zips
Browse files Browse the repository at this point in the history
idk how that feature is named...
  • Loading branch information
DerGoogler committed Sep 7, 2024
1 parent ac3883b commit ca40756
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ repositories {

dependencies {
implementation 'androidx.browser:browser:1.8.0'
implementation "androidx.annotation:annotation:1.8.2"
implementation "androidx.appcompat:appcompat:1.7.0"
implementation 'androidx.core:core:1.13.1'
implementation 'androidx.webkit:webkit:1.11.0'
Expand Down
39 changes: 34 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,35 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter android:autoVerify="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />

<data
android:host="mmrl.dergoogler.com"
android:path="/"
android:scheme="https" />
<data android:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.zip" />
<data android:pathPattern=".*\\..*\\.zip" />
<data android:pathPattern=".*\\..*\\..*\\.zip" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.zip" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.zip" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.zip" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.zip" />
<data android:scheme="content" />
<data android:scheme="file" />
</intent-filter>

<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="mmrl" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="mmrl.dergoogler.com" />
<data android:pathPrefix="/" />
</intent-filter>
</activity>

Expand All @@ -70,5 +89,15 @@
<receiver
android:name="androidx.profileinstaller.ProfileInstallReceiver"
tools:node="remove" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
80 changes: 76 additions & 4 deletions app/src/main/java/com/dergoogler/core/NativeSuFile.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.dergoogler.core;

import android.graphics.BitmapFactory;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.util.Base64;
import android.util.Base64OutputStream;
import android.util.Log;
import android.webkit.JavascriptInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dergoogler.mmrl.MainActivity;
import com.topjohnwu.superuser.io.SuFile;
Expand All @@ -16,7 +21,9 @@
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -61,7 +68,7 @@ public String read(String def) {
return sb.toString();
}
} catch (IOException e) {
Log.e( TAG + ":read", e.toString());
Log.e(TAG + ":read", e.toString());
return def;
}
}
Expand Down Expand Up @@ -92,7 +99,7 @@ public String readAsBase64() {
}
}

private void closeQuietly(Closeable closeable) {
private void closeQuietly(Closeable closeable) {
try {
closeable.close();
} catch (IOException e) {
Expand Down Expand Up @@ -199,7 +206,6 @@ public String readFile(String path) {
}
}

@NonNull
@JavascriptInterface
public String listFiles(String path) {
String[] modules = new SuFile(path).list();
Expand All @@ -225,4 +231,70 @@ public void deleteRecursive(String path) {
public boolean existFile(String path) {
return new SuFile(path).exists();
}

@JavascriptInterface
public @Nullable String getSharedFile() {
Intent intent = ctx.getIntent();
Uri uri = intent.getData();
if (uri != null) {
return createCopyAndReturnRealPath(uri);
} else {
return null;
}
}

public @Nullable String createCopyAndReturnRealPath(Uri uri) {
final ContentResolver contentResolver = ctx.getContentResolver();
if (contentResolver == null)
return null;

// Create file path inside app's data dir
String filePath = ctx.getApplicationInfo().dataDir + File.separator + "cache" + File.separator
+ getFileName(uri);

File file = new File(filePath);
try {
InputStream inputStream = contentResolver.openInputStream(uri);
if (inputStream == null)
return null;

OutputStream outputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0)
outputStream.write(buf, 0, len);

outputStream.close();
inputStream.close();
} catch (IOException ignore) {
return null;
}

return file.getAbsolutePath();
}

public String getFileName(Uri uri) {
String result = null;
if ("content".equals(uri.getScheme())) {
try (Cursor cursor = ctx.getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (index != -1) {
result = cursor.getString(index);
}
}
}
}
if (result == null) {
result = uri.getPath();
if (result != null) {
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
}
}
return result;
}

}
12 changes: 10 additions & 2 deletions app/src/main/java/com/dergoogler/mmrl/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.dergoogler.mmrl;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
Expand All @@ -26,11 +32,14 @@
import com.dergoogler.core.NativeBuildConfig;
import com.dergoogler.core.NativeView;
import com.dergoogler.core.NativeSuZip;
import com.topjohnwu.superuser.io.SuFile;

import org.apache.cordova.*;
import org.apache.cordova.engine.SystemWebChromeClient;
import org.apache.cordova.engine.SystemWebViewEngine;

import java.io.File;

public class MainActivity extends CordovaActivity {

private WebView wv;
Expand Down Expand Up @@ -114,7 +123,6 @@ public void onGlobalLayout() {
wv.addJavascriptInterface(new NativeLog(), "__log__");
wv.addJavascriptInterface(new NativeSuZip(), "__suzip__");


wv.setWebChromeClient(new SystemWebChromeClient((SystemWebViewEngine) wve) {
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path
name="cache"
path="." />
<cache-path
name="cache"
path="./" />
<external-cache-path
name="external_cache"
path="." />
<external-cache-path
name="external_cache"
path="./" />

<files-path
name="files"
path="." />
<files-path
name="files"
path="./" />
<external-files-path
name="external_files"
path="." />
<external-files-path
name="external_files"
path="./" />
</paths>
41 changes: 41 additions & 0 deletions src/activitys/MainApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import InstallTerminalV2Activity from "./InstallTerminalV2Activity";
import { Chooser } from "@Native/Chooser";
import { useConfirm } from "material-ui-confirm";
import VolunteerActivismIcon from "@mui/icons-material/VolunteerActivism";
import { SuFile } from "@Native/SuFile";
import { Log } from "@Native/Log";
import { SuZip } from "@Native/SuZip";
import { Properties } from "properties-file";

const TAG = "MainApplication";

const MainApplication = () => {
const { context } = useActivity();
Expand Down Expand Up @@ -60,6 +66,41 @@ const MainApplication = () => {
[index]
);

React.useEffect(() => {
const sharedFile = SuFile.getSharedFile();
if (sharedFile) {
const file = new SuFile(sharedFile);

if (file.exist()) {
const zipFile = new SuZip(file.getPath(), "module.prop");
const props = new Properties(zipFile.read()).toObject();

if (!props.id) {
return;
}

confirm({
title: strings("install_module", { name: props.name }),
description: strings("install_module_dialog_desc", { name: <strong>{props.name}</strong> }),
confirmationText: strings("yes"),
})
.then(() => {
context.pushPage({
component: InstallTerminalV2Activity,
key: "InstallTerminalV2Activity",
extra: {
exploreInstall: false,
modSource: [file.getPath()],
},
});
})
.catch(() => {});
} else {
Log.i(TAG, "Unable to find shared file");
}
}
}, []);

React.useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get("module");
Expand Down
1 change: 0 additions & 1 deletion src/components/onsenui/RouterNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ class RouterNavigatorClass extends React.Component<HTMLNavigatorClass, State> {
} = this.props;

const pagesToRender = this.state.internalStack.map((item) => {
console.log(item);
return (
<Extra.Provider key={item.props.key + "_extra"} value={item.extra}>
<Context.Provider key={item.props.key + "_context"} value={item.context}>
Expand Down
17 changes: 12 additions & 5 deletions src/native/SuFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface NativeSuFile extends NativeSuFileV2 {
deleteFile(path: string): boolean;
deleteRecursive(path: string): boolean;
existFile(path: string): boolean;
getSharedFile(): string | null;
}

interface NativeSuFileV2 {
Expand Down Expand Up @@ -68,18 +69,17 @@ class SuFile extends Native<NativeSuFile> {

private _restrictedPaths = [/(\/data\/data\/(.+)\/?|(\/storage\/emulated\/0|\/sdcard)\/Android\/(data|media|obb)(.+)?)\/?/i];

public constructor(path: string, opt?: SuFileoptions) {
public constructor(path?: string, opt?: SuFileoptions) {
super(window.__sufile__);
this._readDefaultValue = opt?.readDefaultValue || "";
this._path = path ? String(path) : "";

if (typeof path !== "string") throw new TypeError("Path name isn't a string");
if (this._isRestrictedPath(path)) {
if (this._isRestrictedPath(this._path)) {
throw new Error(`SuFile tried to access "${path}" which has been blocked due security.`);
}

this._path = path;
if (this.isAndroid) {
this._file = this.interface.v2.bind(this.interface)(path);
this._file = this.interface.v2.bind(this.interface)(this._path);
}
}

Expand Down Expand Up @@ -251,6 +251,13 @@ class SuFile extends Native<NativeSuFile> {
public static create(path: string, type: number = SuFile.NEW_FILE): boolean {
return new SuFile(path).create(type);
}

public static getSharedFile(): string | undefined {
if (this.isAndroid) {
return this.static.interface.getSharedFile();
}
return undefined;
}
}

export { SuFile };

0 comments on commit ca40756

Please sign in to comment.