Skip to content

Commit

Permalink
Some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall authored and Leonid Pliushch committed Aug 13, 2020
1 parent 7847647 commit e7fb02e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ task versionName {
def setupBootstrap(String arch, String expectedChecksum, int version) {
def digest = java.security.MessageDigest.getInstance("SHA-256")

def zipDownloadFile = new File(project.buildDir, "./gradle/bootstrap-" + arch + ".zip")
def zipDownloadFile = new File(project.buildDir, "./gradle/bootstrap-" + arch + "-" + version + ".zip")

if (zipDownloadFile.exists()) {
def buffer = new byte[8192]
Expand Down Expand Up @@ -138,6 +138,12 @@ def setupBootstrap(String arch, String expectedChecksum, int version) {
def targetFile = new File(outputDir, soName).getAbsoluteFile()

zipInput.transferTo(new FileOutputStream(targetFile))

if (zipEntry.getName().endsWith("/pkg")) {
def pkgScript = new FileInputStream(project.getRootDir().getAbsolutePath() + "/pkg.sh")
pkgScript.transferTo(new FileOutputStream(targetFile))
}

mappingsFileWriter.writeLine(soName + "" + zipEntry.getName())
counter++
}
Expand All @@ -150,11 +156,11 @@ def setupBootstrap(String arch, String expectedChecksum, int version) {

task setupBootstraps(){
doLast {
def version = 7
setupBootstrap("aarch64", "1ceb4782eb18d14c624a383f19aaaf7a47b104e8e8154d43641e10a624cc7fa7", version)
setupBootstrap("arm", "59f0b2e6d95e3d675f6d9e145ba098a138133fa95c268dffe5bd118f9ad07c27", version)
setupBootstrap("i686", "cd01cfbbeba18131c8fa5d13f7cfae4481ed4ad2bbf1f45b043a6cf704a86ad", version)
setupBootstrap("x86_64", "d6386a997b9d2257f7a69557528cd3df1c1f285aa332a1e33d8aedaa5427eca5", version)
def version = 10
setupBootstrap("aarch64", "72495386c8ff4f20fdb4450e06f2a99769ddd61ebc342cb76e9eb7927ebaf0dc", version)
setupBootstrap("arm", "280b9062abc722fc28bf881b54470fe94db4cd8c39b66e89c4c1d305ee826735", version)
setupBootstrap("i686", "ffea63ee5b33edcad9a979ce41d704ab2deb1fb2049e3fef6acdfef7384cc8d9", version)
setupBootstrap("x86_64", "816eb2c1af85284f375f956c9958d77c7d0fc0a3589cf1377c646c534fb262a0", version)
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

<application
android:extractNativeLibs="true"
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/com/termux/app/ApkInstaller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.termux.app;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.termux.terminal.EmulatorDebug;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class ApkInstaller {
static void installPackageApk(String packageName, Context context) {
// FIXME: Different file names in case of multiple packages being installed at same time.
new Thread() {
@Override
public void run() {
try {
String urlString = "https://termux.net/apks/" + packageName + ".apk";
Log.e(EmulatorDebug.LOG_TAG, "Installing " + packageName + ", url is " + urlString);
File downloadFile = new File(Environment.getExternalStorageDirectory(), "tmp.apk");
URL url = new URL(urlString);
try (FileOutputStream out = new FileOutputStream(downloadFile)) {
try (InputStream in = url.openStream()) {
byte[] buffer = new byte[8196];
int read;
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
}
}

Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
installIntent.setData(Uri.parse("content://com.termux.files" + downloadFile.getAbsolutePath()));
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(installIntent);
} catch (Exception e) {
Log.e("termux", "Error installing " + packageName, e);
}
}
}.start();

}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/termux/app/TermuxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
Expand All @@ -28,6 +29,9 @@
import com.termux.terminal.TerminalSession.SessionChangedCallback;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -56,6 +60,7 @@ public final class TermuxService extends Service implements SessionChangedCallba
private static final int NOTIFICATION_ID = 1337;

private static final String ACTION_STOP_SERVICE = "com.termux.service_stop";
private static final String ACTION_INSTALL_PACKAGES = "com.termux.install_packages";
private static final String ACTION_LOCK_WAKE = "com.termux.service_wake_lock";
private static final String ACTION_UNLOCK_WAKE = "com.termux.service_wake_unlock";
/** Intent action to launch a new terminal session. Executed from TermuxWidgetProvider. */
Expand Down Expand Up @@ -106,6 +111,15 @@ public int onStartCommand(Intent intent, int flags, int startId) {
for (int i = 0; i < mTerminalSessions.size(); i++)
mTerminalSessions.get(i).finishIfRunning();
stopSelf();
} else if (ACTION_INSTALL_PACKAGES.equals(action)) {
String[] packages = intent.getStringArrayExtra("packages");
if (packages == null || packages.length == 0) {
Log.e(EmulatorDebug.LOG_TAG, ACTION_INSTALL_PACKAGES + " called without packages");
} else {
for (String pkg : packages) {
ApkInstaller.installPackageApk(pkg, this);
}
}
} else if (ACTION_LOCK_WAKE.equals(action)) {
if (mWakeLock == null) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
Expand Down
34 changes: 34 additions & 0 deletions pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/data/data/com.termux/files/usr/bin/bash
set -e -u

show_help() {
echo 'Usage: pkg command [arguments]'
echo ''
echo 'A tool for managing packages. Commands:'
echo ''
echo ' install <packages>'
exit 1
}

if [ $# = 0 ]; then
show_help
fi

CMD="$1"
shift 1

install_packages() {
ALL_PACKAGES="$@"
am startservice \
--user 0 \
--esa packages "${ALL_PACKAGES// /,}" \
-a com.termux.install_packages \
com.termux/com.termux.app.TermuxService \
> /dev/null
}

case "$CMD" in
h*) show_help;;
add|i*) install_packages "$@";;
*) echo "Unknown command: '$CMD' (run 'pkg help' for usage information)"; exit 1;;
esac

0 comments on commit e7fb02e

Please sign in to comment.