From 6a1f37e39956e1f3f039be5d9bc802bd019936d4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 20 Jan 2018 06:21:35 +0800 Subject: [PATCH] Initial code --- .gitignore | 10 + build.gradle | 27 ++ example/.gitignore | 1 + example/build.gradle | 29 ++ example/proguard-rules.pro | 21 ++ example/src/main/AndroidManifest.xml | 22 ++ .../topjohnwu/libsuexample/ExampleApp.java | 39 +++ .../topjohnwu/libsuexample/MainActivity.java | 75 +++++ .../drawable-v24/ic_launcher_foreground.xml | 34 +++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++ example/src/main/res/layout/activity_main.xml | 77 +++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3056 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5024 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2096 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2858 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4569 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7098 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6464 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10676 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9250 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15523 bytes example/src/main/res/values/colors.xml | 6 + example/src/main/res/values/strings.xml | 3 + example/src/main/res/values/styles.xml | 8 + gradle.properties | 17 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53636 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 ++++++++++ gradlew.bat | 90 ++++++ settings.gradle | 1 + superuser/.gitignore | 1 + superuser/build.gradle | 28 ++ superuser/proguard-rules.pro | 21 ++ superuser/src/main/AndroidManifest.xml | 1 + .../topjohnwu/superuser/NoShellException.java | 12 + .../java/com/topjohnwu/superuser/Shell.java | 281 ++++++++++++++++++ .../topjohnwu/superuser/ShellCallback.java | 40 +++ .../superuser/ShellCallbackVector.java | 31 ++ .../topjohnwu/superuser/ShellContainer.java | 10 + .../topjohnwu/superuser/StreamGobbler.java | 50 ++++ .../java/com/topjohnwu/superuser/Utils.java | 54 ++++ superuser/src/main/res/raw/script.sh | 60 ++++ 44 files changed, 1395 insertions(+) create mode 100644 .gitignore create mode 100644 build.gradle create mode 100644 example/.gitignore create mode 100644 example/build.gradle create mode 100644 example/proguard-rules.pro create mode 100644 example/src/main/AndroidManifest.xml create mode 100644 example/src/main/java/com/topjohnwu/libsuexample/ExampleApp.java create mode 100644 example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java create mode 100644 example/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 example/src/main/res/drawable/ic_launcher_background.xml create mode 100644 example/src/main/res/layout/activity_main.xml create mode 100644 example/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 example/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 example/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 example/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 example/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 example/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 example/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 example/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 example/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 example/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 example/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 example/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 example/src/main/res/values/colors.xml create mode 100644 example/src/main/res/values/strings.xml create mode 100644 example/src/main/res/values/styles.xml create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle create mode 100644 superuser/.gitignore create mode 100644 superuser/build.gradle create mode 100644 superuser/proguard-rules.pro create mode 100644 superuser/src/main/AndroidManifest.xml create mode 100644 superuser/src/main/java/com/topjohnwu/superuser/NoShellException.java create mode 100644 superuser/src/main/java/com/topjohnwu/superuser/Shell.java create mode 100644 superuser/src/main/java/com/topjohnwu/superuser/ShellCallback.java create mode 100644 superuser/src/main/java/com/topjohnwu/superuser/ShellCallbackVector.java create mode 100644 superuser/src/main/java/com/topjohnwu/superuser/ShellContainer.java create mode 100644 superuser/src/main/java/com/topjohnwu/superuser/StreamGobbler.java create mode 100644 superuser/src/main/java/com/topjohnwu/superuser/Utils.java create mode 100644 superuser/src/main/res/raw/script.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5a96f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*.iml +.idea +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..e6b32bc --- /dev/null +++ b/build.gradle @@ -0,0 +1,27 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.0.1' + + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/example/.gitignore @@ -0,0 +1 @@ +/build diff --git a/example/build.gradle b/example/build.gradle new file mode 100644 index 0000000..618876e --- /dev/null +++ b/example/build.gradle @@ -0,0 +1,29 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 27 + defaultConfig { + applicationId "com.topjohnwu.libsuexample" + minSdkVersion 16 + targetSdkVersion 27 + versionCode 1 + versionName "1.0" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation project(':superuser') +} diff --git a/example/proguard-rules.pro b/example/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/example/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/example/src/main/AndroidManifest.xml b/example/src/main/AndroidManifest.xml new file mode 100644 index 0000000..3b5b598 --- /dev/null +++ b/example/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/src/main/java/com/topjohnwu/libsuexample/ExampleApp.java b/example/src/main/java/com/topjohnwu/libsuexample/ExampleApp.java new file mode 100644 index 0000000..eabc0b4 --- /dev/null +++ b/example/src/main/java/com/topjohnwu/libsuexample/ExampleApp.java @@ -0,0 +1,39 @@ +package com.topjohnwu.libsuexample; + +import android.app.Application; + +import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellContainer; + +/** + * The {@link Application} of the Example app. + *

+ * We implement {@link ShellContainer} to the {@link Application} of + * the app, which means that we would love our root shell to live as long as the + * application itself. + */ +public class ExampleApp extends Application implements ShellContainer { + + /** + * A shell instance living alongside the lifecycle of {@link ExampleApp} + */ + public Shell mShell; + + @Override + public void onCreate() { + super.onCreate(); + // Enable verbose logging flags + Shell.addFlags(Shell.FLAG_VERBOSE_LOGGING); + Shell.setGlobalContainer(this); + } + + @Override + public Shell getShell() { + return mShell; + } + + @Override + public void setShell(Shell shell) { + mShell = shell; + } +} diff --git a/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java b/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java new file mode 100644 index 0000000..9ffe7e0 --- /dev/null +++ b/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java @@ -0,0 +1,75 @@ +package com.topjohnwu.libsuexample; + +import android.app.Activity; +import android.os.Bundle; +import android.text.TextUtils; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ScrollView; +import android.widget.TextView; + +import com.topjohnwu.superuser.NoShellException; +import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellCallback; +import com.topjohnwu.superuser.ShellCallbackVector; + +import java.util.List; + +public class MainActivity extends Activity { + + private TextView console; + private Button cmd, script, clear; + private EditText input; + private ScrollView sv; + private List callback; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + console = findViewById(R.id.console); + input = findViewById(R.id.cmd_input); + sv = findViewById(R.id.sv); + + cmd = findViewById(R.id.run_cmd); + script = findViewById(R.id.run_script); + clear = findViewById(R.id.clear); + + // Run the shell command in the input box + cmd.setOnClickListener(v -> { + Shell.su(callback, input.getText().toString()); + input.setText(""); + }); + + // Load a script from raw resources + script.setOnClickListener(v -> { + try { + Shell.getShell().loadInputStream(callback, callback, + getResources().openRawResource(R.raw.script)); + } catch (NoShellException e) { + e.printStackTrace(); + } + }); + + clear.setOnClickListener(v -> { + callback.clear(); + console.setText(""); + }); + + // We create a ShellCallback to update the UI with the Shell output + callback = new ShellCallback() { + StringBuilder builder = new StringBuilder(); + @Override + public void onShellOutput(String e) { + builder.append(e).append('\n'); + console.setText(builder); + sv.postDelayed(() -> sv.fullScroll(ScrollView.FOCUS_DOWN), 10); + } + + @Override + public void clear() { + builder = new StringBuilder(); + } + }; + } +} diff --git a/example/src/main/res/drawable-v24/ic_launcher_foreground.xml b/example/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..c7bd21d --- /dev/null +++ b/example/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/example/src/main/res/drawable/ic_launcher_background.xml b/example/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..d5fccc5 --- /dev/null +++ b/example/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..8d222fd --- /dev/null +++ b/example/src/main/res/layout/activity_main.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + +